KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > AsyncReadTask


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.web.connector.grizzly;
24
25 import java.io.IOException JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 /**
29  * This task execute and then notify its listener, which usualy execute on
30  * their own thread. The task perform the same operation as its parent,
31  * but the attached <code>ProcessorTask</code> execute on its own thread.
32  *
33  * @author Jean-Francois Arcand
34  */

35 public class AsyncReadTask extends ReadTask {
36    
37     public AsyncReadTask(){
38         super();
39     }
40     
41     
42     public AsyncReadTask(StreamAlgorithm algorithm,
43                     boolean useDirectByteBuffer, boolean useByteBufferView){
44         super(algorithm,useDirectByteBuffer,useByteBufferView);
45     }
46
47     // ----------------------------------------------------------------------//
48

49     /**
50      * Manage the <code>SelectionKey</code>
51      */

52     protected void manageKeepAlive(boolean keepAlive,int count,
53             Exception JavaDoc exception){
54
55         // The key is invalid when the Task has been cancelled.
56
if ( count <= 0 || exception != null){
57             if ( exception != null){
58                 // Make sure we have detached the processorTask
59
detachProcessor();
60                 SelectorThread.logger().log(Level.FINEST,
61                     "SocketChannel Read Exception: "
62                         ,exception);
63             }
64             terminate(false);
65         }
66     }
67
68
69     /**
70      * Execute the <code>ProcessorTask</code>
71      * @return false if the request wasn't fully read by the channel.
72      * so we need to respin the key on the Selector.
73      */

74     public boolean executeProcessorTask() throws IOException JavaDoc{
75         boolean registerKey = false;
76         
77         if (SelectorThread.logger().isLoggable(Level.FINEST))
78             SelectorThread.logger().log(Level.FINEST,"executeProcessorTask");
79         
80         if ( algorithm.getHandler()
81                 .handle(null, Handler.REQUEST_BUFFERED) == Handler.BREAK ){
82             return true;
83         }
84         
85         // Get a processor task. If the processorTask != null, that means we
86
// failed to load all the bytes in a single channel.read().
87
if (processorTask == null){
88             attachProcessor(selectorThread.getProcessorTask());
89         } else {
90             // When spawnProcessorTask is true, we must reconfigure the
91
// ProcessorTask
92
configureProcessorTask();
93         }
94         
95         if (taskEvent == null){
96             taskContext = new TaskContext();
97             taskEvent = new TaskEvent<TaskContext>(taskContext);
98         }
99         
100         // Call the listener and execute the task on it's own pipeline.
101
taskEvent.setStatus(TaskEvent.START);
102         taskContext.setInputStream(inputStream);
103         taskEvent.attach(taskContext);
104         fireTaskEvent(taskEvent);
105         return false;
106     }
107
108       
109     /**
110      * Complete the transaction.
111      */

112     public void terminate(boolean keepAlive){
113         // Safeguard to avoid returning the instance more than once.
114
if ( isReturned ){
115             return;
116         }
117         
118         if (processorTask != null && processorTask.connectionHeaderValue){
119             detachProcessor();
120             registerKey();
121             recycle();
122             returnTask();
123         } else {
124             super.terminate(keepAlive);
125         }
126     }
127     
128     
129     /**
130      * Set appropriate attribute on the <code>ProcessorTask</code>.
131      */

132     public void configureProcessorTask(){
133         super.configureProcessorTask();
134         if ( !getTaskListeners().contains(processorTask) ){
135             processorTask.addTaskListener(this);
136             addTaskListener(processorTask);
137         }
138     }
139 }
140
Popular Tags