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