[weld-commits] Weld SVN: r5173 - in java-se/trunk: src/main/java/org/jboss/weld/environment/se and 2 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Sun Nov 29 18:11:26 EST 2009


Author: peteroyle
Date: 2009-11-29 18:11:26 -0500 (Sun, 29 Nov 2009)
New Revision: 5173

Added:
   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/WeldMainTest.java
Modified:
   java-se/trunk/
   java-se/trunk/src/main/java/org/jboss/weld/environment/se/StartMain.java
Log:
Initial addition of new API for booting Weld from SE


Property changes on: java-se/trunk
___________________________________________________________________
Name: svn:ignore
   + target


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-29 21:13:40 UTC (rev 5172)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/StartMain.java	2009-11-29 23:11:26 UTC (rev 5173)
@@ -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
@@ -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();
-   }
+    /**
+     * 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;
-   }
-
+    public static String[] getParameters()
+    {
+        // TODO(PR): make immutable
+        return PARAMETERS;
+    }
 }

Added: 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	                        (rev 0)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/Weld.java	2009-11-29 23:11:26 UTC (rev 5173)
@@ -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();
+   }
+}

Added: 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	                        (rev 0)
+++ java-se/trunk/src/main/java/org/jboss/weld/environment/se/beans/InstanceManager.java	2009-11-29 23:11:26 UTC (rev 5173)
@@ -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;
+   }
+}

Added: 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	                        (rev 0)
+++ java-se/trunk/src/test/java/org/jboss/weld/environment/se/test/WeldMainTest.java	2009-11-29 23:11:26 UTC (rev 5173)
@@ -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()
+      {
+      }
+   }
+}



More information about the weld-commits mailing list