1 23 package com.sun.enterprise.web.connector.grizzly; 24 25 import java.io.IOException ; 26 import java.net.Socket ; 27 import java.nio.ByteBuffer ; 28 import java.nio.channels.SelectionKey ; 29 import java.nio.channels.SocketChannel ; 30 import java.util.ArrayList ; 31 import java.util.logging.Level ; 32 33 import org.apache.coyote.RequestGroupInfo; 34 35 40 public abstract class TaskBase implements Task, TaskListener{ 41 42 43 47 protected int type; 48 49 50 53 protected ArrayList <TaskListener> listeners; 54 55 56 60 protected Pipeline pipeline; 61 62 63 66 protected SelectionKey key; 67 68 69 72 protected boolean recycle = true; 73 74 75 78 protected SelectorThread selectorThread; 79 80 81 83 public int getType(){ 84 return type; 85 } 86 87 88 91 public void setSelectorThread(SelectorThread selectorThread){ 92 this.selectorThread = selectorThread; 93 } 94 95 96 99 public SelectorThread getSelectorThread(){ 100 return selectorThread; 101 } 102 103 104 107 public void setPipeline(Pipeline pipeline){ 108 this.pipeline = pipeline; 109 } 110 111 112 115 public Pipeline getPipeline(){ 116 return pipeline; 117 } 118 119 120 123 public void setSelectionKey(SelectionKey key){ 124 this.key = key; 125 } 126 127 128 131 public SelectionKey getSelectionKey(){ 132 return key; 133 } 134 135 136 139 public RequestGroupInfo getRequestGroupInfo() { 140 return (selectorThread != null? 141 selectorThread.getRequestGroupInfo() : null); 142 } 143 144 145 149 public boolean isMonitoringEnabled(){ 150 return (selectorThread != null ? 151 selectorThread.isMonitoringEnabled() : false); 152 } 153 154 155 158 public KeepAliveStats getKeepAliveStats() { 159 return (selectorThread != null? 160 selectorThread.getKeepAliveStats() : null); 161 } 162 163 164 169 public void execute(){ 170 if (pipeline != null){ 171 pipeline.addTask(this); 172 } else { 173 run(); 174 } 175 } 176 177 178 180 private void initListener(){ 181 if ( listeners == null ){ 182 listeners = new ArrayList <TaskListener>(); 183 } 184 } 185 186 187 190 public void addTaskListener(TaskListener task){ 191 initListener(); 192 listeners.add(task); 193 } 194 195 196 200 public void removeTaskListener(TaskListener task){ 201 if (listeners == null) return; 202 listeners.remove(task); 203 } 204 205 206 209 public void clearTaskListeners(){ 210 if (listeners == null) return; 211 listeners.clear(); 212 } 213 214 215 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 229 public void recycle(){ 230 ; 231 } 232 233 234 240 public ArrayList getTaskListeners(){ 241 initListener(); 242 return listeners; 243 } 244 245 246 250 public void run(){ 251 try{ 252 doTask(); 253 } catch (IOException ex){ 254 throw new RuntimeException (ex); 255 } 256 } 257 258 259 264 public void setRecycle(boolean recycle){ 265 this.recycle = recycle; 266 } 267 268 269 272 public boolean getRecycle(){ 273 return recycle; 274 } 275 276 277 281 protected Socket getSocket(){ 282 return null; 283 } 284 285 286 290 private SocketChannel getChannel(){ 291 if ( key == null ) { 292 return getSocket().getChannel(); 293 } else { 294 return (SocketChannel )key.channel(); 295 } 296 } 297 298 299 305 public void cancelTask(String message, String code){ 306 SocketChannel channel = getChannel(); 307 308 if (code != null) { 309 SelectorThread.logger().log(Level.WARNING,message); 310 try { 311 ByteBuffer byteBuffer = HtmlHelper.getErrorPage(message, code); 312 while ( byteBuffer.hasRemaining() ) { 313 channel.write(byteBuffer); 314 } 315 } catch (IOException ex){ 316 SelectorThread.logger().log(Level.FINE,"CancelTask failed", ex); 317 } 318 } 319 320 try{ 321 channel.socket().shutdownInput(); 322 } catch (IOException ex){ 323 324 } 325 326 try{ 327 channel.socket().shutdownOutput(); 328 channel.socket().close(); 329 channel.close(); 330 key.cancel(); 331 } catch (IOException ex){ 332 ; 333 } 334 } 335 336 337 340 public Object call() throws Exception { 341 return null; 342 } 343 344 345 } 346 | Popular Tags |