001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.execution.factory;
017:
018: import java.util.Iterator;
019: import java.util.Set;
020:
021: import org.springframework.core.CollectionFactory;
022: import org.springframework.util.Assert;
023: import org.springframework.webflow.definition.FlowDefinition;
024: import org.springframework.webflow.execution.FlowExecutionListener;
025:
026: /**
027: * A holder that holds a listener plus a set of criteria defining the flows in
028: * which that listener applies.
029: * <p>
030: * This is an internal helper class used by the {@link ConditionalFlowExecutionListenerLoader}.
031: *
032: * @see ConditionalFlowExecutionListenerLoader
033: *
034: * @author Keith Donald
035: */
036: class ConditionalFlowExecutionListenerHolder {
037:
038: /**
039: * The held listener.
040: */
041: private FlowExecutionListener listener;
042:
043: /**
044: * The listener criteria set.
045: */
046: private Set criteriaSet = CollectionFactory
047: .createLinkedSetIfPossible(3);
048:
049: /**
050: * Create a new conditional flow execution listener holder.
051: * @param listener the listener to hold
052: */
053: public ConditionalFlowExecutionListenerHolder(
054: FlowExecutionListener listener) {
055: Assert.notNull(listener, "The listener is required");
056: this .listener = listener;
057: }
058:
059: /**
060: * Returns the held listener.
061: */
062: public FlowExecutionListener getListener() {
063: return listener;
064: }
065:
066: /**
067: * Add given criteria.
068: */
069: public void add(FlowExecutionListenerCriteria criteria) {
070: criteriaSet.add(criteria);
071: }
072:
073: /**
074: * Remove given criteria. If not present, does nothing.
075: */
076: public void remove(FlowExecutionListenerCriteria criteria) {
077: criteriaSet.remove(criteria);
078: }
079:
080: /**
081: * Are there any criteria registered?
082: */
083: public boolean isCriteriaSetEmpty() {
084: return criteriaSet.isEmpty();
085: }
086:
087: /**
088: * Determines if the listener held by this holder applies to the specified
089: * flow definition. Will do a logical OR between the registered criteria.
090: * @param flowDefinition the flow
091: * @return true if yes, false otherwise
092: */
093: public boolean listenerAppliesTo(FlowDefinition flowDefinition) {
094: Iterator it = criteriaSet.iterator();
095: while (it.hasNext()) {
096: FlowExecutionListenerCriteria criteria = (FlowExecutionListenerCriteria) it
097: .next();
098: if (criteria.appliesTo(flowDefinition)) {
099: return true;
100: }
101: }
102: return false;
103: }
104: }
|