Skip to content

Commit

Permalink
Refactored Forge start
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Dec 24, 2012
1 parent 0afde12 commit 4902bcd
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void run()
@Override
public Object call() throws Exception
{
forge = new Forge();
forge = ServiceLoader.load(Forge.class).iterator().next();
forge.setServerMode(true).setAddonDir(addonDir).start();
return forge;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

/**
* A class with a main method to bootstrap Forge.
*
*
* You can deploy addons by calling {@link Bootstrap#install(String)}
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
public class Bootstrap
{
Expand All @@ -40,7 +40,7 @@ public Bootstrap(String[] args)
{
String installAddon = null;
String removeAddon = null;
forge = new Forge();
forge = ServiceLoader.load(Forge.class).iterator().next();
if (args.length > 0)
{
for (int i = 0; i < args.length; i++)
Expand Down
40 changes: 40 additions & 0 deletions container-api/src/main/java/org/jboss/forge/container/Forge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.container;

import java.io.File;

public interface Forge
{

/**
* Starts Forge in a new Thread
*/
public abstract Forge startAsync();

public abstract Forge startAsync(ClassLoader loader);

public abstract Forge start();

public abstract Forge start(ClassLoader loader);

public abstract Forge stop();

public abstract AddonRegistry getAddonRegistry();

public abstract Forge setAddonDir(File dir);

public abstract Forge setServerMode(boolean server);

public abstract File getAddonDir();

public abstract AddonRepository getRepository();

public abstract String getVersion();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.jboss.modules.ModuleLoader;
import org.jboss.modules.log.StreamModuleLogger;

public class Forge
public class ForgeImpl implements Forge
{
private static final String PROP_CONCURRENT_PLUGINS = "forge.concurrentAddons";

Expand All @@ -44,7 +44,7 @@ public class Forge

ExecutorService executor;

public Forge()
public ForgeImpl()
{
if (!AddonRepositoryImpl.hasRuntimeAPIVersion())
logger.warning("Could not detect Forge runtime version - " +
Expand Down Expand Up @@ -77,33 +77,54 @@ public Set<AddonThread> getThreads()
return threads;
}

/**
* Starts Forge in a new Thread
/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#startAsync()
*/
@Override
public Forge startAsync()
{
return startAsync(Thread.currentThread().getContextClassLoader());
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#startAsync(java.lang.ClassLoader)
*/
@Override
public Forge startAsync(final ClassLoader loader)
{
new Thread()
{
@Override
public void run()
{
Forge.this.start(loader);
ForgeImpl.this.start(loader);
};
}.start();

return this;
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#start()
*/
@Override
public Forge start()
{
return start(Thread.currentThread().getContextClassLoader());
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#start(java.lang.ClassLoader)
*/
@Override
public Forge start(ClassLoader loader)
{
if (!alive)
Expand Down Expand Up @@ -148,6 +169,12 @@ private void shutdownThreads()
}
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#stop()
*/
@Override
public Forge stop()
{
alive = false;
Expand Down Expand Up @@ -227,6 +254,12 @@ private Set<AddonThread> startAddons(Set<Addon> toStart)
return started;
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#getAddonRegistry()
*/
@Override
public AddonRegistry getAddonRegistry()
{
return AddonRegistryImpl.INSTANCE;
Expand Down Expand Up @@ -328,28 +361,58 @@ else if (!waitlist.containsKey(addonToLoad) && (addonToLoad.getModule() == null)
return addonToLoad;
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#setAddonDir(java.io.File)
*/
@Override
public Forge setAddonDir(File dir)
{
this.repository = AddonRepositoryImpl.forDirectory(dir);
return this;
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#setServerMode(boolean)
*/
@Override
public Forge setServerMode(boolean server)
{
this.serverMode = server;
return this;
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#getAddonDir()
*/
@Override
public File getAddonDir()
{
return repository.getRepositoryDirectory();
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#getRepository()
*/
@Override
public AddonRepository getRepository()
{
return repository;
}

/*
* (non-Javadoc)
*
* @see org.jboss.forge.container.Forge#getVersion()
*/
@Override
public String getVersion()
{
return AddonRepositoryImpl.getRuntimeAPIVersion();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.forge.container.ForgeImpl
2 changes: 1 addition & 1 deletion se/src/main/java/org/jboss/forge/se/init/ForgeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static Forge getInstance()
try
{
final BootstrapClassLoader cl = new BootstrapClassLoader("bootpath");
Class<?> bootstrapType = cl.loadClass("org.jboss.forge.container.Forge");
Class<?> bootstrapType = cl.loadClass("org.jboss.forge.container.ForgeImpl");
return (Forge) Enhancer.create(Forge.class, new ClassLoaderAdapterCallback(cl, bootstrapType.newInstance()));
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.jboss.forge.container.AddonRegistry;
import org.jboss.forge.container.Forge;
import org.jboss.forge.container.ForgeImpl;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -13,10 +14,10 @@ public class BootstrapClassLoaderTestCase
public void shouldBeAbleToLoadEnvironment() throws Exception
{
final BootstrapClassLoader cl = new BootstrapClassLoader("bootpath");
Class<?> bootstrapType = cl.loadClass("org.jboss.forge.container.Forge");
Class<?> bootstrapType = cl.loadClass("org.jboss.forge.container.ForgeImpl");
Method method = bootstrapType.getMethod("startAsync", new Class<?>[] { ClassLoader.class });
Object result = method.invoke(bootstrapType.newInstance(), cl);
Assert.assertEquals(Forge.class.getName(), result.getClass().getName());
Assert.assertEquals(ForgeImpl.class.getName(), result.getClass().getName());
}

@Test
Expand Down

0 comments on commit 4902bcd

Please sign in to comment.