KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > support > ReflectionInterpreter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.beanflow.support;
18
19 import org.apache.servicemix.beanflow.Workflow;
20 import org.apache.servicemix.beanflow.WorkflowStep;
21
22 import java.lang.reflect.Method JavaDoc;
23
24 /**
25  * An interpreter strategy which detects step objects of types
26  *
27  * @{link Runnable} or {@link WorkflowStep} otherwise the step object is turned
28  * into a String and reflection is used to map the step to a method on
29  * the workflow object
30  *
31  * Thanks to Brian Goetz for this idea :)
32  *
33  * @version $Revision: $
34  */

35 public class ReflectionInterpreter<T> implements Interpreter<T> {
36
37     protected static final Class JavaDoc[] NO_TYPE_ARGUMENTS = {};
38     protected static final Object JavaDoc[] NO_PARAMETER_ARGUMENTS = {};
39
40     @SuppressWarnings JavaDoc("unchecked")
41     public void executeStep(T step, Workflow<T> workflow) {
42         if (step instanceof WorkflowStep) {
43             WorkflowStep<T> workflowStep = (WorkflowStep<T>) step;
44             T nextStep = workflowStep.execute(workflow);
45             if (nextStep != null) {
46                 workflow.addStep(nextStep);
47             }
48             else {
49                 workflow.suspend();
50             }
51         }
52         else if (step instanceof Runnable JavaDoc) {
53             Runnable JavaDoc runnable = (Runnable JavaDoc) step;
54             runnable.run();
55             goToNextSequence(step, workflow);
56         }
57         else if (step != null) {
58             String JavaDoc name = step.toString();
59             executeNamedStep(name, workflow);
60         }
61     }
62
63     /**
64      * If the workflow has been told to go to another step do nothing, else lets
65      * go to the next enumeration if we are not suspended, otherwise lets
66      * suspend.
67      *
68      * @param workflow
69      * @param nextStep
70      */

71     protected void goToNextSequence(T nextStep, Workflow<T> workflow) {
72         if (nextStep instanceof Enum JavaDoc) {
73             Enum JavaDoc step = (Enum JavaDoc) nextStep;
74             try {
75                 Object JavaDoc[] enumValues = EnumHelper.getEnumValues(step.getClass());
76                 int index = step.ordinal();
77                 if (++index < enumValues.length) {
78                     workflow.addStep((T) enumValues[index]);
79                 }
80                 else {
81                     workflow.stop();
82                 }
83             }
84             catch (Exception JavaDoc e) {
85                 workflow.fail("Could not extract the values of the enum: " + nextStep + " due to: " + e, e);
86             }
87         }
88         else {
89             workflow.suspend();
90         }
91     }
92
93     public void executeNamedStep(String JavaDoc step, Workflow<T> workflow) {
94         Class JavaDoc<? extends Workflow> type = workflow.getClass();
95         try {
96             Method JavaDoc method = type.getMethod(step, NO_TYPE_ARGUMENTS);
97             Object JavaDoc answer = method.invoke(workflow, NO_PARAMETER_ARGUMENTS);
98             handleStepResult(step, workflow, answer);
99         }
100         catch (Exception JavaDoc e) {
101             workflow.onStepException(step, e);
102         }
103     }
104
105     public void validateStepsExist(Object JavaDoc[] stepValues, Workflow<T> workflow) {
106         Class JavaDoc<? extends Workflow> type = workflow.getClass();
107         for (int i = 0; i < stepValues.length; i++) {
108             Object JavaDoc value = stepValues[i];
109             if (!isValidStep(value)) {
110                 String JavaDoc step = value.toString();
111                 try {
112                     type.getMethod(step, NO_TYPE_ARGUMENTS);
113                 }
114                 catch (Exception JavaDoc e) {
115                     workflow.fail("No " + step + "() method is available in class: " + type.getName()
116                             + " so unable to bind the code to the enumeration of steps", e);
117                 }
118             }
119         }
120     }
121
122     /**
123      * Returns true if the step object is capable of being run directly as
124      * opposed to via reflection
125      */

126     protected boolean isValidStep(Object JavaDoc value) {
127         return value instanceof WorkflowStep || value instanceof Runnable JavaDoc;
128     }
129
130     @SuppressWarnings JavaDoc("unchecked")
131     protected void handleStepResult(String JavaDoc step, Workflow workflow, Object JavaDoc result) {
132         if (result != null) {
133             workflow.addStep(result);
134         }
135         else {
136             workflow.suspend();
137         }
138     }
139 }
140
Popular Tags