1 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 ; 33 import java.util.concurrent.ConcurrentLinkedQueue ; 34 35 50 public class DefaultAsyncHandler implements AsyncHandler{ 51 52 55 private ConcurrentLinkedQueue <AsyncProcessorTask> 56 asyncProcessors = new ConcurrentLinkedQueue <AsyncProcessorTask>(); 57 58 59 62 private ConcurrentLinkedQueue <AsyncProcessorTask> 63 interrruptedQueue = new ConcurrentLinkedQueue <AsyncProcessorTask>(); 64 65 66 70 private ArrayList <AsyncFilter> asyncFilters = 71 new ArrayList <AsyncFilter>(); 72 73 74 77 private String asyncExecutorClassName 78 = "com.sun.enterprise.web.connector.grizzly.async.DefaultAsyncExecutor"; 79 81 82 public DefaultAsyncHandler() { 83 } 84 85 86 89 private AsyncProcessorTask newAsyncProcessorTask(){ 90 AsyncProcessorTask asyncProcessorTask = new AsyncProcessorTask(); 91 asyncProcessorTask 92 .setAsyncExecutor(newAsyncExecutor(asyncProcessorTask)); 93 return asyncProcessorTask; 94 } 95 96 97 100 private AsyncExecutor newAsyncExecutor(AsyncProcessorTask asyncProcessorTask){ 101 102 Class className = null; 103 AsyncExecutor asyncExecutor = null; 104 try{ 105 className = Class.forName(asyncExecutorClassName); 106 asyncExecutor = (AsyncExecutor)className.newInstance(); 107 } catch (ClassNotFoundException ex){ 108 throw new RuntimeException (ex); 109 } catch (InstantiationException ex){ 110 throw new RuntimeException (ex); 111 } catch (IllegalAccessException ex){ 112 throw new RuntimeException (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 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 143 144 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 (); 161 } else if ( apt == null ){ 162 apt = (AsyncProcessorTask)task; 163 } 164 apt.execute(); 165 } 166 167 168 171 public void returnTask(AsyncProcessorTask asyncProcessorTask){ 172 asyncProcessors.offer(asyncProcessorTask); 173 } 174 175 178 public void addToInterruptedQueue(Task task){ 179 interrruptedQueue.offer((AsyncProcessorTask)task); 180 } 181 182 183 186 public void removeFromInterruptedQueue(Task task){ 187 interrruptedQueue.remove(task); 188 } 189 190 191 194 public void setAsyncExecutorClassName(String asyncExecutorClassName){ 195 this.asyncExecutorClassName = asyncExecutorClassName; 196 } 197 198 199 202 public String getAsyncExecutorClassName(){ 203 return asyncExecutorClassName; 204 } 205 206 207 210 public void addAsyncFilter(AsyncFilter asyncFilter) { 211 asyncFilters.add(asyncFilter); 212 } 213 214 215 218 public boolean removeAsyncFilter(AsyncFilter asyncFilter) { 219 return asyncFilters.remove(asyncFilter); 220 } 221 } 222 | Popular Tags |