1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.lifecycle; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.MuleException; |
14 | |
import org.mule.api.lifecycle.LifecycleManager; |
15 | |
import org.mule.api.lifecycle.LifecyclePhase; |
16 | |
import org.mule.lifecycle.phases.NotInLifecyclePhase; |
17 | |
import org.mule.util.StringMessageUtils; |
18 | |
|
19 | |
import java.util.HashMap; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.Map; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import org.apache.commons.collections.set.ListOrderedSet; |
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
|
29 | |
|
30 | 2324 | public class GenericLifecycleManager implements LifecycleManager |
31 | |
{ |
32 | |
|
33 | |
|
34 | |
|
35 | 2 | private static final Log logger = LogFactory.getLog(GenericLifecycleManager.class); |
36 | 2 | protected static final NotInLifecyclePhase notInLifecyclePhase = new NotInLifecyclePhase(); |
37 | 2324 | protected String currentPhase = notInLifecyclePhase.getName(); |
38 | 2324 | protected String executingPhase = null; |
39 | 2324 | protected ListOrderedSet lifecycles = new ListOrderedSet(); |
40 | 2324 | protected Map index = new HashMap(6); |
41 | 2324 | protected Set completedPhases = new HashSet(6); |
42 | |
|
43 | |
public Set getLifecycles() |
44 | |
{ |
45 | 56 | return lifecycles; |
46 | |
} |
47 | |
|
48 | |
public void setLifecycles(Set lifecycles) |
49 | |
{ |
50 | 0 | for (Iterator iterator = lifecycles.iterator(); iterator.hasNext();) |
51 | |
{ |
52 | 0 | LifecyclePhase phase = (LifecyclePhase) iterator.next(); |
53 | 0 | registerLifecycle(phase); |
54 | 0 | } |
55 | 0 | } |
56 | |
|
57 | |
public void registerLifecycle(LifecyclePhase lci) |
58 | |
{ |
59 | 6968 | index.put(lci.getName(), new Integer(lifecycles.size())); |
60 | 6968 | lifecycles.add(lci); |
61 | 6968 | } |
62 | |
|
63 | |
public void firePhase(MuleContext muleContext, String phase) throws MuleException |
64 | |
{ |
65 | 4660 | if (currentPhase.equalsIgnoreCase(phase)) |
66 | |
{ |
67 | 0 | logger.debug("Already in lifecycle phase: " + phase); |
68 | 0 | return; |
69 | |
} |
70 | 4660 | Integer phaseIndex = (Integer) index.get(phase); |
71 | 4660 | if (phaseIndex == null) |
72 | |
{ |
73 | 0 | throw new IllegalArgumentException("No lifeccycle phase registered with name: " + phase); |
74 | |
} |
75 | |
try |
76 | |
{ |
77 | 4660 | setExecutingPhase(phase); |
78 | 4660 | LifecyclePhase li = (LifecyclePhase) lifecycles.get(phaseIndex.intValue()); |
79 | 4660 | li.fireLifecycle(muleContext, currentPhase); |
80 | 4660 | setCurrentPhase(li); |
81 | |
} |
82 | |
finally |
83 | |
{ |
84 | 4660 | setExecutingPhase(null); |
85 | 4660 | } |
86 | 4660 | } |
87 | |
|
88 | |
public String getCurrentPhase() |
89 | |
{ |
90 | 0 | return currentPhase; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public String getExecutingPhase() |
99 | |
{ |
100 | 36536 | return executingPhase; |
101 | |
} |
102 | |
|
103 | |
protected synchronized void setCurrentPhase(LifecyclePhase phase) |
104 | |
{ |
105 | 4660 | completedPhases.add(phase.getName()); |
106 | 4660 | completedPhases.remove(phase.getOppositeLifecyclePhase()); |
107 | 4660 | this.currentPhase = phase.getName(); |
108 | 4660 | } |
109 | |
|
110 | |
protected synchronized void setExecutingPhase(String phase) |
111 | |
{ |
112 | 9320 | this.executingPhase = phase; |
113 | 9320 | } |
114 | |
|
115 | |
public void reset() |
116 | |
{ |
117 | 0 | setExecutingPhase(null); |
118 | 0 | completedPhases.clear(); |
119 | 0 | setCurrentPhase(notInLifecyclePhase); |
120 | 0 | } |
121 | |
|
122 | |
public boolean isPhaseComplete(String phaseName) |
123 | |
{ |
124 | 36928 | return completedPhases.contains(phaseName); |
125 | |
} |
126 | |
|
127 | |
public void applyLifecycle(MuleContext muleContext, Object object) throws MuleException |
128 | |
{ |
129 | 34250 | logger.debug("applying lifecycle to " + object); |
130 | |
|
131 | |
LifecyclePhase lcp; |
132 | |
String phase; |
133 | |
Integer phaseIndex; |
134 | 34250 | for (Iterator iterator = completedPhases.iterator(); iterator.hasNext();) |
135 | |
{ |
136 | 34270 | phase = (String) iterator.next(); |
137 | 34270 | phaseIndex = (Integer) index.get(phase); |
138 | 34270 | lcp = (LifecyclePhase) lifecycles.get(phaseIndex.intValue()); |
139 | |
|
140 | 34270 | if (logger.isDebugEnabled()) |
141 | |
{ |
142 | 0 | logger.debug("phase: " + lcp); |
143 | |
} |
144 | 34270 | lcp.applyLifecycle(object); |
145 | |
} |
146 | |
|
147 | 34250 | if (getExecutingPhase() != null) |
148 | |
{ |
149 | 0 | phaseIndex = (Integer) index.get(getExecutingPhase()); |
150 | 0 | lcp = (LifecyclePhase) lifecycles.get(phaseIndex.intValue()); |
151 | |
|
152 | 0 | if (logger.isDebugEnabled()) |
153 | |
{ |
154 | 0 | logger.debug("and executing: " + lcp); |
155 | |
} |
156 | 0 | lcp.applyLifecycle(object); |
157 | |
} |
158 | 34250 | } |
159 | |
|
160 | |
public void checkPhase(String name) throws IllegalStateException |
161 | |
{ |
162 | 3516 | if (completedPhases.contains(name)) |
163 | |
{ |
164 | 0 | throw new IllegalStateException("Phase '" + name + "' has already been executed"); |
165 | |
} |
166 | |
|
167 | 3516 | if (name.equalsIgnoreCase(executingPhase)) |
168 | |
{ |
169 | 0 | throw new IllegalStateException("Phase '" + name + "' is already currently being executed"); |
170 | |
} |
171 | |
|
172 | 3516 | if (executingPhase != null) |
173 | |
{ |
174 | 0 | throw new IllegalStateException("Currently executing lifecycle phase: " + executingPhase); |
175 | |
} |
176 | |
|
177 | 3516 | Integer phaseIndex = (Integer) index.get(name); |
178 | 3516 | if (phaseIndex == null) |
179 | |
{ |
180 | 0 | throw new IllegalStateException("Phase does not exist: " + name); |
181 | |
} |
182 | 3516 | if (NotInLifecyclePhase.PHASE_NAME.equals(currentPhase)) |
183 | |
{ |
184 | 2320 | if (phaseIndex.intValue() > 0) |
185 | |
{ |
186 | 0 | throw new IllegalStateException("The first lifecycle phase has to be called before the '" + name + "' phase"); |
187 | |
} |
188 | |
} |
189 | |
else |
190 | |
{ |
191 | 1196 | LifecyclePhase phase = (LifecyclePhase) lifecycles.get(phaseIndex.intValue()); |
192 | 1196 | if (!phase.isPhaseSupported(currentPhase)) |
193 | |
{ |
194 | 0 | throw new IllegalStateException("Lifecycle phase: " + currentPhase + " does not support current phase: " |
195 | |
+ name + ". Phases supported are: " + StringMessageUtils.toString(phase.getSupportedPhases())); |
196 | |
} |
197 | |
} |
198 | 3516 | } |
199 | |
} |