[weld-commits] Weld SVN: r5181 - in java-se/trunk/src: main/java/org/jboss/weld/environment/se/beans and 1 other directories.
weld-commits at lists.jboss.org
weld-commits at lists.jboss.org
Tue Dec 1 05:00:30 EST 2009
Author: peteroyle
Date: 2009-12-01 05:00:30 -0500 (Tue, 01 Dec 2009)
New Revision: 5181
Added:
java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java
Modified:
java-se/trunk/src/main/java/org/jboss/weld/environment/se/StartMain.java
java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java
java-se/trunk/src/main/java/org/jboss/weld/environment/se/beans/InstanceManager.java
java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java
java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java
java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/StartMainTest.java
java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/WeldMainTest.java
Log:
Separated out initialize() (Weld) from instance() and event() (WeldContainer)
Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/StartMain.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/StartMain.java 2009-11-30 20:26:04 UTC (rev 5180)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/StartMain.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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>
*
@@ -39,12 +38,11 @@
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;
}
/**
Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java 2009-11-30 20:26:04 UTC (rev 5180)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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;
@@ -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>
*
@@ -50,7 +47,6 @@
private final Bootstrap bootstrap;
private final BeanStore applicationBeanStore;
private WeldManager manager;
- private InstanceManager instanceManager;
public Weld()
{
@@ -64,7 +60,11 @@
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()
@@ -79,30 +79,17 @@
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();
}
}
Copied: java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java (from rev 5175, java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java)
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java (rev 0)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/WeldContainer.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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;
+ }
+}
Modified: java-se/trunk/src/main/java/org/jboss/weld/environment/se/beans/InstanceManager.java
===================================================================
--- java-se/trunk/src/main/java/org/jboss/weld/environment/se/beans/InstanceManager.java 2009-11-30 20:26:04 UTC (rev 5180)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/beans/InstanceManager.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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
Modified: java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java
===================================================================
--- java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java 2009-11-30 20:26:04 UTC (rev 5180)
+++ java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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;
@@ -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);
Modified: java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java
===================================================================
--- java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java 2009-11-30 20:26:04 UTC (rev 5180)
+++ java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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;
@@ -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);
Modified: java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/StartMainTest.java
===================================================================
--- java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/StartMainTest.java 2009-11-30 20:26:04 UTC (rev 5180)
+++ java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/StartMainTest.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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;
@@ -49,8 +50,10 @@
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);
@@ -74,7 +77,8 @@
@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);
@@ -92,7 +96,8 @@
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());
Modified: java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/WeldMainTest.java
===================================================================
--- java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/WeldMainTest.java 2009-11-30 20:26:04 UTC (rev 5180)
+++ java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/WeldMainTest.java 2009-12-01 10:00:30 UTC (rev 5181)
@@ -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;
@@ -37,13 +38,13 @@
{
/**
- * 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);
@@ -56,7 +57,7 @@
}
/**
- * 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()
@@ -64,7 +65,8 @@
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());
More information about the weld-commits
mailing list