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 org.springframework.core.style.StylerUtils;
019: import org.springframework.core.style.ToStringCreator;
020: import org.springframework.util.Assert;
021: import org.springframework.webflow.definition.FlowDefinition;
022:
023: /**
024: * Static factory for creating commonly used flow execution listener criteria.
025: *
026: * @see FlowExecutionListenerCriteria
027: *
028: * @author Keith Donald
029: * @author Erwin Vervaet
030: */
031: public class FlowExecutionListenerCriteriaFactory {
032:
033: /**
034: * Returns a wild card criteria that matches all flows.
035: */
036: public FlowExecutionListenerCriteria allFlows() {
037: return new WildcardFlowExecutionListenerCriteria();
038: }
039:
040: /**
041: * Returns a criteria that just matches a flow with the specified id.
042: * @param flowId the flow id to match
043: */
044: public FlowExecutionListenerCriteria flow(String flowId) {
045: return new FlowIdFlowExecutionListenerCriteria(flowId);
046: }
047:
048: /**
049: * Returns a criteria that just matches a flow if it is identified by one of
050: * the specified ids.
051: * @param flowIds the flow ids to match
052: */
053: public FlowExecutionListenerCriteria flows(String[] flowIds) {
054: return new FlowIdFlowExecutionListenerCriteria(flowIds);
055: }
056:
057: /**
058: * A flow execution listener criteria implementation that matches for all
059: * flows.
060: */
061: private static class WildcardFlowExecutionListenerCriteria
062: implements FlowExecutionListenerCriteria {
063:
064: public boolean appliesTo(FlowDefinition definition) {
065: return true;
066: }
067:
068: public String toString() {
069: return "*";
070: }
071: }
072:
073: /**
074: * A flow execution listener criteria implementation that matches flows with
075: * a specified id.
076: */
077: private static class FlowIdFlowExecutionListenerCriteria implements
078: FlowExecutionListenerCriteria {
079:
080: /**
081: * The flow ids that apply for this criteria.
082: */
083: private String[] flowIds;
084:
085: /**
086: * Create a new flow id matching flow execution listener criteria
087: * implemenation.
088: * @param flowId the flow id to match
089: */
090: public FlowIdFlowExecutionListenerCriteria(String flowId) {
091: Assert.notNull(flowId, "The flow id is required");
092: this .flowIds = new String[] { flowId };
093: }
094:
095: /**
096: * Create a new flow id matching flow execution listener criteria
097: * implemenation.
098: * @param flowIds the flow ids to match
099: */
100: public FlowIdFlowExecutionListenerCriteria(String[] flowIds) {
101: Assert.notEmpty(flowIds, "The flow id array is required");
102: this .flowIds = flowIds;
103: }
104:
105: public boolean appliesTo(FlowDefinition definition) {
106: for (int i = 0; i < flowIds.length; i++) {
107: if (flowIds[i].equals(definition.getId())) {
108: return true;
109: }
110: }
111: return false;
112: }
113:
114: public String toString() {
115: return new ToStringCreator(this ).append("flowIds",
116: StylerUtils.style(flowIds)).toString();
117: }
118: }
119: }
|