KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedListPipeline;
29 import com.sun.enterprise.web.connector.grizzly.Pipeline;
30 import com.sun.enterprise.web.connector.grizzly.ProcessorTask;
31 import com.sun.enterprise.web.connector.grizzly.Task;
32 import java.util.ArrayList JavaDoc;
33 import java.util.concurrent.ConcurrentLinkedQueue JavaDoc;
34
35 /**
36  * Default implementation of <code>AsyncHandler</code>. This class handle
37  * the aysnchronous execution of a <code>ProcessorTask</code>. The request
38  * processing is executed by doing:
39  *
40  * (1) Wrap the <code>ProcessorTask</code> using an instance of
41  * <code>AsyncProcessorTask</code>
42  * (2) Execute the <code>AsyncProcessorTask</code> using the wrapped
43  * <code>ProcessorTask</code> <code>Pipeline</code>
44  * (3) If the <code>AsyncProcessorTask</code> has been interrupted but ready
45  * to be removed from the interrupted queue, remove it and execute the
46  * remaining operations.
47  *
48  * @author Jeanfrancois Arcand
49  */

50 public class DefaultAsyncHandler implements AsyncHandler{
51     
52     /**
53      * Cache instance of <code>AsyncProcessorTask</code>
54      */

55     private ConcurrentLinkedQueue JavaDoc<AsyncProcessorTask>
56             asyncProcessors = new ConcurrentLinkedQueue JavaDoc<AsyncProcessorTask>();
57     
58     
59     /**
60      * A queue used to cache interrupted <code>AsyncProcessorTask</code>.
61      */

62     private ConcurrentLinkedQueue JavaDoc<AsyncProcessorTask>
63             interrruptedQueue = new ConcurrentLinkedQueue JavaDoc<AsyncProcessorTask>();
64                
65     
66     /**
67      * The <code>AsyncFilter</code> to execute asynchronous operations on
68      * a <code>ProcessorTask</code>.
69      */

70     private ArrayList JavaDoc<AsyncFilter> asyncFilters =
71             new ArrayList JavaDoc<AsyncFilter>();
72     
73     
74     /**
75      * The <code>AsyncExecutor</code> class name to use.
76      */

77     private String JavaDoc asyncExecutorClassName
78         = "com.sun.enterprise.web.connector.grizzly.async.DefaultAsyncExecutor";
79     // ------------------------------------------------- Constructor --------//
80

81     
82     public DefaultAsyncHandler() {
83     }
84     
85     
86     /**
87      * Create an instance of <code>AsyncProcessorTask</code>
88      */

89     private AsyncProcessorTask newAsyncProcessorTask(){
90         AsyncProcessorTask asyncProcessorTask = new AsyncProcessorTask();
91         asyncProcessorTask
92                 .setAsyncExecutor(newAsyncExecutor(asyncProcessorTask));
93         return asyncProcessorTask;
94     }
95     
96     
97     /**
98      * Create an instance of <code>DefaultAsyncExecutor</code>
99      */

100     private AsyncExecutor newAsyncExecutor(AsyncProcessorTask asyncProcessorTask){
101         
102         Class JavaDoc className = null;
103         AsyncExecutor asyncExecutor = null;
104         try{
105             className = Class.forName(asyncExecutorClassName);
106             asyncExecutor = (AsyncExecutor)className.newInstance();
107         } catch (ClassNotFoundException JavaDoc ex){
108             throw new RuntimeException JavaDoc(ex);
109         } catch (InstantiationException JavaDoc ex){
110             throw new RuntimeException JavaDoc(ex);
111         } catch (IllegalAccessException JavaDoc ex){
112             throw new RuntimeException JavaDoc(ex);
113         }
114         
115         if ( asyncExecutor != null ){
116             asyncExecutor.setAsyncProcessorTask(asyncProcessorTask);
117             
118             for (AsyncFilter l : asyncFilters){
119                 asyncExecutor.addAsyncFilter(l);
120             }
121         }
122         return asyncExecutor;
123     }
124     
125     
126     /**
127      * Return an instance of <code>AsyncProcessorTask</code>, which is
128      * configured and ready to be executed.
129      */

130     private AsyncProcessorTask getAsyncProcessorTask(){
131         AsyncProcessorTask asyncProcessorTask = asyncProcessors.poll();
132         if ( asyncProcessorTask == null) {
133             asyncProcessorTask = newAsyncProcessorTask();
134         } else {
135             asyncProcessorTask.recycle();
136         }
137         return asyncProcessorTask;
138     }
139     
140     
141     // ---------------------------------------------------- Interface -------//
142

143     
144     /**
145      * Handle an instance of a <code>Task</code>. This method is invoked
146      * first by a <code>ProcessorTask</code>, which delegate its execution to
147      * this handler. Second, this method is invoked once a
148      * <code>ProcessorTask</code> needs to be removed from the interrupted queue.
149      */

150     public void handle(Task task){
151         
152         AsyncProcessorTask apt = null;
153         if ( task.getType() == Task.PROCESSOR_TASK) {
154             apt = getAsyncProcessorTask();
155             apt.setProcessorTask((ProcessorTask)task);
156         }
157         
158         boolean wasInterrupted = interrruptedQueue.remove(task);
159         if ( !wasInterrupted && apt == null) {
160             throw new IllegalStateException JavaDoc();
161         } else if ( apt == null ){
162             apt = (AsyncProcessorTask)task;
163         }
164         apt.execute();
165     }
166     
167
168     /**
169      * Return th <code>Task</code> to the pool
170      */

171     public void returnTask(AsyncProcessorTask asyncProcessorTask){
172         asyncProcessors.offer(asyncProcessorTask);
173     }
174     
175     /**
176      * Add a <code>Task</code> to the interrupted queue.
177      */

178     public void addToInterruptedQueue(Task task){
179         interrruptedQueue.offer((AsyncProcessorTask)task);
180     }
181     
182     
183     /**
184      * Remove the <code>Task</code> from the interrupted queue.
185      */

186     public void removeFromInterruptedQueue(Task task){
187         interrruptedQueue.remove(task);
188     }
189     
190     
191     /**
192      * Set the <code>AsyncExecutor</code> used by this object.
193      */

194     public void setAsyncExecutorClassName(String JavaDoc asyncExecutorClassName){
195         this.asyncExecutorClassName = asyncExecutorClassName;
196     }
197     
198     
199     /**
200      * Get the code>AsyncExecutor</code> used by this object.
201      */

202     public String JavaDoc getAsyncExecutorClassName(){
203         return asyncExecutorClassName;
204     }
205     
206     
207     /**
208      * Add an <code>AsyncFilter</code>
209      */

210     public void addAsyncFilter(AsyncFilter asyncFilter) {
211         asyncFilters.add(asyncFilter);
212     }
213
214     
215     /**
216      * Remove an <code>AsyncFilter</code>
217      */

218     public boolean removeAsyncFilter(AsyncFilter asyncFilter) {
219         return asyncFilters.remove(asyncFilter);
220     }
221 }
222
Popular Tags