From 4c24417ff18329c3b66af342f111dc31d97e8ab6 Mon Sep 17 00:00:00 2001 From: Richard Peter Date: Fri, 25 Apr 2014 13:07:48 -0500 Subject: [PATCH] Issue #2995: Update DB Class Finder to search all plugin jars Change-Id: I42b5207372aa9d6f07afff0d18819c60b85fb3f8 Former-commit-id: 139562820073413e2e18cf92356f9a19d9ced14f [formerly 86d1ff942761b27553427498e971999c0152e318] [formerly 44701f31f286e6f796035d51c3d601b19f335137] [formerly 139562820073413e2e18cf92356f9a19d9ced14f [formerly 86d1ff942761b27553427498e971999c0152e318] [formerly 44701f31f286e6f796035d51c3d601b19f335137] [formerly 5c0f9b5ba5e2518680008930f89eec5c467480de [formerly 44701f31f286e6f796035d51c3d601b19f335137 [formerly f3fe2db624f7f9e6c7b0b65d600ddba5e42de361]]]] Former-commit-id: 5c0f9b5ba5e2518680008930f89eec5c467480de Former-commit-id: 947b9d7423b545c136217450883ac5c749aeca41 [formerly baee58476f445e61f7c5775c387b86d01d74bede] [formerly 22e220f4dbde49d7ef5d1ef995a55d8c5ac54cff [formerly 60684de8b46d1dd3b404808f3b9627afe461b9e2]] Former-commit-id: f265228af3414b3f057894a3a0918b094b692219 [formerly 231dbfc1b9fe499c22878cbd7326b7f1beb6839a] Former-commit-id: 928de60c8719bfe5a358a0036afab124321b9e1b --- .../build.edex/esb/conf/spring/edex-db.xml | 11 +--- .../DatabaseClassAnnotationFinder.java | 61 +++++++++++++++---- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/edexOsgi/build.edex/esb/conf/spring/edex-db.xml b/edexOsgi/build.edex/esb/conf/spring/edex-db.xml index 5ff9b46335..c764b51416 100644 --- a/edexOsgi/build.edex/esb/conf/spring/edex-db.xml +++ b/edexOsgi/build.edex/esb/conf/spring/edex-db.xml @@ -12,15 +12,8 @@ - - - - - com.raytheon - gov.noaa - - - + + diff --git a/edexOsgi/com.raytheon.uf.edex.database/src/com/raytheon/uf/edex/database/DatabaseClassAnnotationFinder.java b/edexOsgi/com.raytheon.uf.edex.database/src/com/raytheon/uf/edex/database/DatabaseClassAnnotationFinder.java index 8689e3dfd4..ea1e26d33a 100644 --- a/edexOsgi/com.raytheon.uf.edex.database/src/com/raytheon/uf/edex/database/DatabaseClassAnnotationFinder.java +++ b/edexOsgi/com.raytheon.uf.edex.database/src/com/raytheon/uf/edex/database/DatabaseClassAnnotationFinder.java @@ -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. * * * @author njensen @@ -49,26 +55,59 @@ import org.reflections.util.ConfigurationBuilder; public class DatabaseClassAnnotationFinder { - private Set> dbAnnotatedClassSet; + private final Set> 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> findClasses(String... packageNames) { + protected Set> 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> set = reflecs.getTypesAnnotatedWith(Entity.class, false);