1 package org.apache.helix.participant.statemachine;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Method;
23 import java.util.Arrays;
24
25 import org.apache.helix.NotificationContext;
26 import org.apache.helix.model.Message;
27
28
29
30
31
32
33
34 public class StateModelParser
35 {
36
37 public Method getMethodForTransition(Class<? extends StateModel> clazz,
38 String fromState, String toState, Class<?>[] paramTypes)
39 {
40 Method method = getMethodForTransitionUsingAnnotation(clazz, fromState,
41 toState, paramTypes);
42 if (method == null)
43 {
44 method = getMethodForTransitionByConvention(clazz, fromState, toState,
45 paramTypes);
46 }
47 return method;
48 }
49
50
51
52
53
54
55
56
57
58
59
60 public Method getMethodForTransitionByConvention(
61 Class<? extends StateModel> clazz, String fromState, String toState,
62 Class<?>[] paramTypes)
63 {
64 Method methodToInvoke = null;
65 String methodName = "onBecome" + toState + "From" + fromState;
66 if (fromState.equals("*"))
67 {
68 methodName = "onBecome" + toState;
69 }
70
71 Method[] methods = clazz.getMethods();
72 for (Method method : methods)
73 {
74 if (method.getName().equalsIgnoreCase(methodName))
75 {
76 Class<?>[] parameterTypes = method.getParameterTypes();
77 if (parameterTypes.length == 2
78 && parameterTypes[0].equals(Message.class)
79 && parameterTypes[1].equals(NotificationContext.class))
80 {
81 methodToInvoke = method;
82 break;
83 }
84 }
85 }
86 return methodToInvoke;
87
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public Method getMethodForTransitionUsingAnnotation(
103 Class<? extends StateModel> clazz, String fromState, String toState,
104 Class<?>[] paramTypes)
105 {
106 StateModelInfo stateModelInfo = clazz.getAnnotation(StateModelInfo.class);
107 Method methodToInvoke = null;
108 if (stateModelInfo != null)
109 {
110 Method[] methods = clazz.getMethods();
111 if (methods != null)
112 {
113 for (Method method : methods)
114 {
115 Transition annotation = method.getAnnotation(Transition.class);
116 if (annotation != null)
117 {
118 boolean matchesFrom = "*".equals(annotation.from()) || annotation.from().equalsIgnoreCase(fromState);
119 boolean matchesTo = "*".equals(annotation.to()) || annotation.to().equalsIgnoreCase(toState);
120 boolean matchesParamTypes = Arrays.equals(paramTypes,
121 method.getParameterTypes());
122 if (matchesFrom && matchesTo && matchesParamTypes)
123 {
124 methodToInvoke = method;
125 break;
126 }
127 }
128 }
129 }
130 }
131
132 return methodToInvoke;
133 }
134
135
136
137
138
139
140
141 public String getInitialState(Class<? extends StateModel> clazz)
142 {
143 StateModelInfo stateModelInfo = clazz.getAnnotation(StateModelInfo.class);
144 if (stateModelInfo != null)
145 {
146 return stateModelInfo.initialState();
147 }else{
148 return StateModel.DEFAULT_INITIAL_STATE;
149 }
150 }
151
152 }