Skip to content

Method: isOpen()

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