1 16 package org.directwebremoting.util; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 21 import javax.servlet.http.HttpServletRequest ; 22 23 27 public class Continuation 28 { 29 33 public Continuation(HttpServletRequest request) 34 { 35 this.proxy = request.getAttribute(ATTRIBUTE_JETTY_CONTINUATION); 36 } 37 38 43 public boolean isAvailable() 44 { 45 return proxy != null; 46 } 47 48 53 public void suspend(long sleepTime) throws Exception 54 { 55 try 56 { 57 suspendMethod.invoke(proxy, new Object [] { new Long (sleepTime) }); 58 } 59 catch (InvocationTargetException ex) 60 { 61 rethrowWithoutWrapper(ex); 62 } 63 } 64 65 71 public void resume() throws Exception 72 { 73 try 74 { 75 resumeMethod.invoke(proxy, new Object [0]); 76 } 77 catch (InvocationTargetException ex) 78 { 79 rethrowWithoutWrapper(ex); 80 } 81 } 82 83 88 public Object getObject() throws Exception 89 { 90 try 91 { 92 return getObject.invoke(proxy, new Object [0]); 93 } 94 catch (InvocationTargetException ex) 95 { 96 return rethrowWithoutWrapper(ex); 97 } 98 } 99 100 105 public void setObject(Object object) throws Exception 106 { 107 try 108 { 109 setObject.invoke(proxy, new Object [] { object }); 110 } 111 catch (InvocationTargetException ex) 112 { 113 rethrowWithoutWrapper(ex); 114 } 115 } 116 117 121 public static void rethrowIfContinuation(Throwable th) 122 { 123 Throwable ex = th; 124 125 if (ex instanceof InvocationTargetException ) 126 { 127 ex = ((InvocationTargetException ) ex).getTargetException(); 128 } 129 130 if ("org.mortbay.jetty.RetryRequest".equals(ex.getClass().getName())) 132 { 133 throw (RuntimeException ) ex; 134 } 135 } 136 137 141 public static boolean isJetty() 142 { 143 return isJetty; 144 } 145 146 152 private static Object rethrowWithoutWrapper(InvocationTargetException ex) throws Exception 153 { 154 Throwable target = ex.getTargetException(); 155 if (target instanceof Exception ) 156 { 157 throw (Exception ) target; 158 } 159 160 if (target instanceof Error ) 161 { 162 throw (Error ) target; 163 } 164 165 throw ex; 166 } 167 168 171 private Object proxy; 172 173 176 private static final Logger log = Logger.getLogger(Continuation.class); 177 178 182 private static final String ATTRIBUTE_JETTY_CONTINUATION = "org.mortbay.jetty.ajax.Continuation"; 183 184 187 protected static Class continuationClass; 188 189 192 protected static Method suspendMethod; 193 194 197 protected static Method resumeMethod; 198 199 202 protected static Method getObject; 203 204 207 protected static Method setObject; 208 209 212 protected static boolean isJetty; 213 214 217 static 218 { 219 try 220 { 221 continuationClass = LocalUtil.classForName("org.mortbay.util.ajax.Continuation"); 222 suspendMethod = continuationClass.getMethod("suspend", new Class [] { Long.TYPE }); 223 resumeMethod = continuationClass.getMethod("resume", new Class [] {}); 224 getObject = continuationClass.getMethod("getObject", new Class [] {}); 225 setObject = continuationClass.getMethod("setObject", new Class [] { Object .class }); 226 isJetty = true; 227 } 228 catch (Exception ex) 229 { 230 isJetty = false; 231 log.debug("No Jetty ContuniationSupport class, using standard Servlet API"); 232 } 233 } 234 } 235 | Popular Tags |