Skip to contentMethod: isOpen()
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.Closeable;
18: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
20: import cz.cvut.kbss.ontodriver.owlapi.connector.ConnectorFactory;
21: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
22: import cz.cvut.kbss.ontodriver.owlapi.list.OwlapiLists;
23: import cz.cvut.kbss.ontodriver.Connection;
24:
25: import java.util.HashSet;
26: import java.util.Map;
27: import java.util.Set;
28:
29: class OwlapiDriver implements Closeable, ConnectionListener {
30:
31: private final OntologyStorageProperties storageProperties;
32: private final Map<String, String> properties;
33: private boolean open = true;
34:
35: private final Set<OwlapiConnection> openConnections = new HashSet<>();
36:
37: OwlapiDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
38: this.storageProperties = storageProperties;
39: this.properties = properties;
40: }
41:
42: @Override
43: public void close() throws OntoDriverException {
44: if (!open) {
45: return;
46: }
47: for (OwlapiConnection c : openConnections) {
48: try {
49: c.removeListener(this);
50: c.close();
51: } catch (Exception e) {
52: if (e instanceof OntoDriverException) {
53: throw (OntoDriverException) e;
54: } else {
55: throw new OwlapiDriverException(e);
56: }
57: }
58: }
59: ConnectorFactory.getInstance().close();
60: this.open = false;
61: }
62:
63: @Override
64: public boolean isOpen() {
65: return open;
66: }
67:
68: Connection acquireConnection() throws OntoDriverException {
69: assert open;
70: final OwlapiAdapter adapter = new OwlapiAdapter(
71: ConnectorFactory.getInstance().getConnector(storageProperties, properties), properties);
72: final OwlapiConnection c = new OwlapiConnection(adapter);
73: c.setTypes(new OwlapiTypes(c, adapter));
74: c.setProperties(new OwlapiProperties(c, adapter));
75: c.setLists(new OwlapiLists(c, adapter));
76: openConnections.add(c);
77: c.addListener(this);
78: return c;
79: }
80:
81: @Override
82: public void connectionClosed(Connection connection) {
83: openConnections.remove(connection);
84: }
85: }