View Javadoc

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