Skip to content

Method: close()

1: /**
2: * Copyright (C) 2022 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.OntologyStorageProperties;
19: import cz.cvut.kbss.ontodriver.ReloadableDataSource;
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: /**
27: * Main entry point to this OWLAPI-based OntoDriver.
28: *
29: * @author ledvima1
30: */
31: public class OwlapiDataSource implements ReloadableDataSource {
32:
33: private OntologyStorageProperties storageProperties;
34: private Map<String, String> properties;
35:
36: private volatile boolean open = true;
37: private boolean connected = false;
38:
39: private OwlapiDriver driver;
40:
41: @Override
42: public synchronized Connection getConnection() throws OntoDriverException {
43: ensureOpen();
44: if (storageProperties == null) {
45: throw new IllegalStateException("OntoDriver is not properly initialized. Cannot acquire connection.");
46: }
47: if (!connected) {
48: connect();
49: }
50: return driver.acquireConnection();
51: }
52:
53: private void ensureOpen() {
54: if (!open) {
55: throw new IllegalStateException("The OntoDriver is closed.");
56: }
57: }
58:
59: private void connect() {
60: this.driver = new OwlapiDriver(storageProperties, properties != null ? properties : Collections.emptyMap());
61: this.connected = true;
62: }
63:
64: @Override
65: public void setStorageProperties(OntologyStorageProperties storageProperties) {
66: this.storageProperties = Objects.requireNonNull(storageProperties);
67: }
68:
69: @Override
70: public void setProperties(Map<String, String> properties) {
71: this.properties = Objects.requireNonNull(properties);
72: }
73:
74: @Override
75: public synchronized void reload() throws OntoDriverException {
76: ensureOpen();
77: if (connected) {
78: driver.reloadData();
79: }
80: }
81:
82: @Override
83: public synchronized void close() throws OntoDriverException {
84:• if (!open) {
85: return;
86: }
87: try {
88:• if (connected) {
89: driver.close();
90: }
91: } finally {
92: this.open = false;
93: }
94: }
95:
96: @Override
97: public boolean isOpen() {
98: return open;
99: }
100: }