Skip to content

Method: ensureOpen()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.owlapi;
19:
20: import cz.cvut.kbss.ontodriver.Connection;
21: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
22: import cz.cvut.kbss.ontodriver.ReloadableDataSource;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24:
25: import java.util.Collections;
26: import java.util.Map;
27: import java.util.Objects;
28:
29: /**
30: * Main entry point to this OWLAPI-based OntoDriver.
31: *
32: * @author ledvima1
33: */
34: public class OwlapiDataSource implements ReloadableDataSource {
35:
36: private OntologyStorageProperties storageProperties;
37: private Map<String, String> properties;
38:
39: private volatile boolean open = true;
40: private boolean connected = false;
41:
42: private OwlapiDriver driver;
43:
44: @Override
45: public synchronized Connection getConnection() throws OntoDriverException {
46: ensureOpen();
47: if (storageProperties == null) {
48: throw new IllegalStateException("OntoDriver is not properly initialized. Cannot acquire connection.");
49: }
50: if (!connected) {
51: connect();
52: }
53: return driver.acquireConnection();
54: }
55:
56: private void ensureOpen() {
57:• if (!open) {
58: throw new IllegalStateException("The OntoDriver is closed.");
59: }
60: }
61:
62: private void connect() {
63: this.driver = new OwlapiDriver(storageProperties, properties != null ? properties : Collections.emptyMap());
64: this.connected = true;
65: }
66:
67: @Override
68: public void setStorageProperties(OntologyStorageProperties storageProperties) {
69: this.storageProperties = Objects.requireNonNull(storageProperties);
70: }
71:
72: @Override
73: public void setProperties(Map<String, String> properties) {
74: this.properties = Objects.requireNonNull(properties);
75: }
76:
77: @Override
78: public synchronized void reload() throws OntoDriverException {
79: ensureOpen();
80: if (connected) {
81: driver.reloadData();
82: }
83: }
84:
85: @Override
86: public synchronized void close() throws OntoDriverException {
87: if (!open) {
88: return;
89: }
90: try {
91: if (connected) {
92: driver.close();
93: }
94: } finally {
95: this.open = false;
96: }
97: }
98:
99: @Override
100: public boolean isOpen() {
101: return open;
102: }
103: }