KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > action > ResultEventFactorySelector


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
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 JavaDoc 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 JavaDoc result) {
58         if (result == null) {
59             return successEventFactory;
60         }
61         else {
62             return forType(result.getClass());
63         }
64     }
65     
66     /**
67      * Select the appropriate result event factory for given result type.
68      * This implementation returns {@link ResultObjectBasedEventFactory} if the
69      * type is {@link ResultObjectBasedEventFactory#isMappedValueType(Class) mapped}
70      * by that result event factory, otherwise {@link SuccessEventFactory} is
71      * returned.
72      * @param resultType the result type
73      * @return the result event factory
74      */

75     protected ResultEventFactory forType(Class JavaDoc resultType) {
76         if (resultObjectBasedEventFactory.isMappedValueType(resultType)) {
77             return resultObjectBasedEventFactory;
78         }
79         else {
80             return successEventFactory;
81         }
82     }
83 }
Popular Tags