Skip to content

Commit

Permalink
git-svn-id: file:///Users/billburke/jboss/resteasy/resteasy-git/svn-s…
Browse files Browse the repository at this point in the history
…erver-sync/resteasy/trunk@1354 2b1ed4c4-5db3-0410-90e4-80a7a6204c25
  • Loading branch information
patriot1burke committed May 18, 2011
1 parent 27f6f39 commit 5748822
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 31 deletions.
@@ -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
Expand Down
@@ -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
Expand Down
Expand Up @@ -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);
}
}
@@ -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);
}
}
@@ -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();
}
}

0 comments on commit 5748822

Please sign in to comment.