View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.continuation;
15  
16  import java.lang.reflect.Constructor;
17  import javax.servlet.ServletRequest;
18  import javax.servlet.ServletRequestWrapper;
19  import javax.servlet.ServletResponse;
20  import javax.servlet.ServletResponseWrapper;
21  
22  /* ------------------------------------------------------------ */
23  /** ContinuationSupport.
24   *
25   * Factory class for accessing Continuation instances, which with either be
26   * native to the container (jetty >= 6), a servlet 3.0 or a faux continuation.
27   *
28   */
29  public class ContinuationSupport
30  {
31      static final boolean __jetty6;
32      static final boolean __servlet3;
33      static final Constructor<? extends Continuation> __newServlet3Continuation;
34      static final Constructor<? extends Continuation> __newJetty6Continuation;
35      static
36      {
37          boolean servlet3Support=false;
38          Constructor<? extends Continuation>s3cc=null;
39          try
40          {
41              boolean servlet3=ServletRequest.class.getMethod("startAsync")!=null;
42              if (servlet3)
43              {
44                  Class<? extends Continuation> s3c = ContinuationSupport.class.getClassLoader().loadClass("org.eclipse.jetty.continuation.Servlet3Continuation").asSubclass(Continuation.class);
45                  s3cc=s3c.getConstructor(ServletRequest.class, ServletResponse.class);
46                  servlet3Support=true;
47              }
48          }
49          catch (Exception e)
50          {}
51          finally
52          {
53              __servlet3=servlet3Support;
54              __newServlet3Continuation=s3cc;
55          }
56  
57  
58          boolean jetty6Support=false;
59          Constructor<? extends Continuation>j6cc=null;
60          try
61          {
62              Class<?> jetty6ContinuationClass = ContinuationSupport.class.getClassLoader().loadClass("org.mortbay.util.ajax.Continuation");
63              boolean jetty6=jetty6ContinuationClass!=null;
64              if (jetty6)
65              {
66                  Class<? extends Continuation> j6c = ContinuationSupport.class.getClassLoader().loadClass("org.eclipse.jetty.continuation.Jetty6Continuation").asSubclass(Continuation.class);
67                  j6cc=j6c.getConstructor(ServletRequest.class, jetty6ContinuationClass);
68                  jetty6Support=true;
69              }
70          }
71          catch (Exception e)
72          {}
73          finally
74          {
75              __jetty6=jetty6Support;
76              __newJetty6Continuation=j6cc;
77          }
78      }
79  
80      /* ------------------------------------------------------------ */
81      /**
82       * Get a Continuation.  The type of the Continuation returned may
83       * vary depending on the container in which the application is 
84       * deployed. It may be an implementation native to the container (eg
85       * org.eclipse.jetty.server.AsyncContinuation) or one of the utility
86       * implementations provided such as {@link FauxContinuation} or 
87       * {@link Servlet3Continuation}.
88       * @param request The request 
89       * @return a Continuation instance
90       */
91      public static Continuation getContinuation(ServletRequest request)
92      {
93          Continuation continuation = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
94          if (continuation!=null)
95              return continuation;
96          
97          while (request instanceof ServletRequestWrapper)
98              request=((ServletRequestWrapper)request).getRequest();
99          
100         if (__servlet3 )
101         {
102             try
103             {
104                 continuation=__newServlet3Continuation.newInstance(request);
105                 request.setAttribute(Continuation.ATTRIBUTE,continuation);
106                 return continuation;
107             }
108             catch(Exception e)
109             {
110                 throw new RuntimeException(e);
111             }
112         }
113 
114         if (__jetty6)
115         {
116             Object c=request.getAttribute("org.mortbay.jetty.ajax.Continuation");
117             try
118             {
119                 continuation= __newJetty6Continuation.newInstance(request,c);
120                 request.setAttribute(Continuation.ATTRIBUTE,continuation);
121                 return continuation;
122             }
123             catch(Exception e)
124             {
125                 throw new RuntimeException(e);
126             }
127         }
128 
129         throw new IllegalStateException("!(Jetty || Servlet 3.0 || ContinuationFilter)");
130     }
131 
132     /* ------------------------------------------------------------ */
133     /**
134      * @param request
135      * @param response
136      * @deprecated use {@link #getContinuation(ServletRequest)}
137      * @return
138      */
139     public static Continuation getContinuation(final ServletRequest request, final ServletResponse response)
140     {
141         return getContinuation(request);
142     }
143 }