KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > async > AsyncProcessorTask


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
24 package com.sun.enterprise.web.connector.grizzly.async;
25
26 import com.sun.enterprise.web.connector.grizzly.AsyncExecutor;
27 import com.sun.enterprise.web.connector.grizzly.AsyncHandler;
28 import com.sun.enterprise.web.connector.grizzly.Pipeline;
29 import com.sun.enterprise.web.connector.grizzly.ProcessorTask;
30 import com.sun.enterprise.web.connector.grizzly.TaskBase;
31 import com.sun.enterprise.web.connector.grizzly.TaskEvent;
32
33 /**
34  * A <code>Task</code> that wraps the execution of an asynchronous execution
35  * of a <code>ProcessorTask</code>. Internaly, this class invoke the associated
36  * <code>AsyncExecutor</code> method to execute the <code>ProcessorTask</code>
37  * lifecycle operations.
38  *
39  * @author Jeanfrancois Arcand
40  */

41 public class AsyncProcessorTask extends TaskBase {
42     
43     public final static int PRE_EXECUTE = 0;
44     public final static int INTERRUPTED = 1;
45     public final static int POST_EXECUTE = 2;
46     public final static int COMPLETED = 3;
47     /**
48      * The <code>AsyncExecutor</code> which drive the execution of the
49      * <code>ProcesssorTask</code>
50      */

51     private AsyncExecutor asyncExecutor;
52
53     
54     /**
55      * The <code>ProcessorTask</code>
56      */

57     private ProcessorTask processorTask;
58     
59     
60     /**
61      * The current execution stage.
62      */

63     private int stage = PRE_EXECUTE;
64     
65     
66     /**
67      * Execute the <code>AsyncExecutor</code> based on the <code>stage</code>
68      * of the <code>ProcessorTask</code> execution.
69      */

70     public void doTask() throws java.io.IOException JavaDoc {
71         boolean contineExecution = true;
72         while ( contineExecution ) {
73             try{
74                 switch(stage){
75                     case PRE_EXECUTE:
76                        stage = INTERRUPTED;
77                        contineExecution = asyncExecutor.preExecute();
78                        break;
79                     case INTERRUPTED:
80                        stage = POST_EXECUTE;
81                        contineExecution = asyncExecutor.interrupt();
82                        break;
83                     case POST_EXECUTE:
84                        contineExecution = asyncExecutor.postExecute();
85                        stage = COMPLETED;
86                        break;
87                 }
88             } catch (Throwable JavaDoc t){
89                 if ( stage <= INTERRUPTED) {
90                     // We must close the connection.
91
stage = POST_EXECUTE;
92                 } else {
93                     stage = COMPLETED;
94                     throw new RuntimeException JavaDoc(t);
95                 }
96             } finally {
97                 // If the execution is completed, return this task to the pool.
98
if ( stage == COMPLETED){
99                     stage = PRE_EXECUTE;
100                     ((DefaultAsyncHandler)processorTask.getAsyncHandler())
101                         .returnTask(this);
102                 }
103             }
104         }
105     }
106
107     /**
108      * Not used.
109      */

110     public void taskEvent(TaskEvent event) {
111     }
112     
113     /**
114      * Return the <code>stage</code> of the current execution.
115      */

116     public int getStage(){
117         return stage;
118     }
119     
120     
121     /**
122      * Reset the object.
123      */

124     public void recycle(){
125         stage = PRE_EXECUTE;
126         processorTask = null;
127     }
128
129     
130     /**
131      * Set the <code>AsyncExecutor</code> used by this <code>Task</code>
132      * to delegate the execution of a <code>ProcessorTask</code>.
133      */

134     public void setAsyncExecutor(AsyncExecutor asyncExecutor){
135         this.asyncExecutor = asyncExecutor;
136     }
137     
138     
139     /**
140      * Get the <code>AsyncExecutor</code>.
141      */

142     public AsyncExecutor getAsyncExecutor(){
143         return asyncExecutor;
144     }
145     
146     
147     /**
148      * Set the <code>ProcessorTask</code> that needs to be executed
149      * asynchronously.
150      */

151     public void setProcessorTask(ProcessorTask processorTask){
152         this.processorTask = processorTask;
153         if ( pipeline == null && processorTask != null) {
154             setPipeline(processorTask.getPipeline());
155         }
156     }
157     
158     
159     /**
160      * Return the <code>ProcessorTask</code>.
161      */

162     public ProcessorTask getProcessorTask(){
163         return processorTask;
164     }
165
166 }
167
Popular Tags