Hi,
over the last couple of months I got a lot of requests from users of
Grizzly Comet[1] to support dwr (or have dwr have support :-)). I took a
look at the code and came with the attached simple patch. Mainly, I've
just added a new class called GrizzlyContinuationSleeper, "hacked" the
Continuation classes to enable Grizzly when DWR is deployed in GlassFish
or Grizzly Standalone Web Server.
The classes I've touched are:
+ PollHandler
+ Continuation
+ Added GrizzlyContinuationSleeper
+ OutputAlarm (guard against null)
PollHandler + Conyinuation looks a little bit hacky (might need to
replace the isJetty/isGrizzly with a better design) but the idea is
there. Let me know what you think and if you are interested to add
support to the current dwr release.
What needs to be done is if accepted is:
+ apply the patch.
+ add the following jar file
http://download.java.net/maven/2/com/sun/grizzly/continuation/1.6-SNAPSHOT/continuation-1.6-SNAPSHOT.jarunder the dwr/jar folder, and when packaging the war file, add it under
WEB-INF/lib
(and for the dwr/pom.xml file add)
> <dependency>
> <groupId>com.sun.grizzly</groupId>
> <artifactId>continuation</artifactId>
> <version>1.6-SNAPSHOT</version>
> <scope>compile</scope>
> </dependency>
The later jar file contains the Grizzly's classes that detect with
Grizzly version is installed and will make DWR works with all GlassFish
version (9.1, 9.1 ur1, Sailfin, OpenESB). I will add those classes to
GlassFish v3, but previous version are 'closed' for commit so I can't
add them. If you think this is a bad idea, we can always document it so
people knows how to enable dwr in GlassFish.
Thanks!
--Jeanfrancois
[1]
http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html? java/org/directwebremoting/dwrp/GrizzlyContinuationSleeper.java
Index: java/org/directwebremoting/dwrp/OutputAlarm.java
===================================================================
RCS file: /cvs/dwr/java/org/directwebremoting/dwrp/OutputAlarm.java,v
retrieving revision 1.6
diff -u -r1.6 OutputAlarm.java
--- java/org/directwebremoting/dwrp/OutputAlarm.java 30 Oct 2007 17:07:24 -0000 1.6
+++ java/org/directwebremoting/dwrp/OutputAlarm.java 22 Nov 2007 03:32:20 -0000
@@ -71,7 +71,9 @@
public void cancel()
{
scriptSession.removeScriptConduit(conduit);
- future.cancel(false);
+ if (future != null){
+ future.cancel(false);
+ }
super.cancel();
}
Index: java/org/directwebremoting/dwrp/PollHandler.java
===================================================================
RCS file: /cvs/dwr/java/org/directwebremoting/dwrp/PollHandler.java,v
retrieving revision 1.41
diff -u -r1.41 PollHandler.java
--- java/org/directwebremoting/dwrp/PollHandler.java 30 Oct 2007 17:07:24 -0000 1.41
+++ java/org/directwebremoting/dwrp/PollHandler.java 22 Nov 2007 03:32:20 -0000
@@ -138,6 +138,10 @@
{
sleeper = new JettyContinuationSleeper(request);
}
+ else if (Continuation.isGrizzly())
+ {
+ sleeper = new GrizzlyContinuationSleeper(request);
+ }
else
{
sleeper = new ThreadWaitSleeper();
Index: java/org/directwebremoting/util/Continuation.java
===================================================================
RCS file: /cvs/dwr/java/org/directwebremoting/util/Continuation.java,v
retrieving revision 1.6
diff -u -r1.6 Continuation.java
--- java/org/directwebremoting/util/Continuation.java 23 Jun 2007 19:28:28 -0000 1.6
+++ java/org/directwebremoting/util/Continuation.java 22 Nov 2007 03:32:20 -0000
@@ -36,6 +36,14 @@
public Continuation(HttpServletRequest request)
{
proxy = request.getAttribute(ATTRIBUTE_JETTY_CONTINUATION);
+ if (proxy == null && isGrizzly()){
+ try{
+ Class<?> gContinuation = LocalUtil.classForName("com.sun.grizzly.Continuation");
+ Method gMethod = gContinuation.getMethod("getContinuation");
+ proxy = gMethod.invoke((Object[])null,(Object[])null);
+ } catch (Throwable t){
+ }
+ }
}
/**
@@ -203,7 +211,12 @@
/**
* Are we using Jetty at all?
*/
- protected static boolean isJetty;
+ protected static boolean isJetty = false;
+
+ /**
+ * Are we using Grizzly at all?
+ */
+ protected static boolean isGrizzly = false;
/**
* Can we use Jetty?
@@ -212,25 +225,42 @@
{
try
{
- continuationClass = LocalUtil.classForName("org.mortbay.util.ajax.Continuation");
+ try{
+ continuationClass = LocalUtil.classForName("org.mortbay.util.ajax.Continuation");
+ isJetty = true;
+ }
+ catch (Exception ex)
+ {
+ Class<?> gContinuation = LocalUtil.classForName("com.sun.grizzly.Continuation");
+ Method gMethod = gContinuation.getMethod("getContinuation");
+ continuationClass = gMethod.invoke(gMethod).getClass();
+ isGrizzly = true;
+ }
suspendMethod = continuationClass.getMethod("suspend", Long.TYPE);
resumeMethod = continuationClass.getMethod("resume");
getObject = continuationClass.getMethod("getObject");
setObject = continuationClass.getMethod("setObject", Object.class);
- isJetty = true;
}
catch (Exception ex)
{
isJetty = false;
- log.debug("No Jetty ContuniationSupport class, using standard Servlet API");
+ log.debug("No Jetty or Grizzly Continuation class, using standard Servlet API");
}
}
/**
- * @return True if we have detected Continuation classes
+ * @return True if we have detected Jetty classes
*/
public static boolean isJetty()
{
return isJetty;
}
+
+ /**
+ * @return True if we have detected Grizzly classes
+ */
+ public static boolean isGrizzly()
+ {
+ return isGrizzly;
+ }
}
/*
* Copyright 2005 Joe Walker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.directwebremoting.dwrp;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.util.Continuation;
/**
* A Sleeper that works with Grizzly Continuations
* @author Jeanfrancois Arcand [jeanfrancois dot arcand at sun dot com]
*/
public class GrizzlyContinuationSleeper implements Sleeper
{
/**
* @param request The request into which we store this as an attribute
*/
public GrizzlyContinuationSleeper(HttpServletRequest request)
{
continuation = new Continuation(request);
}
/* (non-Javadoc)
* @see org.directwebremoting.dwrp.Sleeper#goToSleep(java.lang.Runnable)
*/
public void goToSleep(Runnable awakening)
{
this.onAwakening = awakening;
try
{
continuation.suspend(-1);
}
catch (Exception ex)
{
Continuation.rethrowIfContinuation(ex);
log.warn("Exception", ex);
proxy = new ThreadWaitSleeper();
proxy.goToSleep(onAwakening);
}
}
/* (non-Javadoc)
* @see org.directwebremoting.dwrp.PollHandler.Sleeper#wakeUp()
*/
public void wakeUp()
{
if (proxy != null)
{
proxy.wakeUp();
}
else
{
synchronized (continuation)
{
if (!resumed)
{
try
{
/*
* Flush bytes if any first as before resuming the
* as Grizzly Comet isn't allowing writes once the
* continuation is resumed.
*
* This can be achieved
* by using Grizzly CometHandler, which isn't exposed
* with DWR.
*/
onAwakening.run();
continuation.resume();
}
catch (Exception ex)
{
log.error("Broken reflection", ex);
}
resumed = true;
}
}
}
}
/**
* If continuations fail, we proxy to a Thread Wait version
*/
protected ThreadWaitSleeper proxy = null;
/**
* What we do when we are woken up
*/
protected Runnable onAwakening;
/**
* The continuation object
*/
protected final Continuation continuation;
/**
* Has the continuation been restarted already?
*/
protected boolean resumed = false;
/**
* We remember the notify conduit so we can reuse it
*/
public static final String ATTRIBUTE_JETTY_CONDUIT = "org.directwebremoting.dwrp.notifyConduit";
private static final Log log = LogFactory.getLog(GrizzlyContinuationSleeper.class);
}
---------------------------------------------------------------------
To unsubscribe, e-mail:
dev-unsubscribe@...
For additional commands, e-mail:
dev-help@...