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