Skip to contentMethod: connectionClosed(Connection)
1: /**
2: * Copyright (C) 2019 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.ConfigurationParameter;
19: import cz.cvut.kbss.ontodriver.config.DriverConfigParam;
20: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
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: import org.eclipse.rdf4j.repository.Repository;
26:
27: import java.util.*;
28:
29: class SesameDriver implements Closeable, ConnectionListener {
30:
31: private static final List<ConfigurationParameter> CONFIGS = Arrays
32: .asList(DriverConfigParam.AUTO_COMMIT, SesameConfigParam.USE_INFERENCE,
33: SesameConfigParam.USE_VOLATILE_STORAGE);
34:
35: private final DriverConfiguration configuration;
36: private boolean open;
37: private final ConnectorFactory connectorFactory;
38:
39: private final Set<SesameConnection> openedConnections;
40:
41: SesameDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
42: assert storageProperties != null;
43: assert properties != null;
44:
45: this.configuration = new DriverConfiguration(storageProperties);
46: configuration.addConfiguration(properties, CONFIGS);
47: this.openedConnections = new HashSet<>();
48: this.connectorFactory = ConnectorFactory.getInstance();
49: this.open = true;
50: }
51:
52: @Override
53: public void close() throws OntoDriverException {
54: if (!open) {
55: return;
56: }
57: try {
58: for (SesameConnection c : openedConnections) {
59: c.removeListener();
60: c.close();
61: }
62: connectorFactory.close();
63: } catch (OntoDriverException e) {
64: throw e;
65: } catch (Exception e) {
66: throw new SesameDriverException(e);
67: } finally {
68: this.open = false;
69: }
70: }
71:
72: @Override
73: public boolean isOpen() {
74: return open;
75: }
76:
77: Connection acquireConnection() throws SesameDriverException {
78: assert open;
79: final SesameAdapter adapter = new SesameAdapter(connectorFactory.createStorageConnector(configuration),
80: configuration);
81: final SesameConnection c = new SesameConnection(adapter);
82: c.setLists(new SesameLists(adapter, c::ensureOpen, c::commitIfAuto));
83: c.setTypes(new SesameTypes(adapter, c::ensureOpen, c::commitIfAuto));
84: c.setProperties(new SesameProperties(adapter, c::ensureOpen, c::commitIfAuto));
85: openedConnections.add(c);
86: c.setListener(this);
87: return c;
88: }
89:
90: @Override
91: public void connectionClosed(Connection connection) {
92:• if (connection == null) {
93: return;
94: }
95: openedConnections.remove(connection);
96: }
97:
98: /**
99: * Sets the underlying repository.
100: * <p>
101: * Note that this functionality is supported only for in-memory stores.
102: *
103: * @param repository The new repository
104: */
105: void setRepository(Repository repository) throws SesameDriverException {
106: assert open;
107: connectorFactory.setRepository(repository, configuration);
108: }
109: }