Skip to content

Commit

Permalink
Separated out initialize() (Weld) from instance() and event() (WeldCo…
Browse files Browse the repository at this point in the history
…ntainer)
  • Loading branch information
peteroyle committed Dec 1, 2009
1 parent 67ffb34 commit 56f2710
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 57 deletions.
12 changes: 5 additions & 7 deletions src/main/java/org/jboss/weld/environment/se/StartMain.java
Expand Up @@ -16,13 +16,12 @@
*/
package org.jboss.weld.environment.se;

import javax.enterprise.inject.spi.BeanManager;

import org.jboss.weld.environment.se.events.ContainerInitialized;

/**
* This is the main class that should always be called from the command line for
* a Weld SE app. Something like: <code>
* a WeldContainer SE app. Something like: <code>
* java -jar MyApp.jar org.jboss.weld.environment.se.StarMain arguments
* </code>
*
Expand All @@ -39,12 +38,11 @@ public StartMain(String[] commandLineArgs)
PARAMETERS = commandLineArgs;
}

public BeanManager go()
public WeldContainer go()
{
Weld weld = new Weld().initialize();

weld.getBeanManager().fireEvent(new ContainerInitialized());
return weld.getBeanManager();
WeldContainer weld = new Weld().initialize();
weld.event().select(ContainerInitialized.class).fire(new ContainerInitialized());
return weld;
}

/**
Expand Down
39 changes: 13 additions & 26 deletions src/main/java/org/jboss/weld/environment/se/Weld.java
Expand Up @@ -16,9 +16,6 @@
*/
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;
Expand All @@ -31,13 +28,13 @@
import org.jboss.weld.manager.api.WeldManager;

/**
* An alternative means of booting Weld form an arbitrary main method within an
* An alternative means of booting WeldContainer 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:
* Typical usage of this API looks like this:
* <code>
* Weld weld = new Weld().initialize();
* WeldContainer weld = new Weld().initialize();
* weld.instance().select(Foo.class).get();
* weld.event().select(Bar.class).fire( new Bar() );
* weld.event().select(Bar.class).fire(new Bar());
* weld.shutdown();
* </code>
*
Expand All @@ -50,7 +47,6 @@ public class Weld
private final Bootstrap bootstrap;
private final BeanStore applicationBeanStore;
private WeldManager manager;
private InstanceManager instanceManager;

public Weld()
{
Expand All @@ -64,7 +60,11 @@ public Weld()
this.applicationBeanStore = new ConcurrentHashMapBeanStore();
}

public Weld initialize()
/**
* Boots Weld and creates and returns a WeldContainer instance, through which
* beans and events can be accesed.
*/
public WeldContainer initialize()
{

SEWeldDeployment deployment = new SEWeldDeployment()
Expand All @@ -79,30 +79,17 @@ public Weld initialize()
bootstrap.validateBeans();
bootstrap.endInitialization();

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

return this;
}
return new WeldContainer(instanceManager, manager);

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

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

public BeanManager getBeanManager()
{
return manager;
}

/**
* Convenience method for shutting down the container.
*/
public void shutdown() {
public void shutdown()
{
WeldManagerUtils.getInstanceByType(manager, ShutdownManager.class).shutdown();
}
}
74 changes: 74 additions & 0 deletions src/main/java/org/jboss/weld/environment/se/WeldContainer.java
@@ -0,0 +1,74 @@
/**
* 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.environment.se.beans.InstanceManager;

/**
* Provides convenient access to beans and events, particularly helpful when
* bootstrapping an application in Java SE.
* <p>
* An instance of this class can be obtained using the Weld class by calling:
* <code>
* WeldContainer weld = new Weld().initialize();
* </code>
* @see Weld
*
* @author Peter Royle
*/
public class WeldContainer
{

private final InstanceManager instanceManager;
private final BeanManager beanManager;

protected WeldContainer(InstanceManager instanceManager, BeanManager beanManager)
{
this.instanceManager = instanceManager;
this.beanManager = beanManager;
}

/**
* Provides access to all beans within the application. For example:
* <code>
* Foo foo = weld.instance().select(Foo.class).get();
* </code>
*/
public Instance<Object> instance()
{
return instanceManager.getInstances();
}

/**
* Provides access to all events within the application. For example:
* <code>
* weld.event().select(Bar.class).fire(new Bar());
* </code>
*/
public Event<Object> event()
{
return instanceManager.getEvents();
}

public BeanManager getBeanManager()
{
return beanManager;
}
}
Expand Up @@ -19,14 +19,14 @@
import javax.enterprise.event.Event;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

/**
* 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()
* events. It is primarily used as a delegate for the WeldContainer class's instance()
* and event() methods.
*
* @see Weld
* @see WeldContainer
* @author Peter Royle
*/
public class InstanceManager
Expand Down
Expand Up @@ -19,7 +19,8 @@
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.weld.environment.se.StartMain;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.environment.se.events.Shutdown;
import org.jboss.weld.environment.se.test.decorators.CarDoor;
import org.jboss.weld.environment.se.test.decorators.Door;
Expand All @@ -36,19 +37,16 @@
public class DecoratorsTest
{

public static String[] ARGS_EMPTY = new String[]
{
};

/**
* Test that decorators work as expected in SE.
*/
@Test
public void testDecorators()
{
String[] args = ARGS_EMPTY;
BeanManager manager = new StartMain(args).go();

WeldContainer weld = new Weld().initialize();
BeanManager manager = weld.getBeanManager();

CarDoor carDoor = WeldManagerUtils.getInstanceByType(manager, CarDoor.class);
Assert.assertNotNull(carDoor);

Expand Down
Expand Up @@ -19,7 +19,8 @@
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.weld.environment.se.StartMain;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.environment.se.events.Shutdown;
import org.jboss.weld.environment.se.test.beans.InterceptorTestBean;
import org.jboss.weld.environment.se.test.interceptors.AggregatingInterceptor;
Expand All @@ -35,18 +36,14 @@
public class InterceptorsTest
{

public static String[] ARGS_EMPTY = new String[]
{
};

/**
* Test that interceptors work as expected in SE.
*/
@Test
public void testInterceptors()
{
String[] args = ARGS_EMPTY;
BeanManager manager = new StartMain(args).go();
WeldContainer weld = new Weld().initialize();
BeanManager manager = weld.getBeanManager();

InterceptorTestBean intTestBean = WeldManagerUtils.getInstanceByType(manager, InterceptorTestBean.class);
Assert.assertNotNull(intTestBean);
Expand Down
Expand Up @@ -20,6 +20,7 @@
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.weld.environment.se.StartMain;
import org.jboss.weld.environment.se.WeldContainer;
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;
Expand Down Expand Up @@ -49,7 +50,9 @@ public class StartMainTest
public void testMain()
{
String[] args = ARGS;
BeanManager manager = new StartMain(args).go();

WeldContainer weld = new StartMain(args).go();
BeanManager manager = weld.getBeanManager();

MainTestBean mainTestBean = WeldManagerUtils.getInstanceByType(manager, MainTestBean.class);
Assert.assertNotNull(mainTestBean);
Expand All @@ -74,7 +77,8 @@ public void testMain()
@Test
public void testMainEmptyArgs()
{
BeanManager manager = new StartMain(ARGS_EMPTY).go();
WeldContainer weld = new StartMain(ARGS_EMPTY).go();
BeanManager manager = weld.getBeanManager();

MainTestBean mainTestBean = WeldManagerUtils.getInstanceByType(manager, MainTestBean.class);
Assert.assertNotNull(mainTestBean);
Expand All @@ -92,7 +96,8 @@ public void testObservers()
InitObserverTestBean.reset();
ObserverTestBean.reset();

BeanManager manager = new StartMain(ARGS_EMPTY).go();
WeldContainer weld = new StartMain(ARGS_EMPTY).go();
BeanManager manager = weld.getBeanManager();
manager.fireEvent(new CustomEvent());

Assert.assertTrue(ObserverTestBean.isBuiltInObserved());
Expand Down
Expand Up @@ -20,6 +20,7 @@
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
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;
Expand All @@ -37,13 +38,13 @@ public class WeldMainTest
{

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

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

MainTestBean mainTestBean = weld.instance().select(MainTestBean.class).get();
Assert.assertNotNull(mainTestBean);
Expand All @@ -56,15 +57,16 @@ public void testInitialize()
}

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

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

WeldContainer weld = new Weld().initialize();
weld.event().select(CustomEvent.class).fire(new CustomEvent());

Assert.assertTrue(ObserverTestBean.isBuiltInObserved());
Expand Down

0 comments on commit 56f2710

Please sign in to comment.