1 16 package org.springframework.webflow.execution.factory; 17 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Map.Entry; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.springframework.core.style.StylerUtils; 27 import org.springframework.util.Assert; 28 import org.springframework.util.StringUtils; 29 import org.springframework.webflow.definition.FlowDefinition; 30 import org.springframework.webflow.execution.FlowExecutionListener; 31 32 42 public class ConditionalFlowExecutionListenerLoader implements FlowExecutionListenerLoader { 43 44 47 protected final Log logger = LogFactory.getLog(getClass()); 48 49 55 private List listeners = new LinkedList (); 56 57 61 public void addListener(FlowExecutionListener listener) { 62 addListener(listener, null); 63 } 64 65 70 public void addListeners(FlowExecutionListener[] listeners, FlowExecutionListenerCriteria criteria) { 71 for (int i = 0; i < listeners.length; i++) { 72 addListener(listeners[i], criteria); 73 } 74 } 75 76 82 public void addListener(FlowExecutionListener listener, FlowExecutionListenerCriteria criteria) { 83 if (listener == null) { 84 return; 85 } 86 if (logger.isDebugEnabled()) { 87 logger.debug("Adding flow execution listener " + listener + " with criteria " + criteria); 88 } 89 ConditionalFlowExecutionListenerHolder conditional = getHolder(listener); 90 if (conditional == null) { 91 conditional = new ConditionalFlowExecutionListenerHolder(listener); 92 listeners.add(conditional); 93 } 94 if (criteria == null) { 95 criteria = new FlowExecutionListenerCriteriaFactory().allFlows(); 96 } 97 conditional.add(criteria); 98 } 99 100 109 public void setListeners(Map listenersWithCriteria) { 110 removeAllListeners(); 111 for (Iterator it = listenersWithCriteria.entrySet().iterator(); it.hasNext(); ) { 112 Entry entry = (Entry)it.next(); 113 Assert.isInstanceOf(FlowExecutionListener.class, entry.getKey(), 114 "The key in the listenersWithCriteria map needs to be a FlowExecutionListener object"); 115 FlowExecutionListener listener = (FlowExecutionListener)entry.getKey(); 116 FlowExecutionListenerCriteria criteria = null; 117 if (entry.getValue() instanceof String ) { 118 criteria = getCriteria((String )entry.getValue()); 119 } 120 else if (entry.getValue() instanceof FlowExecutionListenerCriteria) { 121 criteria = (FlowExecutionListenerCriteria)entry.getValue(); 122 } 123 else if (entry.getValue() != null) { 124 throw new IllegalArgumentException ( 125 "The value in the listenersWithCriteria map needs to be a " + 126 "String or a FlowExecutionListenerCriteria object"); 127 } 128 addListener(listener, criteria); 129 } 130 } 131 132 137 public boolean containsListener(FlowExecutionListener listener) { 138 Iterator it = listeners.iterator(); 139 while (it.hasNext()) { 140 ConditionalFlowExecutionListenerHolder h = (ConditionalFlowExecutionListenerHolder)it.next(); 141 if (h.getListener().equals(listener)) { 142 return true; 143 } 144 } 145 return false; 146 } 147 148 152 public void removeListener(FlowExecutionListener listener) { 153 Iterator it = listeners.iterator(); 154 while (it.hasNext()) { 155 ConditionalFlowExecutionListenerHolder h = (ConditionalFlowExecutionListenerHolder)it.next(); 156 if (h.getListener().equals(listener)) { 157 it.remove(); 158 } 159 } 160 } 161 162 165 public void removeAllListeners() { 166 listeners.clear(); 167 } 168 169 174 public void removeListenerCriteria(FlowExecutionListener listener, FlowExecutionListenerCriteria criteria) { 175 if (containsListener(listener)) { 176 ConditionalFlowExecutionListenerHolder listenerHolder = getHolder(listener); 177 listenerHolder.remove(criteria); 178 if (listenerHolder.isCriteriaSetEmpty()) { 179 removeListener(listener); 180 } 181 } 182 } 183 184 190 public FlowExecutionListener[] getListeners(FlowDefinition flowDefinition) { 191 Assert.notNull(flowDefinition, "The Flow to load listeners for cannot be null"); 192 List listenersToAttach = new LinkedList (); 193 for (Iterator it = listeners.iterator(); it.hasNext();) { 194 ConditionalFlowExecutionListenerHolder listenerHolder = (ConditionalFlowExecutionListenerHolder)it.next(); 195 if (listenerHolder.listenerAppliesTo(flowDefinition)) { 196 listenersToAttach.add(listenerHolder.getListener()); 197 } 198 } 199 if (logger.isDebugEnabled()) { 200 logger.debug("Loaded [" + listenersToAttach.size() + "] of possible " + listeners.size() 201 + " listeners for this execution request for flow '" + flowDefinition.getId() 202 + "', the listeners to attach are " + StylerUtils.style(listenersToAttach)); 203 } 204 return (FlowExecutionListener[])listenersToAttach.toArray(new FlowExecutionListener[listenersToAttach.size()]); 205 } 206 207 209 214 protected ConditionalFlowExecutionListenerHolder getHolder(FlowExecutionListener listener) { 215 Iterator it = listeners.iterator(); 216 while (it.hasNext()) { 217 ConditionalFlowExecutionListenerHolder next = (ConditionalFlowExecutionListenerHolder)it.next(); 218 if (next.getListener().equals(listener)) { 219 return next; 220 } 221 } 222 return null; 223 } 224 225 229 protected FlowExecutionListenerCriteria getCriteria(String value) { 230 if ("*".equals(value)) { 231 return new FlowExecutionListenerCriteriaFactory().allFlows(); 232 } 233 else { 234 String [] flowIds = StringUtils.commaDelimitedListToStringArray(value); 235 for (int i = 0; i < flowIds.length; i++) { 236 flowIds[i] = flowIds[i].trim(); 237 } 238 return new FlowExecutionListenerCriteriaFactory().flows(flowIds); 239 } 240 } 241 242 } | Popular Tags |