1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.model; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.component.LifecycleAdapterFactory; |
16 | |
import org.mule.api.context.MuleContextAware; |
17 | |
import org.mule.api.exception.MessagingExceptionHandler; |
18 | |
import org.mule.api.lifecycle.InitialisationException; |
19 | |
import org.mule.api.lifecycle.LifecycleState; |
20 | |
import org.mule.api.model.EntryPointResolver; |
21 | |
import org.mule.api.model.EntryPointResolverSet; |
22 | |
import org.mule.api.model.Model; |
23 | |
import org.mule.component.DefaultComponentLifecycleAdapterFactory; |
24 | |
import org.mule.exception.DefaultServiceExceptionStrategy; |
25 | |
import org.mule.lifecycle.EmptyLifecycleCallback; |
26 | |
import org.mule.model.resolvers.DefaultEntryPointResolverSet; |
27 | |
import org.mule.model.resolvers.LegacyEntryPointResolverSet; |
28 | |
import org.mule.util.ClassUtils; |
29 | |
|
30 | |
import java.util.Collection; |
31 | |
|
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public abstract class AbstractModel implements Model |
41 | |
{ |
42 | |
|
43 | |
public static final String DEFAULT_MODEL_NAME = "main"; |
44 | |
|
45 | 0 | private String name = DEFAULT_MODEL_NAME; |
46 | 0 | private EntryPointResolverSet entryPointResolverSet = null; |
47 | 0 | private LifecycleAdapterFactory lifecycleAdapterFactory = new DefaultComponentLifecycleAdapterFactory(); |
48 | 0 | private MessagingExceptionHandler exceptionListener = new DefaultServiceExceptionStrategy(); |
49 | |
|
50 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
51 | |
protected MuleContext muleContext; |
52 | |
|
53 | 0 | protected ModelLifecycleManager lifecycleManager = new ModelLifecycleManager(this); |
54 | |
|
55 | |
public String getName() |
56 | |
{ |
57 | 0 | return name; |
58 | |
} |
59 | |
|
60 | |
public void setName(String name) |
61 | |
{ |
62 | 0 | this.name = name; |
63 | 0 | } |
64 | |
|
65 | |
public LifecycleState getLifecycleState() |
66 | |
{ |
67 | 0 | return lifecycleManager.getState(); |
68 | |
} |
69 | |
|
70 | |
public EntryPointResolverSet getEntryPointResolverSet() |
71 | |
{ |
72 | 0 | if (null == entryPointResolverSet) |
73 | |
{ |
74 | 0 | entryPointResolverSet = new LegacyEntryPointResolverSet(); |
75 | |
} |
76 | 0 | return entryPointResolverSet; |
77 | |
} |
78 | |
|
79 | |
public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet) |
80 | |
{ |
81 | 0 | this.entryPointResolverSet = entryPointResolverSet; |
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void setEntryPointResolvers(Collection<EntryPointResolver> entryPointResolvers) |
90 | |
{ |
91 | 0 | if (null == entryPointResolverSet) |
92 | |
{ |
93 | 0 | entryPointResolverSet = new DefaultEntryPointResolverSet(); |
94 | |
} |
95 | |
|
96 | 0 | for (EntryPointResolver resolver : entryPointResolvers) |
97 | |
{ |
98 | 0 | entryPointResolverSet.addEntryPointResolver(resolver); |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
public LifecycleAdapterFactory getLifecycleAdapterFactory() |
103 | |
{ |
104 | 0 | return lifecycleAdapterFactory; |
105 | |
} |
106 | |
|
107 | |
public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory) |
108 | |
{ |
109 | 0 | this.lifecycleAdapterFactory = lifecycleAdapterFactory; |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
public void dispose() |
114 | |
{ |
115 | 0 | if(getLifecycleState().isStarted()) |
116 | |
{ |
117 | |
try |
118 | |
{ |
119 | 0 | stop(); |
120 | |
} |
121 | 0 | catch (MuleException e) |
122 | |
{ |
123 | 0 | logger.error("Failed to stop model cleanly as part of a dispoae call: " + getName(), e); |
124 | 0 | } |
125 | |
} |
126 | |
try |
127 | |
{ |
128 | 0 | lifecycleManager.fireDisposePhase(new EmptyLifecycleCallback<AbstractModel>()); |
129 | |
} |
130 | 0 | catch (MuleException e) |
131 | |
{ |
132 | 0 | logger.error("Failed to dispose model: " + getName(), e); |
133 | 0 | } |
134 | |
|
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public void stop() throws MuleException |
143 | |
{ |
144 | 0 | lifecycleManager.fireStopPhase(new EmptyLifecycleCallback<AbstractModel>()); |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void start() throws MuleException |
153 | |
{ |
154 | 0 | lifecycleManager.fireStartPhase(new EmptyLifecycleCallback<AbstractModel>()); |
155 | 0 | } |
156 | |
|
157 | |
public void initialise() throws InitialisationException |
158 | |
{ |
159 | |
try |
160 | |
{ |
161 | 0 | lifecycleManager.fireInitialisePhase(new EmptyLifecycleCallback<AbstractModel>()); |
162 | |
} |
163 | 0 | catch (InitialisationException e) |
164 | |
{ |
165 | 0 | throw e; |
166 | |
} |
167 | 0 | catch (MuleException e) |
168 | |
{ |
169 | 0 | throw new InitialisationException(e, this); |
170 | 0 | } |
171 | 0 | } |
172 | |
|
173 | |
public MessagingExceptionHandler getExceptionListener() |
174 | |
{ |
175 | 0 | return exceptionListener; |
176 | |
} |
177 | |
|
178 | |
public void setExceptionListener(MessagingExceptionHandler exceptionListener) |
179 | |
{ |
180 | 0 | this.exceptionListener = exceptionListener; |
181 | 0 | } |
182 | |
|
183 | |
|
184 | |
|
185 | |
public void setMuleContext(MuleContext context) |
186 | |
{ |
187 | 0 | this.muleContext = context; |
188 | |
|
189 | |
|
190 | 0 | if (exceptionListener instanceof MuleContextAware) |
191 | |
{ |
192 | 0 | ((MuleContextAware)exceptionListener).setMuleContext(muleContext); |
193 | |
} |
194 | 0 | } |
195 | |
|
196 | |
public MuleContext getMuleContext() |
197 | |
{ |
198 | 0 | return muleContext; |
199 | |
} |
200 | |
|
201 | |
@Override |
202 | |
public String toString() |
203 | |
{ |
204 | 0 | return String.format("%s{%s}", ClassUtils.getSimpleName(this.getClass()), getName()); |
205 | |
} |
206 | |
} |