Skip to content

Instantly share code, notes, and snippets.

@singalen
Created June 20, 2012 20:18
Show Gist options
  • Save singalen/2961967 to your computer and use it in GitHub Desktop.
Save singalen/2961967 to your computer and use it in GitHub Desktop.
Jetty+Weld+Jersey-servlet setup for StackOverflow question
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd">
</beans>
package com.foobar.chaos;
import com.foobar.chaos.quartz.CdiAwareJobFactory;
import com.foobar.chaos.resource.email.api.EmailResource;
import com.foobar.foo.core.core.mapping.JacksonJaxbJsonContextResolver;
import com.foobar.foo.core.core.mapping.JsonpBodyWriter;
import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.codehaus.jackson.jaxrs.JsonMappingExceptionMapper;
import org.codehaus.jackson.jaxrs.JsonParseExceptionMapper;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
/**
* foo-chaos application entry point.
*/
@ApplicationPath("/v1")
public class FooChaosApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> providers = new HashSet<Class<?>>();
providers.add(JsonMappingExceptionMapper.class);
providers.add(JacksonJsonProvider.class);
providers.add(JsonpBodyWriter.class);
providers.add(JacksonJaxbJsonContextResolver.class);
providers.add(JacksonJaxbJsonProvider.class);
providers.add(JsonParseExceptionMapper.class);
providers.add(FooBarResource.class);
providers.add(CdiAwareJobFactory.class);
return providers;
}
}
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://www.eclipse.org/jetty/configure.dtd">
<Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref id="webAppCtx"/></Arg>
<Arg>BeanManager</Arg>
<!--
http://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#d0e5286
Notice that Jetty doesn't not have built-in support for an javax.naming.spi.ObjectFactory like
Tomcat, so it's necessary to manually create the javax.naming.Reference to wrap around it.
-->
<Arg>
<New class="javax.naming.Reference">
<Arg>javax.enterprise.inject.spi.BeanManager</Arg>
<Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
<Arg/>
</New>
</Arg>
</New>
</Configure>
package com.foobar.chaos;
import javax.naming.InitialContext;
import javax.naming.Reference;
import org.eclipse.jetty.plus.jndi.Resource;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class JettyLauncher {
private static String contextPath = "/";
private static String resourceBase = "src/main/webapp";
private static int httpPort = 8081;
private static String[] __dftConfigurationClasses =
{
"org.eclipse.jetty.webapp.WebInfConfiguration",
"org.eclipse.jetty.webapp.WebXmlConfiguration",
"org.eclipse.jetty.webapp.MetaInfConfiguration",
"org.eclipse.jetty.webapp.FragmentConfiguration",
"org.eclipse.jetty.plus.webapp.EnvConfiguration",
"org.eclipse.jetty.webapp.JettyWebXmlConfiguration"
} ;
public static void main(String[] args) throws Exception {
System.setProperty("java.naming.factory.url","org.eclipse.jetty.jndi");
System.setProperty("java.naming.factory.initial","org.eclipse.jetty.jndi.InitialContextFactory");
InitialContext ctx = new InitialContext();
ctx.createSubcontext("java:comp");
Server server = new Server(httpPort);
WebAppContext webapp = new WebAppContext();
webapp.setConfigurationClasses(__dftConfigurationClasses);
webapp.setDescriptor("src/main/webapp/WEB-INF/web.xml");
webapp.setContextPath(contextPath);
webapp.setResourceBase(resourceBase);
webapp.setClassLoader(Thread.currentThread().getContextClassLoader());
server.setHandler(webapp);
server.start();
new Resource("BeanManager", new Reference("javax.enterprise.inject.spi.BeanMnanager",
"org.jboss.weld.resources.ManagerObjectFactory", null));
server.join();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.foobar.chaos.FooChaosApplication</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.foobar</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/foo-chaos/v1/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-env-ref>
<description>Object factory for the CDI Bean Manager</description>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment