Package: JenaDriver
JenaDriver
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JenaDriver(OntologyStorageProperties, Map) |
|
|
|
|
|
||||||||||||||||||||
acquireConnection() |
|
|
|
|
|
||||||||||||||||||||
buildConnectorFactory(Map) |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
connectionClosed(JenaConnection) |
|
|
|
|
|
||||||||||||||||||||
ensureOpen() |
|
|
|
|
|
||||||||||||||||||||
isOpen() |
|
|
|
|
|
||||||||||||||||||||
lambda$new$0(Map, ConfigurationParameter) |
|
|
|
|
|
||||||||||||||||||||
lambda$new$1(Map, ConfigurationParameter) |
|
|
|
|
|
||||||||||||||||||||
reloadStorage() |
|
|
|
|
|
||||||||||||||||||||
setDataset(Dataset) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2023 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:• switch (isolationStrategy) {
74: case JenaOntoDriverProperties.READ_COMMITTED:
75: return new ReadCommittedConnectorFactory(configuration);
76: case JenaOntoDriverProperties.SNAPSHOT:
77: return new SnapshotConnectorFactory(configuration);
78: default:
79: throw new IllegalArgumentException("Unsupported transaction isolation strategy " + isolationStrategy);
80: }
81: }
82:
83: JenaConnection acquireConnection() {
84: ensureOpen();
85: final StorageConnector connector = connectorFactory.createConnector();
86: final JenaAdapter adapter = new JenaAdapter(connector, connectorFactory.createInferredConnector(connector));
87: final JenaConnection connection = new JenaConnection(adapter);
88: connection.registerListener(this);
89: connection.setAutoCommit(autoCommit);
90: openConnections.add(connection);
91: return connection;
92: }
93:
94: private void ensureOpen() {
95:• if (!open) {
96: throw new IllegalStateException("Driver is closed.");
97: }
98: }
99:
100: @Override
101: public void connectionClosed(JenaConnection connection) {
102: openConnections.remove(connection);
103: }
104:
105: synchronized void reloadStorage() throws JenaDriverException {
106: ensureOpen();
107: try {
108: connectorFactory.reloadStorage();
109: } catch (IllegalStateException e) {
110: throw new JenaDriverException(e);
111: }
112: }
113:
114: synchronized void setDataset(Dataset dataset) throws JenaDriverException {
115: ensureOpen();
116: try {
117: connectorFactory.setDataset(dataset);
118: } catch (IllegalArgumentException | IllegalStateException e) {
119: throw new JenaDriverException(e);
120: }
121: }
122:
123: @Override
124: public synchronized void close() throws OntoDriverException {
125:• if (!open) {
126: return;
127: }
128:• for (JenaConnection connection : openConnections) {
129: connection.close();
130: }
131: connectorFactory.close();
132: this.open = false;
133: }
134:
135: @Override
136: public boolean isOpen() {
137: return open;
138: }
139: }