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 | 124 | 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 | 124 | public ThreadAttachment(){ |
103 | 124 | attributes = new HashMap<String,Object>(); |
104 | 124 | } |
105 | ||
106 | public int getMode() { | |
107 | 215544 | return mode; |
108 | } | |
109 | ||
110 | public void setMode(int mode) { | |
111 | 161786 | this.mode = mode; |
112 | 161786 | } |
113 | ||
114 | ||
115 | public void setAttribute(String key, Object value){ | |
116 | 92 | attributes.put(key,value); |
117 | 92 | } |
118 | ||
119 | ||
120 | public Object getAttribute(String key){ | |
121 | 53468 | return attributes.get(key); |
122 | } | |
123 | ||
124 | ||
125 | public Object removeAttribute(String key){ | |
126 | 2 | 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 | 53881 | 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 | 161658 | this.inputBB = inputBB; |
168 | 161658 | } |
169 | ||
170 | ||
171 | /** | |
172 | * Return the encrypted {@link ByteBuffer} used to handle response. | |
173 | * @return {@link ByteBuffer} | |
174 | */ | |
175 | public ByteBuffer getOutputBB(){ | |
176 | 194 | 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 | 582 | this.outputBB = outputBB; |
186 | 582 | } |
187 | ||
188 | ||
189 | /** | |
190 | * Set the{@link SSLEngine}. | |
191 | * @return{@link SSLEngine} | |
192 | */ | |
193 | public SSLEngine getSSLEngine() { | |
194 | 53881 | return sslEngine; |
195 | } | |
196 | ||
197 | ||
198 | /** | |
199 | * Get the{@link SSLEngine}. | |
200 | * @param sslEngine{@link SSLEngine} | |
201 | */ | |
202 | public void setSSLEngine(SSLEngine sslEngine) { | |
203 | 161774 | this.sslEngine = sslEngine; |
204 | 161774 | } |
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 | 53884 | if (!threadLock.isHeldByCurrentThread()) { |
227 | 53884 | threadLock.lock(); |
228 | } | |
229 | 53884 | } |
230 | ||
231 | /** | |
232 | * Releases ThreadAttachment association with the current thread | |
233 | */ | |
234 | public void deassociate() { | |
235 | 54123 | if (threadLock.isHeldByCurrentThread()) { |
236 | 53884 | threadLock.unlock(); |
237 | } | |
238 | 54123 | } |
239 | ||
240 | /** | |
241 | * SelectionKey attachment processing | |
242 | * @param selectionKey | |
243 | */ | |
244 | public void process(SelectionKey selectionKey) { | |
245 | 53881 | ((WorkerThread) Thread.currentThread()).attach(this); |
246 | 53881 | } |
247 | ||
248 | /** | |
249 | * SelectionKey attachment postProcessing | |
250 | * @param selectionKey | |
251 | */ | |
252 | public void postProcess(SelectionKey selectionKey) { | |
253 | 53885 | ((WorkerThread) Thread.currentThread()).detach(); |
254 | 53885 | } |
255 | ||
256 | public void reset() { | |
257 | 161892 | mode = Mode.ATTRIBUTES_ONLY; |
258 | 161892 | byteBuffer = null; |
259 | 161892 | sslEngine = null; |
260 | 161892 | inputBB = null; |
261 | 161892 | outputBB = null; |
262 | 161892 | } |
263 | ||
264 | @Override | |
265 | public void release(SelectionKey selectionKey) { | |
266 | 115 | attributes.clear(); |
267 | 115 | reset(); |
268 | ||
269 | 115 | deassociate(); |
270 | 115 | super.release(selectionKey); |
271 | 115 | } |
272 | } |