1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.lifecycle.phases; |
12 | |
|
13 | |
import java.lang.reflect.Method; |
14 | |
import java.util.ArrayList; |
15 | |
import java.util.Collection; |
16 | |
import java.util.HashSet; |
17 | |
import java.util.LinkedHashSet; |
18 | |
import java.util.List; |
19 | |
import java.util.Set; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
import org.mule.api.MuleContext; |
24 | |
import org.mule.api.context.MuleContextAware; |
25 | |
import org.mule.api.lifecycle.LifecycleException; |
26 | |
import org.mule.api.lifecycle.LifecyclePhase; |
27 | |
import org.mule.api.lifecycle.LifecycleStateEnabled; |
28 | |
import org.mule.config.ExceptionHelper; |
29 | |
import org.mule.config.i18n.CoreMessages; |
30 | |
import org.mule.lifecycle.LifecycleObject; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class DefaultLifecyclePhase implements LifecyclePhase, MuleContextAware |
51 | |
{ |
52 | 0 | protected transient final Log logger = LogFactory.getLog(DefaultLifecyclePhase.class); |
53 | |
private Class<?> lifecycleClass; |
54 | |
private final Method lifecycleMethod; |
55 | 0 | private Set<LifecycleObject> orderedLifecycleObjects = new LinkedHashSet<LifecycleObject>(6); |
56 | |
private Class<?>[] ignorredObjectTypes; |
57 | |
private final String name; |
58 | |
private final String oppositeLifecyclePhase; |
59 | |
private Set<String> supportedPhases; |
60 | |
private MuleContext muleContext; |
61 | |
|
62 | |
public DefaultLifecyclePhase(String name, Class<?> lifecycleClass, String oppositeLifecyclePhase) |
63 | 0 | { |
64 | 0 | this.name = name; |
65 | 0 | this.lifecycleClass = lifecycleClass; |
66 | |
|
67 | 0 | lifecycleMethod = lifecycleClass.getMethods()[0]; |
68 | 0 | this.oppositeLifecyclePhase = oppositeLifecyclePhase; |
69 | 0 | } |
70 | |
|
71 | |
public void setMuleContext(MuleContext context) |
72 | |
{ |
73 | 0 | this.muleContext = context; |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
protected List sortLifecycleInstances(Collection objects, LifecycleObject lo) |
86 | |
{ |
87 | 0 | return new ArrayList(objects); |
88 | |
} |
89 | |
|
90 | |
public void addOrderedLifecycleObject(LifecycleObject lco) |
91 | |
{ |
92 | 0 | orderedLifecycleObjects.add(lco); |
93 | 0 | } |
94 | |
|
95 | |
public void removeOrderedLifecycleObject(LifecycleObject lco) |
96 | |
{ |
97 | 0 | orderedLifecycleObjects.remove(lco); |
98 | 0 | } |
99 | |
|
100 | |
protected boolean ignoreType(Class<?> type) |
101 | |
{ |
102 | 0 | if (ignorredObjectTypes == null) |
103 | |
{ |
104 | 0 | return false; |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | for (int i = 0; i < ignorredObjectTypes.length; i++) |
109 | |
{ |
110 | 0 | Class<?> ignorredObjectType = ignorredObjectTypes[i]; |
111 | 0 | if (ignorredObjectType.isAssignableFrom(type)) |
112 | |
{ |
113 | 0 | return true; |
114 | |
} |
115 | |
} |
116 | |
} |
117 | 0 | return false; |
118 | |
} |
119 | |
|
120 | |
public Set<LifecycleObject> getOrderedLifecycleObjects() |
121 | |
{ |
122 | 0 | return orderedLifecycleObjects; |
123 | |
} |
124 | |
|
125 | |
public void setOrderedLifecycleObjects(Set<LifecycleObject> orderedLifecycleObjects) |
126 | |
{ |
127 | 0 | this.orderedLifecycleObjects = orderedLifecycleObjects; |
128 | 0 | } |
129 | |
|
130 | |
public Class<?>[] getIgnoredObjectTypes() |
131 | |
{ |
132 | 0 | return ignorredObjectTypes; |
133 | |
} |
134 | |
|
135 | |
public void setIgnoredObjectTypes(Class<?>[] ignorredObjectTypes) |
136 | |
{ |
137 | 0 | this.ignorredObjectTypes = ignorredObjectTypes; |
138 | 0 | } |
139 | |
|
140 | |
public Class<?> getLifecycleClass() |
141 | |
{ |
142 | 0 | return lifecycleClass; |
143 | |
} |
144 | |
|
145 | |
public void setLifecycleClass(Class<?> lifecycleClass) |
146 | |
{ |
147 | 0 | this.lifecycleClass = lifecycleClass; |
148 | 0 | } |
149 | |
|
150 | |
public String getName() |
151 | |
{ |
152 | 0 | return name; |
153 | |
} |
154 | |
|
155 | |
public Set<String> getSupportedPhases() |
156 | |
{ |
157 | 0 | return supportedPhases; |
158 | |
} |
159 | |
|
160 | |
public void setSupportedPhases(Set<String> supportedPhases) |
161 | |
{ |
162 | 0 | this.supportedPhases = supportedPhases; |
163 | 0 | } |
164 | |
|
165 | |
public void registerSupportedPhase(String phase) |
166 | |
{ |
167 | 0 | if (supportedPhases == null) |
168 | |
{ |
169 | 0 | supportedPhases = new HashSet<String>(); |
170 | |
} |
171 | 0 | supportedPhases.add(phase); |
172 | 0 | } |
173 | |
|
174 | |
public boolean isPhaseSupported(String phase) |
175 | |
{ |
176 | 0 | if (getSupportedPhases() == null) |
177 | |
{ |
178 | 0 | return false; |
179 | |
} |
180 | |
else |
181 | |
{ |
182 | 0 | if (getSupportedPhases().contains(ALL_PHASES)) |
183 | |
{ |
184 | 0 | return true; |
185 | |
} |
186 | |
else |
187 | |
{ |
188 | 0 | return getSupportedPhases().contains(phase); |
189 | |
} |
190 | |
} |
191 | |
} |
192 | |
|
193 | |
public void applyLifecycle(Object o) throws LifecycleException |
194 | |
{ |
195 | 0 | if (o == null) |
196 | |
{ |
197 | 0 | return; |
198 | |
} |
199 | 0 | if (ignoreType(o.getClass())) |
200 | |
{ |
201 | 0 | return; |
202 | |
} |
203 | 0 | if (!getLifecycleClass().isAssignableFrom(o.getClass())) |
204 | |
{ |
205 | 0 | return; |
206 | |
} |
207 | 0 | if (o instanceof LifecycleStateEnabled) |
208 | |
{ |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | 0 | if (((LifecycleStateEnabled) o).getLifecycleState().isPhaseComplete(this.getName())) |
215 | |
{ |
216 | 0 | return; |
217 | |
} |
218 | 0 | else if (!((LifecycleStateEnabled) o).getLifecycleState().isValidTransition(this.getName())) |
219 | |
{ |
220 | 0 | return; |
221 | |
} |
222 | |
} |
223 | |
try |
224 | |
{ |
225 | 0 | lifecycleMethod.invoke(o); |
226 | |
} |
227 | 0 | catch (final Exception e) |
228 | |
{ |
229 | 0 | Throwable t = ExceptionHelper.unwrap(e); |
230 | |
|
231 | 0 | if (t instanceof LifecycleException) |
232 | |
{ |
233 | 0 | throw (LifecycleException) t; |
234 | |
} |
235 | |
|
236 | 0 | throw new LifecycleException(CoreMessages.failedToInvokeLifecycle(lifecycleMethod.getName(), o), |
237 | |
t, this); |
238 | 0 | } |
239 | 0 | } |
240 | |
|
241 | |
public String getOppositeLifecyclePhase() |
242 | |
{ |
243 | 0 | return oppositeLifecyclePhase; |
244 | |
} |
245 | |
} |