Skip to content

Method: isOpen()

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