Cleaning up Velocity. Minor change to Engine and ServiceLoaders to optionally use custom ClassLoader.

Former-commit-id: c0a96b36f4959a8b28b6b73e24ad884845140bd6
This commit is contained in:
Steve Springett
2014-04-26 01:25:56 -05:00
parent 08c7ffc6d9
commit ec53bd4125
5 changed files with 28 additions and 24 deletions

View File

@@ -66,19 +66,34 @@ public class Engine {
* A Map of analyzers grouped by Analysis phase. * A Map of analyzers grouped by Analysis phase.
*/ */
private final Set<FileTypeAnalyzer> fileTypeAnalyzers; private final Set<FileTypeAnalyzer> fileTypeAnalyzers;
/**
* The ClassLoader to use when dynamically loading Analyzer and Update services.
*/
private ClassLoader serviceClassLoader;
/** /**
* The Logger for use throughout the class. * The Logger for use throughout the class.
*/ */
private static final Logger LOGGER = Logger.getLogger(Engine.class.getName()); private static final Logger LOGGER = Logger.getLogger(Engine.class.getName());
/** /**
* Creates a new Engine. * Creates a new Engine.
* *
* @throws DatabaseException thrown if there is an error connecting to the database * @throws DatabaseException thrown if there is an error connecting to the database
*/ */
public Engine() throws DatabaseException { public Engine() throws DatabaseException {
this(Thread.currentThread().getContextClassLoader());
}
/**
* Creates a new Engine using the specified classloader to dynamically load Analyzer and Update services.
*
* @throws DatabaseException thrown if there is an error connecting to the database
*/
public Engine(ClassLoader serviceClassLoader) throws DatabaseException {
this.dependencies = new ArrayList<Dependency>(); this.dependencies = new ArrayList<Dependency>();
this.analyzers = new EnumMap<AnalysisPhase, List<Analyzer>>(AnalysisPhase.class); this.analyzers = new EnumMap<AnalysisPhase, List<Analyzer>>(AnalysisPhase.class);
this.fileTypeAnalyzers = new HashSet<FileTypeAnalyzer>(); this.fileTypeAnalyzers = new HashSet<FileTypeAnalyzer>();
this.serviceClassLoader = serviceClassLoader;
ConnectionFactory.initialize(); ConnectionFactory.initialize();
@@ -110,7 +125,7 @@ public class Engine {
analyzers.put(phase, new ArrayList<Analyzer>()); analyzers.put(phase, new ArrayList<Analyzer>());
} }
final AnalyzerService service = new AnalyzerService(); final AnalyzerService service = new AnalyzerService(serviceClassLoader);
final Iterator<Analyzer> iterator = service.getAnalyzers(); final Iterator<Analyzer> iterator = service.getAnalyzers();
while (iterator.hasNext()) { while (iterator.hasNext()) {
final Analyzer a = iterator.next(); final Analyzer a = iterator.next();
@@ -413,7 +428,7 @@ public class Engine {
* Cycles through the cached web data sources and calls update on all of them. * Cycles through the cached web data sources and calls update on all of them.
*/ */
private void doUpdates() { private void doUpdates() {
final UpdateService service = new UpdateService(); final UpdateService service = new UpdateService(serviceClassLoader);
final Iterator<CachedWebDataSource> iterator = service.getDataSources(); final Iterator<CachedWebDataSource> iterator = service.getDataSources();
while (iterator.hasNext()) { while (iterator.hasNext()) {
final CachedWebDataSource source = iterator.next(); final CachedWebDataSource source = iterator.next();

View File

@@ -36,8 +36,8 @@ public class AnalyzerService {
/** /**
* Creates a new instance of AnalyzerService. * Creates a new instance of AnalyzerService.
*/ */
public AnalyzerService() { public AnalyzerService(ClassLoader classLoader) {
loader = ServiceLoader.load(Analyzer.class); loader = ServiceLoader.load(Analyzer.class, classLoader);
} }
/** /**

View File

@@ -36,8 +36,8 @@ public class UpdateService {
/** /**
* Creates a new instance of UpdateService. * Creates a new instance of UpdateService.
*/ */
public UpdateService() { public UpdateService(ClassLoader classLoader) {
loader = ServiceLoader.load(CachedWebDataSource.class); loader = ServiceLoader.load(CachedWebDataSource.class, classLoader);
} }
/** /**

View File

@@ -36,7 +36,6 @@ import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context; import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.owasp.dependencycheck.analyzer.Analyzer; import org.owasp.dependencycheck.analyzer.Analyzer;
import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties; import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
import org.owasp.dependencycheck.dependency.Dependency; import org.owasp.dependencycheck.dependency.Dependency;
@@ -123,29 +122,19 @@ public class ReportGenerator {
* @return a velocity engine. * @return a velocity engine.
*/ */
private VelocityEngine createVelocityEngine() { private VelocityEngine createVelocityEngine() {
final VelocityEngine ve = new VelocityEngine(); final VelocityEngine engine = new VelocityEngine();
ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, VelocityLoggerRedirect.class.getName()); // Logging redirection for Velocity - Required by Jenkins and other server applications
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, VelocityLoggerRedirect.class.getName());
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); return engine;
return ve;
} }
/** /**
* Creates a new Velocity Context initialized with escape and date tools. * Creates a new Velocity Context.
* *
* @return a Velocity Context. * @return a Velocity Context.
*/ */
private Context createContext() { private Context createContext() {
//REMOVED all of the velocity tools to simplify the engine trying to resolve issues running this in Jenkins return new VelocityContext();
// final ToolManager manager = new ToolManager();
// final Context c = manager.createContext();
// final EasyFactoryConfiguration config = new EasyFactoryConfiguration();
// config.addDefaultTools();
// config.toolbox("application").tool("esc", "org.apache.velocity.tools.generic.EscapeTool").
// tool("org.apache.velocity.tools.generic.DateTool");
// manager.configure(config);
final VelocityContext c = new VelocityContext();
return c;
} }
/** /**

View File

@@ -33,7 +33,7 @@ public class AnalyzerServiceTest extends BaseTest {
*/ */
@Test @Test
public void testGetAnalyzers() { public void testGetAnalyzers() {
AnalyzerService instance = new AnalyzerService(); AnalyzerService instance = new AnalyzerService(Thread.currentThread().getContextClassLoader());
Iterator<Analyzer> result = instance.getAnalyzers(); Iterator<Analyzer> result = instance.getAnalyzers();
boolean found = false; boolean found = false;