Skip to contentMethod: removeListener()
1: /**
2: * Copyright (C) 2019 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.sesame;
16:
17: import cz.cvut.kbss.ontodriver.*;
18: import cz.cvut.kbss.ontodriver.Properties;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
20: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
21: import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
22: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
25: import cz.cvut.kbss.ontodriver.sesame.query.SesamePreparedStatement;
26: import cz.cvut.kbss.ontodriver.sesame.query.SesameStatement;
27:
28: import java.net.URI;
29: import java.util.*;
30:
31: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.getNPXMessageSupplier;
32:
33:
34: class SesameConnection implements Connection {
35:
36: private SesameAdapter adapter;
37: private boolean open;
38: private boolean autoCommit;
39:
40: private Lists lists;
41: private Types types;
42: private Properties properties;
43:
44: private ConnectionListener listener;
45:
46: SesameConnection(SesameAdapter adapter) {
47: assert adapter != null;
48: this.adapter = adapter;
49: this.open = true;
50: }
51:
52: void setLists(SesameLists lists) {
53: this.lists = lists;
54: }
55:
56: void setTypes(SesameTypes types) {
57: this.types = types;
58: }
59:
60: public void setProperties(Properties properties) {
61: this.properties = properties;
62: }
63:
64: void setListener(ConnectionListener listener) {
65: ensureOpen();
66: assert listener != null;
67: this.listener = listener;
68: }
69:
70: void removeListener() {
71: this.listener = null;
72: }
73:
74: @Override
75: public void close() throws Exception {
76: if (!open) {
77: return;
78: }
79: try {
80: adapter.close();
81: if (listener != null) {
82: listener.connectionClosed(this);
83: }
84: } finally {
85: this.open = false;
86: }
87: }
88:
89: @Override
90: public boolean isOpen() {
91: return open;
92: }
93:
94: @Override
95: public void commit() throws OntoDriverException {
96: ensureOpen();
97: if (autoCommit) {
98: return;
99: }
100: adapter.commit();
101: }
102:
103: @Override
104: public void rollback() throws OntoDriverException {
105: ensureOpen();
106: if (autoCommit) {
107: return;
108: }
109: adapter.rollback();
110: }
111:
112: @Override
113: public void setAutoCommit(boolean autoCommit) {
114: ensureOpen();
115: this.autoCommit = autoCommit;
116: }
117:
118: @Override
119: public boolean isAutoCommit() {
120: ensureOpen();
121: return autoCommit;
122: }
123:
124: @Override
125: public Statement createStatement() {
126: ensureOpen();
127: return new SesameStatement(adapter.getQueryExecutor());
128: }
129:
130: @Override
131: public PreparedStatement prepareStatement(String sparql) throws OntoDriverException {
132: ensureOpen();
133: Objects.requireNonNull(sparql);
134: if (sparql.isEmpty()) {
135: throw new IllegalArgumentException("The value for prepared statement cannot be empty.");
136: }
137: return new SesamePreparedStatement(adapter.getQueryExecutor(), sparql);
138: }
139:
140: @Override
141: public boolean isConsistent(URI context) {
142: ensureOpen();
143: return adapter.isConsistent(context);
144: }
145:
146: @Override
147: public List<URI> getContexts() throws OntoDriverException {
148: ensureOpen();
149: return adapter.getContexts();
150: }
151:
152: @Override
153: public URI generateIdentifier(URI classUri) throws OntoDriverException {
154: ensureOpen();
155: Objects.requireNonNull(classUri);
156: try {
157: return adapter.generateIdentifier(classUri);
158: } catch (IdentifierGenerationException e) {
159: throw new SesameDriverException(e);
160: }
161: }
162:
163: @Override
164: public boolean contains(Axiom<?> axiom, URI context) throws OntoDriverException {
165: ensureOpen();
166: Objects.requireNonNull(axiom, getNPXMessageSupplier("axiom"));
167: return adapter.contains(axiom, context);
168: }
169:
170: @Override
171: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws OntoDriverException {
172: ensureOpen();
173: Objects.requireNonNull(descriptor);
174: try {
175: return adapter.find(descriptor);
176: } catch (RuntimeException e) {
177: throw new SesameDriverException(e);
178: }
179: }
180:
181: @Override
182: public void persist(AxiomValueDescriptor descriptor) throws OntoDriverException {
183: ensureOpen();
184: Objects.requireNonNull(descriptor);
185: try {
186: adapter.persist(descriptor);
187: commitIfAuto();
188: } catch (RuntimeException e) {
189: throw new SesameDriverException(e);
190: }
191: }
192:
193: @Override
194: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
195: ensureOpen();
196: Objects.requireNonNull(descriptor);
197: try {
198: adapter.update(descriptor);
199: commitIfAuto();
200: } catch (RuntimeException e) {
201: throw new SesameDriverException(e);
202: }
203: }
204:
205: @Override
206: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
207: ensureOpen();
208: Objects.requireNonNull(descriptor);
209: try {
210: adapter.remove(descriptor);
211: commitIfAuto();
212: } catch (RuntimeException e) {
213: throw new SesameDriverException(e);
214: }
215: }
216:
217: @Override
218: public Lists lists() {
219: ensureOpen();
220: assert lists != null;
221: return lists;
222: }
223:
224: @Override
225: public Types types() {
226: ensureOpen();
227: assert types != null;
228: return types;
229: }
230:
231: @Override
232: public Properties properties() {
233: ensureOpen();
234: assert properties != null;
235: return properties;
236: }
237:
238: void ensureOpen() {
239: if (!open) {
240: throw new IllegalStateException("This connection is closed.");
241: }
242: }
243:
244: void commitIfAuto() throws SesameDriverException {
245: if (autoCommit) {
246: adapter.commit();
247: }
248: }
249:
250: @Override
251: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
252: if (cls.isAssignableFrom(this.getClass())) {
253: return cls.cast(this);
254: }
255: return adapter.unwrap(cls);
256: }
257: }