1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.IOException ; 25 import java.io.Serializable ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 33 import javax.management.MBeanRegistration ; 34 import javax.management.MBeanServer ; 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.ObjectName ; 37 import javax.naming.directory.DirContext ; 38 import javax.servlet.ServletException ; 39 40 import org.apache.catalina.Cluster; 41 import org.apache.catalina.Container; 42 import org.apache.catalina.ContainerEvent; 43 import org.apache.catalina.ContainerListener; 44 import org.apache.catalina.Lifecycle; 45 import org.apache.catalina.LifecycleException; 46 import org.apache.catalina.LifecycleListener; 47 import org.apache.catalina.Loader; 48 import org.apache.catalina.Manager; 49 import org.apache.catalina.Pipeline; 50 import org.apache.catalina.Realm; 51 import org.apache.catalina.Valve; 52 import org.apache.catalina.connector.Request; 53 import org.apache.catalina.connector.Response; 54 import org.apache.catalina.util.LifecycleSupport; 55 import org.apache.catalina.util.StringManager; 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.apache.naming.resources.ProxyDirContext; 59 import org.apache.tomcat.util.modeler.Registry; 60 61 62 121 122 public abstract class ContainerBase 123 implements Container, Lifecycle, Pipeline, MBeanRegistration , Serializable { 124 125 private static org.apache.commons.logging.Log log= 126 org.apache.commons.logging.LogFactory.getLog( ContainerBase.class ); 127 128 134 protected class PrivilegedAddChild 135 implements PrivilegedAction { 136 137 private Container child; 138 139 PrivilegedAddChild(Container child) { 140 this.child = child; 141 } 142 143 public Object run() { 144 addChildInternal(child); 145 return null; 146 } 147 148 } 149 150 151 153 154 157 protected HashMap children = new HashMap (); 158 159 160 163 protected int backgroundProcessorDelay = -1; 164 165 166 169 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 170 171 172 175 protected ArrayList listeners = new ArrayList (); 176 177 178 181 protected Loader loader = null; 182 183 184 187 protected Log logger = null; 188 189 190 193 protected String logName = null; 194 195 196 199 protected Manager manager = null; 200 201 202 205 protected Cluster cluster = null; 206 207 208 211 protected String name = null; 212 213 214 217 protected Container parent = null; 218 219 220 223 protected ClassLoader parentClassLoader = null; 224 225 226 229 protected Pipeline pipeline = new StandardPipeline(this); 230 231 232 235 protected Realm realm = null; 236 237 238 241 protected DirContext resources = null; 242 243 244 247 protected static StringManager sm = 248 StringManager.getManager(Constants.Package); 249 250 251 254 protected boolean started = false; 255 256 protected boolean initialized=false; 257 258 261 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 262 263 264 267 private Thread thread = null; 268 269 270 273 private boolean threadDone = false; 274 275 276 278 279 288 public int getBackgroundProcessorDelay() { 289 return backgroundProcessorDelay; 290 } 291 292 293 300 public void setBackgroundProcessorDelay(int delay) { 301 backgroundProcessorDelay = delay; 302 } 303 304 305 310 public String getInfo() { 311 return this.getClass().getName(); 312 } 313 314 315 320 public Loader getLoader() { 321 322 if (loader != null) 323 return (loader); 324 if (parent != null) 325 return (parent.getLoader()); 326 return (null); 327 328 } 329 330 331 336 public synchronized void setLoader(Loader loader) { 337 338 Loader oldLoader = this.loader; 340 if (oldLoader == loader) 341 return; 342 this.loader = loader; 343 344 if (started && (oldLoader != null) && 346 (oldLoader instanceof Lifecycle)) { 347 try { 348 ((Lifecycle) oldLoader).stop(); 349 } catch (LifecycleException e) { 350 log.error("ContainerBase.setLoader: stop: ", e); 351 } 352 } 353 354 if (loader != null) 356 loader.setContainer(this); 357 if (started && (loader != null) && 358 (loader instanceof Lifecycle)) { 359 try { 360 ((Lifecycle) loader).start(); 361 } catch (LifecycleException e) { 362 log.error("ContainerBase.setLoader: start: ", e); 363 } 364 } 365 366 support.firePropertyChange("loader", oldLoader, this.loader); 368 369 } 370 371 372 377 public Log getLogger() { 378 379 if (logger != null) 380 return (logger); 381 logger = LogFactory.getLog(logName()); 382 return (logger); 383 384 } 385 386 387 392 public Manager getManager() { 393 394 if (manager != null) 395 return (manager); 396 if (parent != null) 397 return (parent.getManager()); 398 return (null); 399 400 } 401 402 403 408 public synchronized void setManager(Manager manager) { 409 410 Manager oldManager = this.manager; 412 if (oldManager == manager) 413 return; 414 this.manager = manager; 415 416 if (started && (oldManager != null) && 418 (oldManager instanceof Lifecycle)) { 419 try { 420 ((Lifecycle) oldManager).stop(); 421 } catch (LifecycleException e) { 422 log.error("ContainerBase.setManager: stop: ", e); 423 } 424 } 425 426 if (manager != null) 428 manager.setContainer(this); 429 if (started && (manager != null) && 430 (manager instanceof Lifecycle)) { 431 try { 432 ((Lifecycle) manager).start(); 433 } catch (LifecycleException e) { 434 log.error("ContainerBase.setManager: start: ", e); 435 } 436 } 437 438 support.firePropertyChange("manager", oldManager, this.manager); 440 441 } 442 443 444 447 public Object getMappingObject() { 448 return this; 449 } 450 451 452 457 public Cluster getCluster() { 458 if (cluster != null) 459 return (cluster); 460 461 if (parent != null) 462 return (parent.getCluster()); 463 464 return (null); 465 } 466 467 468 473 public synchronized void setCluster(Cluster cluster) { 474 Cluster oldCluster = this.cluster; 476 if (oldCluster == cluster) 477 return; 478 this.cluster = cluster; 479 480 if (started && (oldCluster != null) && 482 (oldCluster instanceof Lifecycle)) { 483 try { 484 ((Lifecycle) oldCluster).stop(); 485 } catch (LifecycleException e) { 486 log.error("ContainerBase.setCluster: stop: ", e); 487 } 488 } 489 490 if (cluster != null) 492 cluster.setContainer(this); 493 494 if (started && (cluster != null) && 495 (cluster instanceof Lifecycle)) { 496 try { 497 ((Lifecycle) cluster).start(); 498 } catch (LifecycleException e) { 499 log.error("ContainerBase.setCluster: start: ", e); 500 } 501 } 502 503 support.firePropertyChange("cluster", oldCluster, this.cluster); 505 } 506 507 508 513 public String getName() { 514 515 return (name); 516 517 } 518 519 520 531 public void setName(String name) { 532 533 String oldName = this.name; 534 this.name = name; 535 support.firePropertyChange("name", oldName, this.name); 536 } 537 538 539 543 public Container getParent() { 544 545 return (parent); 546 547 } 548 549 550 561 public void setParent(Container container) { 562 563 Container oldParent = this.parent; 564 this.parent = container; 565 support.firePropertyChange("parent", oldParent, this.parent); 566 567 } 568 569 570 575 public ClassLoader getParentClassLoader() { 576 if (parentClassLoader != null) 577 return (parentClassLoader); 578 if (parent != null) { 579 return (parent.getParentClassLoader()); 580 } 581 return (ClassLoader.getSystemClassLoader()); 582 583 } 584 585 586 595 public void setParentClassLoader(ClassLoader parent) { 596 ClassLoader oldParentClassLoader = this.parentClassLoader; 597 this.parentClassLoader = parent; 598 support.firePropertyChange("parentClassLoader", oldParentClassLoader, 599 this.parentClassLoader); 600 601 } 602 603 604 608 public Pipeline getPipeline() { 609 610 return (this.pipeline); 611 612 } 613 614 615 620 public Realm getRealm() { 621 622 if (realm != null) 623 return (realm); 624 if (parent != null) 625 return (parent.getRealm()); 626 return (null); 627 628 } 629 630 631 636 public synchronized void setRealm(Realm realm) { 637 638 Realm oldRealm = this.realm; 640 if (oldRealm == realm) 641 return; 642 this.realm = realm; 643 644 if (started && (oldRealm != null) && 646 (oldRealm instanceof Lifecycle)) { 647 try { 648 ((Lifecycle) oldRealm).stop(); 649 } catch (LifecycleException e) { 650 log.error("ContainerBase.setRealm: stop: ", e); 651 } 652 } 653 654 if (realm != null) 656 realm.setContainer(this); 657 if (started && (realm != null) && 658 (realm instanceof Lifecycle)) { 659 try { 660 ((Lifecycle) realm).start(); 661 } catch (LifecycleException e) { 662 log.error("ContainerBase.setRealm: start: ", e); 663 } 664 } 665 666 support.firePropertyChange("realm", oldRealm, this.realm); 668 669 } 670 671 672 678 public DirContext getResources() { 679 if (resources != null) 680 return (resources); 681 if (parent != null) 682 return (parent.getResources()); 683 return (null); 684 685 } 686 687 688 694 public synchronized void setResources(DirContext resources) { 695 699 DirContext oldResources = this.resources; 701 if (oldResources == resources) 702 return; 703 Hashtable env = new Hashtable (); 704 if (getParent() != null) 705 env.put(ProxyDirContext.HOST, getParent().getName()); 706 env.put(ProxyDirContext.CONTEXT, getName()); 707 this.resources = new ProxyDirContext(env, resources); 708 support.firePropertyChange("resources", oldResources, this.resources); 710 711 } 712 713 714 716 717 734 public void addChild(Container child) { 735 if (System.getSecurityManager() != null) { 736 PrivilegedAction dp = 737 new PrivilegedAddChild(child); 738 AccessController.doPrivileged(dp); 739 } else { 740 addChildInternal(child); 741 } 742 } 743 744 private void addChildInternal(Container child) { 745 746 if( log.isDebugEnabled() ) 747 log.debug("Add child " + child + " " + this); 748 synchronized(children) { 749 if (children.get(child.getName()) != null) 750 throw new IllegalArgumentException ("addChild: Child name '" + 751 child.getName() + 752 "' is not unique"); 753 child.setParent(this); children.put(child.getName(), child); 755 756 if (started && (child instanceof Lifecycle)) { 758 boolean success = false; 759 try { 760 ((Lifecycle) child).start(); 761 success = true; 762 } catch (LifecycleException e) { 763 log.error("ContainerBase.addChild: start: ", e); 764 throw new IllegalStateException 765 ("ContainerBase.addChild: start: " + e); 766 } finally { 767 if (!success) { 768 children.remove(child.getName()); 769 } 770 } 771 } 772 773 fireContainerEvent(ADD_CHILD_EVENT, child); 774 } 775 776 } 777 778 779 784 public void addContainerListener(ContainerListener listener) { 785 786 synchronized (listeners) { 787 listeners.add(listener); 788 } 789 790 } 791 792 793 798 public void addPropertyChangeListener(PropertyChangeListener listener) { 799 800 support.addPropertyChangeListener(listener); 801 802 } 803 804 805 811 public Container findChild(String name) { 812 813 if (name == null) 814 return (null); 815 synchronized (children) { return ((Container) children.get(name)); 817 } 818 819 } 820 821 822 826 public Container[] findChildren() { 827 828 synchronized (children) { 829 Container results[] = new Container[children.size()]; 830 return ((Container[]) children.values().toArray(results)); 831 } 832 833 } 834 835 836 841 public ContainerListener[] findContainerListeners() { 842 843 synchronized (listeners) { 844 ContainerListener[] results = 845 new ContainerListener[listeners.size()]; 846 return ((ContainerListener[]) listeners.toArray(results)); 847 } 848 849 } 850 851 852 867 public void invoke(Request request, Response response) 868 throws IOException , ServletException { 869 870 pipeline.getFirst().invoke(request, response); 871 872 } 873 874 875 881 public void removeChild(Container child) { 882 883 synchronized(children) { 884 if (children.get(child.getName()) == null) 885 return; 886 children.remove(child.getName()); 887 } 888 889 if (started && (child instanceof Lifecycle)) { 890 try { 891 if( child instanceof ContainerBase ) { 892 if( ((ContainerBase)child).started ) { 893 ((Lifecycle) child).stop(); 894 } 895 } else { 896 ((Lifecycle) child).stop(); 897 } 898 } catch (LifecycleException e) { 899 log.error("ContainerBase.removeChild: stop: ", e); 900 } 901 } 902 903 fireContainerEvent(REMOVE_CHILD_EVENT, child); 904 905 907 } 908 909 910 915 public void removeContainerListener(ContainerListener listener) { 916 917 synchronized (listeners) { 918 listeners.remove(listener); 919 } 920 921 } 922 923 924 929 public void removePropertyChangeListener(PropertyChangeListener listener) { 930 931 support.removePropertyChangeListener(listener); 932 933 } 934 935 936 938 939 944 public void addLifecycleListener(LifecycleListener listener) { 945 946 lifecycle.addLifecycleListener(listener); 947 948 } 949 950 951 955 public LifecycleListener[] findLifecycleListeners() { 956 957 return lifecycle.findLifecycleListeners(); 958 959 } 960 961 962 967 public void removeLifecycleListener(LifecycleListener listener) { 968 969 lifecycle.removeLifecycleListener(listener); 970 971 } 972 973 974 980 public synchronized void start() throws LifecycleException { 981 982 if (started) { 984 if(log.isInfoEnabled()) 985 log.info(sm.getString("containerBase.alreadyStarted", logName())); 986 return; 987 } 988 989 lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); 991 992 started = true; 993 994 if ((loader != null) && (loader instanceof Lifecycle)) 996 ((Lifecycle) loader).start(); 997 logger = null; 998 getLogger(); 999 if ((logger != null) && (logger instanceof Lifecycle)) 1000 ((Lifecycle) logger).start(); 1001 if ((manager != null) && (manager instanceof Lifecycle)) 1002 ((Lifecycle) manager).start(); 1003 if ((cluster != null) && (cluster instanceof Lifecycle)) 1004 ((Lifecycle) cluster).start(); 1005 if ((realm != null) && (realm instanceof Lifecycle)) 1006 ((Lifecycle) realm).start(); 1007 if ((resources != null) && (resources instanceof Lifecycle)) 1008 ((Lifecycle) resources).start(); 1009 1010 Container children[] = findChildren(); 1012 for (int i = 0; i < children.length; i++) { 1013 if (children[i] instanceof Lifecycle) 1014 ((Lifecycle) children[i]).start(); 1015 } 1016 1017 if (pipeline instanceof Lifecycle) 1019 ((Lifecycle) pipeline).start(); 1020 1021 lifecycle.fireLifecycleEvent(START_EVENT, null); 1023 1024 threadStart(); 1026 1027 lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null); 1029 1030 } 1031 1032 1033 1039 public synchronized void stop() throws LifecycleException { 1040 1041 if (!started) { 1043 if(log.isInfoEnabled()) 1044 log.info(sm.getString("containerBase.notStarted", logName())); 1045 return; 1046 } 1047 1048 lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null); 1050 1051 threadStop(); 1053 1054 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 1056 started = false; 1057 1058 if (pipeline instanceof Lifecycle) { 1060 ((Lifecycle) pipeline).stop(); 1061 } 1062 1063 Container children[] = findChildren(); 1065 for (int i = 0; i < children.length; i++) { 1066 if (children[i] instanceof Lifecycle) 1067 ((Lifecycle) children[i]).stop(); 1068 } 1069 children = findChildren(); 1071 for (int i = 0; i < children.length; i++) { 1072 removeChild(children[i]); 1073 } 1074 1075 if ((resources != null) && (resources instanceof Lifecycle)) { 1077 ((Lifecycle) resources).stop(); 1078 } 1079 if ((realm != null) && (realm instanceof Lifecycle)) { 1080 ((Lifecycle) realm).stop(); 1081 } 1082 if ((cluster != null) && (cluster instanceof Lifecycle)) { 1083 ((Lifecycle) cluster).stop(); 1084 } 1085 if ((manager != null) && (manager instanceof Lifecycle)) { 1086 ((Lifecycle) manager).stop(); 1087 } 1088 if ((logger != null) && (logger instanceof Lifecycle)) { 1089 ((Lifecycle) logger).stop(); 1090 } 1091 if ((loader != null) && (loader instanceof Lifecycle)) { 1092 ((Lifecycle) loader).stop(); 1093 } 1094 1095 lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null); 1097 1098 } 1099 1100 1110 public void init() throws Exception { 1111 1112 if( this.getParent() == null ) { 1113 ObjectName parentName=getParentName(); 1115 1116 if( parentName != null && 1118 mserver.isRegistered(parentName)) 1119 { 1120 mserver.invoke(parentName, "addChild", new Object [] { this }, 1121 new String [] {"org.apache.catalina.Container"}); 1122 } 1123 } 1124 initialized=true; 1125 } 1126 1127 public ObjectName getParentName() throws MalformedObjectNameException { 1128 return null; 1129 } 1130 1131 public void destroy() throws Exception { 1132 if( started ) { 1133 stop(); 1134 } 1135 initialized=false; 1136 1137 if ( oname != null ) { 1139 try { 1140 if( controller == oname ) { 1141 Registry.getRegistry(null, null) 1142 .unregisterComponent(oname); 1143 if(log.isDebugEnabled()) 1144 log.debug("unregistering " + oname); 1145 } 1146 } catch( Throwable t ) { 1147 log.error("Error unregistering ", t ); 1148 } 1149 } 1150 1151 if (parent != null) { 1152 parent.removeChild(this); 1153 } 1154 1155 Container children[] = findChildren(); 1157 for (int i = 0; i < children.length; i++) { 1158 removeChild(children[i]); 1159 } 1160 1161 } 1162 1163 1165 1166 1184 public synchronized void addValve(Valve valve) { 1185 1186 pipeline.addValve(valve); 1187 fireContainerEvent(ADD_VALVE_EVENT, valve); 1188 } 1189 1190 public ObjectName [] getValveObjectNames() { 1191 return ((StandardPipeline)pipeline).getValveObjectNames(); 1192 } 1193 1194 1198 public Valve getBasic() { 1199 1200 return (pipeline.getBasic()); 1201 1202 } 1203 1204 1205 1208 public Valve getFirst() { 1209 1210 return (pipeline.getFirst()); 1211 1212 } 1213 1214 1215 1220 public Valve[] getValves() { 1221 1222 return (pipeline.getValves()); 1223 1224 } 1225 1226 1227 1233 public synchronized void removeValve(Valve valve) { 1234 1235 pipeline.removeValve(valve); 1236 fireContainerEvent(REMOVE_VALVE_EVENT, valve); 1237 } 1238 1239 1240 1252 public void setBasic(Valve valve) { 1253 1254 pipeline.setBasic(valve); 1255 1256 } 1257 1258 1259 1264 public void backgroundProcess() { 1265 1266 if (!started) 1267 return; 1268 1269 if (cluster != null) { 1270 try { 1271 cluster.backgroundProcess(); 1272 } catch (Exception e) { 1273 log.warn(sm.getString("containerBase.backgroundProcess.cluster", cluster), e); 1274 } 1275 } 1276 if (loader != null) { 1277 try { 1278 loader.backgroundProcess(); 1279 } catch (Exception e) { 1280 log.warn(sm.getString("containerBase.backgroundProcess.loader", loader), e); 1281 } 1282 } 1283 if (manager != null) { 1284 try { 1285 manager.backgroundProcess(); 1286 } catch (Exception e) { 1287 log.warn(sm.getString("containerBase.backgroundProcess.manager", manager), e); 1288 } 1289 } 1290 if (realm != null) { 1291 try { 1292 realm.backgroundProcess(); 1293 } catch (Exception e) { 1294 log.warn(sm.getString("containerBase.backgroundProcess.realm", realm), e); 1295 } 1296 } 1297 Valve current = pipeline.getFirst(); 1298 while (current != null) { 1299 try { 1300 current.backgroundProcess(); 1301 } catch (Exception e) { 1302 log.warn(sm.getString("containerBase.backgroundProcess.valve", current), e); 1303 } 1304 current = current.getNext(); 1305 } 1306 lifecycle.fireLifecycleEvent(Lifecycle.PERIODIC_EVENT, null); 1307 } 1308 1309 1310 1312 1313 1321 public void fireContainerEvent(String type, Object data) { 1322 1323 if (listeners.size() < 1) 1324 return; 1325 ContainerEvent event = new ContainerEvent(this, type, data); 1326 ContainerListener list[] = new ContainerListener[0]; 1327 synchronized (listeners) { 1328 list = (ContainerListener[]) listeners.toArray(list); 1329 } 1330 for (int i = 0; i < list.length; i++) 1331 ((ContainerListener) list[i]).containerEvent(event); 1332 1333 } 1334 1335 1336 1339 protected String logName() { 1340 1341 if (logName != null) { 1342 return logName; 1343 } 1344 String loggerName = null; 1345 Container current = this; 1346 while (current != null) { 1347 String name = current.getName(); 1348 if ((name == null) || (name.equals(""))) { 1349 name = "/"; 1350 } 1351 loggerName = "[" + name + "]" 1352 + ((loggerName != null) ? ("." + loggerName) : ""); 1353 current = current.getParent(); 1354 } 1355 logName = ContainerBase.class.getName() + "." + loggerName; 1356 return logName; 1357 1358 } 1359 1360 1361 protected String type; 1363 protected String domain; 1364 protected String suffix; 1365 protected ObjectName oname; 1366 protected ObjectName controller; 1367 protected transient MBeanServer mserver; 1368 1369 public ObjectName getJmxName() { 1370 return oname; 1371 } 1372 1373 public String getObjectName() { 1374 if (oname != null) { 1375 return oname.toString(); 1376 } else return null; 1377 } 1378 1379 public String getDomain() { 1380 if( domain==null ) { 1381 Container parent=this; 1382 while( parent != null && 1383 !( parent instanceof StandardEngine) ) { 1384 parent=parent.getParent(); 1385 } 1386 if( parent instanceof StandardEngine ) { 1387 domain=((StandardEngine)parent).getDomain(); 1388 } 1389 } 1390 return domain; 1391 } 1392 1393 public void setDomain(String domain) { 1394 this.domain=domain; 1395 } 1396 1397 public String getType() { 1398 return type; 1399 } 1400 1401 protected String getJSR77Suffix() { 1402 return suffix; 1403 } 1404 1405 public ObjectName preRegister(MBeanServer server, 1406 ObjectName name) throws Exception { 1407 oname=name; 1408 mserver=server; 1409 if (name == null ){ 1410 return null; 1411 } 1412 1413 domain=name.getDomain(); 1414 1415 type=name.getKeyProperty("type"); 1416 if( type==null ) { 1417 type=name.getKeyProperty("j2eeType"); 1418 } 1419 1420 String j2eeApp=name.getKeyProperty("J2EEApplication"); 1421 String j2eeServer=name.getKeyProperty("J2EEServer"); 1422 if( j2eeApp==null ) { 1423 j2eeApp="none"; 1424 } 1425 if( j2eeServer==null ) { 1426 j2eeServer="none"; 1427 } 1428 suffix=",J2EEApplication=" + j2eeApp + ",J2EEServer=" + j2eeServer; 1429 return name; 1430 } 1431 1432 public void postRegister(Boolean registrationDone) { 1433 } 1434 1435 public void preDeregister() throws Exception { 1436 } 1437 1438 public void postDeregister() { 1439 } 1440 1441 public ObjectName [] getChildren() { 1442 ObjectName result[]=new ObjectName [children.size()]; 1443 Iterator it=children.values().iterator(); 1444 int i=0; 1445 while( it.hasNext() ) { 1446 Object next=it.next(); 1447 if( next instanceof ContainerBase ) { 1448 result[i++]=((ContainerBase)next).getJmxName(); 1449 } 1450 } 1451 return result; 1452 } 1453 1454 public ObjectName createObjectName(String domain, ObjectName parent) 1455 throws Exception 1456 { 1457 if( log.isDebugEnabled()) 1458 log.debug("Create ObjectName " + domain + " " + parent ); 1459 return null; 1460 } 1461 1462 public String getContainerSuffix() { 1463 Container container=this; 1464 Container context=null; 1465 Container host=null; 1466 Container servlet=null; 1467 1468 StringBuffer suffix=new StringBuffer (); 1469 1470 if( container instanceof StandardHost ) { 1471 host=container; 1472 } else if( container instanceof StandardContext ) { 1473 host=container.getParent(); 1474 context=container; 1475 } else if( container instanceof StandardWrapper ) { 1476 context=container.getParent(); 1477 host=context.getParent(); 1478 servlet=container; 1479 } 1480 if( context!=null ) { 1481 String path=((StandardContext)context).getPath(); 1482 suffix.append(",path=").append((path.equals("")) ? "/" : path); 1483 } 1484 if( host!=null ) suffix.append(",host=").append( host.getName() ); 1485 if( servlet != null ) { 1486 String name=container.getName(); 1487 suffix.append(",servlet="); 1488 suffix.append((name=="") ? "/" : name); 1489 } 1490 return suffix.toString(); 1491 } 1492 1493 1494 1498 protected void threadStart() { 1499 1500 if (thread != null) 1501 return; 1502 if (backgroundProcessorDelay <= 0) 1503 return; 1504 1505 threadDone = false; 1506 String threadName = "ContainerBackgroundProcessor[" + toString() + "]"; 1507 thread = new Thread (new ContainerBackgroundProcessor(), threadName); 1508 thread.setDaemon(true); 1509 thread.start(); 1510 1511 } 1512 1513 1514 1518 protected void threadStop() { 1519 1520 if (thread == null) 1521 return; 1522 1523 threadDone = true; 1524 thread.interrupt(); 1525 try { 1526 thread.join(); 1527 } catch (InterruptedException e) { 1528 ; 1529 } 1530 1531 thread = null; 1532 1533 } 1534 1535 1536 1538 1539 1543 protected class ContainerBackgroundProcessor implements Runnable { 1544 1545 public void run() { 1546 while (!threadDone) { 1547 try { 1548 Thread.sleep(backgroundProcessorDelay * 1000L); 1549 } catch (InterruptedException e) { 1550 ; 1551 } 1552 if (!threadDone) { 1553 Container parent = (Container) getMappingObject(); 1554 ClassLoader cl = 1555 Thread.currentThread().getContextClassLoader(); 1556 if (parent.getLoader() != null) { 1557 cl = parent.getLoader().getClassLoader(); 1558 } 1559 processChildren(parent, cl); 1560 } 1561 } 1562 } 1563 1564 protected void processChildren(Container container, ClassLoader cl) { 1565 try { 1566 if (container.getLoader() != null) { 1567 Thread.currentThread().setContextClassLoader 1568 (container.getLoader().getClassLoader()); 1569 } 1570 container.backgroundProcess(); 1571 } catch (Throwable t) { 1572 log.error("Exception invoking periodic operation: ", t); 1573 } finally { 1574 Thread.currentThread().setContextClassLoader(cl); 1575 } 1576 Container[] children = container.findChildren(); 1577 for (int i = 0; i < children.length; i++) { 1578 if (children[i].getBackgroundProcessorDelay() <= 0) { 1579 processChildren(children[i], cl); 1580 } 1581 } 1582 } 1583 1584 } 1585 1586 1587} 1588 | Popular Tags |