Skip to content

Package: EntityManagerFactoryImpl

EntityManagerFactoryImpl

nameinstructionbranchcomplexitylinemethod
EntityManagerFactoryImpl(Map)
M: 1 C: 27
96%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 8
100%
M: 0 C: 1
100%
close()
M: 6 C: 44
88%
M: 4 C: 6
60%
M: 4 C: 2
33%
M: 3 C: 13
81%
M: 0 C: 1
100%
createEntityManager()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createEntityManager(Map)
M: 5 C: 33
87%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 7
88%
M: 0 C: 1
100%
getCache()
M: 5 C: 9
64%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
getEntityManagers()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getIdentifier(Object)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
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%
getPersistenceUnitUtil()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getProperties()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getServerSession()
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%
initMetamodel()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
initServerSession()
M: 0 C: 16
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
initStorageProperties()
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
isLoaded(Object)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isLoaded(Object, String)
M: 29 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
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.model;
16:
17: import cz.cvut.kbss.jopa.loaders.EntityLoader;
18: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
19: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
20: import cz.cvut.kbss.jopa.sessions.Cache;
21: import cz.cvut.kbss.jopa.sessions.ServerSession;
22: import cz.cvut.kbss.jopa.utils.Configuration;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
25:
26: import java.util.Collections;
27: import java.util.HashMap;
28: import java.util.Map;
29: import java.util.Set;
30: import java.util.concurrent.ConcurrentHashMap;
31:
32: public class EntityManagerFactoryImpl implements EntityManagerFactory, PersistenceUnitUtil {
33:
34: private volatile boolean open = true;
35:
36: private final Set<AbstractEntityManager> em;
37: private final Configuration configuration;
38: private final OntologyStorageProperties storageProperties;
39:
40: private volatile ServerSession serverSession;
41:
42: private volatile MetamodelImpl metamodel;
43:
44: public EntityManagerFactoryImpl(final Map<String, String> properties) {
45: this.em = Collections
46: .newSetFromMap(new ConcurrentHashMap<>());
47:• this.configuration = new Configuration(properties != null ? properties : Collections.emptyMap());
48: this.storageProperties = initStorageProperties();
49: initMetamodel();
50: }
51:
52: private OntologyStorageProperties initStorageProperties() {
53: return OntologyStorageProperties.driver(configuration.get(JOPAPersistenceProperties.DATA_SOURCE_CLASS))
54: .ontologyUri(configuration.get(JOPAPersistenceProperties.ONTOLOGY_URI_KEY))
55: .physicalUri(configuration
56: .get(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY))
57: .username(configuration.get(JOPAPersistenceProperties.DATA_SOURCE_USERNAME))
58: .password(configuration.get(JOPAPersistenceProperties.DATA_SOURCE_PASSWORD))
59: .build();
60: }
61:
62: private void initMetamodel() {
63: this.metamodel = new MetamodelImpl(configuration, new EntityLoader());
64: }
65:
66: public void close() {
67:• if (!open) {
68: return;
69: }
70: synchronized (this) {
71:• if (!open) {
72: return;
73: }
74: open = false;
75:
76:• for (final EntityManager m : em) {
77:• if (m.isOpen()) {
78: m.close();
79: }
80: }
81: em.clear();
82:• if (serverSession != null) {
83: serverSession.close();
84: this.serverSession = null;
85: }
86: }
87: }
88:
89: public EntityManager createEntityManager() {
90: return this.createEntityManager(Collections.<String, String>emptyMap());
91: }
92:
93: public EntityManager createEntityManager(Map<String, String> map) {
94:• if (!open) {
95: throw new IllegalStateException("The OWLEntityManager has been closed.");
96: }
97:
98: final Map<String, String> newMap = new HashMap<>(map);
99:
100: newMap.putAll(configuration.getProperties());
101:
102: initServerSession();
103:
104: final AbstractEntityManager c = new EntityManagerImpl(this, new Configuration(newMap), this.serverSession);
105:
106: em.add(c);
107: return c;
108: }
109:
110: /**
111: * Initializes the server session if necessary.
112: */
113: private synchronized void initServerSession() {
114:• if (serverSession == null) {
115: this.serverSession = new ServerSession(storageProperties, configuration.getProperties(), metamodel);
116: }
117: }
118:
119: /**
120: * The server session should by initialized by now, but to make sure, there is default initialization with an empty
121: * properties map.
122: *
123: * @return The ServerSession for this factory.
124: */
125: public ServerSession getServerSession() {
126: return serverSession;
127: }
128:
129: public boolean isOpen() {
130: return open;
131: }
132:
133: public Map<String, String> getProperties() {
134: return configuration.getProperties();
135: }
136:
137: public Set<AbstractEntityManager> getEntityManagers() {
138: return Collections.unmodifiableSet(em);
139: }
140:
141: public Metamodel getMetamodel() {
142: return metamodel;
143: }
144:
145: public PersistenceUnitUtil getPersistenceUnitUtil() {
146: return this;
147: }
148:
149: public Object getIdentifier(Object entity) {
150: final EntityType<?> et = getMetamodel().entity(entity.getClass());
151: return EntityPropertiesUtils.getFieldValue(et.getIdentifier().getJavaField(), entity);
152: }
153:
154: public boolean isLoaded(Object entity, String attributeName) {
155:• for (final AbstractEntityManager emi : em) {
156:• if (emi.contains(entity)) {
157:• return attributeName == null || emi.isLoaded(entity, attributeName);
158:
159: }
160: }
161:
162: return false;
163: }
164:
165: public boolean isLoaded(Object entity) {
166: // TODO
167: return false;
168: // return isLoaded(entity);
169: }
170:
171: public Cache getCache() {
172:• if (!isOpen()) {
173: throw new IllegalStateException("The entity manager factory is closed.");
174: }
175: initServerSession();
176: return serverSession.getLiveObjectCache();
177: }
178:
179: }