| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ThreadAttachment |
|
| 0.0;0 | ||||
| ThreadAttachment$Mode |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * | |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 4 | * | |
| 5 | * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved. | |
| 6 | * | |
| 7 | * The contents of this file are subject to the terms of either the GNU | |
| 8 | * General Public License Version 2 only ("GPL") or the Common Development | |
| 9 | * and Distribution License("CDDL") (collectively, the "License"). You | |
| 10 | * may not use this file except in compliance with the License. You can obtain | |
| 11 | * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html | |
| 12 | * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific | |
| 13 | * language governing permissions and limitations under the License. | |
| 14 | * | |
| 15 | * When distributing the software, include this License Header Notice in each | |
| 16 | * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. | |
| 17 | * Sun designates this particular file as subject to the "Classpath" exception | |
| 18 | * as provided by Sun in the GPL Version 2 section of the License file that | |
| 19 | * accompanied this code. If applicable, add the following below the License | |
| 20 | * Header, with the fields enclosed by brackets [] replaced by your own | |
| 21 | * identifying information: "Portions Copyrighted [year] | |
| 22 | * [name of copyright owner]" | |
| 23 | * | |
| 24 | * Contributor(s): | |
| 25 | * | |
| 26 | * If you wish your version of this file to be governed by only the CDDL or | |
| 27 | * only the GPL Version 2, indicate your decision by adding "[Contributor] | |
| 28 | * elects to include this software in this distribution under the [CDDL or GPL | |
| 29 | * Version 2] license." If you don't indicate a single choice of license, a | |
| 30 | * recipient has the option to distribute your version of this file under | |
| 31 | * either the CDDL, the GPL Version 2 or to extend the choice of license to | |
| 32 | * its licensees as provided above. However, if you add GPL Version 2 code | |
| 33 | * and therefore, elected the GPL Version 2 license, then the option applies | |
| 34 | * only if the new code is made subject to such option by the copyright | |
| 35 | * holder. | |
| 36 | * | |
| 37 | */ | |
| 38 | package com.sun.grizzly.util; | |
| 39 | ||
| 40 | import java.nio.ByteBuffer; | |
| 41 | import java.nio.channels.SelectionKey; | |
| 42 | import java.util.HashMap; | |
| 43 | import java.util.Map; | |
| 44 | import java.util.concurrent.locks.ReentrantLock; | |
| 45 | import javax.net.ssl.SSLEngine; | |
| 46 | ||
| 47 | /** | |
| 48 | * This object represent the state of a {@link WorkerThread}. This include | |
| 49 | * the ByteBuffer binded to the WorkerThread, application data etc. | |
| 50 | * | |
| 51 | * @author Jeanfrancois Arcand | |
| 52 | * @author Alexey Stashok | |
| 53 | */ | |
| 54 | public class ThreadAttachment extends SelectionKeyActionAttachment | |
| 55 | implements AttributeHolder { | |
| 56 | ||
| 57 | 0 | public static class Mode { |
| 58 | 1 | public static int ATTRIBUTES_ONLY = 0; |
| 59 | 1 | public static int BYTE_BUFFER = 2; |
| 60 | 1 | public static int INPUT_BB = 4; |
| 61 | 1 | public static int OUTPUT_BB = 8; |
| 62 | 1 | public static int SECURE_BUFFERS = 12; |
| 63 | 1 | public static int SSL_ENGINE = 16; |
| 64 | 1 | public static int SSL_ARTIFACTS = 28; |
| 65 | 1 | public static int STORE_ALL = 31; |
| 66 | }; | |
| 67 | ||
| 68 | 132 | private ReentrantLock threadLock = new ReentrantLock(); |
| 69 | ||
| 70 | private String threadId; | |
| 71 | ||
| 72 | ||
| 73 | private Map<String, Object> attributes; | |
| 74 | ||
| 75 | ||
| 76 | private ByteBuffer byteBuffer; | |
| 77 | ||
| 78 | ||
| 79 | /** | |
| 80 | * The encrypted ByteBuffer used for handshaking and reading request bytes. | |
| 81 | */ | |
| 82 | private ByteBuffer inputBB; | |
| 83 | ||
| 84 | ||
| 85 | /** | |
| 86 | * The encrypted ByteBuffer used for handshaking and writing response bytes. | |
| 87 | */ | |
| 88 | private ByteBuffer outputBB; | |
| 89 | ||
| 90 | ||
| 91 | /** | |
| 92 | * The{@link SSLEngine} used to manage the SSL over NIO request. | |
| 93 | */ | |
| 94 | private SSLEngine sslEngine; | |
| 95 | ||
| 96 | /** | |
| 97 | * ThreadAttachment store mode | |
| 98 | */ | |
| 99 | private int mode; | |
| 100 | ||
| 101 | ||
| 102 | 132 | public ThreadAttachment(){ |
| 103 | 132 | attributes = new HashMap<String,Object>(); |
| 104 | 132 | } |
| 105 | ||
| 106 | public int getMode() { | |
| 107 | 212342 | return mode; |
| 108 | } | |
| 109 | ||
| 110 | public void setMode(int mode) { | |
| 111 | 159392 | this.mode = mode; |
| 112 | 159392 | } |
| 113 | ||
| 114 | ||
| 115 | public void setAttribute(String key, Object value){ | |
| 116 | 110 | attributes.put(key,value); |
| 117 | 110 | } |
| 118 | ||
| 119 | ||
| 120 | public Object getAttribute(String key){ | |
| 121 | 52741 | return attributes.get(key); |
| 122 | } | |
| 123 | ||
| 124 | ||
| 125 | public Object removeAttribute(String key){ | |
| 126 | 1 | return attributes.remove(key); |
| 127 | } | |
| 128 | ||
| 129 | public void setAttributes(Map<String, Object> attributes) { | |
| 130 | 0 | this.attributes = attributes; |
| 131 | 0 | } |
| 132 | ||
| 133 | public Map<String, Object> getAttributes() { | |
| 134 | 0 | return attributes; |
| 135 | } | |
| 136 | ||
| 137 | /** | |
| 138 | * Set the {@link ByteBuffer} shared this thread | |
| 139 | */ | |
| 140 | public void setByteBuffer(ByteBuffer byteBuffer){ | |
| 141 | 12 | this.byteBuffer = byteBuffer; |
| 142 | 12 | } |
| 143 | ||
| 144 | ||
| 145 | /** | |
| 146 | * Return the {@link ByteBuffer} shared this thread | |
| 147 | */ | |
| 148 | public ByteBuffer getByteBuffer(){ | |
| 149 | 4 | return byteBuffer; |
| 150 | } | |
| 151 | ||
| 152 | ||
| 153 | /** | |
| 154 | * Return the encrypted {@link ByteBuffer} used to handle request. | |
| 155 | * @return {@link ByteBuffer} | |
| 156 | */ | |
| 157 | public ByteBuffer getInputBB(){ | |
| 158 | 53081 | return inputBB; |
| 159 | } | |
| 160 | ||
| 161 | ||
| 162 | /** | |
| 163 | * Set the encrypted {@link ByteBuffer} used to handle request. | |
| 164 | * @param inputBB {@link ByteBuffer} | |
| 165 | */ | |
| 166 | public void setInputBB(ByteBuffer inputBB){ | |
| 167 | 159257 | this.inputBB = inputBB; |
| 168 | 159257 | } |
| 169 | ||
| 170 | ||
| 171 | /** | |
| 172 | * Return the encrypted {@link ByteBuffer} used to handle response. | |
| 173 | * @return {@link ByteBuffer} | |
| 174 | */ | |
| 175 | public ByteBuffer getOutputBB(){ | |
| 176 | 103 | return outputBB; |
| 177 | } | |
| 178 | ||
| 179 | ||
| 180 | /** | |
| 181 | * Set the encrypted {@link ByteBuffer} used to handle response. | |
| 182 | * @param outputBB {@link ByteBuffer} | |
| 183 | */ | |
| 184 | public void setOutputBB(ByteBuffer outputBB){ | |
| 185 | 311 | this.outputBB = outputBB; |
| 186 | 311 | } |
| 187 | ||
| 188 | ||
| 189 | /** | |
| 190 | * Set the{@link SSLEngine}. | |
| 191 | * @return{@link SSLEngine} | |
| 192 | */ | |
| 193 | public SSLEngine getSSLEngine() { | |
| 194 | 53081 | return sslEngine; |
| 195 | } | |
| 196 | ||
| 197 | ||
| 198 | /** | |
| 199 | * Get the{@link SSLEngine}. | |
| 200 | * @param sslEngine{@link SSLEngine} | |
| 201 | */ | |
| 202 | public void setSSLEngine(SSLEngine sslEngine) { | |
| 203 | 159380 | this.sslEngine = sslEngine; |
| 204 | 159380 | } |
| 205 | ||
| 206 | ||
| 207 | /** | |
| 208 | * Return the name of the Thread on which this instance is binded. | |
| 209 | */ | |
| 210 | public String getThreadId() { | |
| 211 | 0 | return threadId; |
| 212 | } | |
| 213 | ||
| 214 | ||
| 215 | /** | |
| 216 | * Set the Thread's name on which this instance is binded. | |
| 217 | */ | |
| 218 | public void setThreadId(String threadId) { | |
| 219 | 0 | this.threadId = threadId; |
| 220 | 0 | } |
| 221 | ||
| 222 | /** | |
| 223 | * Associates ThreadAttachment with the current thread | |
| 224 | */ | |
| 225 | public void associate() { | |
| 226 | 53083 | if (!threadLock.isHeldByCurrentThread()) { |
| 227 | 53083 | threadLock.lock(); |
| 228 | } | |
| 229 | 53083 | } |
| 230 | ||
| 231 | /** | |
| 232 | * Releases ThreadAttachment association with the current thread | |
| 233 | */ | |
| 234 | public void deassociate() { | |
| 235 | 53338 | if (threadLock.isHeldByCurrentThread()) { |
| 236 | 53083 | threadLock.unlock(); |
| 237 | } | |
| 238 | 53338 | } |
| 239 | ||
| 240 | /** | |
| 241 | * SelectionKey attachment processing | |
| 242 | * @param selectionKey | |
| 243 | */ | |
| 244 | public void process(SelectionKey selectionKey) { | |
| 245 | 53080 | ((WorkerThread) Thread.currentThread()).attach(this); |
| 246 | 53080 | } |
| 247 | ||
| 248 | /** | |
| 249 | * SelectionKey attachment postProcessing | |
| 250 | * @param selectionKey | |
| 251 | */ | |
| 252 | public void postProcess(SelectionKey selectionKey) { | |
| 253 | 53084 | ((WorkerThread) Thread.currentThread()).detach(); |
| 254 | 53084 | } |
| 255 | ||
| 256 | public void reset() { | |
| 257 | 159506 | mode = Mode.ATTRIBUTES_ONLY; |
| 258 | 159506 | byteBuffer = null; |
| 259 | 159506 | sslEngine = null; |
| 260 | 159506 | inputBB = null; |
| 261 | 159506 | outputBB = null; |
| 262 | 159506 | } |
| 263 | ||
| 264 | @Override | |
| 265 | public void release(SelectionKey selectionKey) { | |
| 266 | 123 | attributes.clear(); |
| 267 | 123 | reset(); |
| 268 | ||
| 269 | 123 | deassociate(); |
| 270 | 123 | super.release(selectionKey); |
| 271 | 123 | } |
| 272 | } |