KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > web > jetty50 > jmx > JSR77ConfigurationMBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id$
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.web.jetty50.jmx;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.management.MBeanException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import org.mortbay.jetty.servlet.ServletHolder;
35 import org.mortbay.jetty.servlet.WebApplicationContext;
36 import org.mortbay.jetty.servlet.jmx.ConfigurationMBean;
37 import org.mortbay.util.LogSupport;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 import org.objectweb.jonas.web.jetty50.JSR77Configuration;
43
44 /**
45  * JSR77Configuration MBean.
46  * It will register/deregister MBeans for the given WebApp.
47  * Expected MBeans creation :
48  * - 1 WebModule
49  * - n Servlet
50  *
51  * @author Guillaume Sauthier
52  */

53 public class JSR77ConfigurationMBean extends ConfigurationMBean {
54
55     /**
56      * logger
57      */

58     private static Log log = LogFactory.getLog(JSR77ConfigurationMBean.class);
59
60     /**
61      * MBeans map, used for deregistration
62      */

63     private Map JavaDoc jsr77MBeanMap = new HashMap JavaDoc();
64
65     /**
66      * Default constructor
67      * @throws MBeanException if parent throws MBeanException
68      */

69     public JSR77ConfigurationMBean() throws MBeanException JavaDoc {
70         super();
71     }
72
73     /**
74      * We do not add any attributes/operations
75      */

76     protected void defineManagedResource() {
77         super.defineManagedResource();
78     }
79
80     /**
81      * Register the other jsr77 mbeans
82      * @param ok Boolean
83      *
84      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
85      */

86
87     public void postRegister(Boolean JavaDoc ok) {
88         super.postRegister(ok);
89         try {
90             defineJsr77MBeans();
91         } catch (Exception JavaDoc e) {
92             log.warn(LogSupport.EXCEPTION, e);
93         }
94     }
95
96     /**
97      * Deregister also all of the jsr77 mbeans we were
98      * responsible for registering.
99      *
100      * @see javax.management.MBeanRegistration#postDeregister()
101      */

102     public void postDeregister() {
103         Iterator JavaDoc itor = jsr77MBeanMap.entrySet().iterator();
104         while (itor.hasNext()) {
105             try {
106                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) itor.next();
107                 getMBeanServer().unregisterMBean((ObjectName JavaDoc) entry.getValue());
108             } catch (Exception JavaDoc e) {
109                 log.warn(LogSupport.EXCEPTION, e);
110             }
111         }
112     }
113
114     /**
115      * Make and register an mbean for each of the jsr77
116      * servlet stats.
117      * Make the WebModule MBean
118      *
119      * @throws Exception if registration fails
120      */

121     private void defineJsr77MBeans() throws Exception JavaDoc {
122         WebApplicationContext context = ((JSR77Configuration) _config).getWebApplicationContext();
123
124         // create a WebModuleMBean
125
J2EEWebModuleMBean wm = new J2EEWebModuleMBean();
126         wm.setManagedResource(context, "objectReference");
127         wm.setBaseObjectName(getBaseObjectName().toString());
128         ObjectName JavaDoc oName = getMBeanServer().registerMBean(wm, null).getObjectName();
129         jsr77MBeanMap.put(context.getName(), oName);
130
131         // TODO a WebModule must have an attribute (servlets) for Servlet's ObjectName
132

133         ServletHolder[] servlets = context.getServletHandler().getServlets();
134         for (int i = 0; null != servlets && i < servlets.length; i++) {
135             FixedJsr77ServletHolderMBean mbean = new FixedJsr77ServletHolderMBean();
136             mbean.setManagedResource(servlets[i], "objectReference");
137             mbean.setBaseObjectName(getBaseObjectName().toString());
138             // add necessary data
139
mbean.setJ2EEDomainName((String JavaDoc) context.getAttribute("J2EEDomainName"));
140             mbean.setJ2EEApplicationName((String JavaDoc) context.getAttribute("J2EEApplicationName"));
141             mbean.setJ2EEServerName((String JavaDoc) context.getAttribute("J2EEServerName"));
142             oName = getMBeanServer().registerMBean(mbean, null).getObjectName();
143             jsr77MBeanMap.put(servlets[i].getName(), oName);
144         }
145     }
146
147 }
148
Popular Tags