1 16 17 package org.apache.jk.common; 18 19 20 import org.apache.jk.core.JkHandler; 21 22 import javax.management.MBeanServer ; 23 import javax.management.ObjectName ; 24 import javax.management.Attribute ; 25 import javax.management.MBeanServerFactory ; 26 import java.io.IOException ; 27 28 43 public class JkMX extends JkHandler 44 { 45 MBeanServer mserver; 46 private boolean enabled=false; 47 private boolean log4jEnabled=true; 48 private int httpport=-1; 49 private String httphost="localhost"; 50 private String authmode="none"; 51 private String authuser=null; 52 private String authpassword=null; 53 private int jrmpport=-1; 54 private String jrmphost="localhost"; 55 private boolean useXSLTProcessor = true; 56 57 public JkMX() { 58 } 59 60 61 62 64 public void setEnabled(boolean b) { 65 enabled=b; 66 } 67 68 public boolean getEnabled() { 69 return enabled; 70 } 71 72 74 public void setLog4jEnabled(boolean b) { 75 log4jEnabled=b; 76 } 77 78 public boolean getLog4jEnabled() { 79 return log4jEnabled; 80 } 81 82 84 public void setPort(int i) { 85 enabled=(i != -1); 86 } 87 88 public int getPort() { 89 return ((httpport != -1) ? httpport : jrmpport); 90 } 91 92 94 public void setHttpPort( int i ) { 95 httpport=i; 96 } 97 98 public int getHttpPort() { 99 return httpport; 100 } 101 102 public void setHttpHost(String host ) { 103 this.httphost=host; 104 } 105 106 public String getHttpHost() { 107 return httphost; 108 } 109 110 public void setAuthMode(String mode) { 111 authmode=mode; 112 } 113 114 public String getAuthMode() { 115 return authmode; 116 } 117 118 public void setAuthUser(String user) { 119 authuser=user; 120 } 121 122 public String getAuthUser() { 123 return authuser; 124 } 125 126 public void setAuthPassword(String password) { 127 authpassword=password; 128 } 129 130 public String getAuthPassword() { 131 return authpassword; 132 } 133 134 136 public void setJrmpPort( int i ) { 137 jrmpport=i; 138 } 139 140 public int getJrmpPort() { 141 return jrmpport; 142 } 143 144 public void setJrmpHost(String host ) { 145 this.jrmphost=host; 146 } 147 148 public String getJrmpHost() { 149 return jrmphost; 150 } 151 152 public boolean getUseXSLTProcessor() { 153 return useXSLTProcessor; 154 } 155 156 public void setUseXSLTProcessor(boolean uxsltp) { 157 useXSLTProcessor = uxsltp; 158 } 159 160 161 ObjectName httpServerName=null; 162 ObjectName jrmpServerName=null; 163 164 167 public void loadAdapter() throws IOException { 168 boolean httpAdapterLoaded = false; 169 boolean jrmpAdapterLoaded = false; 170 171 if ((httpport != -1) && classExists("mx4j.adaptor.http.HttpAdaptor")) { 172 try { 173 httpServerName = registerObject("mx4j.adaptor.http.HttpAdaptor", 174 "Http:name=HttpAdaptor"); 175 176 177 if( httphost!=null ) 178 mserver.setAttribute(httpServerName, new Attribute ("Host", httphost)); 179 mserver.setAttribute(httpServerName, new Attribute ("Port", new Integer (httpport))); 180 181 if( "none".equals(authmode) || "basic".equals(authmode) || "digest".equals(authmode) ) 182 mserver.setAttribute(httpServerName, new Attribute ("AuthenticationMethod", authmode)); 183 184 if( authuser!=null && authpassword!=null ) 185 mserver.invoke(httpServerName, "addAuthorization", 186 new Object [] { 187 authuser, 188 authpassword}, 189 new String [] { "java.lang.String", "java.lang.String" }); 190 191 if(useXSLTProcessor) { 192 ObjectName processorName = registerObject("mx4j.adaptor.http.XSLTProcessor", 193 "Http:name=XSLTProcessor"); 194 mserver.setAttribute(httpServerName, new Attribute ("ProcessorName", processorName)); 195 } 196 197 mserver.invoke(httpServerName, "start", null, null); 199 200 log.info( "Started MX4J console on host " + httphost + " at port " + httpport); 201 202 httpAdapterLoaded = true; 203 204 } catch( Throwable t ) { 205 httpServerName=null; 206 log.error( "Can't load the MX4J http adapter ", t ); 207 } 208 } 209 210 if ((httpport != -1) && (!httpAdapterLoaded) && classExists("mx4j.tools.adaptor.http.HttpAdaptor")) { 211 try { 212 httpServerName = registerObject("mx4j.tools.adaptor.http.HttpAdaptor", 213 "Http:name=HttpAdaptor"); 214 215 216 if( httphost!=null ) 217 mserver.setAttribute(httpServerName, new Attribute ("Host", httphost)); 218 mserver.setAttribute(httpServerName, new Attribute ("Port", new Integer (httpport))); 219 220 if( "none".equals(authmode) || "basic".equals(authmode) || "digest".equals(authmode) ) 221 mserver.setAttribute(httpServerName, new Attribute ("AuthenticationMethod", authmode)); 222 223 if( authuser!=null && authpassword!=null ) 224 mserver.invoke(httpServerName, "addAuthorization", 225 new Object [] { 226 authuser, 227 authpassword}, 228 new String [] { "java.lang.String", "java.lang.String" }); 229 230 if(useXSLTProcessor) { 231 ObjectName processorName = registerObject("mx4j.tools.adaptor.http.XSLTProcessor", 232 "Http:name=XSLTProcessor"); 233 mserver.setAttribute(httpServerName, new Attribute ("ProcessorName", processorName)); 234 } 235 mserver.invoke(httpServerName, "start", null, null); 237 if(log.isInfoEnabled()) 238 log.info( "Started MX4J console on host " + httphost + " at port " + httpport); 239 240 httpAdapterLoaded = true; 241 242 } catch( Throwable t ) { 243 httpServerName=null; 244 log.error( "Can't load the MX4J http adapter ", t ); 245 } 246 } 247 248 if ((jrmpport != -1) && classExists("mx4j.tools.naming.NamingService")) { 249 try { 250 jrmpServerName = registerObject("mx4j.tools.naming.NamingService", 251 "Naming:name=rmiregistry"); 252 mserver.setAttribute(jrmpServerName, new Attribute ("Port", 253 new Integer (jrmpport))); 254 mserver.invoke(jrmpServerName, "start", null, null); 255 if(log.isInfoEnabled()) 256 log.info( "Creating " + jrmpServerName ); 257 258 ObjectName adaptor = registerObject("mx4j.adaptor.rmi.jrmp.JRMPAdaptor", 260 "Adaptor:protocol=jrmp"); 261 262 263 mserver.setAttribute(adaptor, new Attribute ("JNDIName", "jrmp")); 264 265 mserver.invoke( adaptor, "putNamingProperty", 266 new Object [] { 267 javax.naming.Context.INITIAL_CONTEXT_FACTORY, 268 "com.sun.jndi.rmi.registry.RegistryContextFactory"}, 269 new String [] { "java.lang.Object", "java.lang.Object" }); 270 271 String jrpmurl = "rmi://" + jrmphost + ":" + Integer.toString(jrmpport) ; 272 273 mserver.invoke( adaptor, "putNamingProperty", 274 new Object [] { 275 javax.naming.Context.PROVIDER_URL, 276 jrpmurl}, 277 new String [] { "java.lang.Object", "java.lang.Object" }); 278 279 mserver.invoke(adaptor, "start", null, null); 281 if(log.isInfoEnabled()) 282 log.info( "Creating " + adaptor + " on host " + jrmphost + " at port " + jrmpport); 283 284 jrmpAdapterLoaded = true; 285 286 } catch( Exception ex ) { 287 jrmpServerName = null; 288 log.error( "MX4j RMI adapter not loaded: " + ex.toString()); 289 } 290 } 291 292 if ((httpport != -1) && (! httpAdapterLoaded) && classExists("com.sun.jdmk.comm.HtmlAdaptorServer")) { 293 try { 294 httpServerName=registerObject("com.sun.jdmk.comm.HtmlAdaptorServer", 295 "Adaptor:name=html,port=" + httpport); 296 if(log.isInfoEnabled()) 297 log.info("Registering the JMX_RI html adapter " + httpServerName + " at port " + httpport); 298 299 mserver.setAttribute(httpServerName, 300 new Attribute ("Port", new Integer (httpport))); 301 302 mserver.invoke(httpServerName, "start", null, null); 303 304 httpAdapterLoaded = true; 305 } catch( Throwable t ) { 306 httpServerName = null; 307 log.error( "Can't load the JMX_RI http adapter " + t.toString() ); 308 } 309 } 310 311 if ((!httpAdapterLoaded) && (!jrmpAdapterLoaded)) 312 log.warn( "No adaptors were loaded but mx.enabled was defined."); 313 314 } 315 316 public void destroy() { 317 try { 318 if(log.isInfoEnabled()) 319 log.info("Stoping JMX "); 320 321 if( httpServerName!=null ) { 322 mserver.invoke(httpServerName, "stop", null, null); 323 } 324 if( jrmpServerName!=null ) { 325 mserver.invoke(jrmpServerName, "stop", null, null); 326 } 327 } catch( Throwable t ) { 328 log.error( "Destroy error" + t ); 329 } 330 } 331 332 public void init() throws IOException { 333 try { 334 mserver = getMBeanServer(); 335 336 if( enabled ) { 337 loadAdapter(); 338 } 339 if( log4jEnabled) { 340 try { 341 registerObject("org.apache.log4j.jmx.HierarchyDynamicMBean" , 342 "log4j:hierarchy=default"); 343 if(log.isInfoEnabled()) 344 log.info("Registering the JMX hierarchy for Log4J "); 345 } catch( Throwable t ) { 346 if(log.isInfoEnabled()) 347 log.info("Can't enable log4j mx: ",t); 348 } 349 } 350 } catch( Throwable t ) { 351 log.error( "Init error", t ); 352 } 353 } 354 355 public void addHandlerCallback( JkHandler w ) { 356 } 357 358 MBeanServer getMBeanServer() { 359 MBeanServer server; 360 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) { 361 server=(MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 362 } else { 363 server=MBeanServerFactory.createMBeanServer(); 364 } 365 return (server); 366 } 367 368 369 private static boolean classExists(String className) { 370 try { 371 Thread.currentThread().getContextClassLoader().loadClass(className); 372 return true; 373 } catch(Throwable e) { 374 if (log.isInfoEnabled()) 375 log.info( "className [" + className + "] does not exist"); 376 return false; 377 } 378 } 379 380 private ObjectName registerObject(String className, String oName) 381 throws Exception { 382 Class c = Class.forName(className); 383 Object o = c.newInstance(); 384 ObjectName objN = new ObjectName (oName); 385 mserver.registerMBean(o, objN); 386 return objN; 387 } 388 389 private static org.apache.commons.logging.Log log= 390 org.apache.commons.logging.LogFactory.getLog( JkMX.class ); 391 392 393 } 394 395 | Popular Tags |