Package: Rdf4jDataSource
Rdf4jDataSource
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Rdf4jDataSource() |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
ensureConnected() |
|
|
|
|
|
||||||||||||||||||||
ensureOpen() |
|
|
|
|
|
||||||||||||||||||||
getConnection() |
|
|
|
|
|
||||||||||||||||||||
isOpen() |
|
|
|
|
|
||||||||||||||||||||
setProperties(Map) |
|
|
|
|
|
||||||||||||||||||||
setRepository(Repository) |
|
|
|
|
|
||||||||||||||||||||
setStorageProperties(OntologyStorageProperties) |
|
|
|
|
|
Coverage
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.rdf4j;
14:
15: import cz.cvut.kbss.ontodriver.Connection;
16: import cz.cvut.kbss.ontodriver.DataSource;
17: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
18: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
19: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
20: import org.eclipse.rdf4j.repository.Repository;
21:
22: import java.util.Collections;
23: import java.util.Map;
24: import java.util.Objects;
25:
26: public class Rdf4jDataSource implements DataSource {
27:
28: private Rdf4jDriver driver;
29: private volatile boolean open = true;
30: private boolean connected;
31:
32: private OntologyStorageProperties storageProperties;
33: private Map<String, String> properties;
34:
35: @Override
36: public synchronized void close() throws OntoDriverException {
37:• if (!open) {
38: return;
39: }
40: try {
41:• if (connected) {
42: driver.close();
43: }
44: } finally {
45: this.open = false;
46: }
47: }
48:
49: @Override
50: public boolean isOpen() {
51: return open;
52: }
53:
54: @Override
55: public synchronized Connection getConnection() throws Rdf4jDriverException {
56: ensureOpen();
57: ensureConnected();
58: return driver.acquireConnection();
59: }
60:
61: private void ensureOpen() {
62:• if (!open) {
63: throw new IllegalStateException("The data source is closed.");
64: }
65: }
66:
67: private void ensureConnected() throws Rdf4jDriverException {
68:• if (connected) {
69: return;
70: }
71:• if (storageProperties == null) {
72: throw new IllegalStateException("Cannot initialize OntoDriver without storageProperties configuration.");
73: }
74:• if (properties == null) {
75: this.properties = Collections.emptyMap();
76: }
77: this.driver = new Rdf4jDriver(storageProperties, properties);
78: this.connected = true;
79: }
80:
81: @Override
82: public void setStorageProperties(OntologyStorageProperties storageProperties) {
83: ensureOpen();
84: this.storageProperties = Objects.requireNonNull(storageProperties);
85: }
86:
87: @Override
88: public void setProperties(Map<String, String> properties) {
89: ensureOpen();
90: this.properties = Objects.requireNonNull(properties);
91: }
92:
93: /**
94: * Sets the underlying repository.
95: * <p>
96: * Note that this functionality is supported only for in-memory stores.
97: *
98: * @param repository The new repository
99: * @throws Rdf4jDriverException When setting repository fails
100: */
101: public synchronized void setRepository(Repository repository) throws Rdf4jDriverException {
102: ensureOpen();
103: ensureConnected();
104: try {
105: driver.setRepository(repository);
106: } catch (IllegalArgumentException | IllegalStateException e) {
107: throw new Rdf4jDriverException(e);
108: }
109: }
110: }