Skip to content

Method: close()

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