RecordSessionEJBLifecycleListener.java
01 package com.bea.medrec.controller;
02 
03 import javax.management.MBeanServer;
04 import javax.management.MalformedObjectNameException;
05 import javax.management.ObjectName;
06 import javax.naming.InitialContext;
07 import weblogic.application.ApplicationContext;
08 import weblogic.application.ApplicationLifecycleEvent;
09 import weblogic.application.ApplicationLifecycleListener;
10 
11 /**
12  <p>Manages the life cycle of an MBean that keeps track of how many
13  * times RecordSessionEJB writes a prescription to the database.</p>
14  <p>The WebLogic Server deployment service emits notifications
15  * (ApplicationLifecycleEvent objects) at specific parts of an 
16  * application's life cycle. When an ApplicationLifecyceListener
17  * receives such a notification, it invokes one of its methods.</p>
18  *
19  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
20  */
21 public class RecordSessionEJBLifecycleListener
22     extends ApplicationLifecycleListener {
23 
24   private static javax.management.MBeanServer server;
25 
26   /**
27    <p>To register the RecordSessionEJBMBean after RecordSessionEJB starts,
28    * this class implements the ApplicationLifecyceListener.postStart()
29    * method, which is invoked when the class receives notification that
30    * the EJB has started.</p>
31    <p>In this postStart() implementation, the listener looks up the MBeanServer 
32    * interface for the WebLogic Server Runtime MBean Sever. The interface is 
33    * registered in the JNDI tree. Because this listener class is not part of a 
34    * J2EE module, the JNDI name for the MBeanServer interface is 
35    * "java:comp/jmx/runtime". For code that runs inside a J2EE module, the 
36    * JNDI name is "java:comp/env/jmx/runtime".
37    */
38   public void postStart(ApplicationLifecycleEvent evt) {
39     try {
40       ApplicationContext appctx = evt.getApplicationContext();
41       String appID = appctx.getApplicationId();
42       ObjectName MBeanON = getMBeanON(appID);
43       RecordSessionEJBMBeanImpl mgmtClass = new RecordSessionEJBMBeanImpl();
44 
45       // get the mbean server..
46       InitialContext ctx = new InitialContext();
47       server = (MBeanServerctx.lookup("java:comp/jmx/runtime");
48 
49       // register the model MBean in the MBean server
50       server.registerMBean(mgmtClass, MBeanON);
51     catch (Exception e) {
52       e.printStackTrace();
53     }
54   }
55   
56   /**
57    <p>To unregister the RecordSessionEJBMBean when RecordSessionEJB stops,
58    * this class implements the ApplicationLifecyceListener.preStop()
59    * method, which is invoked when the class receives notification that
60    * the EJB is being stopped.</p>
61    */
62   public void preStop(ApplicationLifecycleEvent evt) {
63     try {
64       ApplicationContext appctx = evt.getApplicationContext();
65       String appID = appctx.getApplicationId();
66       ObjectName MBeanON = getMBeanON(appID);
67 
68       // Unregister the model MBean in the MBean server
69       server.unregisterMBean(MBeanON);
70 
71     catch (Exception e) {
72       e.printStackTrace();
73     }
74   }
75 
76   private ObjectName getMBeanON(String AppID)
77       throws MalformedObjectNameException {
78     // Create a JMX object name for the MBean
79     return new ObjectName("com.bea.medrec:Name=" + AppID +
80         ",Type=com.bea.medrec.controller.RecordSessionEJBMBean");
81   }
82 }