-
Notifications
You must be signed in to change notification settings - Fork 14
/
MetamodelImpl.java
160 lines (134 loc) · 5.59 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
/**
* Copyright (C) 2016 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.EntityLoader;
import cz.cvut.kbss.jopa.model.metamodel.*;
import cz.cvut.kbss.jopa.query.NamedQueryManager;
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 {
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 NamedQueryManager namedQueryManager;
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 entityLoader Loader of entity classes
*/
public void build(EntityLoader entityLoader) {
Objects.requireNonNull(entityLoader);
LOG.debug("Building metamodel...");
checkForWeaver();
final Set<Class<?>> discoveredEntities = entityLoader.discoverEntityClasses(configuration);
final MetamodelBuilder metamodelBuilder = new MetamodelBuilder();
metamodelBuilder.buildMetamodel(discoveredEntities);
this.typeMap = metamodelBuilder.getTypeMap();
this.entities = metamodelBuilder.getEntities();
this.inferredClasses = metamodelBuilder.getInferredClasses();
this.namedQueryManager = metamodelBuilder.getNamedQueryManager();
}
/**
* Check the class path for aspectj weaver, which is vital for using lazy loading.
*/
private 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);
}
}
@SuppressWarnings("unchecked")
@Override
public <X> EntityTypeImpl<X> entity(Class<X> cls) {
if (!entities.containsKey(cls)) {
throw new IllegalArgumentException(
"Class " + cls.getName() + " is not a known entity in this persistence unit.");
}
return (EntityTypeImpl<X>) typeMap.get(cls);
}
@Override
public <X> EmbeddableType<X> embeddable(Class<X> cls) {
throw new IllegalArgumentException("Embeddable entities not supported.");
}
@Override
public Set<EmbeddableType<?>> getEmbeddables() {
return Collections.emptySet();
}
@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;
}
@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'attributeType know if we'll need it
if (moduleExtractionSignature == 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<>((int) (signature.length / 0.75 + 1));
try {
for (String uri : signature) {
moduleExtractionSignature.add(URI.create(uri));
}
} catch (IllegalArgumentException e) {
throw new OWLPersistenceException("Invalid URI encountered in module extraction signature.", e);
}
}
}
return moduleExtractionSignature;
}
}