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%

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