Skip to content

Method: SesameDriver(OntologyStorageProperties, Map)

1: /**
2: * Copyright (C) 2022 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.RuntimeConfiguration;
23: import cz.cvut.kbss.ontodriver.sesame.config.SesameConfigParam;
24: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
25: import cz.cvut.kbss.ontodriver.sesame.connector.ConnectorFactory;
26: import cz.cvut.kbss.ontodriver.sesame.connector.ConnectorFactoryImpl;
27: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
28: import cz.cvut.kbss.ontodriver.sesame.loader.DefaultContextInferenceStatementLoaderFactory;
29: import cz.cvut.kbss.ontodriver.sesame.loader.DefaultStatementLoaderFactory;
30: import cz.cvut.kbss.ontodriver.sesame.loader.GraphDBStatementLoaderFactory;
31: import cz.cvut.kbss.ontodriver.sesame.loader.StatementLoaderFactory;
32: import org.eclipse.rdf4j.repository.Repository;
33:
34: import java.util.*;
35:
36: class SesameDriver implements Closeable, ConnectionListener<SesameConnection> {
37:
38: private static final List<ConfigurationParameter> CONFIGS = Arrays
39: .asList(DriverConfigParam.AUTO_COMMIT, SesameConfigParam.USE_INFERENCE,
40: SesameConfigParam.USE_VOLATILE_STORAGE, SesameConfigParam.LOAD_ALL_THRESHOLD,
41: SesameConfigParam.RECONNECT_ATTEMPTS, SesameConfigParam.REPOSITORY_CONFIG,
42: SesameConfigParam.INFERENCE_IN_DEFAULT_CONTEXT);
43:
44: private final DriverConfiguration configuration;
45: private boolean open;
46: private final ConnectorFactory connectorFactory;
47: private final StatementLoaderFactory statementLoaderFactory;
48:
49: private final Set<SesameConnection> openedConnections;
50:
51: SesameDriver(OntologyStorageProperties storageProperties,
52: Map<String, String> properties) throws SesameDriverException {
53:• assert storageProperties != null;
54:• assert properties != null;
55:
56: this.configuration = new DriverConfiguration(storageProperties);
57: configuration.addConfiguration(properties, CONFIGS);
58: this.openedConnections = new HashSet<>();
59: this.connectorFactory = initConnectorFactory(configuration);
60: this.statementLoaderFactory = initStatementLoaderFactory(connectorFactory);
61: this.open = true;
62: }
63:
64: private ConnectorFactory initConnectorFactory(DriverConfiguration configuration) throws SesameDriverException {
65: return new ConnectorFactoryImpl(configuration);
66: }
67:
68: private StatementLoaderFactory initStatementLoaderFactory(
69: ConnectorFactory connectorFactory) throws SesameDriverException {
70: if (configuration.is(SesameConfigParam.INFERENCE_IN_DEFAULT_CONTEXT)) {
71: return new DefaultContextInferenceStatementLoaderFactory();
72: }
73: final Connector connector = connectorFactory.createStorageConnector();
74: if (GraphDBStatementLoaderFactory.isRepositoryGraphDB(connector)) {
75: return new GraphDBStatementLoaderFactory();
76: }
77: return new DefaultStatementLoaderFactory();
78: }
79:
80: @Override
81: public void close() throws OntoDriverException {
82: if (!open) {
83: return;
84: }
85: try {
86: for (SesameConnection c : openedConnections) {
87: c.removeListener();
88: c.close();
89: }
90: connectorFactory.close();
91: } catch (OntoDriverException e) {
92: throw e;
93: } catch (Exception e) {
94: throw new SesameDriverException(e);
95: } finally {
96: this.open = false;
97: }
98: }
99:
100: @Override
101: public boolean isOpen() {
102: return open;
103: }
104:
105: Connection acquireConnection() {
106: assert open;
107: final RuntimeConfiguration config = new RuntimeConfiguration(configuration);
108: config.setStatementLoaderFactory(statementLoaderFactory);
109: final SesameAdapter adapter = new SesameAdapter(connectorFactory.createStorageConnector(), config);
110: final SesameConnection c = new SesameConnection(adapter);
111: c.setLists(new SesameLists(adapter, c::ensureOpen, c::commitIfAuto));
112: c.setTypes(new SesameTypes(adapter, c::ensureOpen, c::commitIfAuto));
113: c.setProperties(new SesameProperties(adapter, c::ensureOpen, c::commitIfAuto));
114: openedConnections.add(c);
115: c.setListener(this);
116: return c;
117: }
118:
119: @Override
120: public void connectionClosed(SesameConnection connection) {
121: if (connection == null) {
122: return;
123: }
124: openedConnections.remove(connection);
125: }
126:
127: /**
128: * Sets the underlying repository.
129: * <p>
130: * Note that this functionality is supported only for in-memory stores.
131: *
132: * @param repository The new repository
133: */
134: void setRepository(Repository repository) throws SesameDriverException {
135: assert open;
136: connectorFactory.setRepository(repository);
137: }
138: }