Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: file:///Users/billburke/jboss/resteasy/resteasy-git/svn-s…
…erver-sync/resteasy/trunk@1354 2b1ed4c4-5db3-0410-90e4-80a7a6204c25
- Loading branch information
patriot1burke
committed
May 18, 2011
1 parent
27f6f39
commit 5748822
Showing
5 changed files
with
203 additions
and
31 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
jaxrs/examples/spring-hibernate-contacts/client/src/main/sql/hsqldb/contact.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
jaxrs/examples/spring-hibernate-contacts/persistence/src/main/sql/hsqldb/contact.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...sy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoaderSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
* <pre> | ||
* 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 | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
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); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...teasy-spring/src/test/java/org/jboss/resteasy/plugins/spring/TestSpringContextLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |