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.definition.registry;
017:
018: import java.io.Serializable;
019:
020: import org.springframework.core.io.Resource;
021: import org.springframework.core.style.ToStringCreator;
022: import org.springframework.util.Assert;
023: import org.springframework.webflow.core.collection.AttributeMap;
024: import org.springframework.webflow.core.collection.CollectionUtils;
025:
026: /**
027: * A pointer to an externalized flow definition resource. Adds assigned
028: * identification information about the resource including the flow id and
029: * attributes.
030: *
031: * @see ExternalizedFlowDefinitionRegistrar
032: *
033: * @author Keith Donald
034: */
035: public class FlowDefinitionResource implements Serializable {
036:
037: /**
038: * The identifier to assign to the flow definition.
039: */
040: private String id;
041:
042: /**
043: * Attributes that can be used to affect flow construction.
044: */
045: private AttributeMap attributes;
046:
047: /**
048: * The externalized location of the flow definition resource.
049: */
050: private Resource location;
051:
052: /**
053: * Creates a new externalized flow definition resource. The flow id assigned will be
054: * the same name as the externalized resource's filename, excluding the extension.
055: * @param location the flow resource location
056: */
057: public FlowDefinitionResource(Resource location) {
058: init(conventionalFlowId(location), location, null);
059: }
060:
061: /**
062: * Creates a new externalized flow definition resource. The flow id assigned will be
063: * the same name as the externalized resource's filename, excluding the extension.
064: * @param location the flow resource location
065: * @param attributes flow definition attributes to be assigned
066: */
067: public FlowDefinitionResource(Resource location,
068: AttributeMap attributes) {
069: init(conventionalFlowId(location), location, attributes);
070: }
071:
072: /**
073: * Creates a new externalized flow definition.
074: * @param id the flow id to be assigned
075: * @param location the flow resource location
076: */
077: public FlowDefinitionResource(String id, Resource location) {
078: init(id, location, null);
079: }
080:
081: /**
082: * Creates a new externalized flow definition.
083: * @param id the flow id to be assigned
084: * @param location the flow resource location
085: * @param attributes flow definition attributes to be assigned
086: */
087: public FlowDefinitionResource(String id, Resource location,
088: AttributeMap attributes) {
089: init(id, location, attributes);
090: }
091:
092: /**
093: * Returns the identifier to assign to the flow definition.
094: */
095: public String getId() {
096: return id;
097: }
098:
099: /**
100: * Returns the externalized flow definition resource location.
101: */
102: public Resource getLocation() {
103: return location;
104: }
105:
106: /**
107: * Returns arbitrary flow definition attributes.
108: */
109: public AttributeMap getAttributes() {
110: return attributes;
111: }
112:
113: public boolean equals(Object o) {
114: if (!(o instanceof FlowDefinitionResource)) {
115: return false;
116: }
117: FlowDefinitionResource other = (FlowDefinitionResource) o;
118: return id.equals(other.id) && location.equals(other.location);
119: }
120:
121: public int hashCode() {
122: return id.hashCode() + location.hashCode();
123: }
124:
125: // internal helpers
126:
127: /**
128: * Initialize this object.
129: */
130: private void init(String id, Resource location,
131: AttributeMap attributes) {
132: Assert
133: .hasText(id,
134: "The id of the externalized flow definition is required");
135: Assert
136: .notNull(location,
137: "The location of the externalized flow definition is required");
138: this .id = id;
139: this .location = location;
140: if (attributes != null) {
141: this .attributes = attributes;
142: } else {
143: this .attributes = CollectionUtils.EMPTY_ATTRIBUTE_MAP;
144: }
145: }
146:
147: // public utilities
148:
149: /**
150: * Returns the flow id assigned to the flow definition contained in given resource.
151: * By convention this will be the filename of the resource, excluding extension.
152: * @see FlowDefinitionResource#FlowDefinitionResource(Resource)
153: * @see FlowDefinitionResource#FlowDefinitionResource(Resource, AttributeMap)
154: * @since 1.0.1
155: */
156: public static String conventionalFlowId(Resource location) {
157: String fileName = location.getFilename();
158: int extensionIndex = fileName.lastIndexOf('.');
159: if (extensionIndex != -1) {
160: return fileName.substring(0, extensionIndex);
161: } else {
162: return fileName;
163: }
164: }
165:
166: public String toString() {
167: return new ToStringCreator(this ).append("id", id).append(
168: "location", location).append("attributes", attributes)
169: .toString();
170: }
171: }
|