001: /**
002: *
003: */package clime.messadmin.providers.jmx;
004:
005: import java.lang.management.ManagementFactory;
006:
007: import javax.management.MBeanRegistrationException;
008: import javax.management.MBeanServer;
009: import javax.management.ObjectName;
010: import javax.management.OperationsException;
011: import javax.servlet.ServletContext;
012: import javax.servlet.http.HttpSession;
013:
014: import clime.messadmin.jmx.mbeans.Session;
015: import clime.messadmin.jmx.mbeans.WebApp;
016: import clime.messadmin.providers.spi.ApplicationLifeCycleProvider;
017: import clime.messadmin.providers.spi.SessionLifeCycleProvider;
018: import clime.messadmin.utils.SessionUtils;
019:
020: /**
021: * Tomcat needs to be started with JMX enabled (warning: insecure setup!):
022: * JAVA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
023: * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html">Monitoring and Management Using JMX</a>
024: *
025: * @author Cédrik LIME
026: */
027: public class JMXProvider implements ApplicationLifeCycleProvider,
028: SessionLifeCycleProvider {
029: private static final String WEBAPP_OBJECTNAME_PRE = "messadmin.";//$NON-NLS-1$
030: private static final String WEBAPP_OBJECTNAME_POST = ":type=WebApp";//$NON-NLS-1$
031: private static final String SESSION_OBJECTNAME_PRE = "messadmin.";//$NON-NLS-1$
032: private static final String SESSION_OBJECTNAME_POST = ".sessions:type=";//$NON-NLS-1$
033:
034: /**
035: *
036: */
037: public JMXProvider() {
038: super ();
039: }
040:
041: /**
042: * {@inheritDoc}
043: */
044: public int getPriority() {
045: return 0;
046: }
047:
048: /**
049: * register MBean for this session
050: * @see clime.messadmin.providers.spi.SessionLifeCycleProvider#sessionCreated(javax.servlet.http.HttpSession)
051: */
052: public void sessionCreated(HttpSession httpSession) {
053: String context = getContext(httpSession);
054: String sessionId = httpSession.getId();
055: String beanName = getBeanName(httpSession);
056: MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
057: try {
058: Session sessionBean = new Session();
059: sessionBean.setContext(context);
060: sessionBean.setSessionId(sessionId);
061: ObjectName name = ObjectName
062: .getInstance(SESSION_OBJECTNAME_PRE + context
063: + SESSION_OBJECTNAME_POST + beanName);
064: mbs.registerMBean(sessionBean, name);
065: } catch (OperationsException oe) {
066: throw new RuntimeException(oe);
067: } catch (MBeanRegistrationException e) {
068: throw new RuntimeException(e);
069: } catch (NullPointerException npe) {
070: throw npe;
071: }
072: }
073:
074: /**
075: * unregister MBean for this session
076: * @see clime.messadmin.providers.spi.SessionLifeCycleProvider#sessionDestroyed(javax.servlet.http.HttpSession)
077: */
078: public void sessionDestroyed(HttpSession httpSession) {
079: String context = getContext(httpSession);
080: String beanName = getBeanName(httpSession);
081: MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
082: try {
083: ObjectName name = ObjectName
084: .getInstance(SESSION_OBJECTNAME_PRE + context
085: + SESSION_OBJECTNAME_POST + beanName);
086: mbs.unregisterMBean(name);
087: } catch (OperationsException oe) {
088: throw new RuntimeException(oe);
089: } catch (MBeanRegistrationException e) {
090: throw new RuntimeException(e);
091: } catch (NullPointerException npe) {
092: throw npe;
093: }
094: }
095:
096: /**
097: * {@inheritDoc}
098: */
099: public void sessionWillPassivate(HttpSession httpSession) {
100: sessionDestroyed(httpSession);
101: }
102:
103: /**
104: * {@inheritDoc}
105: */
106: public void sessionDidActivate(HttpSession httpSession) {
107: sessionCreated(httpSession);
108: }
109:
110: /**
111: * {@inheritDoc}
112: */
113: public void contextInitialized(ServletContext servletContext) {
114: String context = getContext(servletContext);
115: MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
116: try {
117: WebApp webAppMBean = new WebApp();
118: webAppMBean.setContext(context);
119: ObjectName webAppName = ObjectName
120: .getInstance(WEBAPP_OBJECTNAME_PRE + context
121: + WEBAPP_OBJECTNAME_POST);
122: mbs.registerMBean(webAppMBean, webAppName);
123: } catch (OperationsException oe) {
124: throw new RuntimeException(oe);
125: } catch (MBeanRegistrationException e) {
126: throw new RuntimeException(e);
127: } catch (NullPointerException npe) {
128: throw npe;
129: }
130: }
131:
132: /**
133: * {@inheritDoc}
134: */
135: public void contextDestroyed(ServletContext servletContext) {
136: String context = getContext(servletContext);
137: MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
138: try {
139: ObjectName webAppName = ObjectName
140: .getInstance(WEBAPP_OBJECTNAME_PRE + context
141: + WEBAPP_OBJECTNAME_POST);
142: mbs.unregisterMBean(webAppName);
143: } catch (OperationsException oe) {
144: throw new RuntimeException(oe);
145: } catch (MBeanRegistrationException e) {
146: throw new RuntimeException(e);
147: } catch (NullPointerException npe) {
148: throw npe;
149: }
150: }
151:
152: protected String getContext(HttpSession httpSession) {
153: String context = SessionUtils.getContext(httpSession);
154: return context;
155: }
156:
157: protected String getBeanName(HttpSession httpSession) {
158: String beanName = httpSession.getId();//TODO find a better name (session user name?)
159: return beanName;
160: }
161:
162: protected String getContext(ServletContext servletContext) {
163: String context = SessionUtils.getContext(servletContext);
164: return context;
165: }
166: }
|