diff --git a/jetty-distribution/pom.xml b/jetty-distribution/pom.xml index 0bc7d58f91d..2589df3adbb 100644 --- a/jetty-distribution/pom.xml +++ b/jetty-distribution/pom.xml @@ -247,12 +247,17 @@ + javax.servlet + javax.servlet-api + 3.1-b08 + true ${assembly-directory}/lib - servlet-api-3.0.jar + servlet-api-3.1.jar diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java index 1688ed6c64c..4a1b29e4f06 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AsyncContextState.java @@ -28,6 +28,8 @@ import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; +import org.eclipse.jetty.server.handler.ContextHandler; + public class AsyncContextState implements AsyncContext { @@ -92,17 +94,20 @@ public class AsyncContextState implements AsyncContext @Override public T createListener(Class clazz) throws ServletException - { + { + ContextHandler contextHandler = state().getContextHandler(); + if (contextHandler != null) + return contextHandler.getServletContext().createListener(clazz); try { return clazz.newInstance(); } - catch(Exception e) + catch (Exception e) { throw new ServletException(e); } } - + @Override public void dispatch() { diff --git a/jetty-start/src/main/resources/org/eclipse/jetty/start/start.config b/jetty-start/src/main/resources/org/eclipse/jetty/start/start.config index 4dde6bfb2cd..5837685c09e 100644 --- a/jetty-start/src/main/resources/org/eclipse/jetty/start/start.config +++ b/jetty-start/src/main/resources/org/eclipse/jetty/start/start.config @@ -92,7 +92,7 @@ $(jetty.home)/lib/jetty-io-$(version).jar ! available org.eclipse.jetty.io.Buf $(jetty.home)/lib/jetty-xml-$(version).jar ! available org.eclipse.jetty.xml.XmlParser [Server,All,server,default] -$(jetty.home)/lib/servlet-api-3.0.jar ! available javax.servlet.ServletContext +$(jetty.home)/lib/servlet-api-3.1.jar ! available javax.servlet.ServletContext $(jetty.home)/lib/jetty-http-$(version).jar ! available org.eclipse.jetty.http.HttpParser $(jetty.home)/lib/jetty-continuation-$(version).jar ! available org.eclipse.jetty.continuation.Continuation $(jetty.home)/lib/jetty-server-$(version).jar ! available org.eclipse.jetty.server.Server diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/AnnotationTest.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/AnnotationTest.java index b3eecea8535..e4b1220534d 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/AnnotationTest.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/AnnotationTest.java @@ -307,10 +307,15 @@ public class AnnotationTest extends HttpServlet context = request.getContextPath(); if (!context.endsWith("/")) context += "/"; - context += "sec/foo"; - out.println("
"); - + String path = context +"sec/foo"; + + out.println("
"); + out.println("

AsyncListener Resource Injection

"); + out.println("

Click the following link to test that javax.servlet.AsyncListeners are injectable

"); + path = context+"asy/xx"; + out.println("
"); + out.println(""); out.println(""); out.flush(); diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/AsyncListenerServlet.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/AsyncListenerServlet.java new file mode 100644 index 00000000000..5a611a76d55 --- /dev/null +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/AsyncListenerServlet.java @@ -0,0 +1,126 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package com.acme; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import javax.servlet.AsyncContext; +import javax.servlet.AsyncEvent; +import javax.servlet.AsyncListener; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet(urlPatterns="/asy/*", asyncSupported=true) +public class AsyncListenerServlet extends HttpServlet +{ + public static class MyAsyncListener implements AsyncListener + { + @Resource(mappedName="maxAmount") + private Double maxAmount; + + boolean postConstructCalled = false; + boolean resourceInjected = false; + + @PostConstruct + public void postConstruct() + { + postConstructCalled = true; + resourceInjected = (maxAmount != null); + } + + public boolean isPostConstructCalled() + { + return postConstructCalled; + } + + public boolean isResourceInjected() + { + return resourceInjected; + } + + @Override + public void onComplete(AsyncEvent event) throws IOException + { + // TODO Auto-generated method stub + + } + + @Override + public void onTimeout(AsyncEvent event) throws IOException + { + // TODO Auto-generated method stub + + } + + @Override + public void onError(AsyncEvent event) throws IOException + { + // TODO Auto-generated method stub + + } + + @Override + public void onStartAsync(AsyncEvent event) throws IOException + { + // TODO Auto-generated method stub + + } + } + + + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException + { + AsyncContext asyncContext = req.startAsync(); + MyAsyncListener listener = asyncContext.createListener(MyAsyncListener.class); + + PrintWriter writer = resp.getWriter(); + writer.println( ""); + writer.println( ""); + writer.println("

AsyncListener

"); + writer.println("
");
+        writer.println("

@PostConstruct Callback

"); + writer.println("
");
+        writer.println("@PostConstruct");
+        writer.println("private void postConstruct ()");
+        writer.println("{}"); 
+        writer.println("
"); + writer.println("
Result: "+listener.isPostConstructCalled()+""); + + writer.println("

@Resource Injection for env-entry

"); + writer.println("
");
+        writer.println("@Resource(mappedName=\"maxAmount\")");
+        writer.println("private Double maxAmount;");
+        writer.println("
"); + writer.println("
Result: "+(listener.isResourceInjected()?" PASS":" FAIL")+""); + + writer.println( ""); + writer.println( ""); + writer.flush(); + writer.close(); + + asyncContext.complete(); + } +}