Skip to content

Package: ServerSession

ServerSession

nameinstructionbranchcomplexitylinemethod
ServerSession()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
ServerSession(OntologyStorageProperties, Map, Metamodel)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
acquireConnection()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
acquireUnitOfWork()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
close()
M: 5 C: 30
86%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 9
82%
M: 0 C: 1
100%
deregisterEntityFromPersistenceContext(Object, UnitOfWork)
M: 8 C: 25
76%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 0 C: 6
100%
M: 0 C: 1
100%
getLiveObjectCache()
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%
getMetamodel()
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%
getPersistenceContext(Object)
M: 2 C: 8
80%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
initialize(OntologyStorageProperties, Map, Metamodel)
M: 8 C: 38
83%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 9
100%
M: 0 C: 1
100%
isTypeManaged(Class)
M: 0 C: 11
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%
processTypes(Set)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
registerEntityWithPersistenceContext(Object, UnitOfWorkImpl)
M: 8 C: 34
81%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 7
100%
M: 0 C: 1
100%
removeObjectFromCache(Object, URI)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
removePersistenceContext(UnitOfWorkImpl)
M: 0 C: 30
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
transactionFinished(EntityTransaction)
M: 2 C: 26
93%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 2 C: 8
80%
M: 0 C: 1
100%
transactionStarted(EntityTransaction, AbstractEntityManager)
M: 2 C: 14
88%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 3
75%
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.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.metamodel.EntityType;
21: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
22: import cz.cvut.kbss.jopa.model.metamodel.Type;
23: import cz.cvut.kbss.jopa.sessions.cache.CacheFactory;
24: import cz.cvut.kbss.jopa.transactions.EntityTransaction;
25: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
26: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
27:
28: import java.net.URI;
29: import java.util.HashSet;
30: import java.util.Map;
31: import java.util.Set;
32: import java.util.WeakHashMap;
33: import java.util.stream.Collectors;
34:
35: /**
36: * The ServerSession is the primary interface for accessing the ontology. </p>
37: * <p>
38: * It manages an accessor object, which performs the queries.
39: *
40: * @author kidney
41: */
42: public class ServerSession extends AbstractSession {
43:
44: private final Metamodel metamodel;
45: private final Set<Class<?>> managedClasses;
46:
47: private CacheManager liveObjectCache;
48: private StorageAccessor storageAccessor;
49:
50: private Map<EntityTransaction, AbstractEntityManager> runningTransactions;
51: private Map<Object, UnitOfWorkImpl> activePersistenceContexts;
52: private Map<UnitOfWork, Set<Object>> uowsToEntities;
53:
54: protected ServerSession() {
55: this.metamodel = null;
56: this.managedClasses = null;
57: }
58:
59: public ServerSession(OntologyStorageProperties storageProperties,
60: Map<String, String> properties, Metamodel metamodel) {
61: this.metamodel = metamodel;
62: this.managedClasses = processTypes(metamodel.getEntities());
63: initialize(storageProperties, properties, 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 properties Map of setup properties
84: * @param metamodel Metamodel of the managed classes and their attributes.
85: */
86: private void initialize(OntologyStorageProperties storageProperties,
87: Map<String, String> properties, Metamodel metamodel) {
88:• assert properties != null;
89:• assert metamodel != null;
90: this.runningTransactions = new WeakHashMap<>();
91: this.activePersistenceContexts = new WeakHashMap<>();
92: this.uowsToEntities = new WeakHashMap<>();
93: this.liveObjectCache = CacheFactory.createCache(properties);
94: liveObjectCache.setInferredClasses(metamodel.getInferredClasses());
95: this.storageAccessor = new DefaultStorageAccessor(storageProperties, properties);
96: }
97:
98: protected ConnectionWrapper acquireConnection() {
99: return new ConnectionWrapper(storageAccessor.acquireConnection());
100: }
101:
102: @Override
103: public UnitOfWork acquireUnitOfWork() {
104: return new UnitOfWorkImpl(this);
105: }
106:
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: removePersistenceContext(uow);
132: }
133:
134: /**
135: * Close the server session and all connections to the underlying data source.
136: */
137: public void close() {
138:• if (!runningTransactions.isEmpty()) {
139: LOG.warn("There are still transactions running. Marking them for rollback.");
140: runningTransactions.keySet().stream().filter(EntityTransaction::isActive)
141: .forEach(EntityTransaction::setRollbackOnly);
142: }
143:• if (storageAccessor != null && storageAccessor.isOpen()) {
144: try {
145: storageAccessor.close();
146: } catch (OntoDriverException e) {
147: LOG.error("Exception caught when closing the storage accessor.", e);
148: }
149: }
150: liveObjectCache.close();
151: }
152:
153: @Override
154: public void removeObjectFromCache(Object object, URI context) {
155: // do nothing
156: }
157:
158: @Override
159: public Metamodel getMetamodel() {
160: return metamodel;
161: }
162:
163: @Override
164: public boolean isTypeManaged(Class<?> cls) {
165:• return cls != null && managedClasses.contains(cls);
166: }
167:
168: /**
169: * Register the specified entity as managed in the specified {@code UnitOfWork}. </p>
170: * <p>
171: * Registering loaded entities with their owning {@code UnitOfWork} is highly recommended, since it speeds up
172: * persistence context lookup when entity attributes are modified.
173: *
174: * @param entity The entity to register
175: * @param uow Persistence context of the specified entity
176: */
177: protected synchronized void registerEntityWithPersistenceContext(Object entity,
178: UnitOfWorkImpl uow) {
179:• assert entity != null;
180:• assert uow != null;
181:
182: activePersistenceContexts.put(entity, uow);
183:• if (!uowsToEntities.containsKey(uow)) {
184: uowsToEntities.put(uow, new HashSet<>());
185: }
186: uowsToEntities.get(uow).add(entity);
187: }
188:
189: @Override
190: synchronized void deregisterEntityFromPersistenceContext(Object entity, UnitOfWork uow) {
191:• assert entity != null;
192:• assert uow != null;
193: activePersistenceContexts.remove(entity);
194:• if (uowsToEntities.containsKey(uow)) {
195: uowsToEntities.get(uow).remove(entity);
196: }
197: }
198:
199: /**
200: * Get persistence context for the specified entity. </p>
201: *
202: * @param entity The entity
203: * @return Persistence context of the specified entity or null, if it cannot be found
204: */
205: public synchronized UnitOfWorkImpl getPersistenceContext(Object entity) {
206:• if (entity == null) {
207: return null;
208: }
209: return activePersistenceContexts.get(entity);
210: }
211:
212: /**
213: * Remove the specified {@code UnitOfWork} from the list of currently active persistence contexts. </p>
214: * <p>
215: * Also remove all the objects associated with this persistence context.
216: *
217: * @param uow The persistence context to remove
218: */
219: private void removePersistenceContext(UnitOfWorkImpl uow) {
220:• if (uowsToEntities.containsKey(uow)) {
221:• for (Object entity : uowsToEntities.get(uow)) {
222: activePersistenceContexts.remove(entity);
223: }
224: }
225: uowsToEntities.remove(uow);
226: }
227: }