Skip to contentMethod: SesameDataSource()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
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: *
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:
22: import java.util.Collections;
23: import java.util.Map;
24: import java.util.Objects;
25:
26: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.npxMessage;
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: if (!open) {
59: throw new IllegalStateException("The data source is closed.");
60: }
61: if (!connected) {
62: connect();
63: }
64: return driver.acquireConnection();
65: }
66:
67: @Override
68: public void setStorageProperties(OntologyStorageProperties storageProperties) throws OntoDriverException {
69: this.storageProperties = Objects.requireNonNull(storageProperties, npxMessage("storageProperties"));
70: }
71:
72: @Override
73: public void setProperties(Map<String, String> properties) throws OntoDriverException {
74: this.properties = Objects.requireNonNull(properties, npxMessage("properties"));
75: }
76:
77: private void connect() {
78: if (storageProperties == null) {
79: throw new IllegalStateException("Cannot initialize OntoDriver without storageProperties configuration.");
80: }
81: if (properties == null) {
82: this.properties = Collections.emptyMap();
83: }
84: this.driver = new SesameDriver(storageProperties, properties);
85: this.connected = true;
86: }
87: }