Skip to content

Method: getConnection()

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:
22: import java.util.Collections;
23: import java.util.Map;
24: import java.util.Objects;
25:
26: public class SesameDataSource implements DataSource {
27:
28: private SesameDriver driver;
29: private volatile boolean open = true;
30: private boolean connected;
31:
32: private OntologyStorageProperties storageProperties;
33: private Map<String, String> properties;
34:
35: @Override
36: public synchronized void close() throws OntoDriverException {
37: if (!open) {
38: return;
39: }
40: try {
41: if (connected) {
42: driver.close();
43: }
44: } finally {
45: this.open = false;
46: }
47: }
48:
49: @Override
50: public boolean isOpen() {
51: return open;
52: }
53:
54: @Override
55: public synchronized Connection getConnection() throws OntoDriverException {
56:• if (!open) {
57: throw new IllegalStateException("The data source is closed.");
58: }
59:• if (!connected) {
60: connect();
61: }
62: return driver.acquireConnection();
63: }
64:
65: @Override
66: public void setStorageProperties(OntologyStorageProperties storageProperties) throws OntoDriverException {
67: this.storageProperties = Objects.requireNonNull(storageProperties);
68: }
69:
70: @Override
71: public void setProperties(Map<String, String> properties) throws OntoDriverException {
72: this.properties = Objects.requireNonNull(properties);
73: }
74:
75: private void connect() {
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 SesameDriver(storageProperties, properties);
83: this.connected = true;
84: }
85: }