[jboss-svn-commits] JBL Code SVN: r19882 - in labs/jbosslabs/labs-3.0-build: views/admin/src/main/java/org/jboss/labs/admin/auth and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed May 7 09:07:49 EDT 2008


Author: wrzep
Date: 2008-05-07 09:07:49 -0400 (Wed, 07 May 2008)
New Revision: 19882

Added:
   labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/injection/seam/AdminLabsGuiceInjector.java
Modified:
   labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/Guice.java
   labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/LabsGuiceInterceptor.java
   labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/admin/auth/Authenticator.java
   labs/jbosslabs/labs-3.0-build/views/admin/src/main/webapp/WEB-INF/components.xml
Log:
JBLAB-950 @Guice configured with injector instead of module(s)

Modified: labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/Guice.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/Guice.java	2008-05-07 12:16:31 UTC (rev 19881)
+++ labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/Guice.java	2008-05-07 13:07:49 UTC (rev 19882)
@@ -41,7 +41,9 @@
 public @interface Guice
 {
     /**
-     * Name of the Guice module component.
+     * Name of the Guice injector component.
+     * If not specified, the component named "org.jboss.labs.injection.seam.guiceInjector"
+     * will be used (You have to provide it).
      */
-    String value() default "org.jboss.labs.injection.seam.guiceModule";
+    String value() default "";
 }

Modified: labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/LabsGuiceInterceptor.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/LabsGuiceInterceptor.java	2008-05-07 12:16:31 UTC (rev 19881)
+++ labs/jbosslabs/labs-3.0-build/integration/seam-guice/src/main/java/org/jboss/labs/injection/seam/LabsGuiceInterceptor.java	2008-05-07 13:07:49 UTC (rev 19882)
@@ -24,18 +24,15 @@
 package org.jboss.labs.injection.seam;
 
 import com.google.inject.Injector;
-import com.google.inject.Module;
 import org.jboss.seam.Component;
-import org.jboss.seam.core.Expressions;
 import org.jboss.seam.annotations.intercept.AroundInvoke;
 import org.jboss.seam.annotations.intercept.Interceptor;
+import org.jboss.seam.core.Expressions;
 import org.jboss.seam.intercept.AbstractInterceptor;
 import org.jboss.seam.intercept.InvocationContext;
 import org.jboss.seam.log.Log;
 import org.jboss.seam.log.Logging;
 
-import java.util.concurrent.ConcurrentHashMap;
-
 /**
  * Triggers Guice injection on a Seam component.
  *
@@ -49,61 +46,64 @@
     
     private static final long serialVersionUID = -6716553117162905303L;
 
-    private static final ConcurrentHashMap<Class, Injector> INJECTOR_CACHE
-                                    = new ConcurrentHashMap<Class, Injector>();
+    private static final String DEFAULT_INJECTOR_COMPONENT = "org.jboss.labs.injection.seam.guiceInjector";
 
+    private static Injector CACHED_DEFAULT_INJECTOR = null;
+
     @AroundInvoke
     public Object aroundInvoke(InvocationContext invocationContext) throws Exception
     {
-        final Module module = getGuiceModuleForClass(invocationContext.getMethod().getDeclaringClass());
+        final Injector injector = getInjectorForClass(invocationContext.getMethod().getDeclaringClass());
 
-        if (module == null)
+        if (injector == null)
         {
-            throw new IllegalStateException("Guice module not specified.");
+            throw new IllegalStateException("Guice injector not specified.");
         }
 
         log.debug("Injecting members for: "
 				+ invocationContext.getTarget().getClass().getName()
-                + " using module " + module);
+                + " using injector " + injector);
 
-        Injector injector = getModuleInjectorFromCache(module);
         injector.injectMembers(invocationContext.getTarget());        
 
         return invocationContext.proceed();
     }
 
-    private static Injector getModuleInjectorFromCache(final Module module)
+    private static Injector getInjectorForClass(final Class<?> declaringClass)
     {
-        Injector injector = INJECTOR_CACHE.get(module.getClass());
+        final String expr = declaringClass.getAnnotation(Guice.class).value();
 
-        if (injector == null)
-        {
-            log.debug("Injector not found in cache. Creating a new one.");
-
-            injector = com.google.inject.Guice.createInjector(module);
-            INJECTOR_CACHE.put(module.getClass(), injector);
-        }
-
-        return injector;
+        // Optimize lookups for default injector
+        return (expr.length() == 0)
+                            ? getCachedDefaultInjector()
+                            : getInjectorByName(expr);
     }
 
-    private static Module getGuiceModuleForClass(final Class<?> declaringClass)
+    private static Injector getCachedDefaultInjector()
     {
-        Module module = null;
+        if (CACHED_DEFAULT_INJECTOR == null)
+        {
+            Object result = Component.getInstance(DEFAULT_INJECTOR_COMPONENT);
 
-        final String expr = declaringClass.getAnnotation(Guice.class).value();
+            if (result == null)
+            {
+                throw new IllegalArgumentException("Default Guice injector is not specified.");
+            }
 
-        if (expr.length() > 0)
-        {
-            module = getGuiceModuleByName(expr);
+            if (!(result instanceof Injector))
+            {
+                throw new IllegalArgumentException("Expression '" + DEFAULT_INJECTOR_COMPONENT +
+                                "' does not evaluate to a Guice injector.");
+            }
+
+            CACHED_DEFAULT_INJECTOR = (Injector) result;
         }
 
-        return module;
+        return CACHED_DEFAULT_INJECTOR;
     }
 
-    private static Module getGuiceModuleByName(final String expr)
+    private static Injector getInjectorByName(final String expr)
     {
-
         Object result;
 
         if (expr.startsWith("#"))
@@ -115,12 +115,13 @@
             result = Component.getInstance(expr);
         }
 
-        if (!(result instanceof com.google.inject.Module))
+        if (!(result instanceof Injector))
         {
              throw new IllegalArgumentException("Expression '" + expr +
-                            "' does not evaluate to a Guice module.");
+                            "' does not evaluate to a Guice injector.");
         }
 
-        return (Module) result;
+
+        return (Injector) result;
     }
 }

Modified: labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/admin/auth/Authenticator.java
===================================================================
--- labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/admin/auth/Authenticator.java	2008-05-07 12:16:31 UTC (rev 19881)
+++ labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/admin/auth/Authenticator.java	2008-05-07 13:07:49 UTC (rev 19882)
@@ -47,7 +47,7 @@
 
 
 @Name("authenticator")
- at Guice
+ at Guice("myInjector")
 public class Authenticator {
     
     @In(create = true) private Actor actor;

Copied: labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/injection/seam/AdminLabsGuiceInjector.java (from rev 19879, labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/injection/seam/AdminLabsGuiceModule.java)
===================================================================
--- labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/injection/seam/AdminLabsGuiceInjector.java	                        (rev 0)
+++ labs/jbosslabs/labs-3.0-build/views/admin/src/main/java/org/jboss/labs/injection/seam/AdminLabsGuiceInjector.java	2008-05-07 13:07:49 UTC (rev 19882)
@@ -0,0 +1,44 @@
+/*
+* JBoss Labs. http://labs.jboss.com/jbosslabs
+*
+* Copyright © 2008  Red Hat Middleware, LLC. All rights reserved.
+*
+* This copyrighted material is made available to anyone wishing to use,
+* modify, copy, or redistribute it subject to the terms and conditions
+* of the GNU Lesser General Public License, v. 2.1.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT A WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License, v.2.1 along with this distribution; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+* 02110-1301, USA.
+*
+* Red Hat Author(s): Bob McWhirter, Przemyslaw Dej, Ryszard Kozmik,
+*     Tomasz Szymanski, Adam Warski, Pawel Wrzeszcz
+*/
+
+package org.jboss.labs.injection.seam;
+
+import com.google.inject.Injector;
+import org.jboss.labs.injection.LabsInjection;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Factory;
+import org.jboss.seam.annotations.Name;
+
+/**
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ **/
+
+ at Name("adminLabsGuiceInjectorFactory")
+public class AdminLabsGuiceInjector {
+
+    @Factory(value = "org.jboss.labs.injection.seam.guiceInjector", scope = ScopeType.APPLICATION, autoCreate = true)
+    public Injector getInjector() {
+        System.out.println("AdminLabsGuiceInjector.getInjector");
+        return LabsInjection.getInjector();
+    }
+}
\ No newline at end of file

Modified: labs/jbosslabs/labs-3.0-build/views/admin/src/main/webapp/WEB-INF/components.xml
===================================================================
--- labs/jbosslabs/labs-3.0-build/views/admin/src/main/webapp/WEB-INF/components.xml	2008-05-07 12:16:31 UTC (rev 19881)
+++ labs/jbosslabs/labs-3.0-build/views/admin/src/main/webapp/WEB-INF/components.xml	2008-05-07 13:07:49 UTC (rev 19882)
@@ -66,8 +66,8 @@
    <factory name="emailReplyTo" value="jboss at o2.pl" scope="APPLICATION"/>
 
    <!-- Guice integration -->
-   <component name="org.jboss.labs.injection.seam.guiceModule"
-              class="org.jboss.labs.injection.seam.AdminLabsGuiceModule"
-              auto-create="true" scope="APPLICATION" startup="true"/>
+   <!--<component name="org.jboss.labs.injection.seam.guiceModule"-->
+              <!--class="org.jboss.labs.injection.seam.AdminLabsGuiceInjector"-->
+              <!--auto-create="true" scope="APPLICATION" startup="true"/>-->
 
 </components>




More information about the jboss-svn-commits mailing list