Skip to content

Method: reloadStorage()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.jena;
19:
20: import cz.cvut.kbss.ontodriver.Closeable;
21: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
22: import cz.cvut.kbss.ontodriver.config.ConfigurationParameter;
23: import cz.cvut.kbss.ontodriver.config.DriverConfigParam;
24: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
25: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
26: import cz.cvut.kbss.ontodriver.jena.config.Constants;
27: import cz.cvut.kbss.ontodriver.jena.config.JenaConfigParam;
28: import cz.cvut.kbss.ontodriver.jena.config.JenaOntoDriverProperties;
29: import cz.cvut.kbss.ontodriver.jena.connector.*;
30: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
31: import cz.cvut.kbss.ontodriver.jena.util.ConnectionListener;
32: import org.apache.jena.query.Dataset;
33:
34: import java.util.*;
35:
36: class JenaDriver implements Closeable, ConnectionListener {
37:
38: private static final List<ConfigurationParameter> CONFIGS = Arrays
39: .asList(DriverConfigParam.AUTO_COMMIT, DriverConfigParam.REASONER_FACTORY_CLASS,
40: JenaConfigParam.ISOLATION_STRATEGY, JenaConfigParam.STORAGE_TYPE,
41: JenaConfigParam.TREAT_DEFAULT_GRAPH_AS_UNION);
42:
43: private volatile boolean open;
44:
45: private final DriverConfiguration configuration;
46: private final ConnectorFactory connectorFactory;
47:
48: private final Set<JenaConnection> openConnections;
49:
50: private final boolean autoCommit;
51:
52: JenaDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
53: assert properties != null;
54: this.configuration = new DriverConfiguration(storageProperties);
55: CONFIGS.stream().filter(c -> properties.containsKey(c.toString()))
56: .forEach(c -> configuration.setProperty(c, properties.get(c.toString())));
57: this.connectorFactory = buildConnectorFactory(properties);
58: this.openConnections = Collections.synchronizedSet(new HashSet<>());
59: this.autoCommit =
60: configuration.isSet(DriverConfigParam.AUTO_COMMIT) ? configuration.is(DriverConfigParam.AUTO_COMMIT) :
61: Constants.DEFAULT_AUTO_COMMIT;
62: this.open = true;
63: }
64:
65: private ConnectorFactory buildConnectorFactory(Map<String, String> properties) {
66: final String isolationStrategy = configuration
67: .getProperty(JenaConfigParam.ISOLATION_STRATEGY, Constants.DEFAULT_ISOLATION_STRATEGY);
68: if (configuration.isSet(DriverConfigParam.REASONER_FACTORY_CLASS)) {
69: // Once reasoner factory is set, this takes precedence, because only this factory is able to provide
70: // proper reasoning support
71: return new InferenceConnectorFactory(configuration, properties);
72: }
73: return switch (isolationStrategy) {
74: case JenaOntoDriverProperties.READ_COMMITTED -> new ReadCommittedConnectorFactory(configuration);
75: case JenaOntoDriverProperties.SNAPSHOT -> new SnapshotConnectorFactory(configuration);
76: default ->
77: throw new IllegalArgumentException("Unsupported transaction isolation strategy " + isolationStrategy);
78: };
79: }
80:
81: JenaConnection acquireConnection() {
82: ensureOpen();
83: final StorageConnector connector = connectorFactory.createConnector();
84: final JenaAdapter adapter = new JenaAdapter(connector, connectorFactory.createInferredConnector(connector));
85: final JenaConnection connection = new JenaConnection(adapter);
86: connection.registerListener(this);
87: connection.setAutoCommit(autoCommit);
88: openConnections.add(connection);
89: return connection;
90: }
91:
92: private void ensureOpen() {
93: if (!open) {
94: throw new IllegalStateException("Driver is closed.");
95: }
96: }
97:
98: @Override
99: public void connectionClosed(JenaConnection connection) {
100: openConnections.remove(connection);
101: }
102:
103: synchronized void reloadStorage() throws JenaDriverException {
104: ensureOpen();
105: try {
106: connectorFactory.reloadStorage();
107: } catch (IllegalStateException e) {
108: throw new JenaDriverException(e);
109: }
110: }
111:
112: synchronized void setDataset(Dataset dataset) throws JenaDriverException {
113: ensureOpen();
114: try {
115: connectorFactory.setDataset(dataset);
116: } catch (IllegalArgumentException | IllegalStateException e) {
117: throw new JenaDriverException(e);
118: }
119: }
120:
121: @Override
122: public synchronized void close() throws OntoDriverException {
123: if (!open) {
124: return;
125: }
126: for (JenaConnection connection : openConnections) {
127: connection.close();
128: }
129: connectorFactory.close();
130: this.open = false;
131: }
132:
133: @Override
134: public boolean isOpen() {
135: return open;
136: }
137: }