Skip to contentMethod: getResultSetMappingManager()
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.sessions;
16:
17: import cz.cvut.kbss.jopa.accessors.DefaultStorageAccessor;
18: import cz.cvut.kbss.jopa.accessors.StorageAccessor;
19: import cz.cvut.kbss.jopa.model.AbstractEntityManager;
20: import cz.cvut.kbss.jopa.model.MetamodelImpl;
21: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
22: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
23: import cz.cvut.kbss.jopa.model.metamodel.Type;
24: import cz.cvut.kbss.jopa.query.NamedQueryManager;
25: import cz.cvut.kbss.jopa.query.ResultSetMappingManager;
26: import cz.cvut.kbss.jopa.sessions.cache.CacheFactory;
27: import cz.cvut.kbss.jopa.transactions.EntityTransaction;
28: import cz.cvut.kbss.jopa.utils.Configuration;
29: import cz.cvut.kbss.jopa.utils.Wrapper;
30: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
31: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
32:
33: import java.net.URI;
34: import java.util.*;
35: import java.util.stream.Collectors;
36:
37: /**
38: * The ServerSession is the primary interface for accessing the ontology.
39: * <p>
40: * It manages an accessor object, which performs the queries.
41: */
42: public class ServerSession extends AbstractSession implements Wrapper {
43:
44: private final MetamodelImpl metamodel;
45: private final Set<Class<?>> managedClasses;
46:
47: private CacheManager liveObjectCache;
48: private StorageAccessor storageAccessor;
49:
50: private Map<EntityTransaction, AbstractEntityManager> runningTransactions;
51:
52: ServerSession() {
53: super(new Configuration(Collections.emptyMap()));
54: this.metamodel = null;
55: this.managedClasses = null;
56: }
57:
58: public ServerSession(OntologyStorageProperties storageProperties, Configuration configuration,
59: MetamodelImpl metamodel) {
60: super(configuration);
61: this.metamodel = metamodel;
62: this.managedClasses = processTypes(metamodel.getEntities());
63: initialize(storageProperties, configuration, metamodel);
64: }
65:
66: /**
67: * Process the entity types and extract simple Java classes from them.
68: *
69: * @param entities Set of managed entity types.
70: * @return Set of managed entity classes.
71: */
72: private Set<Class<?>> processTypes(Set<EntityType<?>> entities) {
73: Set<Class<?>> types = new HashSet<>(entities.size());
74: types.addAll(entities.stream().map(Type::getJavaType).collect(Collectors.toList()));
75: return types;
76: }
77:
78: /**
79: * Initializes this ServerSession. This in particular means initialization of the ontology accessor and live object
80: * cache.
81: *
82: * @param storageProperties Storage properties
83: * @param configuration Session configuration
84: * @param metamodel Metamodel of the managed classes and their attributes.
85: */
86: private void initialize(OntologyStorageProperties storageProperties, Configuration configuration,
87: Metamodel metamodel) {
88: assert configuration != null;
89: assert metamodel != null;
90: this.runningTransactions = new HashMap<>();
91: this.liveObjectCache = CacheFactory.createCache(configuration.getProperties());
92: liveObjectCache.setInferredClasses(metamodel.getInferredClasses());
93: this.storageAccessor = new DefaultStorageAccessor(storageProperties, configuration.getProperties());
94: }
95:
96: @Override
97: protected ConnectionWrapper acquireConnection() {
98: return new ConnectionWrapper(storageAccessor.acquireConnection());
99: }
100:
101: @Override
102: public UnitOfWork acquireUnitOfWork() {
103: return new UnitOfWorkImpl(this);
104: }
105:
106: @Override
107: public CacheManager getLiveObjectCache() {
108: return liveObjectCache;
109: }
110:
111: public boolean transactionStarted(EntityTransaction t, AbstractEntityManager em) {
112: if (!t.isActive() || t.isRollbackOnly()) {
113: return false;
114: }
115: runningTransactions.put(t, em);
116: return true;
117: }
118:
119: public void transactionFinished(EntityTransaction t) {
120: if (t == null) {
121: return;
122: }
123: AbstractEntityManager em = runningTransactions.remove(t);
124: if (em == null) {
125: return;
126: }
127: UnitOfWorkImpl uow = (UnitOfWorkImpl) em.getCurrentPersistenceContext();
128: if (uow != null && uow.hasChanges()) {
129: getLiveObjectCache().clearInferredObjects();
130: }
131: }
132:
133: /**
134: * Close the server session and all connections to the underlying data source.
135: */
136: public void close() {
137: if (!runningTransactions.isEmpty()) {
138: LOG.warn("There are still transactions running. Marking them for rollback.");
139: runningTransactions.keySet().stream().filter(EntityTransaction::isActive)
140: .forEach(EntityTransaction::setRollbackOnly);
141: }
142: if (storageAccessor != null && storageAccessor.isOpen()) {
143: try {
144: storageAccessor.close();
145: } catch (OntoDriverException e) {
146: LOG.error("Exception caught when closing the storage accessor.", e);
147: }
148: }
149: liveObjectCache.close();
150: }
151:
152: @Override
153: public void removeObjectFromCache(Object object, URI context) {
154: // do nothing
155: }
156:
157: @Override
158: public MetamodelImpl getMetamodel() {
159: return metamodel;
160: }
161:
162: @Override
163: public boolean isTypeManaged(Class<?> cls) {
164: return cls != null && managedClasses.contains(cls);
165: }
166:
167: @Override
168: public NamedQueryManager getNamedQueryManager() {
169: return metamodel.getNamedQueryManager();
170: }
171:
172: @Override
173: public ResultSetMappingManager getResultSetMappingManager() {
174: return metamodel.getResultSetMappingManager();
175: }
176:
177: @Override
178: public <T> T unwrap(Class<T> cls) {
179: Objects.requireNonNull(cls);
180: if (cls.isAssignableFrom(getClass())) {
181: return cls.cast(this);
182: }
183: return storageAccessor.unwrap(cls);
184: }
185: }