Skip to content

Method: close()

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.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.sesame.config.SesameConfigParam;
25: import cz.cvut.kbss.ontodriver.sesame.connector.ConnectorFactory;
26: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
27:
28: import java.util.*;
29:
30: class SesameDriver implements Closeable, ConnectionListener {
31:
32: private final List<ConfigurationParameter> CONFIGS = Arrays
33: .asList(ConfigParam.AUTO_COMMIT, ConfigParam.ONTOLOGY_LANGUAGE,
34: SesameConfigParam.USE_INFERENCE, SesameConfigParam.USE_VOLATILE_STORAGE);
35:
36: private final Configuration configuration;
37: private boolean open;
38: private final ConnectorFactory connectorFactory;
39:
40: private final Set<SesameConnection> openedConnections;
41:
42: SesameDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
43: assert storageProperties != null;
44: assert properties != null;
45:
46: this.configuration = new Configuration(storageProperties);
47: configuration.addConfiguration(properties, CONFIGS);
48: this.openedConnections = new HashSet<>();
49: this.connectorFactory = ConnectorFactory.getInstance();
50: this.open = true;
51: }
52:
53: @Override
54: public void close() throws OntoDriverException {
55:• if (!open) {
56: return;
57: }
58: try {
59:• for (SesameConnection c : openedConnections) {
60: c.removeListener(this);
61: c.close();
62: }
63: connectorFactory.close();
64: } catch (Exception e) {
65:• if (e instanceof OntoDriverException) {
66: throw (OntoDriverException) e;
67: } else {
68: throw new SesameDriverException(e);
69: }
70: } finally {
71: this.open = false;
72: }
73: }
74:
75: @Override
76: public boolean isOpen() {
77: return open;
78: }
79:
80: Connection acquireConnection() throws SesameDriverException {
81: assert open;
82: final SesameAdapter adapter = new SesameAdapter(connectorFactory.createStorageConnector(configuration),
83: configuration);
84: final SesameConnection c = new SesameConnection(adapter);
85: c.setLists(new SesameLists(adapter, c::ensureOpen, c::commitIfAuto));
86: c.setTypes(new SesameTypes(adapter, c::ensureOpen, c::commitIfAuto));
87: c.setProperties(new SesameProperties(adapter, c::ensureOpen, c::commitIfAuto));
88: openedConnections.add(c);
89: c.addListener(this);
90: return c;
91: }
92:
93: @Override
94: public void connectionClosed(Connection connection) {
95: if (connection == null) {
96: return;
97: }
98: openedConnections.remove(connection);
99: }
100: }