Skip to content

Package: DefaultStorageAccessor

DefaultStorageAccessor

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