/** * Returns a criteria that just matches a flow with the specified id. * @param flowId the flow id to match */ public FlowExecutionListenerCriteria flow(String flowId) { return new FlowIdFlowExecutionListenerCriteria(flowId); }
/** * Register a {@link FlowExecutionListener} that observes the lifecycle of flow * executions launched by this executor. * @param listener the listener to be registered * @param criteria the criteria that determines the flow definitions a listener * should observe, delimited by commas or '*' for "all". * Example: 'flow1,flow2,flow3'. */ public FlowExecutorBuilder addFlowExecutionListener(FlowExecutionListener listener, String criteria) { if (this.listenerLoader == null) { this.listenerLoader = new ConditionalFlowExecutionListenerLoader(); } this.listenerLoader.addListener(listener, this.listenerCriteriaFactory.getListenerCriteria(criteria)); return this; }
/** * Determines if the listener held by this holder applies to the specified flow definition. Will do a logical OR * between the registered criteria. * @param flowDefinition the flow * @return true if yes, false otherwise */ public boolean listenerAppliesTo(FlowDefinition flowDefinition) { for (FlowExecutionListenerCriteria criteria : criteriaSet) { if (criteria.appliesTo(flowDefinition)) { return true; } } return false; } }
/** * Add a listener that will listen to executions to flows matching the specified criteria. * @param listener the listener * @param criteria the listener criteria */ public void addListener(FlowExecutionListener listener, FlowExecutionListenerCriteria criteria) { if (listener == null) { throw new IllegalArgumentException("The flow execution listener cannot be null"); } if (logger.isDebugEnabled()) { logger.debug("Adding flow execution listener " + listener + " with criteria " + criteria); } ConditionalFlowExecutionListenerHolder conditional = getHolder(listener); if (conditional == null) { conditional = new ConditionalFlowExecutionListenerHolder(listener); listeners.add(conditional); } if (criteria == null) { criteria = new FlowExecutionListenerCriteriaFactory().allFlows(); } conditional.add(criteria); }
public void testAddMultipleListeners() { FlowExecutionListener listener1 = new FlowExecutionListener() {}; FlowExecutionListener listener2 = new FlowExecutionListener() {}; loader.addListener(listener1, criteriaFactory.allFlows()); loader.addListener(listener2, criteriaFactory.allFlows()); Flow flow = new Flow("foo"); FlowExecutionListener[] listeners = loader.getListeners(flow); assertEquals(2, listeners.length); assertSame(listener1, listeners[0]); assertSame(listener2, listeners[1]); }
public void testStaticListener() { final FlowExecutionListener listener1 = new FlowExecutionListener() {}; loader = new StaticFlowExecutionListenerLoader(listener1); assertEquals(listener1, loader.getListeners(new Flow("foo"))[0]); }
public FlowExecutionListenerCriteria getListenerCriteria(String encodedCriteria) { if ("*".equals(encodedCriteria)) { return allFlows(); } else { String[] flowIds = StringUtils.commaDelimitedListToStringArray(encodedCriteria); for (int i = 0; i < flowIds.length; i++) { flowIds[i] = flowIds[i].trim(); } return flows(flowIds); } }
public void testMultipleFlowMatch() { FlowExecutionListenerCriteria c = factory.flows("foo", "bar"); assertEquals(true, c.appliesTo(new Flow("foo"))); assertEquals(true, c.appliesTo(new Flow("bar"))); assertEquals(false, c.appliesTo(new Flow("baz"))); } }
protected void setUp() { loader = new ConditionalFlowExecutionListenerLoader(); criteriaFactory = new FlowExecutionListenerCriteriaFactory(); }
/** * Returns the array of flow execution listeners for specified flow. * @param flowDefinition the flow definition associated with the execution to be listened to * @return the flow execution listeners that apply */ public FlowExecutionListener[] getListeners(FlowDefinition flowDefinition) { Assert.notNull(flowDefinition, "The Flow to load listeners for cannot be null"); List<FlowExecutionListener> listenersToAttach = new LinkedList<>(); for (ConditionalFlowExecutionListenerHolder listenerHolder : listeners) { if (listenerHolder.listenerAppliesTo(flowDefinition)) { listenersToAttach.add(listenerHolder.getListener()); } } if (logger.isDebugEnabled()) { logger.debug("Loaded [" + listenersToAttach.size() + "] of possible " + listeners.size() + " listeners for this execution request for flow '" + flowDefinition.getId() + "', the listeners to attach are " + StylerUtils.style(listenersToAttach)); } return listenersToAttach.toArray(new FlowExecutionListener[listenersToAttach.size()]); }
/** * Set the listeners to be attached to the flow execution the next time one is started. by this test. Useful for * attaching listeners that do test assertions during the execution of the flow. * @param executionListeners the listeners to attach */ protected void setFlowExecutionListeners(FlowExecutionListener[] executionListeners) { getFlowExecutionImplFactory().setExecutionListenerLoader( new StaticFlowExecutionListenerLoader(executionListeners)); }
public void testAddListenerButNoMatch() { FlowExecutionListener listener = new FlowExecutionListener() {}; loader.addListener(listener, criteriaFactory.flow("bar")); Flow flow = new Flow("foo"); FlowExecutionListener[] listeners = loader.getListeners(flow); assertEquals(0, listeners.length); } }
public FlowExecution createFlowExecution(FlowDefinition flowDefinition) { Assert.isInstanceOf(Flow.class, flowDefinition, "FlowDefinition is of the wrong type: "); if (logger.isDebugEnabled()) { logger.debug("Creating new execution of '" + flowDefinition.getId() + "'"); } FlowExecutionImpl execution = new FlowExecutionImpl((Flow) flowDefinition); execution.setAttributes(executionAttributes); execution.setListeners(executionListenerLoader.getListeners(execution.getDefinition())); execution.setKeyFactory(executionKeyFactory); return execution; }
/** * Lookup the listener criteria holder for the listener provided. * @param listener the listener * @return the holder, or null if not found */ private ConditionalFlowExecutionListenerHolder getHolder(FlowExecutionListener listener) { for (ConditionalFlowExecutionListenerHolder holder : listeners) { if (holder.getListener().equals(listener)) { return holder; } } return null; } }
public void testAddConditionalListener() { FlowExecutionListener listener = new FlowExecutionListener() {}; loader.addListener(listener, criteriaFactory.allFlows()); Flow flow = new Flow("foo"); FlowExecutionListener[] listeners = loader.getListeners(flow); assertEquals(1, listeners.length); assertSame(listener, listeners[0]); }
public void afterPropertiesSet() { listenerLoader = new ConditionalFlowExecutionListenerLoader(); for (Map.Entry<FlowExecutionListener, String> entry : listenersWithCriteria.entrySet()) { FlowExecutionListener listener = entry.getKey(); String criteria = entry.getValue(); listenerLoader.addListener(listener, listenerCriteriaFactory.getListenerCriteria(criteria)); } }
public void testStaticListeners() { final FlowExecutionListener listener1 = new FlowExecutionListener() {}; final FlowExecutionListener listener2 = new FlowExecutionListener() {}; loader = new StaticFlowExecutionListenerLoader(listener1, listener2); assertEquals(listener1, loader.getListeners(new Flow("foo"))[0]); assertEquals(listener2, loader.getListeners(new Flow("foo"))[1]); }
/** * Set a single listener to be attached to the flow execution the next time one is started by this test. Useful for * attaching a listener that does test assertions during the execution of the flow. * @param executionListener the listener to attach */ protected void setFlowExecutionListener(FlowExecutionListener executionListener) { getFlowExecutionImplFactory().setExecutionListenerLoader( new StaticFlowExecutionListenerLoader(executionListener)); }
execution.setListeners(executionListenerLoader.getListeners(execution.getDefinition())); execution.setKeyFactory(executionKeyFactory); return execution;
/** * Returns a criteria that just matches a flow if it is identified by one of the specified ids. * @param flowIds the flow ids to match */ public FlowExecutionListenerCriteria flows(String... flowIds) { return new FlowIdFlowExecutionListenerCriteria(flowIds); }