1 16 package org.mortbay.http.jmx; 17 18 import java.util.HashMap ; 19 20 import javax.management.MBeanException ; 21 import javax.management.MBeanServer ; 22 import javax.management.ObjectName ; 23 24 import org.apache.commons.logging.Log; 25 import org.mortbay.log.LogFactory; 26 import org.mortbay.http.HttpContext; 27 import org.mortbay.util.LifeCycleEvent; 28 import org.mortbay.util.LifeCycleListener; 29 import org.mortbay.util.LogSupport; 30 import org.mortbay.util.jmx.LifeCycleMBean; 31 32 33 34 39 public class HttpContextMBean extends LifeCycleMBean 40 { 41 private static Log log = LogFactory.getLog(HttpContextMBean.class); 42 43 private HttpContext _httpContext; 44 private HashMap _rlMap=new HashMap (3); 45 private HashMap _handlerMap = new HashMap (); 46 47 48 51 public HttpContextMBean() 52 throws MBeanException 53 {} 54 55 56 protected void defineManagedResource() 57 { 58 super.defineManagedResource(); 59 60 defineAttribute("virtualHosts"); 61 defineAttribute("hosts"); 62 defineAttribute("contextPath"); 63 64 defineAttribute("handlers",READ_ONLY,ON_MBEAN); 65 defineAttribute("requestLog",READ_ONLY,ON_MBEAN); 66 67 defineAttribute("classPath"); 68 69 defineAttribute("realm"); 70 defineAttribute("realmName"); 71 72 defineAttribute("redirectNullPath"); 73 defineAttribute("resourceBase"); 74 defineAttribute("maxCachedFileSize"); 75 defineAttribute("maxCacheSize"); 76 defineOperation("flushCache", 77 IMPACT_ACTION); 78 defineOperation("getResource", 79 new String [] {STRING}, 80 IMPACT_ACTION); 81 82 defineAttribute("welcomeFiles"); 83 defineOperation("addWelcomeFile", 84 new String [] {STRING}, 85 IMPACT_INFO); 86 defineOperation("removeWelcomeFile", 87 new String [] {STRING}, 88 IMPACT_INFO); 89 90 defineAttribute("mimeMap"); 91 defineOperation("setMimeMapping",new String [] {STRING,STRING},IMPACT_ACTION); 92 93 94 defineAttribute("statsOn"); 95 defineAttribute("statsOnMs"); 96 defineOperation("statsReset",IMPACT_ACTION); 97 defineAttribute("requests"); 98 defineAttribute("requestsActive"); 99 defineAttribute("requestsActiveMax"); 100 defineAttribute("responses1xx"); 101 defineAttribute("responses2xx"); 102 defineAttribute("responses3xx"); 103 defineAttribute("responses4xx"); 104 defineAttribute("responses5xx"); 105 106 defineOperation("stop",new String [] {"java.lang.Boolean.TYPE"},IMPACT_ACTION); 107 108 defineOperation("destroy", 109 IMPACT_ACTION); 110 111 defineOperation("setInitParameter", 112 new String [] {STRING,STRING}, 113 IMPACT_ACTION); 114 defineOperation("getInitParameter", 115 new String [] {STRING}, 116 IMPACT_INFO); 117 defineOperation("getInitParameterNames", 118 NO_PARAMS, 119 IMPACT_INFO); 120 121 defineOperation("setAttribute",new String [] {STRING,OBJECT},IMPACT_ACTION); 122 defineOperation("getAttribute",new String [] {STRING},IMPACT_INFO); 123 defineOperation("getAttributeNames",NO_PARAMS,IMPACT_INFO); 124 defineOperation("removeAttribute",new String [] {STRING},IMPACT_ACTION); 125 126 defineOperation("addHandler",new String [] {"org.mortbay.http.HttpHandler"},IMPACT_ACTION); 127 defineOperation("addHandler",new String [] {INT,"org.mortbay.http.HttpHandler"},IMPACT_ACTION); 128 defineOperation("removeHandler",new String [] {INT},IMPACT_ACTION); 129 130 131 _httpContext=(HttpContext)getManagedResource(); 132 133 _httpContext.addEventListener(new LifeCycleListener() 134 { 135 136 public void lifeCycleStarting (LifeCycleEvent event) 137 {} 138 139 public void lifeCycleStarted (LifeCycleEvent event) 140 { 141 getHandlers(); 142 } 143 144 public void lifeCycleFailure (LifeCycleEvent event) 145 {} 146 147 public void lifeCycleStopping (LifeCycleEvent event) 148 {} 149 150 public void lifeCycleStopped (LifeCycleEvent event) 151 { 152 destroyHandlers(); 153 } 154 155 }); 156 } 157 158 159 160 protected ObjectName newObjectName(MBeanServer server) 161 { 162 ObjectName oName=super.newObjectName(server); 163 String context=_httpContext.getContextPath(); 164 if (context.length()==0) 165 context="/"; 166 try{oName=new ObjectName (oName+",context="+context);} 167 catch(Exception e){log.warn(LogSupport.EXCEPTION,e);} 168 return oName; 169 } 170 171 172 public void postRegister(Boolean ok) 173 { 174 super.postRegister(ok); 175 if (ok.booleanValue()) 176 getHandlers(); 177 } 178 179 180 public void postDeregister() 181 { 182 _httpContext=null; 183 destroyComponentMBeans(_handlerMap); 184 super.postDeregister(); 185 } 186 187 188 public ObjectName [] getHandlers() 189 { 190 return getComponentMBeans(_httpContext.getHandlers(),_handlerMap); 191 } 192 193 194 public void destroyHandlers() 195 { 196 destroyComponentMBeans(_handlerMap); 197 } 198 199 200 public ObjectName getRequestLog() 201 { 202 Object o = _httpContext.getRequestLog(); 203 if (o==null) 204 return null; 205 206 ObjectName [] on=getComponentMBeans(new Object []{o},_rlMap); 207 if (on.length>0) 208 return on[0]; 209 return null; 210 } 211 212 } 213 214 215 | Popular Tags |