-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityManagerFactoryImpl.java
197 lines (163 loc) · 6.2 KB
/
EntityManagerFactoryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.model;
import cz.cvut.kbss.jopa.loaders.PersistenceUnitClassFinder;
import cz.cvut.kbss.jopa.model.metamodel.EntityType;
import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
import cz.cvut.kbss.jopa.model.query.Query;
import cz.cvut.kbss.jopa.sessions.Cache;
import cz.cvut.kbss.jopa.sessions.ServerSession;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class EntityManagerFactoryImpl implements EntityManagerFactory, PersistenceUnitUtil {
private volatile boolean open = true;
private final Set<AbstractEntityManager> em;
private final Configuration configuration;
private final OntologyStorageProperties storageProperties;
private volatile ServerSession serverSession;
private volatile MetamodelImpl metamodel;
public EntityManagerFactoryImpl(final Map<String, String> properties) {
this.em = Collections.newSetFromMap(new ConcurrentHashMap<>());
this.configuration = new Configuration(properties != null ? properties : Collections.emptyMap());
this.storageProperties = initStorageProperties();
initMetamodel();
}
private OntologyStorageProperties initStorageProperties() {
return OntologyStorageProperties.driver(configuration.get(JOPAPersistenceProperties.DATA_SOURCE_CLASS))
.ontologyUri(configuration.get(JOPAPersistenceProperties.ONTOLOGY_URI_KEY))
.physicalUri(
configuration.get(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY))
.username(configuration.get(OntoDriverProperties.DATA_SOURCE_USERNAME))
.password(configuration.get(OntoDriverProperties.DATA_SOURCE_PASSWORD))
.build();
}
private void initMetamodel() {
this.metamodel = new MetamodelImpl(configuration);
metamodel.build(new PersistenceUnitClassFinder());
}
@Override
public void close() {
verifyOpen();
synchronized (this) {
if (!open) {
return;
}
open = false;
em.stream().filter(EntityManager::isOpen).forEach(EntityManager::close);
em.clear();
if (serverSession != null) {
serverSession.close();
this.serverSession = null;
}
}
}
@Override
public EntityManager createEntityManager() {
return this.createEntityManager(Collections.emptyMap());
}
@Override
public EntityManager createEntityManager(Map<String, String> map) {
verifyOpen();
final Map<String, String> newMap = new HashMap<>(map);
newMap.putAll(configuration.getProperties());
initServerSession();
final AbstractEntityManager c = new EntityManagerImpl(this, new Configuration(newMap), this.serverSession);
em.add(c);
return c;
}
private void verifyOpen() {
if (!open) {
throw new IllegalStateException("The entity manager factory is closed.");
}
}
/**
* Initializes the server session if necessary.
*/
private synchronized void initServerSession() {
if (serverSession == null) {
this.serverSession = new ServerSession(storageProperties, configuration, metamodel);
}
}
/**
* The server session should by initialized by now, but to make sure, there is default initialization with an empty
* properties map.
*
* @return The ServerSession for this factory.
*/
public ServerSession getServerSession() {
return serverSession;
}
@Override
public boolean isOpen() {
return open;
}
@Override
public Map<String, String> getProperties() {
verifyOpen();
return configuration.getProperties();
}
@Override
public Metamodel getMetamodel() {
verifyOpen();
return metamodel;
}
@Override
public PersistenceUnitUtil getPersistenceUnitUtil() {
verifyOpen();
return this;
}
@Override
public void addNamedQuery(String name, Query query) {
verifyOpen();
throw new NotYetImplementedException();
}
@Override
public Object getIdentifier(Object entity) {
final EntityType<?> et = getMetamodel().entity(entity.getClass());
return EntityPropertiesUtils.getFieldValue(et.getIdentifier().getJavaField(), entity);
}
@Override
public boolean isLoaded(Object entity, String attributeName) {
for (final AbstractEntityManager emi : em) {
if (emi.contains(entity)) {
return attributeName == null || emi.isLoaded(entity, attributeName);
}
}
return false;
}
@Override
public boolean isLoaded(Object entity) {
// TODO
return false;
// return isLoaded(entity);
}
@Override
public Cache getCache() {
verifyOpen();
initServerSession();
return serverSession.getLiveObjectCache();
}
void entityManagerClosed(AbstractEntityManager manager) {
assert manager != null;
em.remove(manager);
}
}