Index: WorkerThread.java
===================================================================
--- WorkerThread.java (revision 308)
+++ WorkerThread.java (working copy)
@@ -25,7 +25,7 @@
import java.nio.ByteBuffer;
/**
- * Simple interface to allow the addition of Thread
attributes.
+ * Simple interface to allow the addition of Thread
attributes.
*
* @author Jean-Francois Arcand
*/
@@ -45,4 +45,25 @@
*/
public ByteBuffer getByteBuffer();
+
+ /**
+ * Detach the current set of attributes (state) associated with this instance.
+ * Invoking detach(true) will re-create all the ByteBuffer associated with
+ * this thread, hence this method must be called only when required. If
+ * you only need to invoke the object, call detach(false) instead but make
+ * sure you aren't caching or re-using the ThreadAttachment with another
+ * thread.
+ * @param true to copy the attributes into the ThreadAttachment and re-create
+ * them
+ * @return a new ThreadAttachment
+ */
+ public ThreadAttachment detach(boolean copyState);
+
+
+ /**
+ * Attach the ThreadAttachment to this instance. This will configure this
+ * Thread attributes like ByteBuffer, SSLEngine, etc.
+ * @param ThreadAttachment the attachment.
+ */
+ public void attach(ThreadAttachment threadAttachment);
}
Index: WorkerThreadImpl.java
===================================================================
--- WorkerThreadImpl.java (revision 308)
+++ WorkerThreadImpl.java (working copy)
@@ -70,22 +70,28 @@
/**
* The encrypted ByteBuffer used for handshaking and reading request bytes.
*/
- private ByteBuffer inputBB;
+ protected ByteBuffer inputBB;
/**
* The encrypted ByteBuffer used for handshaking and writing response bytes.
*/
- private ByteBuffer outputBB;
+ protected ByteBuffer outputBB;
/**
* The SSLEngine
used to manage the SSL over NIO request.
*/
- private SSLEngine sslEngine;
-
+ protected SSLEngine sslEngine;
+
/**
+ * The state/attributes on this WorkerThread.
+ */
+ private ThreadAttachment threadAttachment = new ThreadAttachment();
+
+
+ /**
* Create a Thread that will synchronizes/block on
* Pipeline
instance.
* @param threadGroup ThreadGroup
@@ -233,5 +239,53 @@
public void setSSLEngine(SSLEngine sslEngine) {
this.sslEngine = sslEngine;
}
+
+
+ /**
+ * Detach the current set of attributes (state) associated with this instance.
+ * Invoking detach(true) will re-create all the ByteBuffer associated with
+ * this thread, hence this method must be called only when required. If
+ * you only need to invoke the object, call detach(false) instead but make
+ * sure you aren't caching or re-using the ThreadAttachment with another
+ * thread.
+ * @param true to copy the attributes into the ThreadAttachment and re-create
+ * them.
+ * @return a new ThreadAttachment
+ */
+ public ThreadAttachment detach(boolean copyState) {
+ try{
+ threadAttachment.setByteBuffer(byteBuffer);
+ threadAttachment.setSSLEngine(sslEngine);
+ threadAttachment.setInputBB(inputBB);
+ threadAttachment.setOutputBB(outputBB);
+
+ return threadAttachment;
+ } finally {
+ // We cannot cache/re-use this object as it might be referenced
+ // by more than one thread.
+ if (copyState){
+ // Re-create a new ByteBuffer
+ byteBuffer = ByteBufferFactory.allocateView(8192,false);
+ threadAttachment = new ThreadAttachment();
+ }
+ threadAttachment.setThreadId(getName() + "-" + getId());
+ }
+ }
+
+
+ /**
+ * Attach the ThreadAttachment to this instance. This will configure this
+ * Thread attributes like ByteBuffer, SSLEngine, etc.
+ * @param ThreadAttachment the attachment.
+ */
+ public void attach(ThreadAttachment threadAttachment) {
+ byteBuffer = threadAttachment.getByteBuffer();
+ sslEngine = threadAttachment.getSSLEngine();
+ inputBB = threadAttachment.getInputBB();
+ outputBB = threadAttachment.getOutputBB();
+
+ this.threadAttachment = threadAttachment;
+ threadAttachment.setThreadId(getName() + "-" + getId());
+ }
}
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the license at
* https://glassfish.dev.java.net/public/CDDLv1.0.html or
* glassfish/bootstrap/legal/CDDLv1.0.txt.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at glassfish/bootstrap/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* you own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
*/
package com.sun.grizzly.util;
import java.nio.ByteBuffer;
import java.util.WeakHashMap;
import javax.net.ssl.SSLEngine;
/**
* This object represent the state of a WorkerThread
. This include
* the ByteBuffer binded to the WorkerThread, application data etc.
* @author Jeanfrancois
*/
public class ThreadAttachment {
private long timeout;
private String threadId;
private WeakHashMap map;
private ByteBuffer byteBuffer;
/**
* The encrypted ByteBuffer used for handshaking and reading request bytes.
*/
private ByteBuffer inputBB;
/**
* The encrypted ByteBuffer used for handshaking and writing response bytes.
*/
private ByteBuffer outputBB;
/**
* The SSLEngine
used to manage the SSL over NIO request.
*/
private SSLEngine sslEngine;
public ThreadAttachment(){
map = new WeakHashMap();
}
public void setAttribute(String key, Object value){
map.put(key,value);
}
public Object getAttribute(String key){
return map.get(key);
}
public Object removeAttribute(String key){
return map.remove(key);
}
/**
* Set the ByteBuffer
shared this thread
*/
public void setByteBuffer(ByteBuffer byteBuffer){
this.byteBuffer = byteBuffer;
}
/**
* Return the ByteBuffer
shared this thread
*/
public ByteBuffer getByteBuffer(){
return byteBuffer;
}
/**
* Return the encrypted ByteBuffer
used to handle request.
* @return ByteBuffer
*/
public ByteBuffer getInputBB(){
return inputBB;
}
/**
* Set the encrypted ByteBuffer
used to handle request.
* @param inputBB ByteBuffer
*/
public void setInputBB(ByteBuffer inputBB){
this.inputBB = inputBB;
}
/**
* Return the encrypted ByteBuffer
used to handle response.
* @return ByteBuffer
*/
public ByteBuffer getOutputBB(){
return outputBB;
}
/**
* Set the encrypted ByteBuffer
used to handle response.
* @param outputBB ByteBuffer
*/
public void setOutputBB(ByteBuffer outputBB){
this.outputBB = outputBB;
}
/**
* Set the SSLEngine
.
* @return SSLEngine
*/
public SSLEngine getSSLEngine() {
return sslEngine;
}
/**
* Get the SSLEngine
.
* @param sslEngine SSLEngine
*/
public void setSSLEngine(SSLEngine sslEngine) {
this.sslEngine = sslEngine;
}
public String getThreadId() {
return threadId;
}
public void setThreadId(String threadId) {
this.threadId = threadId;
}
public void setTimeout(long timeout){
this.timeout = timeout;
}
}