Skip to content

Method: close()

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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.Connection;
19: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
20: import cz.cvut.kbss.ontodriver.config.ConfigParam;
21: import cz.cvut.kbss.ontodriver.config.Configuration;
22: import cz.cvut.kbss.ontodriver.config.ConfigurationParameter;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.owlapi.config.OwlapiConfigParam;
25: import cz.cvut.kbss.ontodriver.owlapi.connector.ConnectorFactory;
26: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
27: import cz.cvut.kbss.ontodriver.owlapi.list.OwlapiLists;
28:
29: import java.util.*;
30:
31: class OwlapiDriver implements Closeable, ConnectionListener {
32:
33: private static final List<ConfigurationParameter> CONFIGS = Arrays
34: .asList(ConfigParam.AUTO_COMMIT, ConfigParam.MODULE_EXTRACTION_SIGNATURE, ConfigParam.ONTOLOGY_LANGUAGE,
35: ConfigParam.REASONER_FACTORY_CLASS,
36: OwlapiConfigParam.IRI_MAPPING_DELIMITER, OwlapiConfigParam.MAPPING_FILE_LOCATION,
37: OwlapiConfigParam.WRITE_ON_COMMIT);
38:
39: private final Configuration configuration;
40: private boolean open = true;
41:
42: private ConnectorFactory connectorFactory;
43: private final Set<OwlapiConnection> openConnections = new HashSet<>();
44:
45: OwlapiDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
46: this.configuration = new Configuration(storageProperties);
47: configuration.addConfiguration(properties, CONFIGS);
48: this.connectorFactory = ConnectorFactory.createFactory();
49: }
50:
51: @Override
52: public void close() throws OntoDriverException {
53:• if (!open) {
54: return;
55: }
56:• for (OwlapiConnection c : openConnections) {
57: try {
58: c.removeListener(this);
59: c.close();
60: } catch (OntoDriverException e) {
61: throw e;
62: } catch (Exception e) {
63: throw new OwlapiDriverException(e);
64: }
65: }
66: connectorFactory.close();
67: this.open = false;
68: }
69:
70: @Override
71: public boolean isOpen() {
72: return open;
73: }
74:
75: Connection acquireConnection() throws OntoDriverException {
76: assert open;
77: final OwlapiAdapter adapter = new OwlapiAdapter(connectorFactory.getConnector(configuration), configuration);
78: final OwlapiConnection c = new OwlapiConnection(adapter);
79: c.setTypes(new OwlapiTypes(adapter, c::ensureOpen, c::commitIfAuto));
80: c.setProperties(new OwlapiProperties(adapter, c::ensureOpen, c::commitIfAuto));
81: c.setLists(new OwlapiLists(adapter, c::ensureOpen, c::commitIfAuto));
82: openConnections.add(c);
83: c.addListener(this);
84: return c;
85: }
86:
87: @Override
88: public void connectionClosed(Connection connection) {
89: openConnections.remove(connection);
90: }
91: }