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