Skip to content

Method: isOpen()

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 the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.sesame;
14:
15: import cz.cvut.kbss.ontodriver.Closeable;
16: import cz.cvut.kbss.ontodriver.Connection;
17: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
18: import cz.cvut.kbss.ontodriver.config.ConfigParam;
19: import cz.cvut.kbss.ontodriver.config.Configuration;
20: import cz.cvut.kbss.ontodriver.config.ConfigurationParameter;
21: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
22: import cz.cvut.kbss.ontodriver.sesame.config.SesameConfigParam;
23: import cz.cvut.kbss.ontodriver.sesame.connector.ConnectorFactory;
24: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
25:
26: import java.util.*;
27:
28: class SesameDriver implements Closeable, ConnectionListener {
29:
30: private final List<ConfigurationParameter> CONFIGS = Arrays
31: .asList(ConfigParam.AUTO_COMMIT, ConfigParam.ONTOLOGY_LANGUAGE,
32: SesameConfigParam.USE_INFERENCE, SesameConfigParam.USE_VOLATILE_STORAGE);
33:
34: private final Configuration configuration;
35: private boolean open;
36: private final ConnectorFactory connectorFactory;
37:
38: private final Set<SesameConnection> openedConnections;
39:
40: SesameDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
41: assert storageProperties != null;
42: assert properties != null;
43:
44: this.configuration = new Configuration(storageProperties);
45: configuration.addConfiguration(properties, CONFIGS);
46: this.openedConnections = new HashSet<>();
47: this.connectorFactory = ConnectorFactory.getInstance();
48: this.open = true;
49: }
50:
51: @Override
52: public void close() throws OntoDriverException {
53: if (!open) {
54: return;
55: }
56: try {
57: for (SesameConnection c : openedConnections) {
58: c.removeListener(this);
59: c.close();
60: }
61: connectorFactory.close();
62: } catch (Exception e) {
63: if (e instanceof OntoDriverException) {
64: throw (OntoDriverException) e;
65: } else {
66: throw new SesameDriverException(e);
67: }
68: } finally {
69: this.open = false;
70: }
71: }
72:
73: @Override
74: public boolean isOpen() {
75: return open;
76: }
77:
78: Connection acquireConnection() throws SesameDriverException {
79: assert open;
80: final SesameAdapter adapter = new SesameAdapter(connectorFactory.createStorageConnector(configuration),
81: configuration);
82: final SesameConnection c = new SesameConnection(adapter);
83: c.setLists(new SesameLists(adapter, c::ensureOpen, c::commitIfAuto));
84: c.setTypes(new SesameTypes(adapter, c::ensureOpen, c::commitIfAuto));
85: c.setProperties(new SesameProperties(adapter, c::ensureOpen, c::commitIfAuto));
86: openedConnections.add(c);
87: c.addListener(this);
88: return c;
89: }
90:
91: @Override
92: public void connectionClosed(Connection connection) {
93: if (connection == null) {
94: return;
95: }
96: openedConnections.remove(connection);
97: }
98: }