changes to the struts2 plugin, by Ben McCann.

git-svn-id: https://google-guice.googlecode.com/svn/trunk@1431 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/extensions/struts2/example/root/Counter.jsp b/extensions/struts2/example/root/Counter.jsp
new file mode 100644
index 0000000..1e7f9c4
--- /dev/null
+++ b/extensions/struts2/example/root/Counter.jsp
@@ -0,0 +1,15 @@
+<%@ taglib prefix="s" uri="/struts-tags" %>
+
+<html>
+  <body>
+    <h1>Counter Example</h1>
+    <h3><b>Hits in this session:</b>
+      <s:property value="count"/></h3>
+
+    <h3><b>Status:</b>
+      <s:property value="status"/></h3>
+
+    <h3><b>Message:</b>
+      <s:property value="message"/></h3>
+  </body>
+</html>
diff --git a/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java b/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java
new file mode 100644
index 0000000..244d34b
--- /dev/null
+++ b/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java
@@ -0,0 +1,51 @@
+/**
+ * Copyright (C) 2006 Google Inc.
+ *
+ * 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 com.google.inject.struts2.example;
+
+import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Singleton;
+import com.google.inject.servlet.GuiceServletContextListener;
+import com.google.inject.servlet.ServletModule;
+import com.google.inject.struts2.Struts2GuicePluginModule;
+
+/**
+ * Example application module.
+ *
+ * @author crazybob@google.com (Bob Lee)
+ */
+public class ExampleListener extends GuiceServletContextListener {
+
+  public Injector getInjector() {
+    return Guice.createInjector(
+      new Struts2GuicePluginModule(),
+      new ServletModule() {
+        @Override
+        protected void configureServlets() {      
+          // Struts 2 setup
+          bind(StrutsPrepareAndExecuteFilter.class).in(Singleton.class);
+          filter("/*").through(StrutsPrepareAndExecuteFilter.class);
+
+          // Our app-specific code
+          bind(Service.class).to(ServiceImpl.class);
+      }
+    });
+  }
+
+}
diff --git a/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListenerAndModule.java b/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListenerAndModule.java
deleted file mode 100644
index 4fc5cac..0000000
--- a/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListenerAndModule.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * Copyright (C) 2006 Google Inc.
- *
- * 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 com.google.inject.struts2.example;
-
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Singleton;
-import com.google.inject.servlet.GuiceServletContextListener;
-import com.google.inject.servlet.ServletModule;
-import org.apache.struts2.dispatcher.FilterDispatcher;
-
-/**
- * Example application module.
- *
- * @author crazybob@google.com (Bob Lee)
- */
-public class ExampleListenerAndModule extends GuiceServletContextListener {
-
-  protected Injector getInjector() {
-    return Guice.createInjector(new ServletModule() {
-      @Override
-      protected void configureServlets() {
-        bind(Service.class).to(ServiceImpl.class);
-
-        bind(FilterDispatcher.class).in(Singleton.class);
-        filter("/*").through(org.apache.struts2.dispatcher.FilterDispatcher.class);
-      }
-    });
-  }
-}
diff --git a/extensions/struts2/example/root/WEB-INF/classes/struts.xml b/extensions/struts2/example/src/struts.xml
similarity index 89%
rename from extensions/struts2/example/root/WEB-INF/classes/struts.xml
rename to extensions/struts2/example/src/struts.xml
index de434c3..e18dec2 100644
--- a/extensions/struts2/example/root/WEB-INF/classes/struts.xml
+++ b/extensions/struts2/example/src/struts.xml
@@ -3,7 +3,7 @@
     "http://struts.apache.org/dtds/struts-2.0.dtd">
 
 <struts>
-
+<constant name="struts.devMode" value="true"></constant>
   <!-- No need to specify a module here. See ExampleListenerAndModule. -->
 
   <!-- Register some actions, these get injected for you by Guice -->
diff --git a/extensions/struts2/lib/commons-fileupload-1.2.1.jar b/extensions/struts2/lib/commons-fileupload-1.2.1.jar
new file mode 100644
index 0000000..7db423e
--- /dev/null
+++ b/extensions/struts2/lib/commons-fileupload-1.2.1.jar
Binary files differ
diff --git a/extensions/struts2/lib/commons-io-1.3.2.jar b/extensions/struts2/lib/commons-io-1.3.2.jar
new file mode 100644
index 0000000..865c9e4
--- /dev/null
+++ b/extensions/struts2/lib/commons-io-1.3.2.jar
Binary files differ
diff --git a/extensions/struts2/lib/freemarker-2.3.16.jar b/extensions/struts2/lib/freemarker-2.3.16.jar
new file mode 100644
index 0000000..968c1d3
--- /dev/null
+++ b/extensions/struts2/lib/freemarker-2.3.16.jar
Binary files differ
diff --git a/extensions/struts2/lib/freemarker-2.3.8.jar b/extensions/struts2/lib/freemarker-2.3.8.jar
deleted file mode 100644
index 737bfb5..0000000
--- a/extensions/struts2/lib/freemarker-2.3.8.jar
+++ /dev/null
Binary files differ
diff --git a/extensions/struts2/lib/javassist.jar b/extensions/struts2/lib/javassist.jar
new file mode 100644
index 0000000..12c7b6c
--- /dev/null
+++ b/extensions/struts2/lib/javassist.jar
Binary files differ
diff --git a/extensions/struts2/lib/ognl-2.6.9.jar b/extensions/struts2/lib/ognl-2.6.9.jar
deleted file mode 100644
index 0f1c0fb..0000000
--- a/extensions/struts2/lib/ognl-2.6.9.jar
+++ /dev/null
Binary files differ
diff --git a/extensions/struts2/lib/ognl-3.0.jar b/extensions/struts2/lib/ognl-3.0.jar
new file mode 100644
index 0000000..b64b5bb
--- /dev/null
+++ b/extensions/struts2/lib/ognl-3.0.jar
Binary files differ
diff --git a/extensions/struts2/lib/struts-2.0.5-lib.zip b/extensions/struts2/lib/struts-2.0.5-lib.zip
deleted file mode 100644
index d5d75bf..0000000
--- a/extensions/struts2/lib/struts-2.0.5-lib.zip
+++ /dev/null
Binary files differ
diff --git a/extensions/struts2/lib/struts2-api-2.0.5.jar b/extensions/struts2/lib/struts2-api-2.0.5.jar
deleted file mode 100644
index 0d59962..0000000
--- a/extensions/struts2/lib/struts2-api-2.0.5.jar
+++ /dev/null
Binary files differ
diff --git a/extensions/struts2/lib/struts2-core-2.0.5.jar b/extensions/struts2/lib/struts2-core-2.0.5.jar
deleted file mode 100644
index 5e8702b..0000000
--- a/extensions/struts2/lib/struts2-core-2.0.5.jar
+++ /dev/null
Binary files differ
diff --git a/extensions/struts2/lib/struts2-core-2.2.1.jar b/extensions/struts2/lib/struts2-core-2.2.1.jar
new file mode 100644
index 0000000..65b9c3f
--- /dev/null
+++ b/extensions/struts2/lib/struts2-core-2.2.1.jar
Binary files differ
diff --git a/extensions/struts2/lib/xwork-2.0.0.jar b/extensions/struts2/lib/xwork-2.0.0.jar
deleted file mode 100644
index 1418069..0000000
--- a/extensions/struts2/lib/xwork-2.0.0.jar
+++ /dev/null
Binary files differ
diff --git a/extensions/struts2/lib/xwork-core-2.2.1.jar b/extensions/struts2/lib/xwork-core-2.2.1.jar
new file mode 100644
index 0000000..8fb2c81
--- /dev/null
+++ b/extensions/struts2/lib/xwork-core-2.2.1.jar
Binary files differ
diff --git a/extensions/struts2/src/com/google/inject/servlet/Struts2Factory.java b/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java
similarity index 73%
rename from extensions/struts2/src/com/google/inject/servlet/Struts2Factory.java
rename to extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java
index 494811b..62d90c6 100644
--- a/extensions/struts2/src/com/google/inject/servlet/Struts2Factory.java
+++ b/extensions/struts2/src/com/google/inject/struts2/Struts2Factory.java
@@ -14,19 +14,8 @@
  * limitations under the License.
  */
 
-package com.google.inject.servlet;
+package com.google.inject.struts2;
 
-import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.internal.Annotations;
-import com.opensymphony.xwork2.ActionInvocation;
-import com.opensymphony.xwork2.ObjectFactory;
-import com.opensymphony.xwork2.config.ConfigurationException;
-import com.opensymphony.xwork2.config.entities.InterceptorConfig;
-import com.opensymphony.xwork2.inject.Inject;
-import com.opensymphony.xwork2.interceptor.Interceptor;
 import java.lang.annotation.Annotation;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -35,25 +24,36 @@
 import java.util.Set;
 import java.util.logging.Logger;
 
+import com.google.inject.AbstractModule;
+import com.google.inject.Binder;
+import com.google.inject.Injector;
+import com.google.inject.internal.Annotations;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.config.ConfigurationException;
+import com.opensymphony.xwork2.config.entities.InterceptorConfig;
+import com.opensymphony.xwork2.inject.Inject;
+import com.opensymphony.xwork2.interceptor.Interceptor;
+
 /**
  * Cleanup up version from Bob's GuiceObjectFactory. Now works properly with
  * GS2 and fixes several bugs.
  *
  * @author dhanji@gmail.com
+ * @author benmccann.com
  */
 public class Struts2Factory extends ObjectFactory {
 
-  static final Logger logger =
-      Logger.getLogger(Struts2Factory.class.getName());
-
-  Module module;
-  volatile Injector strutsInjector;
-  boolean developmentMode = false;
-  List<ProvidedInterceptor> interceptors
-      = new ArrayList<ProvidedInterceptor>();
+  private static final long serialVersionUID = 1L;
+  private static final Logger logger = Logger.getLogger(Struts2FactoryTest.class.getName());
   private static final String ERROR_NO_INJECTOR =
-      "Cannot find a Guice injector in the servlet context. Are you"
-          + " sure you registered GuiceServletContextListener in your application's web.xml?";
+      "Cannot find a Guice injector.  Are you sure you registered a GuiceServletContextListener "
+    + "that uses the Struts2GuicePluginModule in your application's web.xml?";
+
+  private static @com.google.inject.Inject Injector injector;
+
+  private final List<ProvidedInterceptor> interceptors = new ArrayList<ProvidedInterceptor>();
+  private volatile Injector strutsInjector;
 
   @Override
   public boolean isNoArgConstructorRequired() {
@@ -62,25 +62,14 @@
 
   @Inject(value = "guice.module", required = false)
   void setModule(String moduleClassName) {
-    try {
-      // Instantiate user's module.
-      @SuppressWarnings({"unchecked"})
-      Class<? extends Module> moduleClass =
-          (Class<? extends Module>) Class.forName(moduleClassName);
-      this.module = moduleClass.newInstance();
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
-  }
-
-  @Inject(value = "struts.devMode", required = false)
-  void setDevelopmentMode(String developmentMode) {
-    this.developmentMode = "true".equals(developmentMode.trim());
+    throw new RuntimeException("The struts2 plugin no longer supports"
+        + " specifying a module via the 'guice.module' property in XML."
+        + " Please install your module via a GuiceServletContextListener instead.");
   }
 
   Set<Class<?>> boundClasses = new HashSet<Class<?>>();
 
-  public Class getClassInstance(String name) throws ClassNotFoundException {
+  public Class<?> getClassInstance(String name) throws ClassNotFoundException {
     Class<?> clazz = super.getClassInstance(name);
 
     synchronized (this) {
@@ -107,8 +96,8 @@
     return clazz;
   }
 
-  @SuppressWarnings("unchecked")
-  public Object buildBean(Class clazz, Map extraContext) {
+  @Override @SuppressWarnings("unchecked")
+  public Object buildBean(Class clazz, Map<String, Object> extraContext) {
     if (strutsInjector == null) {
       synchronized (this) {
         if (strutsInjector == null) {
@@ -116,30 +105,19 @@
         }
       }
     }
-
     return strutsInjector.getInstance(clazz);
   }
 
   private void createInjector() {
     logger.info("Loading struts2 Guice support...");
 
-    // Attach to parent Guice injector from GS2.
-    Injector injector = (Injector) GuiceFilter.getServletContext()
-        .getAttribute(GuiceServletContextListener.INJECTOR_NAME);
-
     // Something is wrong, since this should be there if GuiceServletContextListener
     // was present.
-    if (null == injector) {
+    if (injector == null) {
       logger.severe(ERROR_NO_INJECTOR);
       throw new RuntimeException(ERROR_NO_INJECTOR);
     }
 
-    if (module != null) {
-      throw new RuntimeException("The struts2 plugin no longer supports specifying a module"
-          + "via the 'guice.module' property in XML."
-          + " Please install your module via a GuiceServletContextListener instead.");
-    }
-
     this.strutsInjector = injector.createChildInjector(new AbstractModule() {
       protected void configure() {
 
@@ -171,7 +149,8 @@
     // Ensure the interceptor class is present.
     Class<? extends Interceptor> interceptorClass;
     try {
-      interceptorClass = getClassInstance(interceptorConfig.getClassName());
+      interceptorClass = (Class<? extends Interceptor>)
+          getClassInstance(interceptorConfig.getClassName());
     } catch (ClassNotFoundException e) {
       throw new RuntimeException(e);
     }
@@ -182,19 +161,21 @@
     return providedInterceptor;
   }
 
-  Interceptor superBuildInterceptor(InterceptorConfig interceptorConfig,
-      Map interceptorRefParams) throws ConfigurationException {
+  private Interceptor superBuildInterceptor(InterceptorConfig interceptorConfig,
+      Map<String, String> interceptorRefParams) throws ConfigurationException {
     return super.buildInterceptor(interceptorConfig, interceptorRefParams);
   }
 
-  class ProvidedInterceptor implements Interceptor {
+  private class ProvidedInterceptor implements Interceptor {
 
-    final InterceptorConfig config;
-    final Map params;
-    final Class<? extends Interceptor> interceptorClass;
-    Interceptor delegate;
+    private static final long serialVersionUID = 1L;
 
-    ProvidedInterceptor(InterceptorConfig config, Map params,
+    private final InterceptorConfig config;
+    private final Map<String, String> params;
+    private final Class<? extends Interceptor> interceptorClass;
+    private Interceptor delegate;
+
+    ProvidedInterceptor(InterceptorConfig config, Map<String, String> params,
         Class<? extends Interceptor> interceptorClass) {
       this.config = config;
       this.params = params;
diff --git a/extensions/struts2/src/com/google/inject/struts2/Struts2GuicePluginModule.java b/extensions/struts2/src/com/google/inject/struts2/Struts2GuicePluginModule.java
new file mode 100644
index 0000000..5356afe
--- /dev/null
+++ b/extensions/struts2/src/com/google/inject/struts2/Struts2GuicePluginModule.java
@@ -0,0 +1,35 @@
+/**
+ * Copyright (C) 2010 Google Inc.
+ *
+ * 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 com.google.inject.struts2;
+
+import com.google.inject.AbstractModule;
+
+/**
+ * Initializes the Struts 2 Guice Plugin.
+ * Must be added to the injector returned by
+ *     {@link GuiceServletContextListener.getInjector()}.
+ *
+ * @author benmccann.com
+ */
+public class Struts2GuicePluginModule extends AbstractModule {
+
+  @Override
+  protected void configure() {
+    requestStaticInjection(Struts2Factory.class); 
+  }
+
+}
diff --git a/extensions/struts2/src/struts-plugin.xml b/extensions/struts2/src/struts-plugin.xml
index 381a166..40bf990 100644
--- a/extensions/struts2/src/struts-plugin.xml
+++ b/extensions/struts2/src/struts-plugin.xml
@@ -8,7 +8,7 @@
 
   <bean type="com.opensymphony.xwork2.ObjectFactory" 
         name="guice"
-        class="com.google.inject.servlet.Struts2Factory"/>
+        class="com.google.inject.struts2.Struts2Factory"/>
 
   <!--  Make the Guice object factory the automatic default -->
   <constant name="struts.objectFactory" value="guice" />
diff --git a/extensions/struts2/test/com/google/inject/struts2/Struts2FactoryTest.java b/extensions/struts2/test/com/google/inject/struts2/Struts2FactoryTest.java
new file mode 100644
index 0000000..ed73181
--- /dev/null
+++ b/extensions/struts2/test/com/google/inject/struts2/Struts2FactoryTest.java
@@ -0,0 +1,85 @@
+/**
+ * Copyright (C) 2010 Google Inc.
+ *
+ * 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 com.google.inject.struts2;
+
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import com.google.inject.Provides;
+import com.google.inject.Singleton;
+import com.google.inject.servlet.GuiceServletContextListener;
+import com.google.inject.servlet.ServletModule;
+
+/**
+ * Test for Struts2Factory
+ *
+ * @author benmccann.com
+ */
+public class Struts2FactoryTest extends TestCase {
+
+  private static final Date TODAY = new Date();
+  
+  public static class TestListener extends GuiceServletContextListener {
+
+    private final Module module;
+    
+    public TestListener(Module module) {
+      this.module = module;
+    }
+    
+    @Override
+    protected Injector getInjector() {
+      return Guice.createInjector(
+          new Struts2GuicePluginModule(),
+          new ServletModule() {
+            @Override
+            protected void configureServlets() {      
+              // Struts 2 setup
+              bind(StrutsPrepareAndExecuteFilter.class).in(Singleton.class);
+              filter("/*").through(StrutsPrepareAndExecuteFilter.class);
+            }
+          },
+          module
+      );
+    }
+    
+  }
+  
+  public void testStruts2Factory() {
+    Struts2Factory s2Factory = new Struts2Factory();
+    TestListener testListener = new TestListener(new AbstractModule() {
+      @Override
+      protected void configure() {
+      }
+
+      @Provides @SuppressWarnings("unused")
+      Date provideDate() {
+        return TODAY;
+      }
+    });
+    assertEquals(TODAY, testListener.getInjector().getInstance(Date.class));
+    assertEquals(TODAY, s2Factory.buildBean(Date.class, null));
+  }
+
+}