-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityLoader.java
159 lines (146 loc) · 6.58 KB
/
EntityLoader.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* 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.
*
* 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.loaders;
import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.annotations.OWLClass;
import cz.cvut.kbss.jopa.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class EntityLoader {
private static final Logger LOG = LoggerFactory.getLogger(EntityLoader.class);
private static final String JAR_FILE_SUFFIX = ".jar";
private static final String CLASS_FILE_SUFFIX = ".class";
/**
* Discovers and returns all entity classes within the scan package and its subpackages.
* <p>
* I.e., it looks for classes annotated with {@link OWLClass}.
*
* @param configuration Persistence configuration, should contain value for the {@link
* JOPAPersistenceProperties#SCAN_PACKAGE} property
* @return Set of entity classes
* @throws IllegalArgumentException If {@link JOPAPersistenceProperties#SCAN_PACKAGE} values is missing
*/
public Set<Class<?>> discoverEntityClasses(Configuration configuration) {
Objects.requireNonNull(configuration);
if (!configuration.contains(JOPAPersistenceProperties.SCAN_PACKAGE)) {
throw new IllegalArgumentException(
"Missing the " + JOPAPersistenceProperties.SCAN_PACKAGE + " property.");
}
String toScan = configuration.get(JOPAPersistenceProperties.SCAN_PACKAGE);
if (toScan.isEmpty()) {
throw new IllegalArgumentException(JOPAPersistenceProperties.SCAN_PACKAGE + " property cannot be empty.");
}
return discoverEntities(toScan);
}
/**
* Using code from https://github.com/ddopson/java-class-enumerator
*/
private Set<Class<?>> discoverEntities(String scanPath) {
final Set<Class<?>> all = new HashSet<>();
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
Enumeration<URL> urls = loader.getResources(scanPath.replace('.', '/'));
while (urls.hasMoreElements()) {
final URL url = urls.nextElement();
if (url.toString().startsWith("jar:")) {
processJarFile(url, scanPath, all);
} else {
processDirectory(new File(getUrlAsUri(url).getPath()), scanPath, all);
}
}
} catch (IOException e) {
throw new JopaInitializationException("Unable to scan packages for entity classes.", e);
}
if (all.isEmpty()) {
LOG.warn("No entity classes found in package " + scanPath);
}
return all;
}
private static URI getUrlAsUri(URL url) {
try {
// Transformation to URI handles encoding, e.g. of whitespaces in the path
return url.toURI();
} catch (URISyntaxException ex) {
throw new JopaInitializationException(
"Unable to scan resource " + url + ". It is not a valid URI.", ex);
}
}
private void processJarFile(URL jarResource, String packageName, Set<Class<?>> entityClasses) {
final String relPath = packageName.replace('.', '/');
final String jarPath = jarResource.getPath().replaceFirst("[.]jar[!].*", JAR_FILE_SUFFIX)
.replaceFirst("file:", "");
LOG.trace("Scanning jar file {} for entity classes.", jarPath);
try (final JarFile jarFile = new JarFile(jarPath)) {
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
final String entryName = entry.getName();
String className = null;
if (entryName.endsWith(CLASS_FILE_SUFFIX) && entryName.startsWith(relPath)) {
className = entryName.replace('/', '.').replace('\\', '.').replace(CLASS_FILE_SUFFIX, "");
}
if (className != null) {
processClass(className, entityClasses);
}
}
} catch (IOException e) {
throw new JopaInitializationException("Unexpected IOException reading JAR File " + jarPath, e);
}
}
private void processClass(String className, Set<Class<?>> entityClasses) {
try {
final Class<?> cls = Class.forName(className);
if (cls.getAnnotation(OWLClass.class) != null) {
entityClasses.add(cls);
}
} catch (ClassNotFoundException e) {
throw new JopaInitializationException("Unexpected ClassNotFoundException when scanning for entities.", e);
}
}
private void processDirectory(File dir, String packageName, Set<Class<?>> entityClasses) {
LOG.trace("Scanning directory {} for entity classes.", dir);
// Get the list of the files contained in the package
final String[] files = dir.list();
if (files == null) {
return;
}
for (String fileName : files) {
String className = null;
// we are only interested in .class files
if (fileName.endsWith(CLASS_FILE_SUFFIX)) {
// removes the .class extension
className = packageName + '.' + fileName.substring(0, fileName.length() - 6);
}
if (className != null) {
processClass(className, entityClasses);
}
final File subDir = new File(dir, fileName);
if (subDir.isDirectory()) {
processDirectory(subDir, packageName + '.' + fileName, entityClasses);
}
}
}
}