[seam-commits] Seam SVN: r10386 - in trunk: build and 16 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sat Apr 11 02:33:20 EDT 2009


Author: dan.j.allen
Date: 2009-04-11 02:33:19 -0400 (Sat, 11 Apr 2009)
New Revision: 10386

Added:
   trunk/doc/Seam_Reference_Guide/en-US/Guice.xml
   trunk/examples/guice/
   trunk/examples/guice/build.xml
   trunk/examples/guice/readme.txt
   trunk/examples/guice/resources/
   trunk/examples/guice/resources/META-INF/
   trunk/examples/guice/resources/META-INF/application.xml
   trunk/examples/guice/resources/META-INF/jboss-app.xml
   trunk/examples/guice/resources/WEB-INF/
   trunk/examples/guice/resources/WEB-INF/components.xml
   trunk/examples/guice/resources/WEB-INF/faces-config.xml
   trunk/examples/guice/resources/WEB-INF/web.xml
   trunk/examples/guice/resources/seam.properties
   trunk/examples/guice/src/
   trunk/examples/guice/src/org/
   trunk/examples/guice/src/org/jboss/
   trunk/examples/guice/src/org/jboss/seam/
   trunk/examples/guice/src/org/jboss/seam/example/
   trunk/examples/guice/src/org/jboss/seam/example/guice/
   trunk/examples/guice/src/org/jboss/seam/example/guice/Juice.java
   trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBar.java
   trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBarModule.java
   trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceImpl.java
   trunk/examples/guice/src/org/jboss/seam/example/guice/Orange.java
   trunk/examples/guice/src/org/jboss/seam/example/guice/test/
   trunk/examples/guice/src/org/jboss/seam/example/guice/test/GuiceTest.java
   trunk/examples/guice/src/org/jboss/seam/example/guice/test/testng.xml
   trunk/examples/guice/view/
   trunk/examples/guice/view/bar.xhtml
   trunk/examples/guice/view/index.html
   trunk/src/ioc/org/jboss/seam/ioc/guice/
   trunk/src/ioc/org/jboss/seam/ioc/guice/Guice.java
   trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInit.java
   trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInterceptor.java
   trunk/src/ioc/org/jboss/seam/ioc/guice/Injector.java
   trunk/src/ioc/org/jboss/seam/ioc/guice/guice-2.1.xsd
   trunk/src/ioc/org/jboss/seam/ioc/guice/package-info.java
Modified:
   trunk/build.xml
   trunk/build/ioc.pom.xml
   trunk/build/root.pom.xml
   trunk/doc/Seam_Reference_Guide/en-US/master.xml
   trunk/examples/build.xml
Log:
JBSEAM-3044


Modified: trunk/build/ioc.pom.xml
===================================================================
--- trunk/build/ioc.pom.xml	2009-04-11 06:32:03 UTC (rev 10385)
+++ trunk/build/ioc.pom.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -53,6 +53,12 @@
       <scope>provided</scope>
     </dependency>
 
+    <dependency>
+      <groupId>com.google.code.guice</groupId>
+      <artifactId>guice</artifactId>
+      <optional>true</optional>
+    </dependency>
+
    <!-- Microcontainer stuff isn't done -->
 
     <!-- <dependency>
@@ -102,4 +108,4 @@
 
   </dependencies>
 
-</project>
\ No newline at end of file
+</project>

Modified: trunk/build/root.pom.xml
===================================================================
--- trunk/build/root.pom.xml	2009-04-11 06:32:03 UTC (rev 10385)
+++ trunk/build/root.pom.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -850,6 +850,12 @@
           </exclusion>
         </exclusions>
       </dependency>
+
+      <dependency>
+        <groupId>com.google.code.guice</groupId>
+        <artifactId>guice</artifactId>
+        <version>1.0</version>
+      </dependency>
       
       <dependency>
         <groupId>cglib</groupId>

Modified: trunk/build.xml
===================================================================
--- trunk/build.xml	2009-04-11 06:32:03 UTC (rev 10385)
+++ trunk/build.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -461,6 +461,7 @@
 		<cleanexample name="restbay" />
 		<cleanexample name="nestedbooking" />
 		<cleanexample name="wicket" />
+		<cleanexample name="guice" />
 	</target>
 
 	<target name="undeployexamples" description="Undeploy all examples">
@@ -496,6 +497,7 @@
 		<undeployexample name="restbay" />
 		<undeployexample name="nestedbooking" />
 		<undeployexample name="wicket" />
+		<undeployexample name="guice" />
 	</target>
 
 	<target name="testexamples" description="Run the example tests">
@@ -522,6 +524,7 @@
 		<testexample name="hibernate" />
 		<testexample name="jpa" />
 		<testexample name="quartz" />
+		<testexample name="guice" />
 		<ant dir="examples/wiki" target="test" inheritall="false">
 			<property name="test.output.dir" value="${test.dir}/wiki" />
 		</ant>
@@ -567,6 +570,7 @@
 		<ant dir="examples/todo" target="validateConfiguration" inheritall="false" />
 		<ant dir="examples/ui" target="validateConfiguration" inheritall="false" />
 		<ant dir="examples/wicket" target="validateConfiguration" inheritall="false" />		
+		<ant dir="examples/guice" target="validateConfiguration" inheritall="false" />		
 		
 	</target>
 

Added: trunk/doc/Seam_Reference_Guide/en-US/Guice.xml
===================================================================
--- trunk/doc/Seam_Reference_Guide/en-US/Guice.xml	                        (rev 0)
+++ trunk/doc/Seam_Reference_Guide/en-US/Guice.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,152 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
+
+<chapter id="guice">
+
+   <title>Guice integration</title>
+
+   <para>
+      Google Guice is a library that provides lightweight dependency injection through type-safe
+      resolution. The Guice integration (part of the Seam IoC module) allows use of Guice injection
+      for all Seam components annotated with the <literal>@Guice</literal> annotation. In addition
+      to the regular bijection that Seam performs (which becomes optional), Seam also delegates
+      to known Guice injectors to satisify the dependencies of the component. Guice may be useful to
+      tie non-Seam parts of large or legacy applications together with Seam. 
+   </para> 
+
+   <note>
+      The Guice integration is bundled in the jboss-seam-ioc library. This dependency is required
+      for all integration techniques covered in this chapter. You will also need the Guice JAR file
+      on the classpath.
+   </note>
+
+   <section>
+      <title>Creating a hybrid Seam-Guice component</title>
+
+      <para>
+         The goal is to create a hybrid Seam-Guice component. The rule for how to do this is very
+         simple. If you want to use Guice injection in your Seam component, annotate it with the
+         <literal>@Guice</literal> annotation (after importing the type
+         <literal>org.jboss.seam.ioc.guice.Guice</literal>).
+      </para>
+
+      <programlisting role="JAVA">@Name("myGuicyComponent")
+ at Guice public class MyGuicyComponent
+{
+   @Inject MyObject myObject;
+   @Inject @Special MyObject mySpecialObject;
+   ...
+}</programlisting>
+
+      <para>
+          This Guice injection will happen on every method call, just like with bijection. Guice
+          injects based on type and binding. To satisify the dependencies in the previous example,
+          you might have bound the following implementations in a Guice module, where
+          <literal>@Special</literal> is an annotation you define in your application.
+      </para>
+
+      <programlisting role="JAVA">public class MyGuicyModule implements Module
+{
+   public void configure(Binder binder)
+   {
+      binder.bind(MyObject.class)
+         .toInstance(new MyObject("regular"));
+
+      binder.bind(MyObject.class).annotatedWith(Special.class)
+         .toInstance(new MyObject("special"));
+   }
+}</programlisting>
+      
+      <para>
+          Great, but which Guice injector will be used to inject the dependencies?  Well, you need
+          to perform some setup first.
+      </para>
+
+   </section>
+
+   <section>
+
+      <title>Configuring an injector</title>
+
+      <para>
+         You tell Seam which Guice injector to use by hooking it into the injection property
+         of the Guice initization component in the Seam component descriptor (components.xml):
+      </para>
+
+      <programlisting role="XML"><![CDATA[<components xmlns="http://jboss.com/products/seam/components"
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xmlns:guice="http://jboss.org/products/seam/guice"
+   xsi:schemaLocation="
+      http://jboss.com/products/seam/guice
+      http://jboss.com/products/seam/guice-2.1.xsd
+      http://jboss.com/products/seam/components
+      http://jboss.com/products/seam/components-2.1.xsd">
+
+   <guice:init injector="#{myGuiceInjector}"/>
+
+</components>]]></programlisting>
+
+      <para>
+         <literal>myGuiceInjector</literal> must resolve to a Seam component that implements the
+         Guice <literal>Injector</literal> interface.
+      </para>
+      
+      <para>
+         Having to create an injector is boiler-plate code, though. What you really want to be able
+         to do is simply hook up Seam to your Guice modules. Fortunately, there is a built-in Seam
+         component that implements the <literal>Injector</literal> interface to do exactly that. You
+         can configure it in the Seam component descriptor with this additional stanza.
+      </para>
+
+      <programlisting role="XML"><![CDATA[<guice:injector name="myGuiceInjector">
+   <guice:modules>  
+      <value>com.example.guice.GuiceModule1</value>  
+      <value>com.example.guice.GuiceModule2</value>  
+   </guice:modules>  
+</guice:injector>]]></programlisting>
+
+      <para>
+         Of course you can also use an injector that is already used in other, possibly non-Seam
+         part of you application. That's one of the main motivations for creating this integration.
+         Since the injector is defined with EL expression, you can obtain it in whatever way you
+         like. For instance, you may use the Seam factory component pattern to provide injector.
+      </para>
+
+      <programlisting role="JAVA">@Name("myGuiceInjectorFactory")
+public InjectorFactory
+{
+   @Factory(name = "myGuiceInjector", scope = APPLICATION, create = true)
+   public Injector getInjector()
+   {
+      // Your code that returns injector    
+   }
+}</programlisting>
+
+   </section>
+
+   <section>
+      <title>Using multiple injectors</title>
+
+      <para>
+         By default, an injector configured in the Seam component descriptor is used. If you really
+         need to use multiple injectors (AFAIK, you should use multiple modules instead), you can
+         specify different injector for every Seam component in the <literal>@Guice</literal>
+         annotation.
+      </para>
+
+      <programlisting role="JAVA">@Name("myGuicyComponent")
+ at Guice("myGuiceInjector")
+public class MyGuicyComponent
+{
+   @Inject MyObject myObject;
+   ...
+}</programlisting>
+      
+   </section>
+
+   <para>
+      That's all there is to it! Check out the guice example in the Seam distribution to see the
+      Seam Guice integration in action!
+   </para>
+
+</chapter>

Modified: trunk/doc/Seam_Reference_Guide/en-US/master.xml
===================================================================
--- trunk/doc/Seam_Reference_Guide/en-US/master.xml	2009-04-11 06:32:03 UTC (rev 10385)
+++ trunk/doc/Seam_Reference_Guide/en-US/master.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -31,6 +31,7 @@
     <xi:include href="Remoting.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
     <xi:include href="Gwt.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
     <xi:include href="Spring.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
+    <xi:include href="Guice.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
     <xi:include href="Hsearch.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
     <xi:include href="Configuration.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
     <xi:include href="Annotations.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />

Modified: trunk/examples/build.xml
===================================================================
--- trunk/examples/build.xml	2009-04-11 06:32:03 UTC (rev 10385)
+++ trunk/examples/build.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -232,6 +232,12 @@
 		<include name="slf4j-log4j12.jar" if="seam.resteasy.lib" />
 	</fileset>
 
+    <!-- Seam IoC Guice, with required dependencies -->
+    <fileset id="seam.ioc-guice.jar" dir="${lib.dir}">
+        <include name="jboss-seam-ioc.jar" if="seam.ioc-guice.lib" />
+        <include name="guice.jar" if="seam.ioc-guice.lib" />
+    </fileset>
+
 	<!-- jboss-el -->
 	<fileset id="jboss-el.jar" dir="${lib.dir}">
 		<include name="jboss-el.jar" />
@@ -749,6 +755,7 @@
 			<fileset refid="seam.wicket.jar" />
 			<fileset refid="seam.resteasy.jar" />
 			<fileset refid="seam.remoting.jar" />
+			<fileset refid="seam.ioc-guice.jar" />
 			<fileset refid="el-ri.jar" />
 			<fileset refid="trinidad-api.jar" />
 			<fileset refid="metawidget-backend.jar" />


Property changes on: trunk/examples/guice
___________________________________________________________________
Name: svn:ignore
   + dist
exploded-archives


Added: trunk/examples/guice/build.xml
===================================================================
--- trunk/examples/guice/build.xml	                        (rev 0)
+++ trunk/examples/guice/build.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<project name="Seam Guice" default="deploy" basedir=".">
+
+    <!-- Example name -->
+    <property name="Name" value="Seam Guice Example"/>
+    <property name="example.name" value="jboss-seam-guice"/>
+
+    <!-- Libraries -->
+    <property name="seam.ioc-guice.lib" value="yes"/>
+    <property name="seam.debug.lib" value="yes"/>
+    <property name="facelets.lib" value="yes"/>
+
+    <import file="../build.xml"/>
+
+</project>

Added: trunk/examples/guice/readme.txt
===================================================================
--- trunk/examples/guice/readme.txt	                        (rev 0)
+++ trunk/examples/guice/readme.txt	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,5 @@
+Seam Guice Example
+======================
+This is a simple example demonstrating the use of Google Guice with Seam.
+
+example.name=guice

Added: trunk/examples/guice/resources/META-INF/application.xml
===================================================================
--- trunk/examples/guice/resources/META-INF/application.xml	                        (rev 0)
+++ trunk/examples/guice/resources/META-INF/application.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<application xmlns="http://java.sun.com/xml/ns/javaee" 
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
+             version="5">
+             
+    <display-name>Seam Guice Example</display-name>
+    
+    <module>
+        <web>
+            <web-uri>jboss-seam-guice.war</web-uri>
+            <context-root>/seam-guice</context-root>
+        </web>
+    </module>
+    <module>
+        <ejb>jboss-seam-guice.jar</ejb>
+    </module>
+    <module>
+        <ejb>jboss-seam.jar</ejb>
+    </module>
+
+</application>

Added: trunk/examples/guice/resources/META-INF/jboss-app.xml
===================================================================
--- trunk/examples/guice/resources/META-INF/jboss-app.xml	                        (rev 0)
+++ trunk/examples/guice/resources/META-INF/jboss-app.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+  <!DOCTYPE jboss-app
+    PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
+    "http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd">
+
+<jboss-app>
+      <loader-repository> 
+      seam.jboss.org:loader=seam-guice
+      </loader-repository> 
+</jboss-app> 
\ No newline at end of file

Added: trunk/examples/guice/resources/WEB-INF/components.xml
===================================================================
--- trunk/examples/guice/resources/WEB-INF/components.xml	                        (rev 0)
+++ trunk/examples/guice/resources/WEB-INF/components.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<components xmlns="http://jboss.com/products/seam/components"
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xmlns:core="http://jboss.com/products/seam/core"
+   xmlns:guice="http://jboss.com/products/seam/guice"
+   xsi:schemaLocation="
+      http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
+      http://jboss.com/products/seam/guice http://jboss.com/products/seam/guice-2.1.xsd
+      http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd">
+
+   <core:init debug="true"/>
+
+   <guice:injector name="guiceExampleInjector">
+      <guice:modules>
+         <value>org.jboss.seam.example.guice.JuiceBarModule</value>
+      </guice:modules>
+   </guice:injector>
+
+   <guice:init injector="#{guiceExampleInjector}"/>
+
+</components>

Added: trunk/examples/guice/resources/WEB-INF/faces-config.xml
===================================================================
--- trunk/examples/guice/resources/WEB-INF/faces-config.xml	                        (rev 0)
+++ trunk/examples/guice/resources/WEB-INF/faces-config.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,11 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<faces-config version="1.2"
+              xmlns="http://java.sun.com/xml/ns/javaee"
+              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
+    
+    <application>
+        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
+    </application>
+
+</faces-config>

Added: trunk/examples/guice/resources/WEB-INF/web.xml
===================================================================
--- trunk/examples/guice/resources/WEB-INF/web.xml	                        (rev 0)
+++ trunk/examples/guice/resources/WEB-INF/web.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="2.5"
+    xmlns="http://java.sun.com/xml/ns/javaee"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
+
+    <display-name>Seam Guice Example</display-name>
+
+    <listener>
+        <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
+    </listener>
+
+    <filter>
+        <filter-name>Seam Filter</filter-name>
+        <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
+    </filter>
+
+    <filter-mapping>
+        <filter-name>Seam Filter</filter-name>
+        <url-pattern>/*</url-pattern>
+    </filter-mapping>
+
+    <context-param>
+        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
+        <param-value>.xhtml</param-value>
+    </context-param>
+    
+    <context-param>
+        <param-name>facelets.DEVELOPMENT</param-name>
+        <param-value>true</param-value>
+    </context-param>
+    
+    <servlet>
+        <servlet-name>Faces Servlet</servlet-name>
+        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+    
+    <servlet-mapping>
+        <servlet-name>Faces Servlet</servlet-name>
+        <url-pattern>*.seam</url-pattern>
+    </servlet-mapping>
+    
+    <session-config>
+        <session-timeout>10</session-timeout> 
+    </session-config>
+    
+</web-app>

Added: trunk/examples/guice/resources/seam.properties
===================================================================

Added: trunk/examples/guice/src/org/jboss/seam/example/guice/Juice.java
===================================================================
--- trunk/examples/guice/src/org/jboss/seam/example/guice/Juice.java	                        (rev 0)
+++ trunk/examples/guice/src/org/jboss/seam/example/guice/Juice.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,11 @@
+package org.jboss.seam.example.guice;
+
+/**
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+public interface Juice
+{
+   public String getName();
+
+   public int getPrice();
+}

Added: trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBar.java
===================================================================
--- trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBar.java	                        (rev 0)
+++ trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBar.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,26 @@
+package org.jboss.seam.example.guice;
+
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.ioc.guice.Guice;
+import com.google.inject.Inject;
+
+/**
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+ at Name("juiceBar")
+ at Guice // Activates @Inject on a Seam component
+public class JuiceBar
+{
+   @Inject private Juice juiceOfTheDay; // Guice looks at the variable type, not name
+   @Inject @Orange private Juice anotherJuice;
+
+   public Juice getJuiceOfTheDay()
+   {
+       return juiceOfTheDay;
+   }
+
+   public Juice getAnotherJuice()
+   {
+       return anotherJuice;
+   }
+}

Added: trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBarModule.java
===================================================================
--- trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBarModule.java	                        (rev 0)
+++ trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceBarModule.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,16 @@
+package org.jboss.seam.example.guice;
+
+import com.google.inject.Module;
+import com.google.inject.Binder;
+
+/**
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+public class JuiceBarModule implements Module
+{
+   public void configure(Binder binder)
+   {
+      binder.bind(Juice.class).toInstance(new JuiceImpl("Apple Juice", 10));
+      binder.bind(Juice.class).annotatedWith(Orange.class).toInstance(new JuiceImpl("Orange Juice", 12)); 
+   }
+}

Added: trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceImpl.java
===================================================================
--- trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceImpl.java	                        (rev 0)
+++ trunk/examples/guice/src/org/jboss/seam/example/guice/JuiceImpl.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,32 @@
+package org.jboss.seam.example.guice;
+
+/**
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+public class JuiceImpl implements Juice
+{
+   private String name;
+   private int price;
+
+   public JuiceImpl(String name, int price)
+   {
+      this.name = name;
+      this.price = price;
+   }
+
+    public String getName()
+   {
+      return name;
+   }
+
+   public int getPrice()
+   {
+      return price;
+   }
+
+   @Override
+   public String toString()
+   {
+      return name + " (" + price + " cents)";
+   }
+}

Added: trunk/examples/guice/src/org/jboss/seam/example/guice/Orange.java
===================================================================
--- trunk/examples/guice/src/org/jboss/seam/example/guice/Orange.java	                        (rev 0)
+++ trunk/examples/guice/src/org/jboss/seam/example/guice/Orange.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,20 @@
+package org.jboss.seam.example.guice;
+
+import com.google.inject.BindingAnnotation;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.ElementType;
+
+/**
+ * Indicates we want the orange version of a binding.
+ *
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target({ElementType.FIELD, ElementType.PARAMETER})
+ at BindingAnnotation
+public @interface Orange
+{
+}

Added: trunk/examples/guice/src/org/jboss/seam/example/guice/test/GuiceTest.java
===================================================================
--- trunk/examples/guice/src/org/jboss/seam/example/guice/test/GuiceTest.java	                        (rev 0)
+++ trunk/examples/guice/src/org/jboss/seam/example/guice/test/GuiceTest.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,33 @@
+package org.jboss.seam.example.guice.test;
+
+import org.jboss.seam.example.guice.JuiceBar;
+import org.jboss.seam.mock.SeamTest;
+import org.jboss.seam.ioc.guice.Injector;
+import org.testng.annotations.Test;
+
+import java.util.Arrays;
+
+/**
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+public class GuiceTest extends SeamTest
+{
+   private static final String[] expectedModules = { "org.jboss.seam.example.guice.JuiceBarModule" };
+
+   @Test
+   public void testGuice()
+   {
+      new ComponentTest()
+      {
+         protected void testComponents() throws Exception
+         {
+            Injector injector = (Injector) getValue("guiceExampleInjector");
+            assert Arrays.equals(expectedModules, injector.getModules());
+
+            JuiceBar juiceBar = (JuiceBar) getValue("juiceBar");
+            assert "Apple Juice".equals(juiceBar.getJuiceOfTheDay().getName());
+            assert "Orange Juice".equals(juiceBar.getAnotherJuice().getName());             
+         }
+      };
+   }
+}

Added: trunk/examples/guice/src/org/jboss/seam/example/guice/test/testng.xml
===================================================================
--- trunk/examples/guice/src/org/jboss/seam/example/guice/test/testng.xml	                        (rev 0)
+++ trunk/examples/guice/src/org/jboss/seam/example/guice/test/testng.xml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,11 @@
+<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
+
+<suite name="Guice" verbose="2" parallel="false">
+
+   <test name="Guice">
+     <classes>
+       <class name="org.jboss.seam.example.guice.test.GuiceTest"/>
+     </classes>
+   </test>
+
+</suite>
\ No newline at end of file

Added: trunk/examples/guice/view/bar.xhtml
===================================================================
--- trunk/examples/guice/view/bar.xhtml	                        (rev 0)
+++ trunk/examples/guice/view/bar.xhtml	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,22 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+    xmlns:h="http://java.sun.com/jsf/html"
+    xmlns:f="http://java.sun.com/jsf/core">
+    <head>
+        <title>Juice Bar</title>
+    </head>
+    <body>
+        <h1>Juice Bar</h1>
+        <h:form id="main">
+            <div>
+                Which juice do you prefer?
+                <ul>
+                    <li>#{juiceBar.juiceOfTheDay.name}* - #{juiceBar.juiceOfTheDay.price} cents</li>
+                    <li>#{juiceBar.anotherJuice.name} - #{juiceBar.anotherJuice.price} cents</li>
+                </ul>
+                <p><em>* Juice of the day</em></p>
+            </div>
+        </h:form>
+    </body>
+</html>

Added: trunk/examples/guice/view/index.html
===================================================================
--- trunk/examples/guice/view/index.html	                        (rev 0)
+++ trunk/examples/guice/view/index.html	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,5 @@
+<html>
+<head>
+  <meta http-equiv="Refresh" content="0; URL=bar.seam">
+</head>
+</html>
\ No newline at end of file

Added: trunk/src/ioc/org/jboss/seam/ioc/guice/Guice.java
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/guice/Guice.java	                        (rev 0)
+++ trunk/src/ioc/org/jboss/seam/ioc/guice/Guice.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,25 @@
+package org.jboss.seam.ioc.guice;
+
+import org.jboss.seam.annotations.intercept.Interceptors;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Google Guice support. Allows use of Guice injection inside Seam component.
+ *
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+ at Target(TYPE)
+ at Retention(RUNTIME)
+ at Interceptors(GuiceInterceptor.class)
+public @interface Guice
+{
+   /**
+    * Name of the Guice injector component.
+    * By default the injector specified in the components.xml file is used.
+    */
+   String value() default "";
+}

Added: trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInit.java
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInit.java	                        (rev 0)
+++ trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInit.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,48 @@
+package org.jboss.seam.ioc.guice;
+
+import static org.jboss.seam.annotations.Install.FRAMEWORK;
+
+import java.io.Serializable;
+
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Startup;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+
+import com.google.inject.Injector;
+
+/**
+ * Guice support configuration component. Holds reference to the default
+ * injector.
+ * 
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+ at Name("org.jboss.seam.ioc.guice.init")
+ at Scope(ScopeType.APPLICATION)
+ at Startup
+ at Install(precedence = FRAMEWORK, classDependencies = "com.google.inject.Injector")
+ at BypassInterceptors
+public class GuiceInit implements Serializable
+{
+   private static final long serialVersionUID = -1517814449129434488L;
+   
+   private Injector injector;
+   
+   public Injector getInjector()
+   {
+      return injector;
+   }
+   
+   public void setInjector(Injector injector)
+   {
+      this.injector = injector;
+   }
+   
+   @Override
+   public String toString()
+   {
+      return "org.jboss.seam.ioc.guice.init(" + injector + ")";
+   }
+}

Added: trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInterceptor.java
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInterceptor.java	                        (rev 0)
+++ trunk/src/ioc/org/jboss/seam/ioc/guice/GuiceInterceptor.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,99 @@
+package org.jboss.seam.ioc.guice;
+
+import org.jboss.seam.Component;
+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.InvocationContext;
+import org.jboss.seam.log.Log;
+import org.jboss.seam.log.Logging;
+
+import com.google.inject.Injector;
+
+/**
+ * Triggers Guice injection on a Seam component.
+ *
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ * @author Tomasz Szymanski (tszymanski [at] jboss.org)
+ */
+ at Interceptor
+public class GuiceInterceptor
+{
+   private static final Log log = Logging.getLog(GuiceInterceptor.class);
+
+   private static final long serialVersionUID = -6716553117162905303L;
+
+   private transient Injector defaultInjector = null;
+
+   @AroundInvoke
+   public Object aroundInvoke(InvocationContext invocationContext) throws Exception
+   {
+      final Injector injector = getInjectorForClass(invocationContext.getMethod().getDeclaringClass());
+
+      if (log.isTraceEnabled())
+      {
+         log.trace("Injecting members of: #0", invocationContext.getTarget().getClass().getName());
+      }
+
+      injector.injectMembers(invocationContext.getTarget());
+
+      return invocationContext.proceed();
+   }
+
+   private Injector getInjectorForClass(final Class<?> declaringClass)
+   {
+      final String expr;
+      Guice guice = declaringClass.getAnnotation(Guice.class);
+      if (guice != null) {
+         expr = guice.value();
+      }
+      else {
+         expr = null;
+      }
+
+      // Optimize lookups for default injector
+      return (expr != null && expr.length() > 0)
+         ? getInjectorByName(expr) : getCachedDefaultInjector();
+   }
+
+   private Injector getCachedDefaultInjector()
+   {
+      if (defaultInjector == null)
+      {
+         GuiceInit init = (GuiceInit) Component.getInstance(GuiceInit.class);
+
+         if (init != null)
+         {
+            defaultInjector = init.getInjector();
+         }    
+
+         if (defaultInjector == null)
+         {
+            throw new IllegalStateException("Default Guice injector not specified.");
+         }
+      }
+
+      return defaultInjector;
+   }
+
+   public static Injector getInjectorByName(final String expr)
+   {
+      Object result;
+
+      if (expr.startsWith("#"))
+      {
+         result = Expressions.instance().createValueExpression(expr).getValue();
+      }
+      else
+      {
+         result = Component.getInstance(expr);
+      }
+
+      if (!(result instanceof Injector))
+      {
+         throw new IllegalArgumentException("Expression '" + expr + "' does not evaluate to a Guice injector.");
+      }
+
+      return (Injector) result;
+   }
+}

Added: trunk/src/ioc/org/jboss/seam/ioc/guice/Injector.java
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/guice/Injector.java	                        (rev 0)
+++ trunk/src/ioc/org/jboss/seam/ioc/guice/Injector.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,93 @@
+package org.jboss.seam.ioc.guice;
+
+import com.google.inject.Guice;
+import com.google.inject.Module;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.*;
+import static org.jboss.seam.annotations.Install.FRAMEWORK;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Creates Guice injector from a list of modules.
+ *
+ * @author Pawel Wrzeszcz (pwrzeszcz [at] jboss . org)
+ */
+ at Name("org.jboss.seam.ioc.guice.injector")
+ at Scope(ScopeType.APPLICATION)
+ at Startup
+ at Install(value=false, precedence = FRAMEWORK)
+public class Injector implements Serializable
+{
+   private static final long serialVersionUID = 8935525407647910950L;
+
+   private static final LogProvider log = Logging.getLogProvider(Injector.class);
+
+   private String[] modules;
+
+   private com.google.inject.Injector injector = null;
+
+   @Unwrap
+   public com.google.inject.Injector getInjector()
+   {
+      return injector;
+   }
+
+   @Create
+   public void createInjector() throws IllegalAccessException, InstantiationException, ClassNotFoundException
+   {
+      if ((modules == null) || (modules.length == 0))
+      {
+         throw new IllegalArgumentException("No Guice module specified.");
+      }
+
+      log.info("Creating injector '" +
+               "'from modules: " + Arrays.toString(modules));
+
+      final List<Module> moduleList = getModuleList(modules);
+
+      injector = Guice.createInjector(moduleList);
+   }
+
+   private static List<Module> getModuleList(String[] modules) throws IllegalAccessException, InstantiationException, ClassNotFoundException
+   {
+      List<Module> moduleList = new ArrayList<Module>();
+
+      for (String m : modules)
+      {
+         Module module = getModule(m);
+         moduleList.add(module);
+      }
+
+      return moduleList;
+   }
+
+   private static Module getModule(String className)
+   {
+      try
+      {
+         final Class<?> clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
+
+         return (Module) clazz.newInstance();
+      }
+      catch (Exception e)
+      {
+         throw new IllegalArgumentException("Unable to create guice module: " + className, e);
+      }
+   }
+
+   public String[] getModules()
+   {
+      return modules;
+   }
+
+   public void setModules(String[] modules)
+   {
+      this.modules = modules;
+   }
+}

Added: trunk/src/ioc/org/jboss/seam/ioc/guice/guice-2.1.xsd
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/guice/guice-2.1.xsd	                        (rev 0)
+++ trunk/src/ioc/org/jboss/seam/ioc/guice/guice-2.1.xsd	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
+    targetNamespace="http://jboss.com/products/seam/guice" xmlns:guice="http://jboss.com/products/seam/guice"
+    xmlns:components="http://jboss.com/products/seam/components" attributeFormDefault="unqualified">
+
+    <xs:import namespace="http://jboss.com/products/seam/components"
+        schemaLocation="http://jboss.com/products/seam/components-2.1.xsd"/>
+
+    <xs:element name="init">
+        <xs:annotation>
+            <xs:documentation>Guice Support Configuration</xs:documentation>
+        </xs:annotation>
+        <xs:complexType mixed="true">
+            <xs:attributeGroup ref="guice:attlist.guiceInit"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:attributeGroup name="attlist.guiceInit">
+        <xs:attribute name="injector" type="components:string"/>
+    </xs:attributeGroup>
+
+    <xs:element name="injector">
+        <xs:annotation>
+            <xs:documentation>Guice Injector</xs:documentation>
+        </xs:annotation>
+        <xs:complexType mixed="true">
+            <xs:choice minOccurs="0" maxOccurs="unbounded">
+                <xs:element name="modules" type="components:multiValuedProperty">
+                    <xs:annotation>
+                        <xs:documentation>A list of modules</xs:documentation>
+                    </xs:annotation>
+                </xs:element>
+            </xs:choice>
+            <xs:attributeGroup ref="components:attlist.component"/>
+            <xs:attributeGroup ref="guice:attlist.guiceInjector"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:attributeGroup name="attlist.guiceInjector"/>
+
+</xs:schema>

Added: trunk/src/ioc/org/jboss/seam/ioc/guice/package-info.java
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/guice/package-info.java	                        (rev 0)
+++ trunk/src/ioc/org/jboss/seam/ioc/guice/package-info.java	2009-04-11 06:33:19 UTC (rev 10386)
@@ -0,0 +1,6 @@
+ at Namespace(value="http://jboss.com/products/seam/guice", prefix = "org.jboss.seam.ioc.guice")
+ at AutoCreate
+package org.jboss.seam.ioc.guice;
+
+import org.jboss.seam.annotations.Namespace;
+import org.jboss.seam.annotations.AutoCreate;




More information about the seam-commits mailing list