001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id$
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.web.jetty50.jmx;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029:
030: import javax.management.MBeanException;
031: import javax.management.ObjectName;
032:
033: import org.mortbay.jetty.servlet.ServletHolder;
034: import org.mortbay.jetty.servlet.WebApplicationContext;
035: import org.mortbay.jetty.servlet.jmx.ConfigurationMBean;
036: import org.mortbay.util.LogSupport;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: import org.objectweb.jonas.web.jetty50.JSR77Configuration;
042:
043: /**
044: * JSR77Configuration MBean.
045: * It will register/deregister MBeans for the given WebApp.
046: * Expected MBeans creation :
047: * - 1 WebModule
048: * - n Servlet
049: *
050: * @author Guillaume Sauthier
051: */
052: public class JSR77ConfigurationMBean extends ConfigurationMBean {
053:
054: /**
055: * logger
056: */
057: private static Log log = LogFactory
058: .getLog(JSR77ConfigurationMBean.class);
059:
060: /**
061: * MBeans map, used for deregistration
062: */
063: private Map jsr77MBeanMap = new HashMap();
064:
065: /**
066: * Default constructor
067: * @throws MBeanException if parent throws MBeanException
068: */
069: public JSR77ConfigurationMBean() throws MBeanException {
070: super ();
071: }
072:
073: /**
074: * We do not add any attributes/operations
075: */
076: protected void defineManagedResource() {
077: super .defineManagedResource();
078: }
079:
080: /**
081: * Register the other jsr77 mbeans
082: * @param ok Boolean
083: *
084: * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
085: */
086:
087: public void postRegister(Boolean ok) {
088: super .postRegister(ok);
089: try {
090: defineJsr77MBeans();
091: } catch (Exception e) {
092: log.warn(LogSupport.EXCEPTION, e);
093: }
094: }
095:
096: /**
097: * Deregister also all of the jsr77 mbeans we were
098: * responsible for registering.
099: *
100: * @see javax.management.MBeanRegistration#postDeregister()
101: */
102: public void postDeregister() {
103: Iterator itor = jsr77MBeanMap.entrySet().iterator();
104: while (itor.hasNext()) {
105: try {
106: Map.Entry entry = (Map.Entry) itor.next();
107: getMBeanServer().unregisterMBean(
108: (ObjectName) entry.getValue());
109: } catch (Exception e) {
110: log.warn(LogSupport.EXCEPTION, e);
111: }
112: }
113: }
114:
115: /**
116: * Make and register an mbean for each of the jsr77
117: * servlet stats.
118: * Make the WebModule MBean
119: *
120: * @throws Exception if registration fails
121: */
122: private void defineJsr77MBeans() throws Exception {
123: WebApplicationContext context = ((JSR77Configuration) _config)
124: .getWebApplicationContext();
125:
126: // create a WebModuleMBean
127: J2EEWebModuleMBean wm = new J2EEWebModuleMBean();
128: wm.setManagedResource(context, "objectReference");
129: wm.setBaseObjectName(getBaseObjectName().toString());
130: ObjectName oName = getMBeanServer().registerMBean(wm, null)
131: .getObjectName();
132: jsr77MBeanMap.put(context.getName(), oName);
133:
134: // TODO a WebModule must have an attribute (servlets) for Servlet's ObjectName
135:
136: ServletHolder[] servlets = context.getServletHandler()
137: .getServlets();
138: for (int i = 0; null != servlets && i < servlets.length; i++) {
139: FixedJsr77ServletHolderMBean mbean = new FixedJsr77ServletHolderMBean();
140: mbean.setManagedResource(servlets[i], "objectReference");
141: mbean.setBaseObjectName(getBaseObjectName().toString());
142: // add necessary data
143: mbean.setJ2EEDomainName((String) context
144: .getAttribute("J2EEDomainName"));
145: mbean.setJ2EEApplicationName((String) context
146: .getAttribute("J2EEApplicationName"));
147: mbean.setJ2EEServerName((String) context
148: .getAttribute("J2EEServerName"));
149: oName = getMBeanServer().registerMBean(mbean, null)
150: .getObjectName();
151: jsr77MBeanMap.put(servlets[i].getName(), oName);
152: }
153:
154: //set the attribute (servlets) for Servlet's ObjectName
155: wm.setServlets((ObjectName[]) jsr77MBeanMap.values().toArray(
156: new ObjectName[jsr77MBeanMap.values().size()]));
157: }
158:
159: }
|