KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.Socket JavaDoc;
27 import java.nio.ByteBuffer JavaDoc;
28 import java.nio.channels.SelectionKey JavaDoc;
29 import java.nio.channels.SocketChannel JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.logging.Level JavaDoc;
32
33 import org.apache.coyote.RequestGroupInfo;
34
35 /**
36  * Abstract implementation of a <code>Task</code> object.
37  *
38  * @author Jean-Francois Arcand
39  */

40 public abstract class TaskBase implements Task, TaskListener{
41
42     
43     /**
44      * This number represent a specific implementation of a <code>Task</code>
45      * instance.
46      */

47     protected int type;
48     
49
50     /**
51      * List of listeners
52      */

53     protected ArrayList JavaDoc<TaskListener> listeners;
54       
55     
56     /**
57      * The <code>Pipeline</code> object associated with this
58      * <code>Task</code>
59      */

60     protected Pipeline pipeline;
61     
62     
63     /**
64      * The <code>SelectionKey</code> used by this task.
65      */

66     protected SelectionKey JavaDoc key;
67     
68     
69     /**
70      * Recycle this task
71      */

72     protected boolean recycle = true;
73     
74     
75     /**
76      * The <code>SelectorThread</code> who created this task.
77      */

78     protected SelectorThread selectorThread;
79
80
81     // ------------------------------------------------------------------//
82

83     public int getType(){
84         return type;
85     }
86
87     
88     /**
89      * Set the <code>SelectorThread</code> object.
90      */

91     public void setSelectorThread(SelectorThread selectorThread){
92         this.selectorThread = selectorThread;
93     }
94     
95     
96     /**
97      * Return the <code>SelectorThread</code>
98      */

99     public SelectorThread getSelectorThread(){
100         return selectorThread;
101     }
102     
103     
104     /**
105      * Set the pipeline on which Worker Threads will synchronize.
106      */

107     public void setPipeline(Pipeline pipeline){
108         this.pipeline = pipeline;
109     }
110     
111     
112     /**
113      * Return the pipeline used by this object.
114      */

115     public Pipeline getPipeline(){
116         return pipeline;
117     }
118    
119
120     /**
121      * Set the <code>SelectionKey</code>
122      */

123     public void setSelectionKey(SelectionKey JavaDoc key){
124         this.key = key;
125     }
126
127
128     /**
129      * Return the <code>SelectionKey</code> associated with this task.
130      */

131     public SelectionKey JavaDoc getSelectionKey(){
132         return key;
133     }
134
135
136     /**
137      * Gets the <code>RequestGroupInfo</code> from this task.
138      */

139     public RequestGroupInfo getRequestGroupInfo() {
140         return (selectorThread != null?
141                 selectorThread.getRequestGroupInfo() : null);
142     }
143
144
145     /**
146      * Returns <code>true</code> if monitoring has been enabled, false
147      * otherwise.
148      */

149     public boolean isMonitoringEnabled(){
150         return (selectorThread != null ?
151                 selectorThread.isMonitoringEnabled() : false);
152     }
153
154
155     /**
156      * Gets the <code>KeepAliveStats</code> associated with this task.
157      */

158     public KeepAliveStats getKeepAliveStats() {
159         return (selectorThread != null?
160                 selectorThread.getKeepAliveStats() : null);
161     }
162
163
164     /**
165      * Execute the task based on its <code>Pipeline</code>. If the
166      * <code>Pipeline</code> is null, then execute the task on using the
167      * calling thread.
168      */

169     public void execute(){
170         if (pipeline != null){
171             pipeline.addTask(this);
172         } else {
173             run();
174         }
175     }
176     
177     
178     //------------------------------------------------------Task Listener ----//
179

180     private void initListener(){
181         if ( listeners == null ){
182            listeners = new ArrayList JavaDoc<TaskListener>();
183         }
184     }
185     
186     
187     /**
188      * Add the given <code>TaskListener</code> to this <code>Task</code>.
189      */

190     public void addTaskListener(TaskListener task){
191         initListener();
192         listeners.add(task);
193     }
194
195     
196     /**
197      * Remove the given <code>TaskListener/code> from this
198      * <code>Task</code>.
199      */

200     public void removeTaskListener(TaskListener task){
201         if (listeners == null) return;
202         listeners.remove(task);
203     }
204     
205     
206     /**
207      * Clean all the listeners of this <code>Task</code>
208      */

209     public void clearTaskListeners(){
210         if (listeners == null) return;
211         listeners.clear();
212     }
213     
214     
215     /**
216      * Notify listeners.
217      */

218     protected void fireTaskEvent(TaskEvent<?> event){
219         if (listeners == null) return;
220         for (int i=0; i < listeners.size(); i++){
221             listeners.get(i).taskEvent(event);
222         }
223     }
224     
225     
226     /**
227      * Recycle internal state.
228      */

229     public void recycle(){
230        ;
231     }
232     
233     
234     /**
235      * Return all listeners of this <code>Task</code>.
236      *
237      * @return ArrayList containing all <code>TaskListener</code>
238      * instances registered with this <code>Task</code>
239      */

240     public ArrayList JavaDoc getTaskListeners(){
241         initListener();
242         return listeners;
243     }
244     
245
246     /**
247      * Some <code>Pipeline</code> implementation requires a instance of
248      * <code>Runnable</code> instance.
249      */

250     public void run(){
251         try{
252             doTask();
253         } catch (IOException JavaDoc ex){
254             throw new RuntimeException JavaDoc(ex);
255         }
256     }
257     
258
259     /**
260      * Declare whether this <code>Task</code> is recyclable. If so, this
261      * <code>Task</code> will be recycled after every invocation of
262      * <code>doTask()</code>.
263      */

264     public void setRecycle(boolean recycle){
265         this.recycle = recycle;
266     }
267    
268     
269     /**
270      * Return <code>true</code> if this <code>Task</code> is recyclable.
271      */

272     public boolean getRecycle(){
273         return recycle;
274     }
275
276
277     /**
278      * Return the current <code>Socket</code> used by this instance
279      * @return socket the current <code>Socket</code> used by this instance
280      */

281     protected Socket JavaDoc getSocket(){
282         return null;
283     }
284
285
286     /**
287      * Return the underlying <code>Channel</code>, independent of the NIO
288      * mode we are using.
289      */

290     private SocketChannel JavaDoc getChannel(){
291         if ( key == null ) {
292             return getSocket().getChannel();
293         } else {
294             return (SocketChannel JavaDoc)key.channel();
295         }
296     }
297      
298
299     /**
300      * Cancel the task.
301      * @param message the HTTP message to included within the html page
302      * @param code The http code to use. If null, automatically close the
303      * connection without sending an error page.
304      */

305     public void cancelTask(String JavaDoc message, String JavaDoc code){
306         SocketChannel JavaDoc channel = getChannel();
307
308         if (code != null) {
309             SelectorThread.logger().log(Level.WARNING,message);
310             try {
311                 ByteBuffer JavaDoc byteBuffer = HtmlHelper.getErrorPage(message, code);
312                 while ( byteBuffer.hasRemaining() ) {
313                     channel.write(byteBuffer);
314                 }
315             } catch (IOException JavaDoc ex){
316                 SelectorThread.logger().log(Level.FINE,"CancelTask failed", ex);
317             }
318         }
319         
320         try{
321             channel.socket().shutdownInput();
322         } catch (IOException JavaDoc ex){
323             
324         }
325         
326         try{
327             channel.socket().shutdownOutput();
328             channel.socket().close();
329             channel.close();
330             key.cancel();
331         } catch (IOException JavaDoc ex){
332             ;
333         }
334     }
335     
336     
337     /**
338      * By default, do nothing when a <code>Callable</code> is invoked.
339      */

340     public Object JavaDoc call() throws Exception JavaDoc{
341        return null;
342     }
343
344  
345 }
346
Popular Tags