Skip to contentMethod: getSignatureInternal()
1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.model;
14:
15: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
16: import cz.cvut.kbss.jopa.loaders.PersistenceUnitClassFinder;
17: import cz.cvut.kbss.jopa.model.metamodel.*;
18: import cz.cvut.kbss.jopa.query.NamedQueryManager;
19: import cz.cvut.kbss.jopa.query.ResultSetMappingManager;
20: import cz.cvut.kbss.jopa.sessions.MetamodelProvider;
21: import cz.cvut.kbss.jopa.utils.Configuration;
22: import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
23: import org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: import java.net.URI;
27: import java.util.*;
28: import java.util.regex.Pattern;
29:
30: public class MetamodelImpl implements Metamodel, MetamodelProvider {
31:
32: private static final Logger LOG = LoggerFactory.getLogger(Metamodel.class);
33:
34: private static final String ASPECTJ_CLASS = "org.aspectj.weaver.loadtime.Agent";
35:
36: private Map<Class<?>, ManagedType<?>> typeMap;
37: private Map<Class<?>, EntityType<?>> entities;
38: private Set<Class<?>> inferredClasses;
39:
40: private NamedQueryManager namedQueryManager;
41: private ResultSetMappingManager resultSetMappingManager;
42:
43: private final Configuration configuration;
44:
45: private Set<URI> moduleExtractionSignature;
46:
47: protected MetamodelImpl() {
48: // Protected constructor for easier mocking
49: this.configuration = null;
50: }
51:
52: public MetamodelImpl(Configuration configuration) {
53: this.configuration = Objects.requireNonNull(configuration);
54: }
55:
56: /**
57: * Builds the metamodel for entities discovered by the specified entity loader.
58: *
59: * @param classFinder Finder of PU classes
60: */
61: public void build(PersistenceUnitClassFinder classFinder) {
62: Objects.requireNonNull(classFinder);
63: LOG.debug("Building metamodel...");
64: checkForWeaver();
65: classFinder.scanClasspath(configuration);
66:
67: final MetamodelBuilder metamodelBuilder = new MetamodelBuilder(configuration);
68: metamodelBuilder.buildMetamodel(classFinder);
69:
70: this.typeMap = metamodelBuilder.getTypeMap();
71: this.entities = metamodelBuilder.getEntities();
72: this.inferredClasses = metamodelBuilder.getInferredClasses();
73: this.namedQueryManager = metamodelBuilder.getNamedQueryManager();
74: this.resultSetMappingManager = metamodelBuilder.getResultSetMappingManager();
75: }
76:
77: /**
78: * Check the class path for aspectj weaver, which is vital for using lazy loading.
79: */
80: private static void checkForWeaver() {
81: try {
82: MetamodelImpl.class.getClassLoader().loadClass(ASPECTJ_CLASS);
83: } catch (ClassNotFoundException e) {
84: LOG.error("AspectJ not found on classpath. Cannot run without AspectJ.");
85: throw new OWLPersistenceException(e);
86: }
87: }
88:
89: @SuppressWarnings("unchecked")
90: @Override
91: public <X> EntityTypeImpl<X> entity(Class<X> cls) {
92: if (!entities.containsKey(cls)) {
93: throw new IllegalArgumentException(
94: "Class " + cls.getName() + " is not a known entity in this persistence unit.");
95: }
96: return (EntityTypeImpl<X>) typeMap.get(cls);
97: }
98:
99: @Override
100: public Set<EntityType<?>> getEntities() {
101: return new HashSet<>(entities.values());
102: }
103:
104: @Override
105: public Set<ManagedType<?>> getManagedTypes() {
106: return new HashSet<>(typeMap.values());
107: }
108:
109: @Override
110: public Set<Class<?>> getInferredClasses() {
111: return Collections.unmodifiableSet(inferredClasses);
112: }
113:
114: public NamedQueryManager getNamedQueryManager() {
115: return namedQueryManager;
116: }
117:
118: public ResultSetMappingManager getResultSetMappingManager() {
119: return resultSetMappingManager;
120: }
121:
122: @Override
123: public Set<URI> getModuleExtractionExtraSignature() {
124: return Collections.unmodifiableSet(getSignatureInternal());
125: }
126:
127: @Override
128: public void addUriToModuleExtractionSignature(URI uri) {
129: Objects.requireNonNull(uri);
130: synchronized (this) {
131: getSignatureInternal().add(uri);
132: }
133: }
134:
135: private synchronized Set<URI> getSignatureInternal() {
136: // This can be lazily loaded since we don't know if we'll need it
137:• if (moduleExtractionSignature == null) {
138: initModuleExtractionSignature();
139: }
140: return moduleExtractionSignature;
141: }
142:
143: private void initModuleExtractionSignature() {
144: assert configuration != null;
145: final String sig = configuration.get(OntoDriverProperties.MODULE_EXTRACTION_SIGNATURE, "");
146: if (sig.isEmpty()) {
147: this.moduleExtractionSignature = new HashSet<>();
148: } else {
149: final String[] signature = sig.split(Pattern.quote(OntoDriverProperties.SIGNATURE_DELIMITER));
150: this.moduleExtractionSignature = new HashSet<>(signature.length);
151: try {
152: for (String uri : signature) {
153: moduleExtractionSignature.add(URI.create(uri));
154: }
155: } catch (IllegalArgumentException e) {
156: throw new OWLPersistenceException("Invalid URI encountered in module extraction signature.", e);
157: }
158: }
159: }
160:
161: @Override
162: public Metamodel getMetamodel() {
163: return this;
164: }
165:
166: @Override
167: public boolean isEntityType(Class<?> cls) {
168: Objects.requireNonNull(cls);
169: return entities.containsKey(cls);
170: }
171: }