1 20 package org.enhydra.barracuda.core.event; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.apache.log4j.*; 26 27 import org.enhydra.barracuda.plankton.data.*; 28 29 64 public class DefaultDispatchQueue implements DispatchQueue { 65 66 protected static final Logger logger = Logger.getLogger(DefaultDispatchQueue.class.getName()); 68 69 protected boolean requiresResponse = false; 71 protected boolean responseHandled = false; 72 protected List controlList = new ArrayList(); 73 protected List viewList = new ArrayList(); 74 protected List procList = new ArrayList(); 75 76 84 public DefaultDispatchQueue(boolean irequiresResponse) { 85 setRequiresResponse(irequiresResponse); } 88 89 94 public boolean requiresResponse() { 95 return requiresResponse; 96 } 97 98 104 public void setRequiresResponse(boolean val) { 105 if (logger.isDebugEnabled()) logger.debug("Setting requiresResponse:"+val); 106 requiresResponse = val; 107 } 108 109 110 115 public boolean responseHandled() { 116 return responseHandled; 117 } 118 119 124 public void setResponseHandled(boolean val) { 125 if (logger.isDebugEnabled()) logger.debug("Setting responseHandled:"+val); 126 responseHandled = val; 127 } 128 129 132 void resetQueue() { 133 if (logger.isDebugEnabled()) logger.debug("Resetting queue"); 134 controlList = new ArrayList(); 135 viewList = new ArrayList(); 136 procList = new ArrayList(); 137 } 138 139 147 public void addEvent(BaseEvent baseEvent) { 148 if (baseEvent==null) return; 149 removeEvent(baseEvent); 150 if (baseEvent instanceof ViewEvent) { 151 if (logger.isInfoEnabled()) logger.info("Adding ViewEvent:"+baseEvent); 152 viewList.add(baseEvent); 153 } else { 154 if (logger.isInfoEnabled()) logger.info("Adding ControlEvent:"+baseEvent); 155 controlList.add(baseEvent); 156 } 157 } 158 159 165 void removeEvent(BaseEvent baseEvent) { 166 if (baseEvent==null) return; 167 if (logger.isInfoEnabled()) logger.info("Removing all instances of:"+baseEvent); 168 List list = (baseEvent instanceof ViewEvent ? viewList : controlList); 169 Object items[] = list.toArray(); 170 for (int max=items.length, i=max-1; i>-1; i--) { 171 if (baseEvent.equals(items[i])) list.remove(i); 172 } 173 } 174 175 178 boolean hasNextControlEvent() { 179 return (controlList.size()>0); 180 } 181 182 185 boolean hasNextViewEvent() { 186 return (viewList.size()>0); 187 } 188 189 194 BaseEvent peekNextControlEvent() { 195 return (hasNextControlEvent() ? (BaseEvent) controlList.get(0) : null); 196 } 197 198 203 ViewEvent peekNextViewEvent() { 204 return (hasNextViewEvent() ? (ViewEvent) viewList.get(0) : null); 205 } 206 207 212 BaseEvent getNextControlEvent() { 213 BaseEvent controlEvent = peekNextControlEvent(); 214 controlList.remove(0); 215 procList.add(controlEvent); 216 if (logger.isInfoEnabled()) logger.info("Getting next ControlEvent:"+controlEvent); 217 return controlEvent; 218 } 219 220 225 ViewEvent getNextViewEvent() { 226 ViewEvent viewEvent = peekNextViewEvent(); 227 viewList.remove(0); 228 procList.add(viewEvent); 229 if (logger.isInfoEnabled()) logger.info("Getting next ViewEvent:"+viewEvent); 230 return viewEvent; 231 } 232 233 238 public int numberOfEventsRemaining() { 239 return controlList.size() + viewList.size(); 240 } 241 242 248 public List listRemainingEvents() { 249 ArrayList list = new ArrayList(controlList); 250 list.addAll(viewList); 251 return list; 252 } 253 254 259 List listControlEvents() { 260 return new ArrayList(controlList); 261 } 262 263 268 List listViewEvents() { 269 return new ArrayList(viewList); 270 } 271 272 277 public int numberOfEventsProcessed() { 278 return procList.size(); 279 } 280 281 286 public List listProcessedEvents() { 287 return new ArrayList(procList); 288 } 289 290 293 void markEventsHandled() { 294 if (logger.isDebugEnabled()) logger.debug("Marking all events handled"); 295 Iterator it = null; 296 for (int i=0; i<2; i++) { 297 List curList = (i==0 ? controlList : viewList); 298 it = curList.iterator(); 299 while (it.hasNext()) { 300 BaseEvent be = (BaseEvent) it.next(); 301 be.setHandled(true); 302 } 303 } 304 } 305 306 307 314 public void dumpEventQueueToConsole(int targetDepth) { 315 if (logger.isDebugEnabled()) { 316 logger.debug("Dumping Event Queue contents:"); 317 logger.debug("Request Events:"); 318 CollectionsUtil.printStackTrace(controlList, targetDepth+1, logger, null); 319 logger.debug("Response Events:"); 320 CollectionsUtil.printStackTrace(viewList, targetDepth+1, logger, null); 321 } 322 } 323 } 324 | Popular Tags |