Skip to content

Commit

Permalink
Initial addition of new API for booting Weld from SE
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroyle committed Nov 29, 2009
1 parent 5e2a8bb commit 6f8d971
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 58 deletions.
83 changes: 25 additions & 58 deletions src/main/java/org/jboss/weld/environment/se/StartMain.java
Expand Up @@ -18,16 +18,7 @@

import javax.enterprise.inject.spi.BeanManager;

import org.jboss.weld.bootstrap.api.Bootstrap;
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.context.api.BeanStore;
import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.environment.se.discovery.SEWeldDeployment;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import org.jboss.weld.environment.se.util.Reflections;
import org.jboss.weld.environment.se.util.WeldManagerUtils;

/**
* This is the main class that should always be called from the command line for
Expand All @@ -41,58 +32,34 @@
public class StartMain
{

private static final String BOOTSTRAP_IMPL_CLASS_NAME = "org.jboss.weld.bootstrap.WeldBootstrap";
private final Bootstrap bootstrap;
private final BeanStore applicationBeanStore;
public static String[] PARAMETERS;
private WeldManager manager;
public static String[] PARAMETERS;

public StartMain(String[] commandLineArgs)
{
PARAMETERS = commandLineArgs;
try
{
bootstrap = Reflections.newInstance(BOOTSTRAP_IMPL_CLASS_NAME, Bootstrap.class);
}
catch (Exception e)
{
throw new IllegalStateException("Error loading Weld bootstrap, check that Weld is on the classpath", e);
}
this.applicationBeanStore = new ConcurrentHashMapBeanStore();
}
public StartMain(String[] commandLineArgs)
{
PARAMETERS = commandLineArgs;
}

public BeanManager go()
{
SEWeldDeployment deployment = new SEWeldDeployment()
{
};
bootstrap.startContainer(Environments.SE, deployment, this.applicationBeanStore);
final BeanDeploymentArchive mainBeanDepArch = deployment.getBeanDeploymentArchives().get(0);
this.manager = bootstrap.getManager(mainBeanDepArch);
bootstrap.startInitialization();
bootstrap.deployBeans();
WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).setBootstrap(bootstrap);
bootstrap.validateBeans();
bootstrap.endInitialization();
public BeanManager go()
{
Weld weld = new Weld().initialize();

this.manager.fireEvent(new ContainerInitialized());
return this.manager;
}
weld.getBeanManager().fireEvent(new ContainerInitialized());
return weld.getBeanManager();
}

/**
* The main method called from the command line.
*
* @param args the command line arguments
*/
public static void main(String[] args)
{
new StartMain(args).go();
}

public static String[] getParameters()
{
// TODO(PR): make immutable
return PARAMETERS;
}
/**
* The main method called from the command line.
*
* @param args the command line arguments
*/
public static void main(String[] args)
{
new StartMain(args).go();
}

public static String[] getParameters()
{
// TODO(PR): make immutable
return PARAMETERS;
}
}
108 changes: 108 additions & 0 deletions src/main/java/org/jboss/weld/environment/se/Weld.java
@@ -0,0 +1,108 @@
/**
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.se;

import javax.enterprise.event.Event;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.BeanManager;
import org.jboss.weld.bootstrap.api.Bootstrap;
import org.jboss.weld.bootstrap.api.Environments;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.context.api.BeanStore;
import org.jboss.weld.context.api.helpers.ConcurrentHashMapBeanStore;
import org.jboss.weld.environment.se.beans.InstanceManager;
import org.jboss.weld.environment.se.discovery.SEWeldDeployment;
import org.jboss.weld.environment.se.util.Reflections;
import org.jboss.weld.environment.se.util.WeldManagerUtils;
import org.jboss.weld.manager.api.WeldManager;

/**
* An alternative means of booting Weld form an arbitrary main method within an
* SE application, <em>without</em> using the built-in ContainerInitialized event.
* Typical usage of the API looks like this:
* <code>
* Weld weld = new Weld().initialize();
* weld.instance().select(Foo.class).get();
* weld.event().select(Bar.class).fire( new Bar() );
* weld.shutdown();
* </code>
*
* @author Peter Royle
*/
public class Weld
{

private static final String BOOTSTRAP_IMPL_CLASS_NAME = "org.jboss.weld.bootstrap.WeldBootstrap";
private final Bootstrap bootstrap;
private final BeanStore applicationBeanStore;
private WeldManager manager;
private InstanceManager instanceManager;

public Weld()
{
try
{
bootstrap = Reflections.newInstance(BOOTSTRAP_IMPL_CLASS_NAME, Bootstrap.class);
} catch (Exception e)
{
throw new IllegalStateException("Error loading Weld bootstrap, check that Weld is on the classpath", e);
}
this.applicationBeanStore = new ConcurrentHashMapBeanStore();
}

public Weld initialize()
{

SEWeldDeployment deployment = new SEWeldDeployment()
{
};
bootstrap.startContainer(Environments.SE, deployment, this.applicationBeanStore);
final BeanDeploymentArchive mainBeanDepArch = deployment.getBeanDeploymentArchives().get(0);
this.manager = bootstrap.getManager(mainBeanDepArch);
bootstrap.startInitialization();
bootstrap.deployBeans();
WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).setBootstrap(bootstrap);
bootstrap.validateBeans();
bootstrap.endInitialization();

instanceManager = WeldManagerUtils.getInstanceByType(manager, InstanceManager.class);

return this;
}

public Instance<Object> instance()
{
return instanceManager.getInstances();
}

public Instance<Event> event()
{
return instanceManager.getEvents();
}

public BeanManager getBeanManager()
{
return manager;
}

/**
* Convenience method for shutting down the container.
*/
public void shutdown() {
WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).shutdown();
}
}
@@ -0,0 +1,51 @@
/**
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.se.beans;

import javax.enterprise.event.Event;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import org.jboss.weld.environment.se.Weld;

/**
* A managed bean which holds all of the injected instances of managed beans and
* events. It is primarily used as a delegate for the Weld class's instance()
* and event() methods.
*
* @see Weld
* @author Peter Royle
*/
public class InstanceManager
{

@Inject Instance<Object> instances;
@Inject Instance<Event> events;

public InstanceManager()
{
}

public Instance<Object> getInstances()
{
return instances;
}

public Instance<Event> getEvents()
{
return events;
}
}
91 changes: 91 additions & 0 deletions src/test/java/org/jboss/weld/environment/se/test/WeldMainTest.java
@@ -0,0 +1,91 @@
/**
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.se.test;

import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.events.Shutdown;
import org.jboss.weld.environment.se.test.beans.CustomEvent;
import org.jboss.weld.environment.se.test.beans.InitObserverTestBean;
import org.jboss.weld.environment.se.test.beans.MainTestBean;
import org.jboss.weld.environment.se.test.beans.ObserverTestBean;
import org.jboss.weld.environment.se.test.beans.ParametersTestBean;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
*
* @author Peter Royle
*/
public class WeldMainTest
{

/**
* Test the alternate API for boting Weld from an SE app.
*/
@Test
public void testInitialize()
{

Weld weld = new Weld().initialize();

MainTestBean mainTestBean = weld.instance().select(MainTestBean.class).get();
Assert.assertNotNull(mainTestBean);

ParametersTestBean paramsBean = mainTestBean.getParametersTestBean();
Assert.assertNotNull(paramsBean);
Assert.assertNotNull(paramsBean.getParameters());

shutdownManager(weld.getBeanManager());
}

/**
* Test the firing of observers using the alternate API for boting Weld from an SE app.
*/
@Test
public void testObservers()
{
InitObserverTestBean.reset();
ObserverTestBean.reset();

Weld weld = new Weld().initialize();
weld.getBeanManager().fireEvent(new CustomEvent());
// TODO(PR): this should work. What's with the compile error?
// weld.event().select(CustomEvent.class).fire(new CustomEvent());

Assert.assertTrue(ObserverTestBean.isBuiltInObserved());
Assert.assertTrue(ObserverTestBean.isCustomObserved());
Assert.assertFalse(ObserverTestBean.isInitObserved());

Assert.assertFalse(InitObserverTestBean.isInitObserved());
}

private void shutdownManager(BeanManager manager)
{
manager.fireEvent(manager, new ShutdownAnnotation());
}

private static class ShutdownAnnotation extends AnnotationLiteral<Shutdown>
{

public ShutdownAnnotation()
{
}
}
}

0 comments on commit 6f8d971

Please sign in to comment.