Skip to content
This repository has been archived by the owner on Jul 3, 2018. It is now read-only.

Commit

Permalink
#2 refactored sandbox to use discovery service
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-conway committed Jun 2, 2016
1 parent e20de6a commit 1f1b4fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 110 deletions.
5 changes: 5 additions & 0 deletions publisher-sandbox/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<artifactId>publisher-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.iplantc.de</groupId>
<artifactId>publisher-discovery</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.xeustechnologies</groupId>
<artifactId>jcl-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@
*/
package org.iplantc.de.publish.sandbox;

import java.io.File;
import java.lang.annotation.Annotation;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.iplantc.de.publish.mechanism.api.annotations.PublicationDriver;
import org.iplantc.de.publish.discovery.PublisherDiscoveryConfiguration;
import org.iplantc.de.publish.discovery.PublisherDiscoveryService;
import org.iplantc.de.publish.mechanism.api.PublisherPluginDescription;
import org.iplantc.de.publish.mechanism.api.exception.PublicationException;
import org.iplantc.de.publish.mechanism.api.exception.PublicationRuntimeException;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ConfigurationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xeustechnologies.jcl.JarClassLoader;
import org.xeustechnologies.jcl.context.DefaultContextLoader;
import org.xeustechnologies.jcl.context.JclContext;

/**
* Black box host of publishing mechanisms for testing and validation purposes
Expand All @@ -34,8 +21,8 @@
public class PublisherEmulator {

private SandboxConfiguration sandboxConfiguration;
private JarClassLoader jcl;
private List<URL> urls = new ArrayList<URL>();
private PublisherDiscoveryService publisherDiscoveryService;
private List<PublisherPluginDescription> publisherPluginDescriptions;

public static final Logger log = LoggerFactory
.getLogger(PublisherEmulator.class);
Expand All @@ -48,47 +35,6 @@ public PublisherEmulator() {

}

public List<PublisherDescription> listPublisherDescriptions() {
log.info("listPublisherDescriptions()");
Set<Class<?>> classes = listPublisherClasses();

for (Class clazz : classes) {
log.info("processing class:{}", clazz);
for (Annotation annotation : clazz.getDeclaredAnnotations()) {
log.info("...annotation:{}", annotation.toString());
}
}

return null;

}

/**
* Method returns the classes for all registered publishers. Good for
* debugging and testing purposes
*
* @return <code>Set</code> of all of the annotated publisher classes
*/
public Set<Class<?>> listPublisherClasses() {

/*
* see: https://github.com/ronmamo/reflections
*/

ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder
.setUrls(urls)
.addClassLoader(JclContext.get())
.addScanners(new SubTypesScanner(),
new TypeAnnotationsScanner());
Reflections reflections = new Reflections(configurationBuilder);

Set<Class<?>> mechanisms = reflections.getTypesAnnotatedWith(
PublicationDriver.class, true);
log.info("mechanisms:{}", mechanisms);
return mechanisms;
}

/**
* @return the sandboxConfiguration
*/
Expand Down Expand Up @@ -123,45 +69,27 @@ public void init() throws PublicationException {
"no jar file plugin directory specified");
}

log.info("scanning for plugin jars at:{}",
sandboxConfiguration.getJarFilePluginDir());
loadPublisherClasses(sandboxConfiguration.getJarFilePluginDir());
PublisherDiscoveryConfiguration config = new PublisherDiscoveryConfiguration();
config.setJarFilePluginDir(sandboxConfiguration.getJarFilePluginDir());
publisherDiscoveryService = new PublisherDiscoveryService();
publisherDiscoveryService.setPublisherDiscoveryConfiguration(config);
log.info("initializing plugin discovery service to load plugins");
publisherDiscoveryService.init();
this.publisherPluginDescriptions = publisherDiscoveryService
.listPublisherDescriptions();
log.info("found plugins:{}", publisherPluginDescriptions);

}

private void loadPublisherClasses(String libDir) {

/*
* See https://github.com/kamranzafar/JCL for usage of JCL
*/

File dependencyDirectory = new File(libDir);
File[] files = dependencyDirectory.listFiles();
ArrayList<URI> uris = new ArrayList<URI>();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().endsWith(".jar")) {
log.info("adding jar:{} to candidates", files[i]);
uris.add(files[i].toURI());
}
}

log.info("creating jar class loader...");
jcl = new JarClassLoader();

for (URI uri : uris) {
log.info("adding uri for jar:{}", uri);
try {
jcl.add(uri.toURL());
urls.add(uri.toURL()); // testing outside jcl FIXME: decide!
} catch (MalformedURLException e) {
log.error("malformed url for jar file:{}", uri, e);
throw new PublicationRuntimeException("error loading jar file",
e);
}
public List<PublisherPluginDescription> listLoadedPlugins()
throws PublicationException {
log.info("listLoadedPlugins()");
if (publisherPluginDescriptions == null) {
log.error("init() has not been called, cannot list plugins");
throw new PublicationException("init() has not been called");
}

DefaultContextLoader context = new DefaultContextLoader(jcl);
context.loadContext();
return publisherDiscoveryService.listPublisherDescriptions();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import java.util.List;
import java.util.Properties;
import java.util.Set;

import junit.framework.Assert;

import org.iplantc.de.publish.mechanism.api.PublisherPluginDescription;
import org.iplantc.de.publish.mechanism.api.exception.PublicationException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -50,20 +50,6 @@ public void testInitWithConfig() throws PublicationException {

}

@Test
public void testListPublishers() throws PublicationException {
SandboxConfiguration sandboxConfiguration = new SandboxConfiguration();
sandboxConfiguration
.setJarFilePluginDir(testingProperties
.getProperty(PublisherTestingProperties.TEST_PUBLISHER_DIR_PROPERTY));
PublisherEmulator emulator = new PublisherEmulator();
emulator.setSandboxConfiguration(sandboxConfiguration);
emulator.init();
Set<Class<?>> mechanisms = emulator.listPublisherClasses();
Assert.assertFalse("no mechanisms returned", mechanisms.isEmpty());

}

@Test
public void testListPublicationDescriptions() throws PublicationException {
SandboxConfiguration sandboxConfiguration = new SandboxConfiguration();
Expand All @@ -73,8 +59,8 @@ public void testListPublicationDescriptions() throws PublicationException {
PublisherEmulator emulator = new PublisherEmulator();
emulator.setSandboxConfiguration(sandboxConfiguration);
emulator.init();
List<PublisherDescription> descriptions = emulator
.listPublisherDescriptions();
List<PublisherPluginDescription> descriptions = emulator
.listLoadedPlugins();
Assert.assertNotNull("null descriptions found:{}", descriptions);

}
Expand Down

0 comments on commit 1f1b4fa

Please sign in to comment.