Skip to content

Method: getEmbeddables()

1: /**
2: * Copyright (C) 2016 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.exception.MetamodelInitializationException;
16: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
17: import cz.cvut.kbss.jopa.loaders.EntityLoader;
18: import cz.cvut.kbss.jopa.model.metamodel.*;
19: import cz.cvut.kbss.jopa.utils.Configuration;
20: import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.lang.reflect.Field;
25: import java.net.URI;
26: import java.net.URISyntaxException;
27: import java.util.*;
28:
29: public class MetamodelImpl implements Metamodel {
30:
31: private static final Logger LOG = LoggerFactory.getLogger(Metamodel.class);
32:
33: private static final String ASPECTJ_CLASS = "org.aspectj.weaver.loadtime.Agent";
34:
35: private final Map<Class<?>, EntityType<?>> typeMap = new HashMap<>();
36:
37: private final Set<Class<?>> inferredClasses = new HashSet<>();
38:
39: private final Configuration configuration;
40:
41: private Set<URI> moduleExtractionSignature;
42:
43: private final Set<Class<?>> entities = new HashSet<>();
44:
45: public MetamodelImpl(Configuration configuration, EntityLoader entityLoader) {
46: this.configuration = Objects.requireNonNull(configuration);
47: Objects.requireNonNull(entityLoader);
48: build(entityLoader);
49: }
50:
51: private void build(EntityLoader entityLoader) {
52: LOG.debug("Building metamodel ... ");
53: checkForWeaver();
54:
55: loadEntities(entityLoader);
56:
57: entities.forEach(cls -> processOWLClass(cls));
58: }
59:
60: /**
61: * Check the class path for aspectj weaver, which is vital for using lazy loading.
62: */
63: private void checkForWeaver() {
64: try {
65: @SuppressWarnings("unused")
66: Class<?> c = MetamodelImpl.class.getClassLoader().loadClass(
67: ASPECTJ_CLASS);
68: } catch (ClassNotFoundException e) {
69: LOG.error("AspectJ not found on classpath. Cannot run without AspectJ.");
70: throw new OWLPersistenceException(e);
71: }
72: }
73:
74: private void loadEntities(EntityLoader entityLoader) {
75: Set<Class<?>> discoveredEntities = entityLoader.discoverEntityClasses(configuration);
76: entities.addAll(discoveredEntities);
77: }
78:
79: private <X> void processOWLClass(final Class<X> cls) {
80: if (typeMap.containsKey(cls)) {
81: return;
82: }
83:
84: LOG.debug("processing OWL class: {}", cls);
85:
86: final EntityClassProcessor classProcessor = new EntityClassProcessor();
87:
88: final EntityTypeImpl<X> et = classProcessor.processEntityType(cls);
89:
90: typeMap.put(cls, et);
91: final EntityFieldMetamodelProcessor<X> fieldProcessor = new EntityFieldMetamodelProcessor<>(cls, et, this);
92:
93: for (final Field field : cls.getDeclaredFields()) {
94: fieldProcessor.processField(field);
95: }
96:
97: if (et.getIdentifier() == null) {
98: throw new MetamodelInitializationException("Missing identifier field in entity class " + cls);
99: }
100: }
101:
102: @SuppressWarnings("unchecked")
103: public <X> EntityType<X> entity(Class<X> cls) {
104: if (!typeMap.containsKey(cls)) {
105: processOWLClass(cls);
106: }
107:
108: return (EntityType<X>) typeMap.get(cls);
109: }
110:
111: public <X> EmbeddableType<X> embeddable(Class<X> cls) {
112: throw new IllegalArgumentException("Embeddable entities not supported.");
113: }
114:
115: public Set<EmbeddableType<?>> getEmbeddables() {
116: return Collections.emptySet();
117: }
118:
119: public Set<EntityType<?>> getEntities() {
120: return new HashSet<>(typeMap.values());
121: }
122:
123: public Set<ManagedType<?>> getManagedTypes() {
124: return new HashSet<>(typeMap.values());
125: }
126:
127: public <X> ManagedType<X> managedType(Class<X> cls) {
128: return entity(cls);
129: }
130:
131: public Set<Class<?>> getInferredClasses() {
132: return Collections.unmodifiableSet(inferredClasses);
133: }
134:
135: public void addInferredClass(Class<?> cls) {
136: inferredClasses.add(cls);
137: }
138:
139: @Override
140: public Set<URI> getModuleExtractionExtraSignature() {
141: return Collections.unmodifiableSet(getSignatureInternal());
142: }
143:
144: @Override
145: public void addUriToModuleExtractionSignature(URI uri) {
146: if (uri == null) {
147: throw new NullPointerException();
148: }
149: synchronized (this) {
150: getSignatureInternal().add(uri);
151: }
152: }
153:
154: private synchronized Set<URI> getSignatureInternal() {
155: // This can be lazily loaded since we don'attributeType know if we'll need it
156: if (moduleExtractionSignature == null) {
157: final String sig = configuration.get(OntoDriverProperties.MODULE_EXTRACTION_SIGNATURE);
158: if (sig == null) {
159: this.moduleExtractionSignature = new HashSet<>();
160: } else {
161: final String[] signature = sig.split(OntoDriverProperties.SIGNATURE_DELIMITER);
162: this.moduleExtractionSignature = new HashSet<>(signature.length);
163: try {
164: for (String uri : signature) {
165: moduleExtractionSignature.add(new URI(uri));
166: }
167: } catch (URISyntaxException e) {
168: throw new OWLPersistenceException("Invalid URI encountered in module extraction signature.", e);
169: }
170: }
171: }
172: return moduleExtractionSignature;
173: }
174: }