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  import org.mortbay.util.ajax.WaitingContinuation;
23  
24  /* ------------------------------------------------------------ */
25  /** ContinuationSupport.
26   *
27   * Factory class for accessing Continuation instances, which with either be
28   * native to the container (jetty >= 6), a servlet 3.0 or a faux continuation.
29   *
30   */
31  public class ContinuationSupport
32  {
33      static final boolean __jetty6;
34      static final boolean __servlet3;
35      static final Class<?> __waitingContinuation;
36      static final Constructor<? extends Continuation> __newServlet3Continuation;
37      static final Constructor<? extends Continuation> __newJetty6Continuation;
38      static
39      {
40          boolean servlet3Support=false;
41          Constructor<? extends Continuation>s3cc=null;
42          try
43          {
44              boolean servlet3=ServletRequest.class.getMethod("startAsync")!=null;
45              if (servlet3)
46              {
47                  Class<? extends Continuation> s3c = ContinuationSupport.class.getClassLoader().loadClass("org.eclipse.jetty.continuation.Servlet3Continuation").asSubclass(Continuation.class);
48                  s3cc=s3c.getConstructor(ServletRequest.class);
49                  servlet3Support=true;
50              }
51          }
52          catch (Exception e)
53          {}
54          finally
55          {
56              __servlet3=servlet3Support;
57              __newServlet3Continuation=s3cc;
58          }
59          
60          boolean jetty6Support=false;
61          Constructor<? extends Continuation>j6cc=null;
62          try
63          {
64              Class<?> jetty6ContinuationClass = ContinuationSupport.class.getClassLoader().loadClass("org.mortbay.util.ajax.Continuation");
65              boolean jetty6=jetty6ContinuationClass!=null;
66              if (jetty6)
67              {
68                  Class<? extends Continuation> j6c = ContinuationSupport.class.getClassLoader().loadClass("org.eclipse.jetty.continuation.Jetty6Continuation").asSubclass(Continuation.class);
69                  j6cc=j6c.getConstructor(ServletRequest.class, jetty6ContinuationClass);
70                  jetty6Support=true;
71              }
72          }
73          catch (Exception e)
74          {}
75          finally
76          {
77              __jetty6=jetty6Support;
78              __newJetty6Continuation=j6cc;
79          }
80          
81          Class<?> waiting=null;
82          try
83          {
84              waiting=ContinuationSupport.class.getClassLoader().loadClass("org.mortbay.util.ajax.WaitingContinuation");
85          }
86          catch (Exception e)
87          {
88          }
89          finally
90          {
91              __waitingContinuation=waiting;
92          }
93      }
94  
95      /* ------------------------------------------------------------ */
96      /**
97       * Get a Continuation.  The type of the Continuation returned may
98       * vary depending on the container in which the application is 
99       * deployed. It may be an implementation native to the container (eg
100      * org.eclipse.jetty.server.AsyncContinuation) or one of the utility
101      * implementations provided such as {@link FauxContinuation} or 
102      * {@link Servlet3Continuation}.
103      * @param request The request 
104      * @return a Continuation instance
105      */
106     public static Continuation getContinuation(ServletRequest request)
107     {
108         Continuation continuation = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
109         if (continuation!=null)
110             return continuation;
111         
112         while (request instanceof ServletRequestWrapper)
113             request=((ServletRequestWrapper)request).getRequest();
114         
115         if (__servlet3 )
116         {
117             try
118             {
119                 continuation=__newServlet3Continuation.newInstance(request);
120                 request.setAttribute(Continuation.ATTRIBUTE,continuation);
121                 return continuation;
122             }
123             catch(Exception e)
124             {
125                 throw new RuntimeException(e);
126             }
127         }
128 
129         if (__jetty6)
130         {
131             Object c=request.getAttribute("org.mortbay.jetty.ajax.Continuation");
132             try
133             {
134                 if (c==null || __waitingContinuation==null || __waitingContinuation.isInstance(c))
135                     continuation=new FauxContinuation(request);
136                 else
137                     continuation= __newJetty6Continuation.newInstance(request,c);
138                 request.setAttribute(Continuation.ATTRIBUTE,continuation);
139                 return continuation;
140             }
141             catch(Exception e)
142             {
143                 throw new RuntimeException(e);
144             }
145         }
146 
147         throw new IllegalStateException("!(Jetty || Servlet 3.0 || ContinuationFilter)");
148     }
149 
150     /* ------------------------------------------------------------ */
151     /**
152      * @param request
153      * @param response
154      * @deprecated use {@link #getContinuation(ServletRequest)}
155      * @return
156      */
157     public static Continuation getContinuation(final ServletRequest request, final ServletResponse response)
158     {
159         return getContinuation(request);
160     }
161 }