Skip to content

Package: DefaultStorageAccessor

DefaultStorageAccessor

nameinstructionbranchcomplexitylinemethod
DefaultStorageAccessor(OntologyStorageProperties, Map)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
acquireConnection()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
close()
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
initDataSource(OntologyStorageProperties, Map)
M: 0 C: 48
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 12
100%
M: 0 C: 1
100%
isOpen()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
unwrap(Class)
M: 0 C: 34
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

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.jopa.accessors;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
18: import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
19: import cz.cvut.kbss.jopa.utils.ReflectionUtils;
20: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
21: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
22: import cz.cvut.kbss.ontodriver.Connection;
23: import cz.cvut.kbss.ontodriver.DataSource;
24:
25: import java.util.Map;
26:
27: public class DefaultStorageAccessor implements StorageAccessor {
28:
29: private final DataSource dataSource;
30: private boolean open;
31:
32: public DefaultStorageAccessor(OntologyStorageProperties storageProperties, Map<String, String> properties) {
33: this.dataSource = initDataSource(storageProperties, properties);
34: this.open = true;
35: }
36:
37: private static DataSource initDataSource(OntologyStorageProperties storageProperties, Map<String, String> properties) {
38: final Class<?> dataSourceCls;
39: try {
40: dataSourceCls = Class.forName(storageProperties.getDriver());
41: DataSource ds = (DataSource) ReflectionUtils.instantiateUsingDefaultConstructor(dataSourceCls);
42: ds.setStorageProperties(storageProperties);
43:• if (properties != null) {
44: ds.setProperties(properties);
45: }
46: return ds;
47: } catch (ClassNotFoundException e) {
48: throw new DataSourceCreationException(
49: "Unable to find OntoDriver data source class " + storageProperties.getDriver(), e);
50: } catch (cz.cvut.kbss.jopa.exception.InstantiationException | OntoDriverException e) {
51: throw new DataSourceCreationException(
52: "Unable to create instance of OntoDriver data source " + storageProperties.getDriver(), e);
53: }
54: }
55:
56: @Override
57: public Connection acquireConnection() {
58: try {
59: final Connection conn = dataSource.getConnection();
60: conn.setAutoCommit(false);
61: return conn;
62: } catch (OntoDriverException e) {
63: throw new StorageAccessException("Unable to acquire storage connection.", e);
64: }
65: }
66:
67: @Override
68: public void close() {
69:• if (!open) {
70: return;
71: }
72: try {
73: dataSource.close();
74: } catch (OntoDriverException e) {
75: throw new StorageAccessException("Error when closing the data source.", e);
76: } finally {
77: this.open = false;
78: }
79: }
80:
81: @Override
82: public boolean isOpen() {
83: return open;
84: }
85:
86: @Override
87: public <T> T unwrap(Class<T> cls) {
88:• if (cls.isAssignableFrom(getClass())) {
89: return cls.cast(this);
90:• } else if (cls.isAssignableFrom(dataSource.getClass())) {
91: return cls.cast(dataSource);
92: }
93: throw new OWLPersistenceException("Instance of class " + cls + " not found.");
94: }
95: }