From f6d5ddc066fa29ba0d7c6d3bb32737fb5a6db24a Mon Sep 17 00:00:00 2001 From: Jozef Hartinger Date: Wed, 10 Aug 2011 14:29:34 +0200 Subject: [PATCH] SEAMREST-47 --- .../main/docbook/en-US/rest-installation.xml | 26 ++++++++++ examples/exceptions/pom.xml | 51 ++++++++++++++++++- examples/exceptions/readme.txt | 5 ++ .../src/main/tomcat/WEB-INF/web.xml | 45 ++++++++++++++++ .../src/main/webapp/WEB-INF/web.xml | 6 +++ .../org/jboss/seam/rest/SeamRestStartup.java | 28 +++++----- .../seam/rest/SeamRestStartupListener.java | 48 +++++++++++++++++ 7 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 examples/exceptions/src/main/tomcat/WEB-INF/web.xml create mode 100644 examples/exceptions/src/main/webapp/WEB-INF/web.xml create mode 100644 impl/src/main/java/org/jboss/seam/rest/SeamRestStartupListener.java diff --git a/docs/reference/src/main/docbook/en-US/rest-installation.xml b/docs/reference/src/main/docbook/en-US/rest-installation.xml index ba587df..9a447d4 100755 --- a/docs/reference/src/main/docbook/en-US/rest-installation.xml +++ b/docs/reference/src/main/docbook/en-US/rest-installation.xml @@ -106,4 +106,30 @@ public class MyApplication extends Application } }]]> + + + Servlet container support + + + Seam REST can be used with plain Servlet containers such as Apache Tomcat 7. Firstly, we need to enhance the + Servlet container + capabilities. This is done by bundling Weld and RESTEasy within the application and configuring + them. See the + jaxrs-exceptions example and its tomcat build profile for more details. + + + + In a EE6-compliant environment, Seam REST would be bootstrapped by a Servlet listener. However, + weld-servlet + does not support CDI injection into Servlet listeners. Therefore, add the following line to your application's + web.xml + file to bootstrap Seam REST using Servlet. + + + + Servlet REST Listener Startup + org.jboss.seam.rest.SeamRestStartupListener + 1 +]]> + \ No newline at end of file diff --git a/examples/exceptions/pom.xml b/examples/exceptions/pom.xml index ba9cfd5..1211be4 100644 --- a/examples/exceptions/pom.xml +++ b/examples/exceptions/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 @@ -122,6 +123,54 @@ + + tomcat + + + org.jboss.weld.servlet + weld-servlet + 1.1.2.Final + + + org.jboss.resteasy + jaxrs-api + + + org.jboss.resteasy + resteasy-jaxrs + compile + + + org.jboss.resteasy + resteasy-cdi + compile + + + org.jboss.resteasy + resteasy-jaxb-provider + compile + + + javax.validation + validation-api + + + + + + org.apache.maven.plugins + maven-war-plugin + + + + src/main/tomcat + + + + + + + diff --git a/examples/exceptions/readme.txt b/examples/exceptions/readme.txt index 330b2e8..a909343 100644 --- a/examples/exceptions/readme.txt +++ b/examples/exceptions/readme.txt @@ -19,6 +19,11 @@ mvn clean package $GF_HOME/bin/asadmin start-domain $GF_HOME/bin/asadmin deploy target/rest-exceptions.war +Deploying to Tomcat 7 +====================== +mvn clean package -Ptomcat +deploy the generated archive + Running tests ====================== Deploy the application diff --git a/examples/exceptions/src/main/tomcat/WEB-INF/web.xml b/examples/exceptions/src/main/tomcat/WEB-INF/web.xml new file mode 100644 index 0000000..fc28209 --- /dev/null +++ b/examples/exceptions/src/main/tomcat/WEB-INF/web.xml @@ -0,0 +1,45 @@ + + + + + + Resteasy + org.jboss.resteasy.plugins.server.servlet.FilterDispatcher + + javax.ws.rs.Application + org.jboss.seam.rest.examples.exceptions.JaxrsExceptionsApplication + + + resteasy.scan + true + + + resteasy.servlet.mapping.prefix + /api + + + resteasy.injector.factory + org.jboss.resteasy.cdi.CdiInjectorFactory + + + + + Resteasy + /api/* + + + + + org.jboss.weld.environment.servlet.Listener + + + + + + Servlet REST Listener Startup + org.jboss.seam.rest.SeamRestStartupListener + 1 + + + diff --git a/examples/exceptions/src/main/webapp/WEB-INF/web.xml b/examples/exceptions/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..ce9dfbf --- /dev/null +++ b/examples/exceptions/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/impl/src/main/java/org/jboss/seam/rest/SeamRestStartup.java b/impl/src/main/java/org/jboss/seam/rest/SeamRestStartup.java index 8e1eb71..b17b209 100644 --- a/impl/src/main/java/org/jboss/seam/rest/SeamRestStartup.java +++ b/impl/src/main/java/org/jboss/seam/rest/SeamRestStartup.java @@ -1,11 +1,9 @@ package org.jboss.seam.rest; +import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Event; import javax.inject.Inject; import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.annotation.WebListener; import org.jboss.logging.Logger; import org.jboss.seam.rest.client.RestClientExtension; @@ -20,9 +18,11 @@ * * @author Jozef Hartinger */ -@WebListener -public class SeamRestStartup implements ServletContextListener { +@ApplicationScoped +public class SeamRestStartup { + private static final Logger log = Logger.getLogger(SeamRestStartup.class); + @Inject private RestClientExtension restClientExtension; @Inject @@ -35,10 +35,17 @@ public class SeamRestStartup implements ServletContextListener { @Inject @RestResource private Event event; + + private boolean initialized = false; - @Override - public void contextInitialized(ServletContextEvent sce) { - event.fire(sce.getServletContext()); + public synchronized void init(ServletContext sc) + { + if (initialized) + { + return; + } + + event.fire(sc); log.infov( "Seam REST {0} (Client integration: {1}, Catch integration: {2}, {3} exception mapping rules, Templating provider: {4})", @@ -46,9 +53,6 @@ public void contextInitialized(ServletContextEvent sce) { restClientExtension.isClientIntegrationEnabled() ? "enabled" : "disabled", exceptionMappingExtension .isCatchIntegrationEnabled() ? "enabled" : "disabled", exceptionMapper.getMappings().size(), templating .getProvider() == null ? "null" : templating.getProvider().toString()); - } - - @Override - public void contextDestroyed(ServletContextEvent sce) { + initialized = true; } } diff --git a/impl/src/main/java/org/jboss/seam/rest/SeamRestStartupListener.java b/impl/src/main/java/org/jboss/seam/rest/SeamRestStartupListener.java new file mode 100644 index 0000000..4815e5f --- /dev/null +++ b/impl/src/main/java/org/jboss/seam/rest/SeamRestStartupListener.java @@ -0,0 +1,48 @@ +package org.jboss.seam.rest; + +import javax.inject.Inject; +import javax.servlet.ServletConfig; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebListener; +import javax.servlet.http.HttpServlet; + +import org.jboss.logging.Logger; + +/** + * We also extend HttpServlet so that Seam REST can be bootstrapped by eagerly-loaded Servlet on Tomcat 7, where + * weld-servlet does not support CDI injection into Servlet Listeners. + * + * @author Jozef Hartinger + * + */ +@WebListener +public class SeamRestStartupListener extends HttpServlet implements ServletContextListener { + + private static final Logger log = Logger.getLogger(SeamRestStartupListener.class); + + private static final long serialVersionUID = -3618026329655575903L; + + @Inject + private SeamRestStartup startup; + + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + startup.init(config.getServletContext()); + } + + @Override + public void contextInitialized(ServletContextEvent sce) { + if (startup == null) { + log.warn("Listener injection does not work. You are probably running within a Servlet container. Make sure to configure Seam REST to use Servlet bootstrap instead."); + return; + } + startup.init(sce.getServletContext()); + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + } +}