Issue #2995: Update DB Class Finder to search all plugin jars

Change-Id: I42b5207372aa9d6f07afff0d18819c60b85fb3f8

Former-commit-id: 4f606b3302 [formerly 1395628200] [formerly 86d1ff9427] [formerly 86d1ff9427 [formerly 44701f31f2]] [formerly 5c0f9b5ba5 [formerly 86d1ff9427 [formerly 44701f31f2] [formerly 5c0f9b5ba5 [formerly f3fe2db624f7f9e6c7b0b65d600ddba5e42de361]]]]
Former-commit-id: 5c0f9b5ba5
Former-commit-id: 67e58fdc617214ac48a982117006c119173d1c25 [formerly 22e220f4dbde49d7ef5d1ef995a55d8c5ac54cff] [formerly baee58476f [formerly 60684de8b4]]
Former-commit-id: baee58476f
Former-commit-id: 947b9d7423
This commit is contained in:
Richard Peter 2014-04-25 13:07:48 -05:00
parent 0fc8930cbd
commit f23c30eeaa
2 changed files with 52 additions and 20 deletions

View file

@ -12,15 +12,8 @@
<tx:annotation-driven transaction-manager="metadataTxManager"
proxy-target-class="true" />
<!-- The db class finder will search the packages listed for classes with @Entity or @Embeddable -->
<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>
<!-- 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="metadataDbSessionConfig"
class="com.raytheon.uf.edex.database.DatabaseSessionConfiguration">

View file

@ -19,6 +19,9 @@
**/
package com.raytheon.uf.edex.database;
import java.io.File;
import java.io.FileFilter;
import java.net.MalformedURLException;
import java.util.Set;
import javax.persistence.Embeddable;
@ -26,9 +29,12 @@ import javax.persistence.Entity;
import org.reflections.Reflections;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
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
* 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
* ------------ ---------- ----------- --------------------------
* Oct 11, 2013 njensen Initial creation
*
* Apr 25, 2014 2995 rjpeter Updated to scan PLUGINDIR for all files.
* </pre>
*
* @author njensen
@ -49,26 +55,59 @@ import org.reflections.util.ConfigurationBuilder;
public class DatabaseClassAnnotationFinder {
private Set<Class<?>> dbAnnotatedClassSet;
private final Set<Class<?>> dbAnnotatedClassSet;
public DatabaseClassAnnotationFinder(String... packageNames) {
dbAnnotatedClassSet = findClasses(packageNames);
private final IUFStatusHandler statusHandler = UFStatus
.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.
*
* @param packageNames
* The start of pacakge names to include, e.g. com.raytheon
* @return
*/
protected Set<Class<?>> findClasses(String... packageNames) {
protected Set<Class<?>> findClasses() {
long t0 = System.currentTimeMillis();
ConfigurationBuilder cb = new ConfigurationBuilder();
for (String pkg : packageNames) {
cb.addUrls(ClasspathHelper.forPackage(pkg));
File pluginDir = new File(PropertiesFactory.getInstance()
.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());
Reflections reflecs = cb.build();
Set<Class<?>> set = reflecs.getTypesAnnotatedWith(Entity.class, false);