Skip to content

Method: getConnection()

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.owlapi;
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: /**
29: * Main entry point to this OWLAPI-based OntoDriver.
30: *
31: * @author ledvima1
32: */
33: public class OwlapiDataSource implements DataSource {
34:
35: private OntologyStorageProperties storageProperties;
36: private Map<String, String> properties;
37:
38: private volatile boolean open = true;
39: private boolean connected = false;
40:
41: private OwlapiDriver driver;
42:
43: @Override
44: public synchronized Connection getConnection() throws OntoDriverException {
45: ensureOpen();
46:• if (storageProperties == null) {
47: throw new IllegalStateException("OntoDriver is not properly initialized. Cannot acquire connection.");
48: }
49:• if (!connected) {
50: connect();
51: }
52: return driver.acquireConnection();
53: }
54:
55: private void ensureOpen() {
56: if (!open) {
57: throw new IllegalStateException("The OntoDriver is closed.");
58: }
59: }
60:
61: private void connect() {
62: this.driver = new OwlapiDriver(storageProperties,
63: properties != null ? properties : Collections.<String, String>emptyMap());
64: this.connected = true;
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: @Override
78: public synchronized void close() throws OntoDriverException {
79: if (!open) {
80: return;
81: }
82: try {
83: if (connected) {
84: driver.close();
85: }
86: } finally {
87: this.open = false;
88: }
89: }
90:
91: @Override
92: public boolean isOpen() {
93: return open;
94: }
95: }