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: 36
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: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

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