1 23 package com.sun.enterprise.web.connector.grizzly; 24 25 import java.io.IOException ; 26 27 import java.net.Socket ; 28 29 import java.nio.ByteBuffer ; 30 import java.nio.channels.SocketChannel ; 31 32 import java.util.logging.Level ; 33 34 43 public class ReadTask extends TaskBase { 44 45 46 50 protected TaskContext taskContext; 51 52 53 57 protected TaskEvent taskEvent; 58 59 60 64 protected ByteBuffer byteBuffer; 65 66 67 70 protected ProcessorTask processorTask; 71 72 73 76 protected int maxPostSize = 25 * 1024 * 1024; 77 78 79 82 protected ByteBufferInputStream inputStream; 83 84 85 89 protected StreamAlgorithm algorithm; 90 91 92 96 protected boolean bytesAvailable = false; 97 98 99 102 protected volatile boolean isReturned = false; 103 104 105 107 108 public ReadTask(){ 109 ; } 111 112 113 public ReadTask(StreamAlgorithm algorithm, 114 boolean useDirectByteBuffer, boolean useByteBufferView){ 115 type = READ_TASK; 116 this.algorithm = algorithm; 117 byteBuffer = algorithm.allocate(useDirectByteBuffer,useByteBufferView); 118 inputStream = new ByteBufferInputStream(); 119 } 120 121 122 125 public void attachProcessor(ProcessorTask processorTask){ 126 this.processorTask = processorTask; 127 configureProcessorTask(); 128 } 129 130 131 134 public void configureProcessorTask(){ 135 processorTask.useAlternateKeepAlive(true); 138 processorTask.setSelectionKey(key); 139 processorTask.setSocket(((SocketChannel )key.channel()).socket()); 140 processorTask.setHandler(algorithm.getHandler()); 141 142 } 143 144 145 148 public void detachProcessor(){ 149 if (processorTask != null){ 150 processorTask.recycle(); 151 } 152 153 if ( listeners != null ) { 155 for (int i=listeners.size()-1; i > -1; i--){ 156 if ( taskEvent == null ) { 157 taskEvent = new TaskEvent<ReadTask>(); 158 } 159 taskEvent.attach(this); 160 taskEvent.setStatus(TaskEvent.COMPLETED); 161 listeners.get(i).taskEvent(taskEvent); 162 } 163 clearTaskListeners(); 164 } 165 166 if (recycle && processorTask != null){ 167 selectorThread.returnTask(processorTask); 168 processorTask = null; 169 } 170 } 171 172 173 178 public void doTask() throws IOException { 179 int count = 0; 180 Socket socket = null; 181 SocketChannel socketChannel = null; 182 boolean keepAlive = false; 183 Exception exception = null; 184 isReturned = false; 185 186 try { 187 socketChannel = (SocketChannel )key.channel(); 188 socket = socketChannel.socket(); 189 algorithm.setSocketChannel(socketChannel); 190 191 int loop = 0; 192 int bufferSize = 0; 193 while ( socketChannel.isOpen() && (bytesAvailable || 194 ((count = socketChannel.read(byteBuffer))> -1))){ 195 196 if ( count == 0 && !bytesAvailable){ 198 loop++; 199 if (loop > 2){ 200 break; 201 } 202 continue; 203 } 204 bytesAvailable = false; 205 206 byteBuffer = algorithm.preParse(byteBuffer); 207 inputStream.setByteBuffer(byteBuffer); 208 inputStream.setSelectionKey(key); 209 210 if ( algorithm.parse(byteBuffer) ){ 212 keepAlive = executeProcessorTask(); 213 if (!keepAlive) { 214 break; 215 } 216 } else { 217 keepAlive = true; 220 } 221 } 222 } catch (IOException ex) { 224 exception = ex; 225 } catch (RuntimeException ex) { 226 exception = ex; 227 } finally { 228 manageKeepAlive(keepAlive,count,exception); 229 } 230 } 231 232 233 237 protected void manageKeepAlive(boolean keepAlive,int count, 238 Exception exception){ 239 240 if ( count == -1 || !key.isValid() || exception != null ){ 242 keepAlive = false; 243 244 if ( exception != null){ 245 detachProcessor(); 247 SelectorThread.logger(). 248 log(Level.FINEST, "SocketChannel Read Exception: ",exception); 249 } 250 } 251 252 if (keepAlive) { 253 registerKey(); 254 } 255 256 terminate(keepAlive); 257 } 258 259 260 267 public boolean executeProcessorTask() throws IOException { 268 boolean registerKey = false; 269 270 if (SelectorThread.logger().isLoggable(Level.FINEST)) 271 SelectorThread.logger().log(Level.FINEST,"executeProcessorTask"); 272 273 if ( algorithm.getHandler() 274 .handle(null, Handler.REQUEST_BUFFERED) == Handler.BREAK ){ 275 return true; 276 } 277 278 if (processorTask == null){ 281 attachProcessor(selectorThread.getProcessorTask()); 282 } 283 284 try { 285 registerKey = processorTask.process(inputStream,null); 288 } catch (Exception e) { 289 SelectorThread.logger() 290 .log(Level.SEVERE,"readTask.processException", e); 291 } 292 detachProcessor(); 293 return registerKey; 294 } 295 296 297 300 protected void returnTask(){ 301 if ( recycle && !isReturned ) { 302 isReturned = true; 303 selectorThread.returnTask(this); 304 } 305 } 306 307 308 public void taskEvent(TaskEvent event){ 309 if (event.getStatus() == TaskEvent.COMPLETED){ 310 terminate(false); 311 } 312 } 313 314 315 318 public void terminate(boolean keepAlive){ 319 if ( isReturned ){ 321 return; 322 } 323 324 if ( !keepAlive ){ 325 finishConnection(); 326 } 327 recycle(); 328 returnTask(); 329 } 330 331 332 335 public void recycle(){ 336 byteBuffer = algorithm.postParse(byteBuffer); 337 338 byteBuffer.clear(); 339 inputStream.recycle(); 340 algorithm.recycle(); 341 key = null; 342 inputStream.setSelectionKey(null); 343 } 344 345 347 348 353 protected void finishConnection(){ 354 355 if (SelectorThread.logger().isLoggable(Level.FINEST)) 356 SelectorThread.logger().log(Level.FINEST,"finishConnection"); 357 358 try{ 359 if (taskContext != null){ 360 taskContext.recycle(); 361 } 362 } catch (IOException ioe){ 363 ; 364 } 365 366 selectorThread.cancelKey(key); 367 } 368 369 370 373 protected void registerKey(){ 374 if (key.isValid()){ 375 if (SelectorThread.logger().isLoggable(Level.FINEST)) 376 SelectorThread.logger().log(Level.FINEST,"registerKey"); 377 378 selectorThread.registerKey(key); 379 } else { 380 terminate(false); 381 } 382 } 383 384 385 387 388 392 public ProcessorTask getProcessorTask(){ 393 return processorTask; 394 } 395 396 397 401 public void setStreamAlgorithm(StreamAlgorithm algorithm){ 402 this.algorithm = algorithm; 403 } 404 405 406 409 public ByteBuffer getByteBuffer(){ 410 return byteBuffer; 411 } 412 413 414 418 public void setBytesAvailable(boolean bytesAvailable){ 419 this.bytesAvailable = bytesAvailable; 420 } 421 422 } 423 | Popular Tags |