KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > ara > IsolatedTask


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.ara;
25
26 import com.sun.enterprise.web.connector.grizzly.Pipeline;
27 import com.sun.enterprise.web.connector.grizzly.ReadTask;
28 import com.sun.enterprise.web.connector.grizzly.SelectorThread;
29 import com.sun.enterprise.web.connector.grizzly.SelectorThreadConfig;
30 import com.sun.enterprise.web.connector.grizzly.StreamAlgorithm;
31 import com.sun.enterprise.web.connector.grizzly.Task;
32 import com.sun.enterprise.web.connector.grizzly.TaskEvent;
33 import com.sun.enterprise.web.connector.grizzly.TaskListener;
34
35 import java.io.IOException JavaDoc;
36 import java.net.Socket JavaDoc;
37 import java.nio.ByteBuffer JavaDoc;
38 import java.nio.channels.SelectionKey JavaDoc;
39 import java.nio.channels.SocketChannel JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.concurrent.ConcurrentHashMap JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43 import java.util.logging.Level JavaDoc;
44
45 /**
46  * This task is used to configure an instance of <code>.ReadTask</code> based
47  * on the <code>Rule</code> implementation.
48  *
49  * @author Jeanfrancois Arcand
50  */

51 public class IsolatedTask extends TaskWrapper implements TaskListener{
52
53     public final static int ISOLATED_TASK = 4;
54              
55     /**
56      * The algorithm used to determine the context-root, the HTTP method,
57      * the protocol etc.
58      */

59     protected StreamAlgorithm algorithm;
60     
61     
62     /**
63      * The <code>RuleExecutor</code> used to apply <code>Rule</code>
64      */

65     protected RulesExecutor rulesExecutor;
66     
67    
68     /**
69      * List of listeners
70      */

71     protected ArrayList JavaDoc<TaskListener> listeners = new ArrayList JavaDoc<TaskListener>();
72
73     
74     /**
75      * The <code>ByteBuffer</code> initial position before applying the
76      * <code>Algorithm</code>
77      */

78     protected int initialBytePosition;
79     
80     
81     /**
82      * The <code>ByteBuffer</code> initial limit before applying the
83      * <code>Algorithm</code>
84      */

85     protected int initialByteLimit;
86     
87     
88     /**
89      * The <code>TaskEvent</code> used between this task and it's attached
90      * <code>ReadTask</code>
91      */

92     protected TaskEvent<IsolatedTask> taskEvent;
93     
94     
95     /**
96      * Cache the <code>SelectionKey</code> to avoid parsing the
97      * requests bytes more than once.
98      */

99     private static ConcurrentHashMap JavaDoc<SelectionKey JavaDoc,Pipeline> cacheKey =
100             new ConcurrentHashMap JavaDoc<SelectionKey JavaDoc,Pipeline>();
101     
102     
103     public IsolatedTask(){
104         taskEvent = new TaskEvent<IsolatedTask>();
105         taskEvent.attach(this);
106     }
107     
108     
109     /**
110      * Apply a set of <code>Rule</code>s to the current bytes requests using
111      * an instance of <code>ReadTask</code> byte buffer. Once the
112      * <code>Rule</code> has been successfully applied, execute it.
113      */

114     public void doTask() throws IOException JavaDoc {
115         ReadTask readTask = (ReadTask)wrappedTask;
116
117         Pipeline pipeline = cacheKey.get(readTask.getSelectionKey());
118         if ( pipeline != null ){
119             readTask.setPipeline(pipeline);
120             readTask.execute();
121             return;
122         }
123         
124         ByteBuffer JavaDoc byteBuffer = readTask.getByteBuffer();
125         try {
126             SocketChannel JavaDoc socketChannel =
127                         (SocketChannel JavaDoc)readTask.getSelectionKey().channel();
128             Socket JavaDoc socket = socketChannel.socket();
129            
130             socketChannel.read(byteBuffer);
131         
132             int position = byteBuffer.position();
133             int limit = byteBuffer.limit();
134
135             // If we weren't able to parse the token, return to the
136
// SelectorThread
137
boolean execute = true;
138             
139             // XXXX Cache the Key to avoid parsing several time
140
if (algorithm.parse(byteBuffer)) {
141                 execute = rulesExecutor.execute(this);
142             }
143             
144             if ( execute ){
145                 // Tell the ReadTask to not load bytes and re-use the one
146
// already loaded.
147
readTask.setBytesAvailable(true);
148                 byteBuffer.limit(limit);
149                 byteBuffer.position(position);
150                 
151                 if ( rulesExecutor.isCachingAllowed()) {
152                     // Cache the key.
153
cacheKey.put(readTask.getSelectionKey(),
154                                  readTask.getPipeline());
155                 }
156                 
157                 // Get notification once the task has completed.
158
readTask.addTaskListener(this);
159                 
160                 readTask.execute();
161             }
162         } catch (Exception JavaDoc ex){
163             SelectorThread.logger()
164                 .log(Level.SEVERE,"IsolatedTask logic exception.",ex);
165         } finally {
166             fireTaskEvent(taskEvent);
167         }
168     }
169         
170     
171     /**
172      * Set the <code>RuleExecutor</code> instance used by this task.
173      */

174     public void setRulesExecutor(RulesExecutor rulesExecutor){
175         this.rulesExecutor = rulesExecutor;
176     }
177     
178     /**
179      * Set the <code>Algorithm</code> used by this task.
180      */

181     public void setAlgorithm(StreamAlgorithm algorithm){
182         this.algorithm = algorithm;
183     }
184     
185     
186     /**
187      * Wrao the <code>Task</code> with this task.
188      */

189     public IsolatedTask wrap(Task task){
190         wrappedTask = task;
191         return this;
192     }
193      
194     
195     // ------------------------------------------------------ Execution -----//
196

197     
198     /**
199      * Execute that task using the current Thread.
200      */

201     public void execute(){
202         run();
203     }
204     
205     
206     /**
207      * Execute the logic required to isolate the task.
208      */

209     public void run(){
210         try{
211             doTask();
212         } catch (IOException JavaDoc ex){
213             throw new RuntimeException JavaDoc(ex);
214         };
215     }
216     
217     // ----------------------------------------------------- Task Listener ----/
218

219     
220      /**
221      * Add the given <code>TaskListener</code> to this <code>Task</code>.
222      */

223     public void addTaskListener(TaskListener task){
224         listeners.add(task);
225     }
226
227     
228     /**
229      * Remove the given <code>TaskListener/code> from this
230      * <code>Task</code>.
231      */

232     public void removeTaskListener(TaskListener task){
233         listeners.remove(task);
234     }
235     
236     
237     /**
238      * Clean all the listeners of this <code>Task</code>
239      */

240     public void clearTaskListeners(){
241         listeners.clear();
242     }
243
244     
245     /**
246      * Notify listeners of that class that the processing has completed.
247      */

248     protected void fireTaskEvent(TaskEvent<?> event){
249         for (int i=0; i < listeners.size(); i++){
250             listeners.get(i).taskEvent(event);
251         }
252     }
253
254     
255     /**
256      * Remove the <code>SelectionKey</code> from the cache.
257      */

258     public void taskEvent(TaskEvent event) {
259         if (event.getStatus() == TaskEvent.COMPLETED)
260             cacheKey.remove(event.attachement());
261     }
262
263     
264     /**
265      * This task type.
266      */

267     public int getType(){
268         return ISOLATED_TASK;
269     }
270     
271 }
272
Popular Tags