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: @Override
67: public void close() {
68:• if (!open) {
69: return;
70: }
71: synchronized (this) {
72:• if (!open) {
73: return;
74: }
75: open = false;
76:
77:• for (final EntityManager m : em) {
78:• if (m.isOpen()) {
79: m.close();
80: }
81: }
82: em.clear();
83:• if (serverSession != null) {
84: serverSession.close();
85: this.serverSession = null;
86: }
87: }
88: }
89:
90: @Override
91: public EntityManager createEntityManager() {
92: return this.createEntityManager(Collections.<String, String>emptyMap());
93: }
94:
95: @Override
96: public EntityManager createEntityManager(Map<String, String> map) {
97:• if (!open) {
98: throw new IllegalStateException("The OWLEntityManager has been closed.");
99: }
100:
101: final Map<String, String> newMap = new HashMap<>(map);
102:
103: newMap.putAll(configuration.getProperties());
104:
105: initServerSession();
106:
107: final AbstractEntityManager c = new EntityManagerImpl(this, new Configuration(newMap), this.serverSession);
108:
109: em.add(c);
110: return c;
111: }
112:
113: /**
114: * Initializes the server session if necessary.
115: */
116: private synchronized void initServerSession() {
117:• if (serverSession == null) {
118: this.serverSession = new ServerSession(storageProperties, configuration.getProperties(), metamodel);
119: }
120: }
121:
122: /**
123: * The server session should by initialized by now, but to make sure, there is default initialization with an empty
124: * properties map.
125: *
126: * @return The ServerSession for this factory.
127: */
128: public ServerSession getServerSession() {
129: return serverSession;
130: }
131:
132: @Override
133: public boolean isOpen() {
134: return open;
135: }
136:
137: @Override
138: public Map<String, String> getProperties() {
139: return configuration.getProperties();
140: }
141:
142: public Set<AbstractEntityManager> getEntityManagers() {
143: return Collections.unmodifiableSet(em);
144: }
145:
146: @Override
147: public Metamodel getMetamodel() {
148: return metamodel;
149: }
150:
151: @Override
152: public PersistenceUnitUtil getPersistenceUnitUtil() {
153: return this;
154: }
155:
156: @Override
157: public Object getIdentifier(Object entity) {
158: final EntityType<?> et = getMetamodel().entity(entity.getClass());
159: return EntityPropertiesUtils.getFieldValue(et.getIdentifier().getJavaField(), entity);
160: }
161:
162: @Override
163: public boolean isLoaded(Object entity, String attributeName) {
164:• for (final AbstractEntityManager emi : em) {
165:• if (emi.contains(entity)) {
166:• return attributeName == null || emi.isLoaded(entity, attributeName);
167:
168: }
169: }
170:
171: return false;
172: }
173:
174: @Override
175: public boolean isLoaded(Object entity) {
176: // TODO
177: return false;
178: // return isLoaded(entity);
179: }
180:
181: @Override
182: public Cache getCache() {
183:• if (!isOpen()) {
184: throw new IllegalStateException("The entity manager factory is closed.");
185: }
186: initServerSession();
187: return serverSession.getLiveObjectCache();
188: }
189:
190: }