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.context.notification.ServerNotification; |
18 | |
import org.mule.api.lifecycle.InitialisationException; |
19 | |
import org.mule.api.model.EntryPointResolver; |
20 | |
import org.mule.api.model.EntryPointResolverSet; |
21 | |
import org.mule.api.model.Model; |
22 | |
import org.mule.component.DefaultLifecycleAdapterFactory; |
23 | |
import org.mule.context.notification.ModelNotification; |
24 | |
import org.mule.model.resolvers.DefaultEntryPointResolverSet; |
25 | |
import org.mule.model.resolvers.LegacyEntryPointResolverSet; |
26 | |
import org.mule.service.DefaultServiceExceptionStrategy; |
27 | |
|
28 | |
import java.beans.ExceptionListener; |
29 | |
import java.util.Collection; |
30 | |
import java.util.Iterator; |
31 | |
|
32 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
33 | |
|
34 | |
import org.apache.commons.logging.Log; |
35 | |
import org.apache.commons.logging.LogFactory; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 1552 | public abstract class AbstractModel implements Model |
43 | |
{ |
44 | |
|
45 | |
public static final String DEFAULT_MODEL_NAME = "main"; |
46 | |
|
47 | 1552 | private String name = DEFAULT_MODEL_NAME; |
48 | 1552 | private EntryPointResolverSet entryPointResolverSet = null; |
49 | 1552 | private LifecycleAdapterFactory lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory(); |
50 | 1552 | private AtomicBoolean initialised = new AtomicBoolean(false); |
51 | 1552 | private AtomicBoolean started = new AtomicBoolean(false); |
52 | 1552 | private ExceptionListener exceptionListener = new DefaultServiceExceptionStrategy(); |
53 | |
|
54 | 1552 | protected transient Log logger = LogFactory.getLog(getClass()); |
55 | |
protected MuleContext muleContext; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public String getName() |
63 | |
{ |
64 | 6642 | return name; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public void setName(String name) |
73 | |
{ |
74 | 1150 | this.name = name; |
75 | 1150 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public EntryPointResolverSet getEntryPointResolverSet() |
83 | |
{ |
84 | 44 | if (null == entryPointResolverSet) |
85 | |
{ |
86 | 44 | entryPointResolverSet = new LegacyEntryPointResolverSet(); |
87 | |
} |
88 | 44 | return entryPointResolverSet; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet) |
97 | |
{ |
98 | 0 | this.entryPointResolverSet = entryPointResolverSet; |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setEntryPointResolvers(Collection entryPointResolvers) |
107 | |
{ |
108 | 0 | if (null == entryPointResolverSet) |
109 | |
{ |
110 | 0 | entryPointResolverSet = new DefaultEntryPointResolverSet(); |
111 | |
} |
112 | 0 | for (Iterator resolvers = entryPointResolvers.iterator(); resolvers.hasNext();) |
113 | |
{ |
114 | 0 | entryPointResolverSet.addEntryPointResolver((EntryPointResolver) resolvers.next()); |
115 | |
} |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public LifecycleAdapterFactory getLifecycleAdapterFactory() |
124 | |
{ |
125 | 76 | return lifecycleAdapterFactory; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory) |
134 | |
{ |
135 | 0 | this.lifecycleAdapterFactory = lifecycleAdapterFactory; |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
public void dispose() |
140 | |
{ |
141 | 1144 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSING)); |
142 | 1144 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSED)); |
143 | 1144 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public void stop() throws MuleException |
151 | |
{ |
152 | 26 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPING)); |
153 | 26 | started.set(false); |
154 | 26 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPED)); |
155 | 26 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public void start() throws MuleException |
163 | |
{ |
164 | 36 | if (!initialised.get()) |
165 | |
{ |
166 | 0 | throw new IllegalStateException("Not Initialised"); |
167 | |
} |
168 | |
|
169 | 36 | if (!started.get()) |
170 | |
{ |
171 | 36 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTING)); |
172 | 36 | started.set(true); |
173 | 36 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTED)); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | logger.debug("Model already started"); |
178 | |
} |
179 | 36 | } |
180 | |
|
181 | |
public void initialise() throws InitialisationException |
182 | |
{ |
183 | 1536 | if (!initialised.get()) |
184 | |
{ |
185 | 1536 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISING)); |
186 | 1536 | initialised.set(true); |
187 | 1536 | fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISED)); |
188 | |
} |
189 | |
else |
190 | |
{ |
191 | 0 | logger.debug("Model already initialised"); |
192 | |
} |
193 | 1536 | } |
194 | |
|
195 | |
public ExceptionListener getExceptionListener() |
196 | |
{ |
197 | 398 | return exceptionListener; |
198 | |
} |
199 | |
|
200 | |
public void setExceptionListener(ExceptionListener exceptionListener) |
201 | |
{ |
202 | 0 | this.exceptionListener = exceptionListener; |
203 | 0 | } |
204 | |
|
205 | |
void fireNotification(ServerNotification notification) |
206 | |
{ |
207 | 5484 | if (muleContext != null) |
208 | |
{ |
209 | 5484 | muleContext.fireNotification(notification); |
210 | |
} |
211 | 0 | else if (logger.isWarnEnabled()) |
212 | |
{ |
213 | 0 | logger.debug("MuleContext is not yet available for firing notifications, ignoring event: " + notification); |
214 | |
} |
215 | 5484 | } |
216 | |
|
217 | |
public void setMuleContext(MuleContext context) |
218 | |
{ |
219 | 1536 | this.muleContext = context; |
220 | |
|
221 | |
|
222 | 1536 | if(exceptionListener instanceof MuleContextAware) |
223 | |
{ |
224 | 1536 | ((MuleContextAware)exceptionListener).setMuleContext(muleContext); |
225 | |
} |
226 | 1536 | } |
227 | |
|
228 | |
} |