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.ProcessorTask; 29 import com.sun.enterprise.web.connector.grizzly.SelectorThread; 30 import java.util.ArrayList ; 31 import java.util.StringTokenizer ; 32 import java.util.logging.Level ; 33 34 43 public class DefaultAsyncExecutor implements AsyncExecutor{ 44 45 private final static String ASYNC_FILTER = 46 "com.sun.enterprise.web.connector.grizzly.asyncFilters"; 47 48 49 53 private AsyncProcessorTask asyncProcessorTask; 54 55 56 59 private ProcessorTask processorTask; 60 61 62 66 private static AsyncFilter[] sharedAsyncFilters = null;; 67 68 69 73 private ArrayList <AsyncFilter> asyncFilters = 74 new ArrayList <AsyncFilter>(); 75 76 77 80 private boolean invokeFilter = true; 81 82 83 86 static { 87 loadFilters(); 88 } 89 90 91 public DefaultAsyncExecutor(){ 92 init(); 93 } 94 95 96 private void init(){ 97 if (sharedAsyncFilters != null){ 98 for (AsyncFilter o : sharedAsyncFilters){ 99 asyncFilters.add(o); 100 } 101 } 102 103 } 104 105 107 111 public boolean preExecute() throws Exception { 112 processorTask = asyncProcessorTask.getProcessorTask(); 113 processorTask.preProcess(); 114 processorTask.parseRequest(); 115 return true; 116 } 117 118 119 124 public boolean interrupt() throws Exception { 125 if ( asyncFilters == null || asyncFilters.size() == 0 ) { 126 processorTask.invokeAdapter(); 127 return true; 128 } else { 129 processorTask.getAsyncHandler() 130 .addToInterruptedQueue(asyncProcessorTask); 131 132 return invokeFilters(); 133 } 134 } 135 136 137 140 private boolean invokeFilters(){ 141 boolean continueExec = true; 142 for (AsyncFilter asf: asyncFilters){ 143 continueExec = asf.doFilter(this); 144 if ( !continueExec ){ 145 break; 146 } 147 } 148 return continueExec; 149 } 150 151 152 156 public boolean postExecute() throws Exception { 157 processorTask.postResponse(); 158 processorTask.postProcess(); 159 processorTask.terminateProcess(); 160 161 processorTask = null; 163 return false; 164 } 165 166 167 170 public void setAsyncProcessorTask(AsyncProcessorTask asyncProcessorTask){ 171 this.asyncProcessorTask = asyncProcessorTask; 172 } 173 174 175 178 public AsyncProcessorTask getAsyncProcessorTask(){ 179 return asyncProcessorTask; 180 } 181 182 183 185 186 189 protected static void loadFilters(){ 190 if ( System.getProperty(ASYNC_FILTER) != null){ 191 StringTokenizer st = new StringTokenizer ( 192 System.getProperty(ASYNC_FILTER),","); 193 194 sharedAsyncFilters = new AsyncFilter[st.countTokens()]; 195 int i = 0; 196 while (st.hasMoreTokens()){ 197 AsyncFilter asf = loadInstance(st.nextToken()); 198 if ( asf != null ) 199 sharedAsyncFilters[i++] = asf; 200 } 201 } 202 } 203 204 205 208 private static AsyncFilter loadInstance(String property){ 209 Class className = null; 210 try{ 211 className = Class.forName(property); 212 return (AsyncFilter)className.newInstance(); 213 } catch (ClassNotFoundException ex){ 214 SelectorThread.logger().log(Level.WARNING,ex.getMessage(),ex); 215 } catch (InstantiationException ex){ 216 SelectorThread.logger().log(Level.WARNING,ex.getMessage(),ex); 217 } catch (IllegalAccessException ex){ 218 SelectorThread.logger().log(Level.WARNING,ex.getMessage(),ex); 219 } 220 return null; 221 } 222 223 224 227 public void addAsyncFilter(AsyncFilter asyncFilter) { 228 asyncFilters.add(asyncFilter); 229 } 230 231 232 235 public boolean removeAsyncFilter(AsyncFilter asyncFilter) { 236 return asyncFilters.remove(asyncFilter); 237 } 238 } 239 | Popular Tags |