Skip to contentMethod: setProperties(Properties)
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.rdf4j;
16:
17: import cz.cvut.kbss.ontodriver.*;
18: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
20: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
21: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
24: import cz.cvut.kbss.ontodriver.rdf4j.query.Rdf4jPreparedStatement;
25: import cz.cvut.kbss.ontodriver.rdf4j.query.Rdf4jStatement;
26:
27: import java.net.URI;
28: import java.util.Collection;
29: import java.util.List;
30: import java.util.Objects;
31: import java.util.Set;
32:
33: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.getNPXMessageSupplier;
34:
35:
36: class Rdf4jConnection implements Connection {
37:
38: private final Rdf4jAdapter adapter;
39: private boolean open;
40: private boolean autoCommit;
41:
42: private Lists lists;
43: private Types types;
44: private Properties properties;
45:
46: private ConnectionListener<Rdf4jConnection> listener;
47:
48: Rdf4jConnection(Rdf4jAdapter adapter) {
49: assert adapter != null;
50: this.adapter = adapter;
51: this.open = true;
52: }
53:
54: void setLists(Rdf4jLists lists) {
55: this.lists = lists;
56: }
57:
58: void setTypes(Rdf4jTypes types) {
59: this.types = types;
60: }
61:
62: public void setProperties(Properties properties) {
63: this.properties = properties;
64: }
65:
66: void setListener(ConnectionListener<Rdf4jConnection> listener) {
67: ensureOpen();
68: assert listener != null;
69: this.listener = listener;
70: }
71:
72: void removeListener() {
73: this.listener = null;
74: }
75:
76: @Override
77: public void close() throws Exception {
78: if (!open) {
79: return;
80: }
81: try {
82: adapter.close();
83: if (listener != null) {
84: listener.connectionClosed(this);
85: }
86: } finally {
87: this.open = false;
88: }
89: }
90:
91: @Override
92: public boolean isOpen() {
93: return open;
94: }
95:
96: @Override
97: public void commit() throws OntoDriverException {
98: ensureOpen();
99: if (autoCommit) {
100: return;
101: }
102: adapter.commit();
103: }
104:
105: @Override
106: public void rollback() throws OntoDriverException {
107: ensureOpen();
108: if (autoCommit) {
109: return;
110: }
111: adapter.rollback();
112: }
113:
114: @Override
115: public void setAutoCommit(boolean autoCommit) {
116: ensureOpen();
117: this.autoCommit = autoCommit;
118: }
119:
120: @Override
121: public boolean isAutoCommit() {
122: ensureOpen();
123: return autoCommit;
124: }
125:
126: @Override
127: public Statement createStatement() {
128: ensureOpen();
129: return new Rdf4jStatement(adapter.getQueryExecutor());
130: }
131:
132: @Override
133: public PreparedStatement prepareStatement(String sparql) {
134: ensureOpen();
135: Objects.requireNonNull(sparql);
136: if (sparql.isEmpty()) {
137: throw new IllegalArgumentException("The value for prepared statement cannot be empty.");
138: }
139: return new Rdf4jPreparedStatement(adapter.getQueryExecutor(), sparql);
140: }
141:
142: @Override
143: public boolean isConsistent(URI context) {
144: ensureOpen();
145: return adapter.isConsistent(context);
146: }
147:
148: @Override
149: public List<URI> getContexts() throws OntoDriverException {
150: ensureOpen();
151: return adapter.getContexts();
152: }
153:
154: @Override
155: public URI generateIdentifier(URI classUri) throws OntoDriverException {
156: ensureOpen();
157: Objects.requireNonNull(classUri);
158: try {
159: return adapter.generateIdentifier(classUri);
160: } catch (IdentifierGenerationException e) {
161: throw new Rdf4jDriverException(e);
162: }
163: }
164:
165: @Override
166: public boolean contains(Axiom<?> axiom, Set<URI> contexts) throws OntoDriverException {
167: ensureOpen();
168: Objects.requireNonNull(axiom, getNPXMessageSupplier("axiom"));
169: Objects.requireNonNull(contexts);
170: return adapter.contains(axiom, contexts);
171: }
172:
173: @Override
174: public boolean isInferred(Axiom<?> axiom, Set<URI> contexts) throws OntoDriverException {
175: ensureOpen();
176: Objects.requireNonNull(axiom, getNPXMessageSupplier("axiom"));
177: Objects.requireNonNull(contexts);
178: return adapter.isInferred(axiom, contexts);
179: }
180:
181: @Override
182: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws OntoDriverException {
183: ensureOpen();
184: Objects.requireNonNull(descriptor);
185: try {
186: return adapter.find(descriptor);
187: } catch (RuntimeException e) {
188: throw new Rdf4jDriverException(e);
189: }
190: }
191:
192: @Override
193: public void persist(AxiomValueDescriptor descriptor) throws OntoDriverException {
194: ensureOpen();
195: Objects.requireNonNull(descriptor);
196: try {
197: adapter.persist(descriptor);
198: commitIfAuto();
199: } catch (RuntimeException e) {
200: throw new Rdf4jDriverException(e);
201: }
202: }
203:
204: @Override
205: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
206: ensureOpen();
207: Objects.requireNonNull(descriptor);
208: try {
209: adapter.update(descriptor);
210: commitIfAuto();
211: } catch (RuntimeException e) {
212: throw new Rdf4jDriverException(e);
213: }
214: }
215:
216: @Override
217: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
218: ensureOpen();
219: Objects.requireNonNull(descriptor);
220: try {
221: adapter.remove(descriptor);
222: commitIfAuto();
223: } catch (RuntimeException e) {
224: throw new Rdf4jDriverException(e);
225: }
226: }
227:
228: @Override
229: public Lists lists() {
230: ensureOpen();
231: assert lists != null;
232: return lists;
233: }
234:
235: @Override
236: public Types types() {
237: ensureOpen();
238: assert types != null;
239: return types;
240: }
241:
242: @Override
243: public Properties properties() {
244: ensureOpen();
245: assert properties != null;
246: return properties;
247: }
248:
249: void ensureOpen() {
250: if (!open) {
251: throw new IllegalStateException("This connection is closed.");
252: }
253: }
254:
255: void commitIfAuto() throws Rdf4jDriverException {
256: if (autoCommit) {
257: adapter.commit();
258: }
259: }
260:
261: @Override
262: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
263: if (cls.isAssignableFrom(this.getClass())) {
264: return cls.cast(this);
265: }
266: return adapter.unwrap(cls);
267: }
268: }