Skip to content

Method: isOpen()

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import cz.cvut.kbss.ontodriver.Connection;
18: import cz.cvut.kbss.ontodriver.DataSource;
19: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
20: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
21: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
22: import org.eclipse.rdf4j.repository.Repository;
23:
24: import java.util.Collections;
25: import java.util.Map;
26: import java.util.Objects;
27:
28: public class SesameDataSource implements DataSource {
29:
30: private SesameDriver driver;
31: private volatile boolean open = true;
32: private boolean connected;
33:
34: private OntologyStorageProperties storageProperties;
35: private Map<String, String> properties;
36:
37: @Override
38: public synchronized void close() throws OntoDriverException {
39: if (!open) {
40: return;
41: }
42: try {
43: if (connected) {
44: driver.close();
45: }
46: } finally {
47: this.open = false;
48: }
49: }
50:
51: @Override
52: public boolean isOpen() {
53: return open;
54: }
55:
56: @Override
57: public synchronized Connection getConnection() throws OntoDriverException {
58: ensureOpen();
59: ensureConnected();
60: return driver.acquireConnection();
61: }
62:
63: private void ensureOpen() {
64: if (!open) {
65: throw new IllegalStateException("The data source is closed.");
66: }
67: }
68:
69: private void ensureConnected() {
70: if (connected) {
71: return;
72: }
73: if (storageProperties == null) {
74: throw new IllegalStateException("Cannot initialize OntoDriver without storageProperties configuration.");
75: }
76: if (properties == null) {
77: this.properties = Collections.emptyMap();
78: }
79: this.driver = new SesameDriver(storageProperties, properties);
80: this.connected = true;
81: }
82:
83: @Override
84: public void setStorageProperties(OntologyStorageProperties storageProperties) {
85: ensureOpen();
86: this.storageProperties = Objects.requireNonNull(storageProperties);
87: }
88:
89: @Override
90: public void setProperties(Map<String, String> properties) {
91: ensureOpen();
92: this.properties = Objects.requireNonNull(properties);
93: }
94:
95: /**
96: * Sets the underlying repository.
97: * <p>
98: * Note that this functionality is supported only for in-memory stores.
99: *
100: * @param repository The new repository
101: */
102: public synchronized void setRepository(Repository repository) throws SesameDriverException {
103: ensureOpen();
104: ensureConnected();
105: try {
106: driver.setRepository(repository);
107: } catch (IllegalArgumentException | IllegalStateException e) {
108: throw new SesameDriverException(e);
109: }
110: }
111: }