Skip to content

Commit

Permalink
Minor changes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 30, 2012
1 parent b5bb562 commit a214cd3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarFile;

import org.jboss.forge.container.AddonRegistry.AddonEntry;
Expand All @@ -26,22 +28,26 @@

/**
* TODO See {@link JarModuleLoader} for how to do dynamic dependencies from an XML file within.
*
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
* @author <a href="mailto:gegastaldi@gmail.com">George Gastaldi</a>
*/
public class AddonModuleLoader extends ModuleLoader
{
private static final ModuleIdentifier PLUGIN_CONTAINER_API = ModuleIdentifier.create("org.jboss.forge.api");
private static final ModuleIdentifier PLUGIN_CONTAINER = ModuleIdentifier.create("org.jboss.forge");
private static final ModuleIdentifier WELD = ModuleIdentifier.create("org.jboss.weld");

private List<AddonEntry> installed;
private Map<String, AddonEntry> installed;
private ModuleLoader parent;

public AddonModuleLoader(List<AddonEntry> installed)
public AddonModuleLoader(List<AddonEntry> installedAddons)
{
this.installed = installed;
this.installed = new HashMap<String, AddonRegistry.AddonEntry>();
for (AddonEntry entry : installedAddons)
{
this.installed.put(entry.toModuleId(), entry);
}
this.parent = Module.getBootModuleLoader();
}

Expand All @@ -60,16 +66,7 @@ protected Module preloadModule(ModuleIdentifier identifier) throws ModuleLoadExc
@Override
protected ModuleSpec findModule(ModuleIdentifier id) throws ModuleLoadException
{
AddonEntry found = null;
for (AddonEntry plugin : installed)
{
if (plugin.toModuleId().equals(id.toString()))
{
found = plugin;
break;
}
}

AddonEntry found = installed.get(id.toString());
if (found != null)
{
Builder builder = ModuleSpec.build(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

import javax.enterprise.inject.spi.BeanManager;

Expand All @@ -13,7 +14,6 @@
import org.jboss.forge.container.util.Assert;
import org.jboss.forge.container.util.BeanManagerUtils;
import org.jboss.forge.container.util.ClassLoaders;
import org.jboss.forge.container.util.ClassLoaders.Task;
import org.jboss.forge.container.weld.ModularWeld;
import org.jboss.modules.Module;
import org.jboss.weld.environment.se.Weld;
Expand All @@ -35,10 +35,10 @@ public AddonRunnable(Module module, AddonModuleRegistry registry, Set<Module> ad
@Override
public void run()
{
ClassLoaders.executeIn(module.getClassLoader(), new Task()
ClassLoaders.executeIn(module.getClassLoader(), new Callable<Object>()
{
@Override
public void perform() throws Exception
public Object call() throws Exception
{
Weld weld = new ModularWeld(module);
WeldContainer container = weld.initialize();
Expand All @@ -63,6 +63,7 @@ public void perform() throws Exception

manager.fireEvent(new ContainerShutdown());
weld.shutdown();
return null;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.jboss.forge.container;

import java.util.Set;
import java.util.concurrent.Callable;

import javax.enterprise.inject.spi.BeanManager;

import org.jboss.forge.container.event.ContainerShutdown;
import org.jboss.forge.container.event.ContainerStartup;
import org.jboss.forge.container.util.ClassLoaders;
import org.jboss.forge.container.util.ClassLoaders.Task;
import org.jboss.forge.container.weld.ModularWeld;
import org.jboss.modules.Module;
import org.jboss.weld.environment.se.Weld;
Expand All @@ -29,10 +29,10 @@ public ControlRunnable(Module module, AddonModuleRegistry registry, Set<Module>
@Override
public void run()
{
ClassLoaders.executeIn(module.getClassLoader(), new Task()
ClassLoaders.executeIn(module.getClassLoader(), new Callable<Object>()
{
@Override
public void perform() throws Exception
public Object call() throws Exception
{
Weld weld = new ModularWeld(module);
WeldContainer container = weld.initialize();
Expand All @@ -49,6 +49,7 @@ public void perform() throws Exception

manager.fireEvent(new ContainerShutdown());
weld.shutdown();
return null;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
package org.jboss.forge.container.util;

import java.util.concurrent.Callable;

import org.jboss.forge.container.exception.ContainerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* // TODO document
* Utility class for common {@link ClassLoader} operations.
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="mailto:gegastaldi@gmail.com">George Gastaldi</a>
*/
public class ClassLoaders
{
private static Logger log = LoggerFactory.getLogger(ClassLoaders.class);

public static interface Task
{
void perform() throws Exception;
}

/**
* Execute the given {@link Task} in the {@link ClassLoader} provided.
* Execute the given {@link Callable} in the {@link ClassLoader} provided.
*/
public static void executeIn(ClassLoader loader, Task task)
{log.debug("[Thread " + Thread.currentThread().getName() + "] ClassLoader ["
+ loader + "] task began.");
public static <V> V executeIn(ClassLoader loader, Callable<V> task)
{
log.debug("[Thread " + Thread.currentThread().getName() + "] ClassLoader ["
+ loader + "] task began.");
ClassLoader original = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(loader);
task.perform();
return task.call();
}
catch (Exception e)
{
Expand Down

3 comments on commit a214cd3

@lincolnthree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent.

@lincolnthree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learned something new (Callable)

@gastaldi
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad you liked it :)

Please sign in to comment.