-
Notifications
You must be signed in to change notification settings - Fork 14
/
MetamodelImpl.java
182 lines (154 loc) · 6.32 KB
/
MetamodelImpl.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
/**
* Copyright (C) 2020 Czech Technical University in Prague
* <p>
* 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.
* <p>
* 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.exceptions.OWLPersistenceException;
import cz.cvut.kbss.jopa.loaders.PersistenceUnitClassFinder;
import cz.cvut.kbss.jopa.model.metamodel.*;
import cz.cvut.kbss.jopa.query.NamedQueryManager;
import cz.cvut.kbss.jopa.query.ResultSetMappingManager;
import cz.cvut.kbss.jopa.sessions.MetamodelProvider;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.*;
import java.util.regex.Pattern;
public class MetamodelImpl implements Metamodel, MetamodelProvider {
private static final Logger LOG = LoggerFactory.getLogger(Metamodel.class);
private static final String ASPECTJ_CLASS = "org.aspectj.weaver.loadtime.Agent";
private Map<Class<?>, ManagedType<?>> typeMap;
private Map<Class<?>, EntityType<?>> entities;
private Set<Class<?>> inferredClasses;
private TypeReferenceMap typeReferenceMap;
private NamedQueryManager namedQueryManager;
private ResultSetMappingManager resultSetMappingManager;
private final Configuration configuration;
private Set<URI> moduleExtractionSignature;
protected MetamodelImpl() {
// Protected constructor for easier mocking
this.configuration = null;
}
public MetamodelImpl(Configuration configuration) {
this.configuration = Objects.requireNonNull(configuration);
}
/**
* Builds the metamodel for entities discovered by the specified entity loader.
*
* @param classFinder Finder of PU classes
*/
public void build(PersistenceUnitClassFinder classFinder) {
Objects.requireNonNull(classFinder);
LOG.debug("Building metamodel...");
checkForWeaver();
classFinder.scanClasspath(configuration);
final MetamodelBuilder metamodelBuilder = new MetamodelBuilder(configuration);
metamodelBuilder.buildMetamodel(classFinder);
this.typeMap = metamodelBuilder.getTypeMap();
this.entities = metamodelBuilder.getEntities();
this.inferredClasses = metamodelBuilder.getInferredClasses();
this.namedQueryManager = metamodelBuilder.getNamedQueryManager();
this.resultSetMappingManager = metamodelBuilder.getResultSetMappingManager();
this.typeReferenceMap = metamodelBuilder.getTypeReferenceMap();
}
/**
* Check the class path for aspectj weaver, which is vital for using lazy loading.
*/
private static void checkForWeaver() {
try {
MetamodelImpl.class.getClassLoader().loadClass(ASPECTJ_CLASS);
} catch (ClassNotFoundException e) {
LOG.error("AspectJ not found on classpath. Cannot run without AspectJ.");
throw new OWLPersistenceException(e);
}
}
@Override
public <X> EntityTypeImpl<X> entity(Class<X> cls) {
if (!isEntityType(cls)) {
throw new IllegalArgumentException(
"Class " + cls.getName() + " is not a known entity in this persistence unit.");
}
return (EntityTypeImpl<X>) entities.get(cls);
}
@Override
public Set<EntityType<?>> getEntities() {
return new HashSet<>(entities.values());
}
@Override
public Set<ManagedType<?>> getManagedTypes() {
return new HashSet<>(typeMap.values());
}
@Override
public Set<Class<?>> getInferredClasses() {
return Collections.unmodifiableSet(inferredClasses);
}
public NamedQueryManager getNamedQueryManager() {
return namedQueryManager;
}
public ResultSetMappingManager getResultSetMappingManager() {
return resultSetMappingManager;
}
@Override
public Set<URI> getModuleExtractionExtraSignature() {
return Collections.unmodifiableSet(getSignatureInternal());
}
@Override
public void addUriToModuleExtractionSignature(URI uri) {
Objects.requireNonNull(uri);
synchronized (this) {
getSignatureInternal().add(uri);
}
}
private synchronized Set<URI> getSignatureInternal() {
// This can be lazily loaded since we don't know if we'll need it
if (moduleExtractionSignature == null) {
initModuleExtractionSignature();
}
return moduleExtractionSignature;
}
private void initModuleExtractionSignature() {
assert configuration != null;
final String sig = configuration.get(OntoDriverProperties.MODULE_EXTRACTION_SIGNATURE, "");
if (sig.isEmpty()) {
this.moduleExtractionSignature = new HashSet<>();
} else {
final String[] signature = sig.split(Pattern.quote(OntoDriverProperties.SIGNATURE_DELIMITER));
this.moduleExtractionSignature = new HashSet<>(signature.length);
try {
for (String uri : signature) {
moduleExtractionSignature.add(URI.create(uri));
}
} catch (IllegalArgumentException e) {
throw new OWLPersistenceException("Invalid URI encountered in module extraction signature.", e);
}
}
}
@Override
public Metamodel getMetamodel() {
return this;
}
@Override
public boolean isEntityType(Class<?> cls) {
Objects.requireNonNull(cls);
return entities.containsKey(cls);
}
/**
* Gets types which contain an attribute of the specified type.
*
* @param cls Type referred to
* @return Set of referring types, possibly empty
*/
public Set<Class<?>> getReferringTypes(Class<?> cls) {
return typeReferenceMap.getReferringTypes(cls);
}
}