Skip to content

Method: addListener(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.owlapi;
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.OntoDriverException;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
24: import cz.cvut.kbss.ontodriver.owlapi.list.OwlapiLists;
25:
26: import java.net.URI;
27: import java.util.*;
28:
29: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.npxMessage;
30:
31: /**
32: * Default implementation of the {@link Connection} interface for OWLAPI driver.
33: */
34: public class OwlapiConnection implements Connection {
35:
36: private boolean open;
37: private boolean autoCommit;
38:
39: private final OwlapiAdapter adapter;
40:
41: private OwlapiTypes types;
42: private OwlapiProperties properties;
43: private OwlapiLists lists;
44:
45: private final Set<ConnectionListener> listeners = new HashSet<>(4);
46:
47: public OwlapiConnection(OwlapiAdapter adapter) {
48: this.adapter = adapter;
49: this.open = true;
50: this.autoCommit = false;
51: }
52:
53: void addListener(ConnectionListener listener) {
54:• assert listener != null;
55: listeners.add(listener);
56: }
57:
58: void removeListener(ConnectionListener listener) {
59: assert listener != null;
60: listeners.remove(listener);
61: }
62:
63: void setTypes(OwlapiTypes types) {
64: this.types = types;
65: }
66:
67: void setProperties(OwlapiProperties properties) {
68: this.properties = properties;
69: }
70:
71: void setLists(OwlapiLists lists) {
72: this.lists = lists;
73: }
74:
75: @Override
76: public boolean isOpen() {
77: return open;
78: }
79:
80: @Override
81: public void commit() throws OntoDriverException {
82: ensureOpen();
83: adapter.commit();
84: }
85:
86: public void ensureOpen() {
87: if (!open) {
88: throw new IllegalStateException("Connection is closed.");
89: }
90: }
91:
92: @Override
93: public void rollback() throws OntoDriverException {
94: ensureOpen();
95: adapter.rollback();
96: }
97:
98: @Override
99: public void setAutoCommit(boolean autoCommit) {
100: this.autoCommit = autoCommit;
101: }
102:
103: @Override
104: public boolean isAutoCommit() {
105: return autoCommit;
106: }
107:
108: @Override
109: public Statement createStatement() throws OntoDriverException {
110: ensureOpen();
111: return adapter.createStatement(this);
112: }
113:
114: @Override
115: public PreparedStatement prepareStatement(String sparql) throws OntoDriverException {
116: ensureOpen();
117: return adapter.prepareStatement(sparql, this);
118: }
119:
120: @Override
121: public boolean isConsistent(URI context) throws OntoDriverException {
122: ensureOpen();
123: return adapter.isConsistent(context);
124: }
125:
126: @Override
127: public List<URI> getContexts() throws OntoDriverException {
128: ensureOpen();
129: return adapter.getContexts();
130: }
131:
132: @Override
133: public boolean contains(Axiom<?> axiom, URI context) throws OntoDriverException {
134: ensureOpen();
135: Objects.requireNonNull(axiom, npxMessage("axiom"));
136: return adapter.containsAxiom(axiom, context);
137: }
138:
139: @Override
140: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws OntoDriverException {
141: ensureOpen();
142: Objects.requireNonNull(descriptor, npxMessage("descriptor"));
143: try {
144: return adapter.find(descriptor);
145: } catch (RuntimeException e) {
146: throw new OwlapiDriverException(e);
147: }
148: }
149:
150: @Override
151: public void persist(AxiomValueDescriptor descriptor) throws OntoDriverException {
152: ensureOpen();
153: Objects.requireNonNull(descriptor);
154: try {
155: adapter.persist(descriptor);
156: commitIfAuto();
157: } catch (RuntimeException e) {
158: throw new OwlapiDriverException(e);
159: }
160: }
161:
162: @Override
163: public URI generateIdentifier(URI classUri) throws OntoDriverException {
164: ensureOpen();
165: Objects.requireNonNull(classUri);
166: return adapter.generateIdentifier(classUri);
167: }
168:
169: @Override
170: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
171: ensureOpen();
172: Objects.requireNonNull(descriptor);
173: try {
174: adapter.update(descriptor);
175: commitIfAuto();
176: } catch (RuntimeException e) {
177: throw new OwlapiDriverException(e);
178: }
179: }
180:
181: @Override
182: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
183: ensureOpen();
184: Objects.requireNonNull(descriptor);
185: try {
186: adapter.remove(descriptor);
187: commitIfAuto();
188: } catch (RuntimeException e) {
189: throw new OwlapiDriverException(e);
190: }
191: }
192:
193: @Override
194: public Lists lists() {
195: ensureOpen();
196: assert lists != null;
197: return lists;
198: }
199:
200: @Override
201: public Types types() {
202: ensureOpen();
203: assert types != null;
204: return types;
205: }
206:
207: @Override
208: public Properties properties() {
209: ensureOpen();
210: assert properties != null;
211: return properties;
212: }
213:
214: @Override
215: public void close() throws Exception {
216: if (!open) {
217: return;
218: }
219: listeners.stream().forEach(listener -> listener.connectionClosed(this));
220: this.open = false;
221: }
222:
223: public void commitIfAuto() throws OwlapiDriverException {
224: if (autoCommit) {
225: adapter.commit();
226: }
227: }
228:
229: @Override
230: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
231: // TODO Add support for unwrap here
232: throw new OwlapiDriverException("Not supported, yet.");
233: }
234: }