Issue #2995: Update DB Class Finder to search all plugin jars
Change-Id: I42b5207372aa9d6f07afff0d18819c60b85fb3f8 Former-commit-id:1395628200
[formerly86d1ff9427
] [formerly44701f31f2
] [formerly1395628200
[formerly86d1ff9427
] [formerly44701f31f2
] [formerly5c0f9b5ba5
[formerly44701f31f2
[formerly f3fe2db624f7f9e6c7b0b65d600ddba5e42de361]]]] Former-commit-id:5c0f9b5ba5
Former-commit-id:947b9d7423
[formerlybaee58476f
] [formerly 22e220f4dbde49d7ef5d1ef995a55d8c5ac54cff [formerly60684de8b4
]] Former-commit-id: f265228af3414b3f057894a3a0918b094b692219 [formerly231dbfc1b9
] Former-commit-id:928de60c87
This commit is contained in:
parent
66e9679408
commit
4c24417ff1
2 changed files with 52 additions and 20 deletions
|
@ -12,15 +12,8 @@
|
||||||
<tx:annotation-driven transaction-manager="metadataTxManager"
|
<tx:annotation-driven transaction-manager="metadataTxManager"
|
||||||
proxy-target-class="true" />
|
proxy-target-class="true" />
|
||||||
|
|
||||||
<!-- The db class finder will search the packages listed for classes with @Entity or @Embeddable -->
|
<!-- The db class finder will search the plugin dir for classes with @Entity or @Embeddable -->
|
||||||
<bean id="dbClassFinder" class="com.raytheon.uf.edex.database.DatabaseClassAnnotationFinder" >
|
<bean id="dbClassFinder" class="com.raytheon.uf.edex.database.DatabaseClassAnnotationFinder" />
|
||||||
<constructor-arg>
|
|
||||||
<list>
|
|
||||||
<value>com.raytheon</value>
|
|
||||||
<value>gov.noaa</value>
|
|
||||||
</list>
|
|
||||||
</constructor-arg>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="metadataDbSessionConfig"
|
<bean id="metadataDbSessionConfig"
|
||||||
class="com.raytheon.uf.edex.database.DatabaseSessionConfiguration">
|
class="com.raytheon.uf.edex.database.DatabaseSessionConfiguration">
|
||||||
|
|
|
@ -19,6 +19,9 @@
|
||||||
**/
|
**/
|
||||||
package com.raytheon.uf.edex.database;
|
package com.raytheon.uf.edex.database;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
@ -26,9 +29,12 @@ import javax.persistence.Entity;
|
||||||
|
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
import org.reflections.scanners.TypeAnnotationsScanner;
|
import org.reflections.scanners.TypeAnnotationsScanner;
|
||||||
import org.reflections.util.ClasspathHelper;
|
|
||||||
import org.reflections.util.ConfigurationBuilder;
|
import org.reflections.util.ConfigurationBuilder;
|
||||||
|
|
||||||
|
import com.raytheon.uf.common.status.IUFStatusHandler;
|
||||||
|
import com.raytheon.uf.common.status.UFStatus;
|
||||||
|
import com.raytheon.uf.edex.core.props.PropertiesFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses the reflections package to find classes on the classpath that match the
|
* Uses the reflections package to find classes on the classpath that match the
|
||||||
* start of a package name and will be needed to access the database.
|
* start of a package name and will be needed to access the database.
|
||||||
|
@ -40,7 +46,7 @@ import org.reflections.util.ConfigurationBuilder;
|
||||||
* Date Ticket# Engineer Description
|
* Date Ticket# Engineer Description
|
||||||
* ------------ ---------- ----------- --------------------------
|
* ------------ ---------- ----------- --------------------------
|
||||||
* Oct 11, 2013 njensen Initial creation
|
* Oct 11, 2013 njensen Initial creation
|
||||||
*
|
* Apr 25, 2014 2995 rjpeter Updated to scan PLUGINDIR for all files.
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author njensen
|
* @author njensen
|
||||||
|
@ -49,26 +55,59 @@ import org.reflections.util.ConfigurationBuilder;
|
||||||
|
|
||||||
public class DatabaseClassAnnotationFinder {
|
public class DatabaseClassAnnotationFinder {
|
||||||
|
|
||||||
private Set<Class<?>> dbAnnotatedClassSet;
|
private final Set<Class<?>> dbAnnotatedClassSet;
|
||||||
|
|
||||||
public DatabaseClassAnnotationFinder(String... packageNames) {
|
private final IUFStatusHandler statusHandler = UFStatus
|
||||||
dbAnnotatedClassSet = findClasses(packageNames);
|
.getHandler(DatabaseClassAnnotationFinder.class);
|
||||||
|
|
||||||
|
public DatabaseClassAnnotationFinder() {
|
||||||
|
dbAnnotatedClassSet = findClasses();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the classpath for classes that will be needed by the database
|
* Searches the plugin dir for classes that will be needed by the database
|
||||||
* layer.
|
* layer.
|
||||||
*
|
*
|
||||||
* @param packageNames
|
|
||||||
* The start of pacakge names to include, e.g. com.raytheon
|
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected Set<Class<?>> findClasses(String... packageNames) {
|
protected Set<Class<?>> findClasses() {
|
||||||
long t0 = System.currentTimeMillis();
|
long t0 = System.currentTimeMillis();
|
||||||
ConfigurationBuilder cb = new ConfigurationBuilder();
|
ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||||
for (String pkg : packageNames) {
|
File pluginDir = new File(PropertiesFactory.getInstance()
|
||||||
cb.addUrls(ClasspathHelper.forPackage(pkg));
|
.getEnvProperties().getEnvValue("PLUGINDIR"));
|
||||||
|
|
||||||
|
if (!pluginDir.exists()) {
|
||||||
|
throw new AssertionError(
|
||||||
|
"Cannot find Database classes to load. PluginDir ["
|
||||||
|
+ pluginDir.getAbsolutePath() + "] does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File[] pluginJarFiles = pluginDir.listFiles(new FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File file) {
|
||||||
|
if (file.isFile()) {
|
||||||
|
String name = file.getName();
|
||||||
|
if (name.endsWith(".jar")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
for (File jarFile : pluginJarFiles) {
|
||||||
|
try {
|
||||||
|
cb.addUrls(jarFile.toURI().toURL());
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
statusHandler
|
||||||
|
.error("Unable to scan jar file ["
|
||||||
|
+ jarFile.getAbsolutePath()
|
||||||
|
+ "] for database annotations. File will be skipped",
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cb.setScanners(new TypeAnnotationsScanner());
|
cb.setScanners(new TypeAnnotationsScanner());
|
||||||
Reflections reflecs = cb.build();
|
Reflections reflecs = cb.build();
|
||||||
Set<Class<?>> set = reflecs.getTypesAnnotatedWith(Entity.class, false);
|
Set<Class<?>> set = reflecs.getTypesAnnotatedWith(Entity.class, false);
|
||||||
|
|
Loading…
Add table
Reference in a new issue