Revision: 1061
          http://stripes.svn.sourceforge.net/stripes/?rev=1061&view=rev
Author:   bengunter
Date:     2009-02-26 15:55:14 +0000 (Thu, 26 Feb 2009)

Log Message:
-----------
STS-614: Replaced SpringInjectionFactory with SpringInjectionPostProcessor, 
which can be used by DefaultObjectFactory. Added a unit test for injection via 
the post-processor.

Modified Paths:
--------------
    
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringHelper.java
    
trunk/tests/src/net/sourceforge/stripes/integration/spring/SpringHelperTests.java

Added Paths:
-----------
    
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionPostProcessor.java

Removed Paths:
-------------
    
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionFactory.java

Modified: 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringHelper.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringHelper.java  
    2009-02-26 15:51:48 UTC (rev 1060)
+++ 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringHelper.java  
    2009-02-26 15:55:14 UTC (rev 1061)
@@ -90,6 +90,13 @@
      */
     public static void injectBeans(Object bean, ServletContext ctx) {
         ApplicationContext ac = 
WebApplicationContextUtils.getWebApplicationContext(ctx);
+
+        if (ac == null) {
+            final String name = ctx.getServletContextName();
+            throw new IllegalStateException(
+                    "No Spring application context was found in servlet 
context \"" + name + "\"");
+        }
+
         injectBeans(bean, ac);
     }
 

Deleted: 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionFactory.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionFactory.java
    2009-02-26 15:51:48 UTC (rev 1060)
+++ 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionFactory.java
    2009-02-26 15:55:14 UTC (rev 1061)
@@ -1,57 +0,0 @@
-/* Copyright 2009 Frederic Daoud
- *
- * 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 net.sourceforge.stripes.integration.spring;
-
-import javax.servlet.ServletContext;
-import net.sourceforge.stripes.config.Configuration;
-import net.sourceforge.stripes.controller.DefaultObjectFactory;
-import net.sourceforge.stripes.util.Log;
-
-/**
- * <p>
- * An extension of {...@link DefaultObjectFactory} that calls
- * {...@link SpringHelper#injectBeans((Object, ServletContext))} in the 
{...@link #postProcess(Object)}
- * method, to inject dependencies marked with {...@link SpringBean} in every 
type of object created
- * by Stripes (Action Beans, Interceptors, Type Converters, Formatters, etc.).
- * </p>
- * 
- * @author Freddy Daoud
- * @since Stripes 1.6
- */
-public class SpringInjectionFactory extends DefaultObjectFactory {
-    private static final Log log = 
Log.getInstance(SpringInjectionFactory.class);
-    private ServletContext servletContext;
-
-    /**
-     * Gets the {...@link ServletContext} so that it can be used to inject 
Spring beans.
-     */
-    @Override
-    public void init(Configuration configuration) throws Exception {
-        super.init(configuration);
-        servletContext = configuration.getServletContext();
-    }
-
-    /**
-     * Calls {...@link SpringHelper#injectBeans((Object, ServletContext))} to 
inject dependencies
-     * marked with {...@link SpringBean} into the object before returning it.
-     */
-    @Override
-    protected <T> T postProcess(T object) {
-        log.debug("Running Spring dependency injection for instance of ",
-            object.getClass().getSimpleName());
-        SpringHelper.injectBeans(object, servletContext);
-        return object;
-    }
-}

Added: 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionPostProcessor.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionPostProcessor.java
                              (rev 0)
+++ 
trunk/stripes/src/net/sourceforge/stripes/integration/spring/SpringInjectionPostProcessor.java
      2009-02-26 15:55:14 UTC (rev 1061)
@@ -0,0 +1,70 @@
+/* Copyright 2009 Frederic Daoud, Ben Gunter
+ *
+ * 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 net.sourceforge.stripes.integration.spring;
+
+import javax.servlet.ServletContext;
+
+import net.sourceforge.stripes.config.Configuration;
+import net.sourceforge.stripes.controller.DefaultObjectFactory;
+import net.sourceforge.stripes.controller.ObjectPostProcessor;
+import net.sourceforge.stripes.util.Log;
+
+/**
+ * <p>
+ * An implementation of {...@link ObjectPostProcessor} that calls {...@link
+ * SpringHelper#injectBeans((Object, ServletContext))} to inject dependencies 
marked with
+ * {...@link SpringBean} in every type of object created by Stripes (Action 
Beans, Interceptors, Type
+ * Converters, Formatters, etc.).
+ * </p>
+ * 
+ * @author Freddy Daoud, Ben Gunter
+ * @since Stripes 1.6
+ */
+public class SpringInjectionPostProcessor implements 
ObjectPostProcessor<Object> {
+    private static final Log log = 
Log.getInstance(SpringInjectionPostProcessor.class);
+    private ServletContext servletContext;
+
+    /** Get the servlet context from the object factory's configuration. */
+    public void setObjectFactory(DefaultObjectFactory factory) {
+        Configuration configuration = factory.getConfiguration();
+        if (configuration == null) {
+            final String name = getClass().getSimpleName();
+            throw new IllegalStateException("The object factory passed to " + 
name
+                    + " has no configuration. The configuration is required by 
"
+                    + getClass().getSimpleName() + " to get the servlet 
context.");
+        }
+
+        ServletContext servletContext = configuration.getServletContext();
+        if (this.servletContext != null && this.servletContext != 
servletContext) {
+            final String name = getClass().getSimpleName();
+            throw new IllegalStateException("An attempt was made to use a 
single instance of "
+                    + name + " in two different servlet contexts. " + name + " 
instances "
+                    + "cannot be shared across servlet contexts.");
+        }
+
+        this.servletContext = servletContext;
+    }
+
+    /**
+     * Calls {...@link SpringHelper#injectBeans((Object, ServletContext))} to 
inject dependencies
+     * marked with {...@link SpringBean} into the object before returning it.
+     */
+    public Object postProcess(Object object) {
+        log.debug("Running Spring dependency injection for instance of ", 
object.getClass()
+                .getSimpleName());
+        SpringHelper.injectBeans(object, servletContext);
+        return object;
+    }
+}

Modified: 
trunk/tests/src/net/sourceforge/stripes/integration/spring/SpringHelperTests.java
===================================================================
--- 
trunk/tests/src/net/sourceforge/stripes/integration/spring/SpringHelperTests.java
   2009-02-26 15:51:48 UTC (rev 1060)
+++ 
trunk/tests/src/net/sourceforge/stripes/integration/spring/SpringHelperTests.java
   2009-02-26 15:55:14 UTC (rev 1061)
@@ -1,9 +1,17 @@
 package net.sourceforge.stripes.integration.spring;
 
+import javax.servlet.ServletContext;
+
+import net.sourceforge.stripes.StripesTestFixture;
+import net.sourceforge.stripes.config.Configuration;
+import net.sourceforge.stripes.controller.DefaultObjectFactory;
 import net.sourceforge.stripes.exception.StripesRuntimeException;
 import net.sourceforge.stripes.test.TestActionBean;
 import net.sourceforge.stripes.test.TestBean;
+
 import org.springframework.context.support.StaticApplicationContext;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.StaticWebApplicationContext;
 import org.testng.Assert;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
@@ -19,7 +27,7 @@
 
     @BeforeClass(alwaysRun=true)
     protected void setupSpringContext() {
-        ctx = new StaticApplicationContext();
+        ctx = new StaticWebApplicationContext();
         ctx.registerSingleton("test/TestBean", TestBean.class);
         ctx.registerSingleton("testActionBean", TestActionBean.class);
         ctx.registerPrototype("test/testActionBean", TestActionBean.class);
@@ -273,4 +281,24 @@
         Assert.assertNotNull(target.number3);
         Assert.assertNotNull(target.number4);
     }
+
+    // 
/////////////////////////////////////////////////////////////////////////
+    public static class PostProcessorTarget {
+        private TestBean bean;
+        @SpringBean("test/TestBean")
+        public void setBean(TestBean bean) { this.bean = bean; }
+        public TestBean getBean() { return bean; }
+    }
+
+    @Test(groups = "fast", dependsOnMethods = "testExplicitSetterInjection")
+    public void testInjectionViaObjectPostProcessor() throws Exception {
+        Configuration configuration = 
StripesTestFixture.getDefaultConfiguration();
+        ServletContext sc = configuration.getServletContext();
+        
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, 
this.ctx);
+        DefaultObjectFactory factory = new DefaultObjectFactory();
+        factory.init(configuration);
+        factory.addPostProcessor(new SpringInjectionPostProcessor());
+        PostProcessorTarget target = 
factory.newInstance(PostProcessorTarget.class);
+        Assert.assertNotNull(target.getBean());
+    }
 }
\ No newline at end of file


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to