01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.springframework.webflow.action;
17:
18: import java.lang.reflect.Method;
19:
20: /**
21: * Helper that selects the {@link ResultEventFactory} to use for
22: * a particular result object.
23: *
24: * @see EvaluateAction
25: * @see BeanInvokingActionFactory
26: *
27: * @author Keith Donald
28: */
29: public class ResultEventFactorySelector {
30:
31: /**
32: * The event factory instance for mapping a return value to a success event.
33: */
34: private SuccessEventFactory successEventFactory = new SuccessEventFactory();
35:
36: /**
37: * The event factory instance for mapping a result object to an event, using
38: * the type of the result object as the mapping criteria.
39: */
40: private ResultObjectBasedEventFactory resultObjectBasedEventFactory = new ResultObjectBasedEventFactory();
41:
42: /**
43: * Select the appropriate result event factory for attempts to invoke the
44: * given method.
45: * @param method the method
46: * @return the result event factory
47: */
48: public ResultEventFactory forMethod(Method method) {
49: return forType(method.getReturnType());
50: }
51:
52: /**
53: * Select the appropriate result event factory for the given result.
54: * @param result the result
55: * @return the result event factory
56: */
57: public ResultEventFactory forResult(Object result) {
58: if (result == null) {
59: return successEventFactory;
60: } else {
61: return forType(result.getClass());
62: }
63: }
64:
65: /**
66: * Select the appropriate result event factory for given result type.
67: * This implementation returns {@link ResultObjectBasedEventFactory} if the
68: * type is {@link ResultObjectBasedEventFactory#isMappedValueType(Class) mapped}
69: * by that result event factory, otherwise {@link SuccessEventFactory} is
70: * returned.
71: * @param resultType the result type
72: * @return the result event factory
73: */
74: protected ResultEventFactory forType(Class resultType) {
75: if (resultObjectBasedEventFactory.isMappedValueType(resultType)) {
76: return resultObjectBasedEventFactory;
77: } else {
78: return successEventFactory;
79: }
80: }
81: }
|