Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WorkerThread |
|
| 1.1363636363636365;1,136 |
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 javax.net.ssl.SSLEngine; | |
42 | ||
43 | /** | |
44 | * Simple interface to allow the addition of <code>Thread</code> attributes. | |
45 | * | |
46 | * @author Jean-Francois Arcand | |
47 | */ | |
48 | public abstract class WorkerThread extends Thread { | |
49 | ||
50 | ||
51 | /** | |
52 | * The {@link ByteBuffer} used when {@link Task} are executed. | |
53 | */ | |
54 | protected ByteBuffer byteBuffer; | |
55 | ||
56 | ||
57 | /** | |
58 | * The encrypted ByteBuffer used for handshaking and reading request bytes. | |
59 | */ | |
60 | protected ByteBuffer inputBB; | |
61 | ||
62 | ||
63 | /** | |
64 | * The encrypted ByteBuffer used for handshaking and writing response bytes. | |
65 | */ | |
66 | protected ByteBuffer outputBB; | |
67 | ||
68 | ||
69 | /** | |
70 | * The{@link SSLEngine} used to manage the SSL over NIO request. | |
71 | */ | |
72 | protected SSLEngine sslEngine; | |
73 | ||
74 | 0 | public WorkerThread() { |
75 | 0 | } |
76 | ||
77 | public WorkerThread(String name) { | |
78 | 0 | super(name); |
79 | 0 | } |
80 | ||
81 | public WorkerThread(Runnable target) { | |
82 | 0 | super(target); |
83 | 0 | } |
84 | ||
85 | public WorkerThread(Runnable target, String name) { | |
86 | 0 | super(target, name); |
87 | 0 | } |
88 | ||
89 | public WorkerThread(ThreadGroup group, String name) { | |
90 | 353 | super(group, name); |
91 | 353 | } |
92 | ||
93 | public WorkerThread(ThreadGroup group, Runnable target) { | |
94 | 0 | super(group, target); |
95 | 0 | } |
96 | ||
97 | public WorkerThread(ThreadGroup group, Runnable target, String name) { | |
98 | 0 | super(group, target, name); |
99 | 0 | } |
100 | ||
101 | public WorkerThread(ThreadGroup group, Runnable target, String name, long stackSize) { | |
102 | 0 | super(group, target, name, stackSize); |
103 | 0 | } |
104 | ||
105 | /** | |
106 | * Set the {@link ByteBuffer} shared this thread | |
107 | */ | |
108 | public void setByteBuffer(ByteBuffer byteBuffer){ | |
109 | 107877 | this.byteBuffer = byteBuffer; |
110 | 107877 | } |
111 | ||
112 | ||
113 | /** | |
114 | * Return the {@link ByteBuffer} shared this thread | |
115 | */ | |
116 | public ByteBuffer getByteBuffer(){ | |
117 | 389388 | return byteBuffer; |
118 | } | |
119 | ||
120 | ||
121 | /** | |
122 | * Return the encrypted {@link ByteBuffer} used to handle request. | |
123 | * @return {@link ByteBuffer} | |
124 | */ | |
125 | public ByteBuffer getInputBB(){ | |
126 | 161985 | return inputBB; |
127 | } | |
128 | ||
129 | ||
130 | /** | |
131 | * Set the encrypted {@link ByteBuffer} used to handle request. | |
132 | * @param inputBB {@link ByteBuffer} | |
133 | */ | |
134 | public void setInputBB(ByteBuffer inputBB){ | |
135 | 107877 | this.inputBB = inputBB; |
136 | 107877 | } |
137 | ||
138 | ||
139 | /** | |
140 | * Return the encrypted {@link ByteBuffer} used to handle response. | |
141 | * @return {@link ByteBuffer} | |
142 | */ | |
143 | public ByteBuffer getOutputBB(){ | |
144 | 162382 | return outputBB; |
145 | } | |
146 | ||
147 | ||
148 | /** | |
149 | * Set the encrypted {@link ByteBuffer} used to handle response. | |
150 | * @param outputBB {@link ByteBuffer} | |
151 | */ | |
152 | public void setOutputBB(ByteBuffer outputBB){ | |
153 | 107969 | this.outputBB = outputBB; |
154 | 107969 | } |
155 | ||
156 | ||
157 | /** | |
158 | * Set the{@link SSLEngine}. | |
159 | * @return{@link SSLEngine} | |
160 | */ | |
161 | public SSLEngine getSSLEngine() { | |
162 | 269751 | return sslEngine; |
163 | } | |
164 | ||
165 | ||
166 | /** | |
167 | * Get the{@link SSLEngine}. | |
168 | * @param sslEngine{@link SSLEngine} | |
169 | */ | |
170 | public void setSSLEngine(SSLEngine sslEngine) { | |
171 | 116 | this.sslEngine = sslEngine; |
172 | 116 | } |
173 | ||
174 | ||
175 | /** | |
176 | * Updates Thread associated attachment according to the passed mode. | |
177 | * | |
178 | * @return updated ThreadAttachment | |
179 | */ | |
180 | public abstract ThreadAttachment updateAttachment(int mode); | |
181 | ||
182 | ||
183 | /** | |
184 | * Get the current set of attributes (state) associated with this instance. | |
185 | * Unlike detach(), this method doesn't clear the WorkerThread attributes. | |
186 | * | |
187 | * @return the Thread associated ThreadAttachment | |
188 | */ | |
189 | public abstract ThreadAttachment getAttachment(); | |
190 | ||
191 | ||
192 | /** | |
193 | * Detach the current set of attributes (state) associated with this instance. | |
194 | * Method will re-create all the ByteBuffers associated with this thread. | |
195 | * | |
196 | * @return a new ThreadAttachment | |
197 | */ | |
198 | public abstract ThreadAttachment detach(); | |
199 | ||
200 | ||
201 | /** | |
202 | * Attach the ThreadAttachment to this instance. This will configure this | |
203 | * Thread attributes like ByteBuffer, SSLEngine, etc. | |
204 | * @param ThreadAttachment the attachment. | |
205 | */ | |
206 | public abstract void attach(ThreadAttachment threadAttachment); | |
207 | ||
208 | /** | |
209 | * Stop this thread. If this Thread is performing atask, the task will be | |
210 | * completed. | |
211 | */ | |
212 | public abstract void terminate(); | |
213 | ||
214 | protected void reset() { | |
215 | 388885 | if (byteBuffer != null) { |
216 | 388885 | byteBuffer.clear(); |
217 | } | |
218 | ||
219 | 388880 | if (inputBB != null) { |
220 | 228 | inputBB.clear(); |
221 | } | |
222 | ||
223 | 388885 | if (outputBB != null) { |
224 | 94638 | outputBB.clear(); |
225 | } | |
226 | ||
227 | 388130 | sslEngine = null; |
228 | 388882 | } |
229 | } |