diff --git a/jaxrs/examples/spring-hibernate-contacts/client/src/main/sql/hsqldb/contact.properties b/jaxrs/examples/spring-hibernate-contacts/client/src/main/sql/hsqldb/contact.properties index 7a4c8832ccb..000db95da88 100644 --- a/jaxrs/examples/spring-hibernate-contacts/client/src/main/sql/hsqldb/contact.properties +++ b/jaxrs/examples/spring-hibernate-contacts/client/src/main/sql/hsqldb/contact.properties @@ -1,5 +1,5 @@ #HSQL Database Engine 1.8.0.5 -#Tue Jul 06 10:10:51 EDT 2010 +#Wed May 18 19:57:26 EDT 2011 hsqldb.script_format=0 runtime.gc_interval=0 sql.enforce_strict_size=false diff --git a/jaxrs/examples/spring-hibernate-contacts/persistence/src/main/sql/hsqldb/contact.properties b/jaxrs/examples/spring-hibernate-contacts/persistence/src/main/sql/hsqldb/contact.properties index 5decd188f87..88893a4d8d6 100644 --- a/jaxrs/examples/spring-hibernate-contacts/persistence/src/main/sql/hsqldb/contact.properties +++ b/jaxrs/examples/spring-hibernate-contacts/persistence/src/main/sql/hsqldb/contact.properties @@ -1,5 +1,5 @@ #HSQL Database Engine 1.8.0.5 -#Tue Jul 06 10:10:43 EDT 2010 +#Wed May 18 19:56:52 EDT 2011 hsqldb.script_format=0 runtime.gc_interval=0 sql.enforce_strict_size=false diff --git a/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoader.java b/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoader.java index c496216ea4b..6c70b730fe8 100644 --- a/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoader.java +++ b/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoader.java @@ -22,37 +22,11 @@ public class SpringContextLoader extends ContextLoader private final static Logger logger = Logger .getLogger(SpringContextLoader.class); + private SpringContextLoaderSupport springContextLoaderSupport = new SpringContextLoaderSupport(); + protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext configurableWebApplicationContext) { super.customizeContext(servletContext, configurableWebApplicationContext); - - final ResteasyProviderFactory providerFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName()); - if (providerFactory == null) - throw new RuntimeException("RESTeasy Provider Factory is null, do you have the ResteasyBootstrap listener configured?"); - - - final Registry registry = (Registry) servletContext.getAttribute(Registry.class.getName()); - if (registry == null) - throw new RuntimeException("RESTeasy Registry is null, do ou have the ResteasyBootstrap listener configured?"); - - final Dispatcher dispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName()); - if (dispatcher == null) - throw new RuntimeException("RESTeasy Dispatcher is null, do ou have the ResteasyBootstrap listener configured?"); - - ApplicationListener listener = new ApplicationListener() - { - public void onApplicationEvent(ApplicationEvent event) - { - if (event instanceof ContextRefreshedEvent) - { - ContextRefreshedEvent cre = (ContextRefreshedEvent) event; - ConfigurableListableBeanFactory autowireCapableBeanFactory = (ConfigurableListableBeanFactory) cre - .getApplicationContext().getAutowireCapableBeanFactory(); - new SpringBeanProcessor(dispatcher, registry, providerFactory) - .postProcessBeanFactory(autowireCapableBeanFactory); - } - } - }; - configurableWebApplicationContext.addApplicationListener(listener); + this.springContextLoaderSupport.customizeContext(servletContext,configurableWebApplicationContext); } } diff --git a/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoaderSupport.java b/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoaderSupport.java new file mode 100644 index 00000000000..0eaa223ab0e --- /dev/null +++ b/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoaderSupport.java @@ -0,0 +1,80 @@ +package org.jboss.resteasy.plugins.spring; + +import javax.servlet.ServletContext; + +import org.jboss.resteasy.core.Dispatcher; +import org.jboss.resteasy.logging.Logger; +import org.jboss.resteasy.spi.Registry; +import org.jboss.resteasy.spi.ResteasyProviderFactory; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.web.context.ConfigurableWebApplicationContext; +import org.springframework.web.context.ContextLoader; + +/** + * Provides access to RESTEasy's SpringContextLoader implementation without having + * to extend {@link org.springframework.web.context.ContextLoader}. This is useful + * if you have your own SpringContextLoaderListener and dont' want to + * return RESTEasy's {@link SpringContextLoader}. + * + * Usage: + *
+ * public class MyCustomSpringContextLoader extends ContextLoader 
+ * {
+ *    private SpringContextLoaderSupport springContextLoaderSupport = 
+ *       new SpringContextLoaderSupport();
+ *
+ *     protected void customizeContext(
+ *        ServletContext servletContext, 
+ *        ConfigurableWebApplicationContext configurableWebApplicationContext)
+ *    {
+ *       super.customizeContext(servletContext, configurableWebApplicationContext);
+ *
+ *       // Your custom code 
+ *
+ *       this.springContextLoaderSupport.customizeContext(servletContext.configurableWebApplicationContext);
+ *
+ *       // Your custom code 
+ *    }
+ * }
+ * 
+ */ +public class SpringContextLoaderSupport +{ + private final static Logger logger = Logger + .getLogger(SpringContextLoader.class); + + public void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext configurableWebApplicationContext) + { + final ResteasyProviderFactory providerFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName()); + if (providerFactory == null) + throw new RuntimeException("RESTeasy Provider Factory is null, do you have the ResteasyBootstrap listener configured?"); + + + final Registry registry = (Registry) servletContext.getAttribute(Registry.class.getName()); + if (registry == null) + throw new RuntimeException("RESTeasy Registry is null, do ou have the ResteasyBootstrap listener configured?"); + + final Dispatcher dispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName()); + if (dispatcher == null) + throw new RuntimeException("RESTeasy Dispatcher is null, do ou have the ResteasyBootstrap listener configured?"); + + ApplicationListener listener = new ApplicationListener() + { + public void onApplicationEvent(ApplicationEvent event) + { + if (event instanceof ContextRefreshedEvent) + { + ContextRefreshedEvent cre = (ContextRefreshedEvent) event; + ConfigurableListableBeanFactory autowireCapableBeanFactory = (ConfigurableListableBeanFactory) cre + .getApplicationContext().getAutowireCapableBeanFactory(); + new SpringBeanProcessor(dispatcher, registry, providerFactory) + .postProcessBeanFactory(autowireCapableBeanFactory); + } + } + }; + configurableWebApplicationContext.addApplicationListener(listener); + } +} diff --git a/jaxrs/resteasy-spring/src/test/java/org/jboss/resteasy/plugins/spring/TestSpringContextLoader.java b/jaxrs/resteasy-spring/src/test/java/org/jboss/resteasy/plugins/spring/TestSpringContextLoader.java new file mode 100644 index 00000000000..815f89933df --- /dev/null +++ b/jaxrs/resteasy-spring/src/test/java/org/jboss/resteasy/plugins/spring/TestSpringContextLoader.java @@ -0,0 +1,118 @@ +package org.jboss.resteasy.plugins.spring; + +import javax.servlet.ServletContext; + +import org.springframework.web.context.support.StaticWebApplicationContext; +import org.springframework.mock.web.MockServletContext; + +import org.junit.BeforeClass; +import org.junit.Before; +import org.junit.Test; + +import org.jboss.resteasy.core.Dispatcher; +import org.jboss.resteasy.mock.MockDispatcherFactory; +import org.jboss.resteasy.spi.Registry; +import org.jboss.resteasy.spi.ResteasyProviderFactory; +import org.jboss.resteasy.core.ResourceMethodRegistry; + +import static org.junit.Assert.assertEquals; + +/** + * Tests that SpringContextLoader does proper validations and adds an application listener. + * This was used to extract the code into SpringContextLoaderSupport, so that it could be re-used + * without having to extend ContextLoader. Probably should move these tests to directly + * test SpringContextLoaderSupport and replace this test with a test that simply asserts + * that SpringContextLoaderSupport was callled. + */ +public class TestSpringContextLoader +{ + + private SpringContextLoader contextLoader; + + @BeforeClass + public static void setup() + { + } + + @Before + public void setupEditor() + { + contextLoader = new SpringContextLoader(); + } + + @Test(expected=RuntimeException.class) + public void testThatProviderFactoryIsRequired() + { + contextLoader.customizeContext( + mockServletContext(null,someRegistry(),someDispatcher()), + mockWebApplicationContext()); + } + + @Test(expected=RuntimeException.class) + public void testThatRegistryIsRequired() + { + contextLoader.customizeContext( + mockServletContext(someProviderFactory(),null,someDispatcher()), + mockWebApplicationContext()); + } + + @Test(expected=RuntimeException.class) + public void testThatDispatcherIsRequired() + { + contextLoader.customizeContext( + mockServletContext(someProviderFactory(),someRegistry(),null), + mockWebApplicationContext()); + } + + @Test + public void testThatWeAddedAnApplicationListener() + { + StaticWebApplicationContext context = mockWebApplicationContext(); + int numListeners = context.getApplicationListeners().size(); + contextLoader.customizeContext( + mockServletContext(someProviderFactory(),someRegistry(),someDispatcher()), + context); + int numListenersNow = context.getApplicationListeners().size(); + assertEquals("Expected to add exactly one new listener; in fact added " + (numListenersNow - numListeners), + numListeners + 1,numListenersNow); + } + + private StaticWebApplicationContext mockWebApplicationContext() + { + return new StaticWebApplicationContext(); + } + + private ServletContext mockServletContext( + ResteasyProviderFactory providerFactory, + Registry registry, + Dispatcher dispatcher) + { + MockServletContext context = new MockServletContext(); + + if (providerFactory != null) + context.setAttribute(ResteasyProviderFactory.class.getName(),providerFactory); + + if (registry != null) + context.setAttribute(Registry.class.getName(),registry); + + if (dispatcher != null) + context.setAttribute(Dispatcher.class.getName(),dispatcher); + + return context; + } + + private Registry someRegistry() + { + return new ResourceMethodRegistry(someProviderFactory()); + } + + private ResteasyProviderFactory someProviderFactory() + { + return new ResteasyProviderFactory(); + } + + private Dispatcher someDispatcher() + { + return MockDispatcherFactory.createDispatcher(); + } +}