1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package it.imolinfo.jbi4ejb.jbi.component.runtime;
31
32 import it.imolinfo.jbi4ejb.Logger;
33 import it.imolinfo.jbi4ejb.LoggerFactory;
34 import it.imolinfo.jbi4ejb.jbi.Messages;
35 import it.imolinfo.jbi4ejb.jbi.component.Jbi4EjbLifeCycle;
36
37 import javax.jbi.JBIException;
38 import javax.jbi.component.ComponentContext;
39 import javax.jbi.component.ComponentLifeCycle;
40 import javax.management.MBeanServer;
41 import javax.management.ObjectName;
42 import javax.management.StandardMBean;
43
44
45
46
47
48
49
50
51
52
53 public abstract class AbstractComponentLifeCycle implements ComponentLifeCycle {
54
55 private static final Logger LOG = LoggerFactory.getLogger( AbstractComponentLifeCycle.class);
56 private static final Messages MESSAGES = Messages.getMessages( AbstractComponentLifeCycle.class);
57
58
59 private ComponentRuntime mCompRuntime;
60
61 private ObjectName mExtensionMBeanName;
62
63 private StandardMBean mExtensionMBeanImpl;
64
65 private MessageExchangeReceiver mMsgExchReceiver;
66
67
68 private AbstractComponentLifeCycle() {}
69
70
71
72
73 protected AbstractComponentLifeCycle(ComponentRuntime compRuntime) {
74 this.mCompRuntime = compRuntime;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public final void init(ComponentContext context) throws JBIException {
88
89 if ( context == null ) {
90 throw new JBIException("Null Component Context received in " +
91 "Component Lifecycle init ");
92 }
93
94 initContext(context);
95
96 initMessageExchangeHandlerFactory();
97
98 initMessageExchangeReceiver();
99
100 registerExtensionMBean();
101
102 onInit();
103
104 LOG.info("EJB000201_Component_initialized", new Object[]{RuntimeHelper.getComponentName()});
105 }
106
107
108
109
110
111
112 public final void start() throws JBIException {
113
114
115 openDeliveryChannel();
116
117 activateServiceProviders();
118 activateServiceConsumers();
119
120 startMessageExchangeProcessing();
121
122 onStart();
123
124 LOG.info("EJB000202_Component_started", new Object[]{RuntimeHelper.getComponentName()});
125 }
126
127
128
129
130
131
132 public final void stop() throws JBIException {
133
134 stopMessageExchangeProcessing();
135
136 deactivateServiceConsumers();
137 deactivateServiceProviders();
138
139 closeDeliveryChannel();
140
141 onStop();
142 LOG.info("EJB000203_Component_stopped", new Object[]{RuntimeHelper.getComponentName()});
143 }
144
145
146
147
148
149
150 public final void shutDown() throws JBIException {
151
152 shutdownMessageExchangeReceiver();
153
154 unregisterExtensionMBean();
155
156 onShutDown();
157 LOG.info("EJB000204_Component_shut_down", new Object[]{RuntimeHelper.getComponentName()});
158 }
159
160
161
162
163
164
165
166
167
168 public final ObjectName getExtensionMBeanName() {
169 return this.mExtensionMBeanName;
170 }
171
172
173
174
175 private void registerExtensionMBean() throws JBIException {
176
177 this.mExtensionMBeanName = this.createExtensionMBeanName();
178
179 if ( this.mExtensionMBeanName == null ) {
180 RuntimeHelper.logDebug("No Extension MBean is registerd with MBeanServer for " +
181 RuntimeHelper.getComponentName());
182 return;
183 }
184
185 this.mExtensionMBeanImpl = this.createExtensionMBean();
186
187 if ( this.mExtensionMBeanImpl == null ) {
188 this.mExtensionMBeanName = null;
189 RuntimeHelper.logDebug("No Extension MBean is registerd with MBeanServer for " +
190 RuntimeHelper.getComponentName());
191 return;
192 }
193
194 try {
195 MBeanServer mbServer = RuntimeHelper.getComponentContext().getMBeanServer();
196 mbServer.registerMBean(this.mExtensionMBeanImpl, this.mExtensionMBeanName);
197 } catch (Exception e) {
198 this.mExtensionMBeanName = null;
199 this.mExtensionMBeanImpl = null;
200 String msg=MESSAGES.getString("EJB000205_Can_not_register_extension_MBean_with_MBeanServer", new Object[] {RuntimeHelper.getComponentName()});
201 LOG.error(msg,e);
202 throw new JBIException(msg,e);
203
204 }
205 }
206
207
208
209 private void unregisterExtensionMBean() throws JBIException {
210 if ( this.mExtensionMBeanName != null ) {
211 try {
212 MBeanServer mbServer = RuntimeHelper.getComponentContext().getMBeanServer();
213 mbServer.unregisterMBean(this.mExtensionMBeanName);
214 } catch (Exception e) {
215 String msg=MESSAGES.getString("EJB000206_Can_not_register_extension_MBean_from_MBeanServer", new Object[] {RuntimeHelper.getComponentName()});
216 LOG.error(msg,e);
217 throw new JBIException(msg,e);
218 } finally {
219 this.mExtensionMBeanName = null;
220 this.mExtensionMBeanImpl = null;
221 }
222 }
223 }
224
225
226
227
228
229 public final ComponentRuntime getComponentRuntime() {
230 return this.mCompRuntime;
231 }
232
233
234
235
236 private void initContext(ComponentContext context) {
237 RuntimeContext.getInstance().setComponentContext(context);
238 RuntimeContext.getInstance().setLogger(this.getClass().getPackage().getName(), null);
239 }
240
241
242
243
244 private void initMessageExchangeReceiver() throws JBIException {
245 try {
246
247
248 this.mMsgExchReceiver = createMessageExchangeReceiver();
249 if ( this.mMsgExchReceiver != null ) {
250 this.mMsgExchReceiver.initReceiver();
251 }
252 } catch (Exception ex) {
253 String msg=MESSAGES.getString("EJB000207_Failure_in__init_Message_Exchange_Receiver");
254 LOG.error(msg,ex);
255 throw new JBIException(msg,ex);
256 }
257 }
258
259
260
261
262 private void shutdownMessageExchangeReceiver() throws JBIException {
263 try {
264 if ( this.mMsgExchReceiver != null ) {
265 this.mMsgExchReceiver.shutdownReceiver();
266 }
267 } catch (Exception ex) {
268 ex.printStackTrace();
269 } finally {
270 this.mMsgExchReceiver = null;
271 }
272 }
273
274
275
276
277
278 private void startMessageExchangeProcessing() throws JBIException {
279 try {
280
281 if ( this.mMsgExchReceiver != null ) {
282 this.mMsgExchReceiver.startProcessing();
283 }
284 } catch (Exception ex) {
285 String msg=MESSAGES.getString("EJB000208_Failure_in__start_Message_Exchange_Processing");
286 LOG.error(msg,ex);
287 throw new JBIException(msg,ex);
288 }
289 }
290
291
292
293
294
295 private void stopMessageExchangeProcessing() throws JBIException {
296 try {
297
298 if ( this.mMsgExchReceiver != null ) {
299 this.mMsgExchReceiver.stopProcessing();
300 }
301 } catch (Exception ex) {
302 String msg=MESSAGES.getString("EJB000209_Failure_in__stop_Message_Exchange_Processing");
303 LOG.error(msg,ex);
304 throw new JBIException(msg,ex);
305 }
306
307 }
308
309
310
311
312 private void openDeliveryChannel() {
313 RuntimeContext.getInstance().openDeliveryChannel();
314 }
315
316
317
318 private void closeDeliveryChannel() {
319 RuntimeContext.getInstance().closeDeliveryChannel();
320 }
321
322
323
324 protected abstract ObjectName createExtensionMBeanName();
325
326
327
328 protected abstract StandardMBean createExtensionMBean();
329
330
331
332
333
334 protected abstract void initMessageExchangeHandlerFactory() throws JBIException;
335
336
337
338
339
340
341
342
343
344
345
346
347 protected abstract MessageExchangeReceiver createMessageExchangeReceiver() throws Exception;
348
349
350
351
352
353
354
355
356
357 protected abstract void activateServiceProviders() throws JBIException;
358
359
360
361
362
363
364
365 protected abstract void deactivateServiceProviders() throws JBIException;
366
367
368
369
370
371
372
373
374
375 protected abstract void activateServiceConsumers() throws JBIException;
376
377
378
379
380
381
382
383
384 protected abstract void deactivateServiceConsumers() throws JBIException;
385
386
387
388
389 protected void onInit() throws JBIException {
390
391 }
392
393
394
395
396 protected void onStart() throws JBIException {
397
398 }
399
400
401
402
403 protected void onStop() throws JBIException {
404
405 }
406
407
408
409
410 protected void onShutDown() throws JBIException {
411
412 }
413
414 }