1 16 17 package org.apache.webapp.admin; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.management.MBeanServer ; 26 import javax.management.ObjectName ; 27 28 29 39 40 public class Lists { 41 42 43 45 46 49 protected Lists() { } 50 51 52 54 55 58 private static List verbosityLevels = new ArrayList (); 59 60 static { 61 verbosityLevels.add(new LabelValueBean("0", "0")); 62 verbosityLevels.add(new LabelValueBean("1", "1")); 63 verbosityLevels.add(new LabelValueBean("2", "2")); 64 verbosityLevels.add(new LabelValueBean("3", "3")); 65 verbosityLevels.add(new LabelValueBean("4", "4")); 66 } 67 68 71 private static List booleanValues = new ArrayList (); 72 73 static { 74 booleanValues.add(new LabelValueBean("True", "true")); 75 booleanValues.add(new LabelValueBean("False", "false")); 76 } 77 78 81 private static List clientAuthValues = new ArrayList (); 82 83 static { 84 clientAuthValues.add(new LabelValueBean("True","true")); 85 clientAuthValues.add(new LabelValueBean("False","false")); 86 clientAuthValues.add(new LabelValueBean("Want","want")); 87 } 88 89 91 92 96 public static List getVerbosityLevels() { 97 98 return (verbosityLevels); 99 100 } 101 102 106 public static List getBooleanValues() { 107 108 return (booleanValues); 109 110 } 111 115 public static List getClientAuthValues() { 116 117 return (clientAuthValues); 118 119 } 120 121 130 public static List getConnectors(MBeanServer mbserver, ObjectName service) 131 throws Exception { 132 133 StringBuffer sb = new StringBuffer (service.getDomain()); 134 sb.append(":type=Connector,*"); 135 ObjectName search = new ObjectName (sb.toString()); 136 ArrayList connectors = new ArrayList (); 137 Iterator names = mbserver.queryNames(search, null).iterator(); 138 while (names.hasNext()) { 139 connectors.add(names.next().toString()); 140 } 141 Collections.sort(connectors); 142 return (connectors); 143 144 } 145 146 147 156 public static List getConnectors(MBeanServer mbserver, String service) 157 throws Exception { 158 159 return (getConnectors(mbserver, new ObjectName (service))); 160 161 } 162 163 164 173 public static List getContexts(MBeanServer mbserver, ObjectName host) 174 throws Exception { 175 176 StringBuffer sb = new StringBuffer (host.getDomain()); 177 sb.append(":j2eeType=WebModule,*"); 178 ObjectName search = new ObjectName (sb.toString()); 179 ArrayList contexts = new ArrayList (); 180 Iterator names = mbserver.queryNames(search, null).iterator(); 181 String name = null; 182 ObjectName oname = null; 183 String hostPrefix = "//"+host.getKeyProperty("host"); 184 String hostAttr = null; 185 while (names.hasNext()) { 186 name = names.next().toString(); 187 oname = new ObjectName (name); 188 hostAttr = oname.getKeyProperty("name"); 189 if (hostAttr.startsWith(hostPrefix)) { 190 contexts.add(name); 191 } 192 } 193 Collections.sort(contexts); 194 return (contexts); 195 196 } 197 198 199 211 public static List getDefaultContexts(MBeanServer mbserver, String 212 container) throws Exception { 213 214 return (getDefaultContexts(mbserver, new ObjectName (container))); 215 216 } 217 218 219 231 public static List getDefaultContexts(MBeanServer mbserver, ObjectName 232 container) throws Exception { 233 234 StringBuffer sb = new StringBuffer (container.getDomain()); 236 sb.append(":type=DefaultContext"); 237 String type = container.getKeyProperty("type"); 238 String host = container.getKeyProperty("host"); 239 if ("Host".equals(type)) { 240 host = container.getKeyProperty("host"); 241 } 242 if (host != null) { 243 sb.append(",host="); 244 sb.append(host); 245 } 246 ObjectName search = new ObjectName (sb.toString()); 247 ArrayList defaultContexts = new ArrayList (); 248 Iterator names = mbserver.queryNames(search, null).iterator(); 249 while (names.hasNext()) { 250 String name = names.next().toString(); 251 defaultContexts.add(name); 252 } 253 Collections.sort(defaultContexts); 254 return (defaultContexts); 255 256 } 257 258 259 268 public static List getContexts(MBeanServer mbserver, String host) 269 throws Exception { 270 271 return (getContexts(mbserver, new ObjectName (host))); 272 273 } 274 275 284 public static List getHosts(MBeanServer mbserver, ObjectName service) 285 throws Exception { 286 287 StringBuffer sb = new StringBuffer (service.getDomain()); 288 sb.append(":type=Host,*"); 289 ObjectName search = new ObjectName (sb.toString()); 290 ArrayList hosts = new ArrayList (); 291 Iterator names = mbserver.queryNames(search, null).iterator(); 292 while (names.hasNext()) { 293 hosts.add(names.next().toString()); 294 } 295 Collections.sort(hosts); 296 return (hosts); 297 298 } 299 300 301 310 public static List getHosts(MBeanServer mbserver, String service) 311 throws Exception { 312 313 return (getHosts(mbserver, new ObjectName (service))); 314 315 } 316 317 318 328 public static List getRealms(MBeanServer mbserver, ObjectName container) 329 throws Exception { 330 331 ObjectName search = getSearchObject(container, "Realm"); 332 ArrayList realms = new ArrayList (); 333 Iterator names = mbserver.queryNames(search, null).iterator(); 334 while (names.hasNext()) { 335 realms.add(names.next().toString()); 336 } 337 Collections.sort(realms); 338 return (realms); 339 340 } 341 342 343 353 public static List getRealms(MBeanServer mbserver, String container) 354 throws Exception { 355 356 return (getRealms(mbserver, new ObjectName (container))); 357 358 } 359 360 370 public static List getValves(MBeanServer mbserver, ObjectName container) 371 throws Exception { 372 373 StringBuffer sb = new StringBuffer (container.getDomain()); 374 sb.append(":type=Valve"); 375 String type = container.getKeyProperty("type"); 376 String j2eeType = container.getKeyProperty("j2eeType"); 377 sb.append(TomcatTreeBuilder.WILDCARD); 378 String host = ""; 379 String path = ""; 380 String name = container.getKeyProperty("name"); 381 if ((name != null) && (name.length() > 0)) { 382 name = name.substring(2); 384 int i = name.indexOf("/"); 385 host = name.substring(0,i); 386 path = name.substring(i); 387 } else if ("Host".equals(type)) { 388 host = container.getKeyProperty("host"); 390 } 391 392 ObjectName search = new ObjectName (sb.toString()); 393 ArrayList valves = new ArrayList (); 394 Iterator names = mbserver.queryNames(search, null).iterator(); 395 while (names.hasNext()) { 396 ObjectName valve = (ObjectName ) names.next(); 397 String vpath = valve.getKeyProperty("path"); 398 String vhost = valve.getKeyProperty("host"); 399 400 String valveType = null; 401 String className = (String ) 402 mbserver.getAttribute(valve, "className"); 403 int period = className.lastIndexOf("."); 404 if (period >= 0) 405 valveType = className.substring(period + 1); 406 407 if ("AccessLogValve".equalsIgnoreCase(valveType) || 409 "RemoteAddrValve".equalsIgnoreCase(valveType) || 410 "RemoteHostValve".equalsIgnoreCase(valveType) || 411 "RequestDumperValve".equalsIgnoreCase(valveType) || 412 "SingleSignOn".equalsIgnoreCase(valveType)) { 413 if ("Service".equalsIgnoreCase(type)) { 416 if ((vpath == null) && (vhost == null)) { 417 valves.add(valve.toString()); 418 } 419 } 420 421 if ("Host".equalsIgnoreCase(type)) { 422 if ((vpath == null) && (host.equalsIgnoreCase(vhost))) { 423 valves.add(valve.toString()); 424 } 425 } 426 427 if ("WebModule".equalsIgnoreCase(j2eeType)) { 428 if ((path.equalsIgnoreCase(vpath)) && (host.equalsIgnoreCase(vhost))) { 429 valves.add(valve.toString()); 430 } 431 } 432 } 433 } 434 Collections.sort(valves); 435 return (valves); 436 } 437 438 439 449 public static List getValves(MBeanServer mbserver, String container) 450 throws Exception { 451 452 return (getValves(mbserver, new ObjectName (container))); 453 454 } 455 456 463 public static List getServers(MBeanServer mbserver, String domain) 464 throws Exception { 465 466 ObjectName search = new ObjectName (domain+":type=Server,*"); 467 ArrayList servers = new ArrayList (); 468 Iterator names = mbserver.queryNames(search, null).iterator(); 469 while (names.hasNext()) { 470 servers.add(names.next().toString()); 471 } 472 Collections.sort(servers); 473 return (servers); 474 475 } 476 477 478 487 public static List getServices(MBeanServer mbserver, ObjectName server) 488 throws Exception { 489 490 StringBuffer sb = new StringBuffer ("*:type=Service,*"); 492 ObjectName search = new ObjectName (sb.toString()); 494 ArrayList services = new ArrayList (); 495 Iterator names = mbserver.queryNames(search, null).iterator(); 496 while (names.hasNext()) { 497 services.add(names.next().toString()); 498 } 499 Collections.sort(services); 500 return (services); 501 502 } 503 504 505 514 public static List getServices(MBeanServer mbserver, String server) 515 throws Exception { 516 517 return (getServices(mbserver, new ObjectName (server))); 518 519 } 520 521 522 531 public static String getAdminAppService 532 (MBeanServer mbserver, String domain, HttpServletRequest request) 533 throws Exception { 534 535 String adminDomain = TomcatTreeBuilder.DEFAULT_DOMAIN; 536 StringBuffer sb = new StringBuffer (adminDomain); 538 sb.append(":type=Service,*"); 539 ObjectName search = new ObjectName (sb.toString()); 540 Iterator names = mbserver.queryNames(search, null).iterator(); 541 String service = null; 542 while (names.hasNext()) { 543 service = ((ObjectName )names.next()).getKeyProperty("serviceName"); 544 } 545 return service; 546 547 } 548 549 550 559 public static String getAdminAppHost 560 (MBeanServer mbserver, String domain, HttpServletRequest request) 561 throws Exception { 562 563 String adminDomain = TomcatTreeBuilder.DEFAULT_DOMAIN; 565 StringBuffer sb = new StringBuffer (adminDomain); 566 sb.append(":j2eeType=WebModule,*"); 567 ObjectName search = new ObjectName (sb.toString()); 568 Iterator names = mbserver.queryNames(search, null).iterator(); 569 String contextPath = request.getContextPath(); 570 String host = null; 571 String name = null; 572 ObjectName oname = null; 573 while (names.hasNext()) { 574 name = names.next().toString(); 575 oname = new ObjectName (name); 576 host = oname.getKeyProperty("name"); 577 host = host.substring(2); 578 int i = host.indexOf("/"); 579 if (contextPath.equals(host.substring(i))) { 580 host = host.substring(0,i); 581 return host; 582 } 583 } 584 return host; 585 586 } 587 588 589 597 public static ObjectName getSearchObject(ObjectName container, String type) 598 throws Exception { 599 600 StringBuffer sb = new StringBuffer (container.getDomain()); 601 sb.append(":type="+type); 602 String containerType = container.getKeyProperty("type"); 603 String name = container.getKeyProperty("name"); 604 if ((name != null) && (name.length() > 0)) { 605 name = name.substring(2); 607 int i = name.indexOf("/"); 608 String host = name.substring(0,i); 609 String path = name.substring(i); 610 sb.append(",path="); 611 sb.append(path); 612 sb.append(",host="); 613 sb.append(host); 614 } else if ("Host".equals(containerType)) { 615 String host = container.getKeyProperty("host"); 617 sb.append(",host="); 618 sb.append(host); 619 } 620 621 return new ObjectName (sb.toString()); 622 623 } 624 625 } 626 | Popular Tags |