KickJava   Java API By Example, From Geeks To Geeks.

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


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.async;
24
25 import com.sun.enterprise.web.connector.grizzly.AsyncExecutor;
26 import com.sun.enterprise.web.connector.grizzly.AsyncFilter;
27 import com.sun.enterprise.web.connector.grizzly.AsyncHandler;
28 import com.sun.enterprise.web.connector.grizzly.ProcessorTask;
29 import com.sun.enterprise.web.connector.grizzly.SelectorThread;
30 import java.util.ArrayList JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.logging.Level JavaDoc;
33
34 /**
35  * Default implementation of the <code>AsyncExecutor</code>. This class will
36  * execute a <code>ProcessorTask</code> asynchronously, by interrupting the
37  * process based on the logic defined in its associated <code>AsyncFilter</code>
38  * If no <code>AsyncFilter</code> are defined, the <code>ProcessorTask</code>
39  * will not be interrupted and executed synchronously.
40  *
41  * @author Jeanfrancois Arcand
42  */

43 public class DefaultAsyncExecutor implements AsyncExecutor{
44
45     private final static String JavaDoc ASYNC_FILTER =
46             "com.sun.enterprise.web.connector.grizzly.asyncFilters";
47
48     
49     /**
50      * The <code>AsyncProcessorTask</code> used to wrap the
51      * <code>ProcessorTask</code>
52      */

53     private AsyncProcessorTask asyncProcessorTask;
54        
55     
56     /**
57      * The associated <code>ProcessorTask</code>
58      */

59     private ProcessorTask processorTask;
60     
61     
62     /**
63      * The <code>AsyncFilter</code> to execute asynchronous operations on
64      * a <code>ProcessorTask</code>.
65      */

66     private static AsyncFilter[] sharedAsyncFilters = null;;
67     
68     
69     /**
70      * The <code>AsyncFilter</code> to execute asynchronous operations on
71      * a <code>ProcessorTask</code>.
72      */

73     private ArrayList JavaDoc<AsyncFilter> asyncFilters =
74             new ArrayList JavaDoc<AsyncFilter>();
75     
76     
77     /**
78      * Do we need to invoke filters?
79      */

80     private boolean invokeFilter = true;
81
82     
83     /**
84      * Loads filters implementation.
85      */

86     static {
87         loadFilters();
88     }
89     
90     
91     public DefaultAsyncExecutor(){
92         init();
93     }
94     
95     
96     private void init(){
97         if (sharedAsyncFilters != null){
98             for (AsyncFilter o : sharedAsyncFilters){
99                 asyncFilters.add(o);
100             }
101         }
102         
103     }
104     
105     // ------------------------------------------------Asynchrounous Execution --/
106

107     /**
108      * Pre-execute a <code>ProcessorTask</code> by parsing the request
109      * line.
110      */

111     public boolean preExecute() throws Exception JavaDoc{
112         processorTask = asyncProcessorTask.getProcessorTask();
113         processorTask.preProcess();
114         processorTask.parseRequest();
115         return true;
116     }
117     
118     
119     /**
120      * Interrupt the <code>ProcessorTask</code> if <code>AsyncFilter</code>
121      * has been defined.
122      * @return true if the execution can continue, false if delayed.
123      */

124     public boolean interrupt() throws Exception JavaDoc{
125         if ( asyncFilters == null || asyncFilters.size() == 0 ) {
126             processorTask.invokeAdapter();
127             return true;
128         } else {
129             processorTask.getAsyncHandler()
130                 .addToInterruptedQueue(asyncProcessorTask);
131                                          
132             return invokeFilters();
133         }
134     }
135
136     
137     /**
138      * Invoke the <code>AsyncFilter</code>
139      */

140     private boolean invokeFilters(){
141         boolean continueExec = true;
142         for (AsyncFilter asf: asyncFilters){
143             continueExec = asf.doFilter(this);
144             if ( !continueExec ){
145                 break;
146             }
147         }
148         return continueExec;
149     }
150     
151     
152     /**
153      * Post-execute the <code>ProcessorTask</code> by preparing the response,
154      * flushing the response and then close or keep-alive the connection.
155      */

156     public boolean postExecute() throws Exception JavaDoc{
157         processorTask.postResponse();
158         processorTask.postProcess();
159         processorTask.terminateProcess();
160         
161         // De-reference so under stress we don't have a simili leak.
162
processorTask = null;
163         return false;
164     }
165
166       
167     /**
168      * Set the <code>AsyncProcessorTask</code>.
169      */

170     public void setAsyncProcessorTask(AsyncProcessorTask asyncProcessorTask){
171         this.asyncProcessorTask = asyncProcessorTask;
172     }
173     
174     
175     /**
176      * Return <code>AsyncProcessorTask</code>.
177      */

178     public AsyncProcessorTask getAsyncProcessorTask(){
179         return asyncProcessorTask;
180     }
181     
182     
183     // --------------------------------------------------------- Util --------//
184

185     
186     /**
187      * Load the list of <code>AsynchFilter</code>.
188      */

189     protected static void loadFilters(){
190         if ( System.getProperty(ASYNC_FILTER) != null){
191             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(
192                     System.getProperty(ASYNC_FILTER),",");
193             
194             sharedAsyncFilters = new AsyncFilter[st.countTokens()];
195             int i = 0;
196             while (st.hasMoreTokens()){
197                 AsyncFilter asf = loadInstance(st.nextToken());
198                 if ( asf != null )
199                     sharedAsyncFilters[i++] = asf;
200             }
201         }
202     }
203     
204     
205     /**
206      * Instanciate a class based on a property.
207      */

208     private static AsyncFilter loadInstance(String JavaDoc property){
209         Class JavaDoc className = null;
210         try{
211             className = Class.forName(property);
212             return (AsyncFilter)className.newInstance();
213         } catch (ClassNotFoundException JavaDoc ex){
214             SelectorThread.logger().log(Level.WARNING,ex.getMessage(),ex);
215         } catch (InstantiationException JavaDoc ex){
216             SelectorThread.logger().log(Level.WARNING,ex.getMessage(),ex);
217         } catch (IllegalAccessException JavaDoc ex){
218             SelectorThread.logger().log(Level.WARNING,ex.getMessage(),ex);
219         }
220         return null;
221     }
222
223     
224     /**
225      * Add an <code>AsyncFilter</code>
226      */

227     public void addAsyncFilter(AsyncFilter asyncFilter) {
228         asyncFilters.add(asyncFilter);
229     }
230
231     
232     /**
233      * Remove an <code>AsyncFilter</code>
234      */

235     public boolean removeAsyncFilter(AsyncFilter asyncFilter) {
236         return asyncFilters.remove(asyncFilter);
237     }
238 }
239
Popular Tags