Package: JenaDataSource
JenaDataSource
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JenaDataSource() |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
connect() |
|
|
|
|
|
||||||||||||||||||||
ensureOpen() |
|
|
|
|
|
||||||||||||||||||||
getConnection() |
|
|
|
|
|
||||||||||||||||||||
isOpen() |
|
|
|
|
|
||||||||||||||||||||
reload() |
|
|
|
|
|
||||||||||||||||||||
setDataset(Dataset) |
|
|
|
|
|
||||||||||||||||||||
setProperties(Map) |
|
|
|
|
|
||||||||||||||||||||
setStorageProperties(OntologyStorageProperties) |
|
|
|
|
|
Coverage
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.Connection;
21: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
22: import cz.cvut.kbss.ontodriver.ReloadableDataSource;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
25: import org.apache.jena.query.Dataset;
26:
27: import java.util.Collections;
28: import java.util.Map;
29: import java.util.Objects;
30:
31: public class JenaDataSource implements ReloadableDataSource {
32:
33: private volatile boolean open = true;
34:
35: private OntologyStorageProperties storageProperties;
36: private Map<String, String> properties;
37:
38: private JenaDriver driver;
39:
40: @Override
41: public synchronized Connection getConnection() {
42: ensureOpen();
43:• if (driver == null) {
44: connect();
45: }
46: return driver.acquireConnection();
47: }
48:
49: private void ensureOpen() {
50:• if (!open) {
51: throw new IllegalStateException("The data source is closed.");
52: }
53: }
54:
55: private void connect() {
56:• if (storageProperties == null) {
57: throw new IllegalStateException("Data source cannot connect without ontology storage properties.");
58: }
59:• this.driver = new JenaDriver(storageProperties, properties != null ? properties : Collections.emptyMap());
60: }
61:
62: @Override
63: public synchronized void setStorageProperties(OntologyStorageProperties storageProperties) {
64: this.storageProperties = Objects.requireNonNull(storageProperties);
65: }
66:
67: @Override
68: public synchronized void setProperties(Map<String, String> properties) {
69: this.properties = Objects.requireNonNull(properties);
70: }
71:
72: @Override
73: public void close() throws OntoDriverException {
74:• if (!open) {
75: return;
76: }
77: try {
78:• if (driver != null) {
79: driver.close();
80: }
81: } finally {
82: this.open = false;
83: }
84: }
85:
86: @Override
87: public boolean isOpen() {
88: return open;
89: }
90:
91: @Override
92: public void reload() throws JenaDriverException {
93: ensureOpen();
94: driver.reloadStorage();
95: }
96:
97: /**
98: * Sets dataset in the underlying storage.
99: * <p>
100: * Not that this operation is supported only for in-memory storage.
101: *
102: * @param dataset Dataset to set
103: * @throws JenaDriverException If setting new dataset fails
104: */
105: public void setDataset(Dataset dataset) throws JenaDriverException {
106: ensureOpen();
107: driver.setDataset(dataset);
108: }
109: }