1
2
3
4
5
6
7
8
9
10
11
12
13 package it.imolinfo.jbi4corba.jbi.component.runtime;
14
15 import it.imolinfo.jbi4corba.Logger;
16 import it.imolinfo.jbi4corba.LoggerFactory;
17 import it.imolinfo.jbi4corba.jbi.component.Jbi4CorbaPerformanceMeasurement;
18 import it.imolinfo.jbi4corba.jbi.component.ReadWriteTextFile;
19
20 import java.io.File;
21
22 import javax.jbi.JBIException;
23 import javax.jbi.component.ComponentContext;
24 import javax.jbi.component.ComponentLifeCycle;
25 import javax.jbi.management.MBeanNames;
26 import javax.management.MBeanServer;
27 import javax.management.ObjectName;
28 import javax.management.StandardMBean;
29
30 import com.sun.jbi.configuration.RuntimeConfigurationHelper;
31 import com.sun.jbi.eManager.provider.StatusProviderHelper;
32 import com.sun.jbi.internationalization.Messages;
33
34
35
36
37
38
39
40
41
42
43
44 public abstract class AbstractComponentLifeCycle implements ComponentLifeCycle {
45
46
47 private static final Logger LOG = LoggerFactory
48 .getLogger(AbstractComponentLifeCycle.class);
49
50 private static final Messages MESSAGES = Messages
51 .getMessages(AbstractComponentLifeCycle.class);
52
53
54 private ComponentRuntime mCompRuntime;
55
56 private ObjectName mExtensionMBeanName;
57
58 private StandardMBean mExtensionMBeanImpl;
59
60 private MessageExchangeReceiver mMsgExchReceiver;
61
62 protected RuntimeConfiguration mRuntimeConfig;
63 protected RuntimeConfigurationHelper mRuntimeConfigHelper;
64
65
66 public static final String SHORT_DISPLAY_NAME = "sun-jbi4corba-bc";
67
68
69 public static final String PERF_CAT_NORMALIZATION = "Normalization";
70 public static final String PERF_CAT_DENORMALIZATION = "Denormalization";
71 public static final String[] JBI4CORBABC_PERF_CATEGORIES = new String[] {
72 PERF_CAT_NORMALIZATION, PERF_CAT_DENORMALIZATION };
73 protected ComponentContext jbiContext = null;
74 protected StatusProviderHelper mStatusProviderHelper;
75
76
77
78
79
80
81 protected AbstractComponentLifeCycle(ComponentRuntime compRuntime) {
82 this.mCompRuntime = compRuntime;
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96 public final void init(ComponentContext context) throws JBIException {
97
98 if (context == null) {
99 String msg = MESSAGES
100 .getString("CRB000429_Null_Component_Context_received_in_Component_Lifecycle_init");
101 LOG.error(msg);
102 throw new JBIException(msg);
103 }
104
105 Messages.registerContext(context);
106
107 initContext(context);
108
109
110 registerExtensionMBean();
111
112 registerMonitoringMbean();
113
114 registerRuntimeConfigurationMBean();
115
116
117 initMessageExchangeHandlerFactory();
118
119 initMessageExchangeReceiver(mRuntimeConfig);
120
121
122
123 onInit();
124
125 LOG.info("CRB000401_Component_initialized",
126 new Object[] { RuntimeHelper.getComponentName() });
127 }
128
129
130
131
132
133
134
135 public final void start() throws JBIException {
136
137
138 openDeliveryChannel();
139
140 activateServiceProviders();
141 activateServiceConsumers();
142
143 startMessageExchangeProcessing();
144
145 onStart();
146 LOG.info("CRB000402_Component_started", new Object[] { RuntimeHelper
147 .getComponentName() });
148 }
149
150
151
152
153
154
155
156 public final void stop() throws JBIException {
157
158 stopMessageExchangeProcessing();
159
160 deactivateServiceConsumers();
161 deactivateServiceProviders();
162
163 closeDeliveryChannel();
164
165 onStop();
166 LOG.info("CRB000403_Component_stopped", new Object[] { RuntimeHelper
167 .getComponentName() });
168 }
169
170
171
172
173
174
175
176 public final void shutDown() throws JBIException {
177
178 shutdownMessageExchangeReceiver();
179
180 unregisterExtensionMBean();
181
182 unregisterManagementMbean();
183
184 unregisterRuntimeConfigurationMBean();
185
186 onShutDown();
187 LOG.info("CRB000404_Component_shut_downd", new Object[] { RuntimeHelper
188 .getComponentName() });
189 }
190
191
192
193
194
195
196
197
198
199
200 public final ObjectName getExtensionMBeanName() {
201 return this.mExtensionMBeanName;
202 }
203
204
205
206
207
208 private void registerExtensionMBean() throws JBIException {
209
210 this.mExtensionMBeanName = this.createExtensionMBeanName();
211
212 if (this.mExtensionMBeanName == null) {
213 RuntimeHelper
214 .logDebug("No Extension MBean is registerd with MBeanServer for "
215 + RuntimeHelper.getComponentName());
216 return;
217 }
218
219
220 this.mExtensionMBeanImpl = this.createExtensionMBean();
221
222 if (this.mExtensionMBeanImpl == null) {
223 this.mExtensionMBeanName = null;
224 RuntimeHelper
225 .logDebug("No Extension MBean is registerd with MBeanServer for "
226 + RuntimeHelper.getComponentName());
227 return;
228 }
229
230
231 try {
232 MBeanServer mbServer = RuntimeHelper.getComponentContext()
233 .getMBeanServer();
234 mbServer.registerMBean(this.mExtensionMBeanImpl,
235 this.mExtensionMBeanName);
236 } catch (Exception e) {
237 this.mExtensionMBeanName = null;
238 this.mExtensionMBeanImpl = null;
239 String msg = MESSAGES
240 .getString(
241 "CRB000405_Cannot_register_Extension_MBean_with_MBeanServer",
242 new Object[] { RuntimeHelper.getComponentName() });
243 LOG.error(msg, e);
244 throw new JBIException(msg, e);
245 }
246 }
247
248 private void registerMonitoringMbean() throws JBIException {
249 MBeanNames mbnHndl = null;
250
251 try {
252 jbiContext = RuntimeContext.getInstance().getComponentContext();
253 mbnHndl = jbiContext.getMBeanNames();
254 if (mbnHndl != null) {
255 ObjectName statusMBeanObjName = mbnHndl
256 .createCustomComponentMBeanName("Statistics");
257 mStatusProviderHelper = new StatusProviderHelper(
258 SHORT_DISPLAY_NAME, statusMBeanObjName, jbiContext
259 .getMBeanServer());
260 mStatusProviderHelper.registerMBean(
261 JBI4CORBABC_PERF_CATEGORIES,
262 new Jbi4CorbaPerformanceMeasurement());
263 }
264 LOG.info("CRB000406_Jbi4Corba_Register_mbean",
265 new Object[] { jbiContext.getComponentName() });
266 } catch (final Exception ex) {
267 String msg = MESSAGES.getString(
268 "CRB000407_Jbi4Corba_Failed_register_mbean",
269 new Object[] { RuntimeHelper.getComponentName() });
270 LOG.error(msg, ex);
271 throw new JBIException(msg, ex);
272 }
273 }
274
275
276
277
278
279 private void registerRuntimeConfigurationMBean() throws JBIException {
280 try {
281 ComponentContext context = RuntimeContext.getInstance()
282 .getComponentContext();
283 String configData = "";
284 String configSchema = "";
285 String configDataFileLoc = context.getInstallRoot()
286 + File.separator + "META-INF" + File.separator
287 + "componentConfiguration.xml";
288 File configDataFile = new File(configDataFileLoc);
289 if (configDataFile.exists()) {
290 configData = ReadWriteTextFile.getContents(configDataFile);
291 String configSchemaFileLoc = context.getInstallRoot()
292 + File.separator + "META-INF" + File.separator
293 + "componentConfiguration.xsd";
294 File configSchemaFile = new File(configSchemaFileLoc);
295 if (configSchemaFile.exists()) {
296 configSchema = ReadWriteTextFile
297 .getContents(configSchemaFile);
298 }
299 mRuntimeConfig = new RuntimeConfiguration(context
300 .getWorkspaceRoot(), configData, configSchema);
301 } else {
302
303
304 mRuntimeConfig = new RuntimeConfiguration(context
305 .getWorkspaceRoot(), configSchema);
306 String msg = MESSAGES
307 .getString("CRB000408_Failed_to_locate_configuration_file");
308 LOG.warn(msg);
309
310 }
311
312 mRuntimeConfigHelper = new RuntimeConfigurationHelper(
313 RuntimeConfigurationHelper.COMPONENT_TYPE_BINDING, context
314 .getComponentName(), context.getMBeanServer());
315 mRuntimeConfigHelper.registerMBean(mRuntimeConfig);
316 RuntimeHelper.logInfo("Registering runtime config mbean for"
317 + RuntimeHelper.getComponentName());
318
319 } catch (final Exception ex) {
320 String msg = MESSAGES.getString(
321 "CRB000409_Failed_to_register_runtime_configuration_mbean",
322 new Object[] { ex.toString() });
323 LOG.error(msg, ex);
324 throw new JBIException(msg, ex);
325 }
326 }
327
328
329
330
331 private void unregisterExtensionMBean() throws JBIException {
332 if (this.mExtensionMBeanName != null) {
333 try {
334 MBeanServer mbServer = RuntimeHelper.getComponentContext()
335 .getMBeanServer();
336 mbServer.unregisterMBean(this.mExtensionMBeanName);
337 } catch (Exception e) {
338 String msg = MESSAGES
339 .getString(
340 "CRB000410_Cannot_unregister_Extension_MBean_with_MBeanServer",
341 new Object[] { RuntimeHelper.getComponentName() });
342 LOG.error(msg, e);
343 throw new JBIException(msg, e);
344 } finally {
345 this.mExtensionMBeanName = null;
346 this.mExtensionMBeanImpl = null;
347 }
348 }
349 }
350
351 private void unregisterManagementMbean() throws JBIException {
352 try {
353 mStatusProviderHelper.unregisterMBean();
354 } catch (final Exception ex) {
355 String msg = MESSAGES.getString(
356 "CRB000430_Failed_to_unregister_statusProviderMBean",
357 new Object[] { jbiContext.getComponentName() });
358 LOG.error(msg, ex);
359 throw new JBIException(msg, ex);
360 }
361 }
362
363
364
365
366 private void unregisterRuntimeConfigurationMBean() throws JBIException {
367 try {
368 if (mRuntimeConfigHelper != null) {
369 mRuntimeConfigHelper.unregisterMBean();
370 }
371 } catch (Exception ex) {
372 LOG.info("CRB000425_Exception_during_runtimecfg_mbean_deregister",
373 new Object[] { ex.getLocalizedMessage() });
374
375 }
376 }
377
378
379
380
381
382 public final ComponentRuntime getComponentRuntime() {
383 return this.mCompRuntime;
384 }
385
386
387
388
389
390
391
392 private void initContext(ComponentContext context) {
393 RuntimeContext.getInstance().setComponentContext(context);
394 RuntimeContext.getInstance().setLogger(
395 this.getClass().getPackage().getName(), null);
396 }
397
398
399
400
401
402
403
404 private void initMessageExchangeReceiver(RuntimeConfiguration mRuntimeConfig)
405 throws JBIException {
406 try {
407
408
409 this.mMsgExchReceiver = createMessageExchangeReceiver();
410 if (this.mMsgExchReceiver != null) {
411 this.mMsgExchReceiver.initReceiver(mRuntimeConfig);
412 }
413 } catch (Exception ex) {
414 String msg = MESSAGES
415 .getString("CRB000426_Failure_in_init_MessageExchangeReceiver");
416 LOG.error(msg, ex);
417 throw new JBIException(msg, ex);
418
419 }
420 }
421
422
423
424
425
426
427 private void shutdownMessageExchangeReceiver() throws JBIException {
428 try {
429 if (this.mMsgExchReceiver != null) {
430 this.mMsgExchReceiver.shutdownReceiver();
431 }
432 } catch (Exception ex) {
433 ex.printStackTrace();
434 } finally {
435 this.mMsgExchReceiver = null;
436 }
437 }
438
439
440
441
442
443
444
445 private void startMessageExchangeProcessing() throws JBIException {
446 try {
447
448 if (this.mMsgExchReceiver != null) {
449 this.mMsgExchReceiver.startProcessing();
450 }
451 } catch (Exception ex) {
452 String msg = MESSAGES
453 .getString("CRB000427_Failure_in_start_MessageExchangeProcessing");
454 LOG.error(msg, ex);
455 throw new JBIException(msg, ex);
456 }
457 }
458
459
460
461
462
463
464
465 private void stopMessageExchangeProcessing() throws JBIException {
466 try {
467
468 if (this.mMsgExchReceiver != null) {
469 this.mMsgExchReceiver.stopProcessing();
470 }
471 } catch (Exception ex) {
472 String msg = MESSAGES
473 .getString("CRB000428_Failure_in_stop_MessageExchangeProcessing");
474 LOG.error(msg, ex);
475 throw new JBIException(msg, ex);
476 }
477
478 }
479
480
481
482
483
484 private void openDeliveryChannel() {
485 RuntimeContext.getInstance().openDeliveryChannel();
486 }
487
488
489
490
491 private void closeDeliveryChannel() {
492 RuntimeContext.getInstance().closeDeliveryChannel();
493 }
494
495
496
497
498 protected abstract ObjectName createExtensionMBeanName();
499
500
501
502
503 protected abstract StandardMBean createExtensionMBean();
504
505
506
507
508
509
510
511 protected abstract void initMessageExchangeHandlerFactory()
512 throws JBIException;
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527 protected abstract MessageExchangeReceiver createMessageExchangeReceiver()
528 throws Exception;
529
530
531
532
533
534
535
536
537
538
539 protected abstract void activateServiceProviders() throws JBIException;
540
541
542
543
544
545
546
547
548 protected abstract void deactivateServiceProviders() throws JBIException;
549
550
551
552
553
554
555
556
557
558
559
560 protected abstract void activateServiceConsumers() throws JBIException;
561
562
563
564
565
566
567
568
569
570 protected abstract void deactivateServiceConsumers() throws JBIException;
571
572
573
574
575
576
577 protected void onInit() throws JBIException {
578
579 }
580
581
582
583
584
585
586 protected void onStart() throws JBIException {
587
588 }
589
590
591
592
593
594
595 protected void onStop() throws JBIException {
596
597 }
598
599
600
601
602
603
604 protected void onShutDown() throws JBIException {
605
606 }
607
608 }