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