Skip to content

Method: scanClasspath(Configuration)

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.loaders;
16:
17: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
18: import cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMapping;
19: import cz.cvut.kbss.jopa.utils.Configuration;
20:
21: import java.util.Objects;
22: import java.util.Set;
23:
24: /**
25: * Scans classpath to discover classes relevant to persistence unit building.
26: * <p>
27: * Only classes under the package configured via {@link JOPAPersistenceProperties#SCAN_PACKAGE} are processed.
28: */
29: public class PersistenceUnitClassFinder {
30:
31: private final ClasspathScanner classProcessor = new ClasspathScanner();
32:
33: private final EntityLoader entityLoader = new EntityLoader();
34: private final ResultSetMappingLoader resultSetMappingLoader = new ResultSetMappingLoader();
35:
36: private boolean scanned = false;
37:
38: /**
39: * Scans application classpath based on the {@link JOPAPersistenceProperties#SCAN_PACKAGE}, looking for classes
40: * relevant for the persistence provider.
41: * <p>
42: * These classes include:
43: * <p>
44: * <ul> <li>Entities, i.e. classes annotated with {@link cz.cvut.kbss.jopa.model.annotations.OWLClass},</li>
45: * <li>Result result mapping classes, i.e. classes annotated with {@link cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMapping}
46: * or {@link cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMappings}</li> </ul>
47: *
48: * @param configuration Persistence configuration, should contain value for the {@link
49: * JOPAPersistenceProperties#SCAN_PACKAGE} property
50: * @throws IllegalArgumentException If {@link JOPAPersistenceProperties#SCAN_PACKAGE} values is missing
51: */
52: public void scanClasspath(Configuration configuration) {
53: Objects.requireNonNull(configuration);
54:• if (!configuration.contains(JOPAPersistenceProperties.SCAN_PACKAGE)) {
55: throw new IllegalArgumentException("Missing the " + JOPAPersistenceProperties.SCAN_PACKAGE + " property.");
56: }
57: String toScan = configuration.get(JOPAPersistenceProperties.SCAN_PACKAGE);
58:• if (toScan.isEmpty()) {
59: throw new IllegalArgumentException(JOPAPersistenceProperties.SCAN_PACKAGE + " property cannot be empty.");
60: }
61: classProcessor.addListener(entityLoader);
62: classProcessor.addListener(resultSetMappingLoader);
63: classProcessor.processClasses(toScan);
64: this.scanned = true;
65: }
66:
67: /**
68: * Gets entity classes found during classpath scanning.
69: *
70: * @return Set of entity classes discovered on classpath
71: */
72: public Set<Class<?>> getEntities() {
73: assert scanned;
74: return entityLoader.getEntities();
75: }
76:
77: /**
78: * Gets {@link SparqlResultSetMapping}s found during classpath scanning.
79: *
80: * @return Set of result set mapping annotations discovered on classpath
81: */
82: public Set<SparqlResultSetMapping> getResultSetMappings() {
83: assert scanned;
84: return resultSetMappingLoader.getMappings();
85: }
86: }