Skip to content

Method: setStorageProperties(OntologyStorageProperties)

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