From 0933ec3faf4b34896449903af11949fc2e8e5f7b Mon Sep 17 00:00:00 2001 From: fhanik Date: Thu, 24 May 2007 15:47:29 +0000 Subject: [PATCH] Add in support to check if the current thread is a Tomcat 'worker' thread or not, that way we can decide if the invokations are appropriate or not git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@541337 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/catalina/connector/CometEventImpl.java | 37 ++++++++++++++++++++-- .../apache/catalina/connector/CoyoteAdapter.java | 15 ++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/java/org/apache/catalina/connector/CometEventImpl.java b/java/org/apache/catalina/connector/CometEventImpl.java index 40058d1fe..9a1c4a9cb 100644 --- a/java/org/apache/catalina/connector/CometEventImpl.java +++ b/java/org/apache/catalina/connector/CometEventImpl.java @@ -19,7 +19,7 @@ package org.apache.catalina.connector; import java.io.IOException; - +import java.util.HashSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -29,7 +29,6 @@ import org.apache.catalina.util.StringManager; public class CometEventImpl implements CometEvent { - /** * The string manager for this package. */ @@ -69,7 +68,14 @@ public class CometEventImpl implements CometEvent { */ protected EventSubType eventSubType = null; + /** + * Current set of operations + */ + protected HashSet cometOperations = new HashSet(3); + + protected WorkerThreadCheck threadCheck = new WorkerThreadCheck(); + private static final Object threadCheckHolder = new Object(); // --------------------------------------------------------- Public Methods /** @@ -136,7 +142,13 @@ public class CometEventImpl implements CometEvent { public void register(CometEvent.CometOperation... operations) throws IOException, IllegalStateException { - throw new UnsupportedOperationException(); + //add it to the registered set + for (CometEvent.CometOperation co : operations ) { + if ( !cometOperations.contains(co) ) { + cometOperations.add(co); + //TODO notify poller + } + } } public void unregister(CometOperation... operations) @@ -151,5 +163,24 @@ public class CometEventImpl implements CometEvent { public CometOperation[] getRegisteredOps() { throw new UnsupportedOperationException(); } + + protected void setWorkerThread() { + threadCheck.set(threadCheckHolder); + } + + protected void unsetWorkerThread() { + threadCheck.set(null); + } + + protected void checkWorkerThread() throws IllegalStateException { + //throw exception if not on worker thread + if ( !(threadCheck.get() == threadCheckHolder) ) + throw new IllegalStateException("The operation can only be performed when invoked by a Tomcat worker thread."); + } + + //inner class used to keep track if the current thread is a worker thread. + private static class WorkerThreadCheck extends ThreadLocal { + + } } diff --git a/java/org/apache/catalina/connector/CoyoteAdapter.java b/java/org/apache/catalina/connector/CoyoteAdapter.java index 12d20efa6..594683b3e 100644 --- a/java/org/apache/catalina/connector/CoyoteAdapter.java +++ b/java/org/apache/catalina/connector/CoyoteAdapter.java @@ -119,7 +119,10 @@ public class CoyoteAdapter boolean error = false; boolean read = false; + CometEvent event = request.getEvent(); try { + if ( event!=null && (event instanceof CometEventImpl)) + ((CometEventImpl)event).setWorkerThread(); if (status == SocketStatus.OPEN) { if (response.isClosed()) { // The event has been closed asynchronously, so call end instead of @@ -198,6 +201,9 @@ public class CoyoteAdapter error = true; return false; } finally { + if ( event!=null && (event instanceof CometEventImpl)) + ((CometEventImpl)event).unsetWorkerThread(); + req.getRequestProcessor().setWorkerThreadName(null); // Recycle the wrapper request and response if (error || response.isClosed() || !request.isComet()) { @@ -250,7 +256,7 @@ public class CoyoteAdapter } boolean comet = false; - + CometEvent event = null; try { // Parse and set Catalina and configuration specific @@ -261,6 +267,10 @@ public class CoyoteAdapter connector.getContainer().getPipeline().getFirst().invoke(request, response); if (request.isComet()) { + event = request.getEvent(); + if ( event!=null && (event instanceof CometEventImpl)) + ((CometEventImpl)event).setWorkerThread(); + if (!response.isClosed() && !response.isError()) { if (request.getAvailable()) { // Invoke a read event right away if there are available bytes @@ -291,6 +301,9 @@ public class CoyoteAdapter } catch (Throwable t) { log.error(sm.getString("coyoteAdapter.service"), t); } finally { + if ( event!=null && (event instanceof CometEventImpl)) + ((CometEventImpl)event).unsetWorkerThread(); + req.getRequestProcessor().setWorkerThreadName(null); // Recycle the wrapper request and response if (!comet) { -- 2.11.0