1 16 package org.mortbay.http.jmx; 17 18 import java.util.HashMap ; 19 20 import javax.management.InstanceNotFoundException ; 21 import javax.management.MBeanException ; 22 import javax.management.ObjectName ; 23 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 24 import javax.management.modelmbean.ModelMBean ; 25 26 import org.apache.commons.logging.Log; 27 import org.mortbay.log.LogFactory; 28 import org.mortbay.http.HttpServer; 29 import org.mortbay.http.Version; 30 import org.mortbay.util.ComponentEvent; 31 import org.mortbay.util.ComponentListener; 32 import org.mortbay.util.LogSupport; 33 import org.mortbay.util.jmx.LifeCycleMBean; 34 import org.mortbay.util.jmx.ModelMBeanImpl; 35 36 37 46 public class HttpServerMBean extends LifeCycleMBean 47 implements ComponentListener 48 { 49 private static Log log = LogFactory.getLog(HttpServerMBean.class); 50 51 private HttpServer _httpServer; 52 private HashMap _mbeanMap = new HashMap (); 53 54 55 59 protected HttpServerMBean(HttpServer httpServer) 60 throws MBeanException , InstanceNotFoundException 61 { 62 _httpServer=httpServer; 63 _httpServer.addEventListener(this); 64 try{super.setManagedResource(_httpServer,"objectReference");} 65 catch(InvalidTargetObjectTypeException e){log.warn(LogSupport.EXCEPTION,e);} 66 } 67 68 69 73 public HttpServerMBean() 74 throws MBeanException , InstanceNotFoundException 75 { 76 this(new HttpServer()); 77 } 78 79 80 public void setManagedResource(Object o,String s) 81 throws MBeanException , InstanceNotFoundException , InvalidTargetObjectTypeException 82 { 83 if (o!=null) 84 ((HttpServer)o).addEventListener(this); 85 super.setManagedResource(o,s); 86 } 87 88 89 protected void defineManagedResource() 90 { 91 super.defineManagedResource(); 92 93 defineAttribute("listeners",READ_ONLY); 94 defineAttribute("contexts",READ_ONLY); 95 defineAttribute("version",READ_ONLY,ON_MBEAN); 96 defineAttribute("components",READ_ONLY,ON_MBEAN); 97 defineAttribute("requestLog"); 98 99 defineAttribute("trace"); 100 101 102 defineOperation("addListener",new String []{"java.lang.String"},IMPACT_ACTION); 103 defineOperation("addListener",new String []{"org.mortbay.util.InetAddrPort"},IMPACT_ACTION); 104 defineOperation("addListener",new String []{"org.mortbay.http.HttpListener"},IMPACT_ACTION); 105 defineOperation("removeListener",new String []{"org.mortbay.http.HttpListener"},IMPACT_ACTION); 106 defineOperation("addContext",new String []{"org.mortbay.http.HttpContext"},IMPACT_ACTION); 107 defineOperation("removeContext",new String []{"org.mortbay.http.HttpContext"},IMPACT_ACTION); 108 defineOperation("addContext",new String []{"java.lang.String"},IMPACT_ACTION); 109 defineOperation("addContext",new String []{"java.lang.String","java.lang.String"},IMPACT_ACTION); 110 111 defineAttribute("requestsPerGC"); 112 113 defineAttribute("statsOn"); 114 defineAttribute("statsOnMs"); 115 defineOperation("statsReset",IMPACT_ACTION); 116 defineAttribute("connections"); 117 defineAttribute("connectionsOpen"); 118 defineAttribute("connectionsOpenMin"); 119 defineAttribute("connectionsOpenMax"); 120 defineAttribute("connectionsDurationAve"); 121 defineAttribute("connectionsDurationMin"); 122 defineAttribute("connectionsDurationMax"); 123 defineAttribute("connectionsDurationTotal"); 124 defineAttribute("connectionsRequestsAve"); 125 defineAttribute("connectionsRequestsMin"); 126 defineAttribute("connectionsRequestsMax"); 127 defineAttribute("errors"); 128 defineAttribute("requests"); 129 defineAttribute("requestsActive"); 130 defineAttribute("requestsActiveMin"); 131 defineAttribute("requestsActiveMax"); 132 defineAttribute("requestsDurationAve"); 133 defineAttribute("requestsDurationMin"); 134 defineAttribute("requestsDurationMax"); 135 136 defineOperation("stop",new String []{"java.lang.Boolean.TYPE"},IMPACT_ACTION); 137 defineOperation("save",new String []{"java.lang.String"},IMPACT_ACTION); 138 defineOperation("destroy",IMPACT_ACTION); 139 } 140 141 142 public synchronized void addComponent(ComponentEvent event) 143 { 144 try 145 { 146 if(log.isDebugEnabled())log.debug("Component added "+event); 147 Object o=event.getComponent(); 148 149 ModelMBean mbean=ModelMBeanImpl.mbeanFor(o); 150 if (mbean==null) 151 log.warn("No MBean for "+o); 152 else 153 { 154 ObjectName oName=null; 155 if (mbean instanceof ModelMBeanImpl) 156 { 157 ((ModelMBeanImpl)mbean).setBaseObjectName(getObjectName().toString()); 158 oName= 159 getMBeanServer().registerMBean(mbean,null).getObjectName(); 160 } 161 else 162 { 163 oName=uniqueObjectName(getMBeanServer(), 164 o, 165 getObjectName().toString()); 166 oName=getMBeanServer().registerMBean(mbean,oName) 167 .getObjectName(); 168 } 169 Holder holder = new Holder(oName,mbean); 170 _mbeanMap.put(o,holder); 171 } 172 } 173 catch(Exception e) 174 { 175 log.warn(LogSupport.EXCEPTION,e); 176 } 177 } 178 179 180 public String getVersion() 181 { 182 return Version.getDetail(); 183 } 184 185 186 187 public ObjectName [] getComponents() 188 { 189 Holder[] h=(Holder[])_mbeanMap.values().toArray(new Holder[_mbeanMap.size()]); 190 ObjectName [] on = new ObjectName [h.length]; 191 for (int i=0;i<on.length;i++) 192 on[i]=h[i].oName; 193 return on; 194 } 195 196 197 198 public synchronized void removeComponent(ComponentEvent event) 199 { 200 if(log.isDebugEnabled())log.debug("Component removed "+event); 201 202 try 203 { 204 Object o=event.getComponent(); 205 Holder holder=(Holder)_mbeanMap.remove(o); 206 if (holder!=null) 207 getMBeanServer().unregisterMBean(holder.oName); 208 else if (o==_httpServer) 209 getMBeanServer().unregisterMBean(this.getObjectName()); 210 } 211 catch(Exception e) 212 { 213 log.warn(LogSupport.EXCEPTION,e); 214 } 215 } 216 217 218 221 public void postRegister(Boolean ok) 222 { 223 super.postRegister(ok); 224 } 225 226 227 public void postDeregister() 228 { 229 _httpServer.removeEventListener(this); 230 _httpServer=null; 231 if (_mbeanMap!=null) 232 _mbeanMap.clear(); 233 _mbeanMap=null; 234 235 super.postDeregister(); 236 } 237 238 239 240 private static class Holder 241 { 242 Holder(ObjectName oName,Object mbean){ this.oName=oName; this.mbean=mbean;} 243 ObjectName oName; 244 Object mbean; 245 } 246 } 247 248 | Popular Tags |