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