1 2 3 27 28 29 package org.apache.catalina.core; 30 31 import java.beans.PropertyChangeListener ; 32 import java.beans.PropertyChangeSupport ; 33 import java.lang.reflect.Constructor ; 34 import java.util.ArrayList ; 35 import java.util.Enumeration ; 36 import java.util.HashMap ; 37 import java.util.Hashtable ; 38 import java.util.Vector ; 39 40 41 import javax.management.MBeanRegistration ; 42 import javax.management.MBeanServer ; 43 import javax.management.MalformedObjectNameException ; 44 import javax.management.ObjectName ; 45 import javax.naming.directory.DirContext ; 46 47 import org.apache.catalina.Container; 48 import org.apache.catalina.Context; 49 import org.apache.catalina.DefaultContext; 50 import org.apache.catalina.Lifecycle; 51 import org.apache.catalina.LifecycleEvent; 52 import org.apache.catalina.LifecycleListener; 53 import org.apache.catalina.Loader; 54 import org.apache.catalina.Manager; 55 import org.apache.catalina.deploy.ApplicationParameter; 56 import org.apache.catalina.deploy.ContextEjb; 57 import org.apache.catalina.deploy.ContextEnvironment; 58 import org.apache.catalina.deploy.ContextResource; 59 import org.apache.catalina.deploy.ContextResourceLink; 60 import org.apache.catalina.deploy.NamingResources; 61 import org.apache.catalina.deploy.ResourceParams; 62 import org.apache.catalina.loader.WebappLoader; 63 import org.apache.catalina.mbeans.MBeanUtils; 64 import org.apache.catalina.util.StringManager; 65 import com.sun.org.apache.commons.modeler.ManagedBean; 66 import com.sun.org.apache.commons.modeler.Registry; 67 import org.apache.naming.ContextAccessController; 68 69 78 79 public class StandardDefaultContext 80 implements DefaultContext, LifecycleListener, MBeanRegistration { 81 82 83 85 86 89 public StandardDefaultContext() { 90 91 namingResources.setContainer(this); 92 93 } 94 95 96 98 99 102 private Hashtable contexts = new Hashtable (); 103 104 105 109 private String applicationListeners[] = new String [0]; 110 111 112 115 private ApplicationParameter applicationParameters[] = 116 new ApplicationParameter[0]; 117 118 119 122 private boolean cookies = true; 123 124 125 129 private boolean crossContext = true; 130 131 132 135 private static final String info = 136 "org.apache.catalina.core.DefaultContext/1.0"; 137 138 139 143 private String instanceListeners[] = new String [0]; 144 145 146 149 private String mapperClass = 150 "org.apache.catalina.core.StandardContextMapper"; 151 152 153 156 private NamingResources namingResources = new NamingResources(); 157 158 159 163 private HashMap parameters = new HashMap (); 164 165 166 169 private boolean reloadable = false; 170 171 172 175 private boolean swallowOutput = false; 176 177 178 182 private String wrapperLifecycles[] = new String [0]; 183 184 185 189 private String wrapperListeners[] = new String [0]; 190 191 192 195 private String wrapperClass = "org.apache.catalina.core.StandardWrapper"; 196 197 198 201 private boolean useNaming = true; 202 203 204 209 DirContext dirContext = null; 210 211 212 215 protected String name = "defaultContext"; 216 217 218 221 protected Container parent = null; 222 223 224 227 protected Vector lifecycle = new Vector (); 228 229 230 233 protected Loader loader = null; 234 235 236 239 protected Manager manager = null; 240 241 242 245 protected boolean caseSensitive = true; 246 247 248 251 protected boolean allowLinking = false; 252 253 254 257 protected int cacheMaxSize = 10240; 259 260 263 protected int cacheTTL = 5000; 264 265 266 269 protected boolean cachingAllowed = true; 270 271 272 275 protected int managerChecksFrequency = 6; 276 277 278 281 protected static final StringManager sm = 282 StringManager.getManager(Constants.Package); 283 284 285 288 protected PropertyChangeSupport support = new PropertyChangeSupport (this); 289 290 291 293 294 297 public void setCaseSensitive(boolean caseSensitive) { 298 this.caseSensitive = caseSensitive; 299 } 300 301 302 305 public boolean isCaseSensitive() { 306 return caseSensitive; 307 } 308 309 310 313 public void setAllowLinking(boolean allowLinking) { 314 this.allowLinking = allowLinking; 315 } 316 317 318 321 public boolean isAllowLinking() { 322 return allowLinking; 323 } 324 325 326 329 public void setCacheTTL(int cacheTTL) { 330 this.cacheTTL = cacheTTL; 331 } 332 333 334 337 public int getCacheTTL() { 338 return cacheTTL; 339 } 340 341 342 345 public int getCacheMaxSize() { 346 return cacheMaxSize; 347 } 348 349 350 353 public void setCacheMaxSize(int cacheMaxSize) { 354 this.cacheMaxSize = cacheMaxSize; 355 } 356 357 358 361 public void setCachingAllowed(boolean cachingAllowed) { 362 this.cachingAllowed = cachingAllowed; 363 } 364 365 366 369 public boolean isCachingAllowed() { 370 return cachingAllowed; 371 } 372 373 374 377 public void setManagerChecksFrequency(int managerChecksFrequency) { 378 this.managerChecksFrequency = managerChecksFrequency; 379 } 380 381 382 385 public int getManagerChecksFrequency() { 386 return managerChecksFrequency; 387 } 388 389 390 393 public boolean isUseNaming() { 394 395 return (useNaming); 396 397 } 398 399 400 403 public void setUseNaming(boolean useNaming) { 404 this.useNaming = useNaming; 405 } 406 407 408 411 public boolean getCookies() { 412 413 return (this.cookies); 414 415 } 416 417 418 423 public void setCookies(boolean cookies) { 424 boolean oldCookies = this.cookies; 425 this.cookies = cookies; 426 427 } 428 429 430 433 public boolean getCrossContext() { 434 435 return (this.crossContext); 436 437 } 438 439 440 445 public void setCrossContext(boolean crossContext) { 446 boolean oldCrossContext = this.crossContext; 447 this.crossContext = crossContext; 448 449 } 450 451 452 457 public String getInfo() { 458 459 return (info); 460 461 } 462 463 464 467 public boolean getReloadable() { 468 469 return (this.reloadable); 470 471 } 472 473 474 479 public void setReloadable(boolean reloadable) { 480 boolean oldReloadable = this.reloadable; 481 this.reloadable = reloadable; 482 483 } 484 485 486 489 public boolean getSwallowOutput() { 490 491 return (this.swallowOutput); 492 493 } 494 495 496 501 public void setSwallowOutput(boolean swallowOutput) { 502 boolean oldSwallowOutput = this.swallowOutput; 503 this.swallowOutput = swallowOutput; 504 505 } 506 507 508 512 public String getWrapperClass() { 513 514 return (this.wrapperClass); 515 516 } 517 518 519 525 public void setWrapperClass(String wrapperClass) { 526 this.wrapperClass = wrapperClass; 527 528 } 529 530 531 537 public void setResources(DirContext resources) { 538 this.dirContext = resources; 539 540 } 541 542 548 public DirContext getResources() { 549 550 return this.dirContext; 551 552 } 553 554 555 559 public Loader getLoader() { 560 561 return loader; 562 563 } 564 565 566 571 public void setLoader(Loader loader) { 572 Loader oldLoader = this.loader; 573 this.loader = loader; 574 575 support.firePropertyChange("loader", oldLoader, this.loader); 577 } 578 579 580 584 public Manager getManager() { 585 586 return manager; 587 588 } 589 590 591 596 public void setManager(Manager manager) { 597 Manager oldManager = this.manager; 598 this.manager = manager; 599 600 support.firePropertyChange("manager", oldManager, this.manager); 602 } 603 604 605 607 608 613 public void addLifecycleListener(LifecycleListener listener) { 614 lifecycle.add(listener); 615 } 616 617 618 620 623 624 public String getName() { 625 return (this.name); 626 } 627 628 public void setName(String name) { 629 this.name = name; 630 } 631 632 633 637 public Container getParent() { 638 639 return (parent); 640 641 } 642 643 644 655 public void setParent(Container container) { 656 Container oldParent = this.parent; 657 this.parent = container; 658 support.firePropertyChange("parent", oldParent, this.parent); 659 660 } 661 662 664 665 671 public void addApplicationListener(String listener) { 672 673 synchronized (applicationListeners) { 674 String results[] =new String [applicationListeners.length + 1]; 675 for (int i = 0; i < applicationListeners.length; i++) 676 results[i] = applicationListeners[i]; 677 results[applicationListeners.length] = listener; 678 applicationListeners = results; 679 } 680 681 } 682 683 684 689 public void addApplicationParameter(ApplicationParameter parameter) { 690 691 synchronized (applicationParameters) { 692 ApplicationParameter results[] = 693 new ApplicationParameter[applicationParameters.length + 1]; 694 System.arraycopy(applicationParameters, 0, results, 0, 695 applicationParameters.length); 696 results[applicationParameters.length] = parameter; 697 applicationParameters = results; 698 } 699 700 } 701 702 703 708 public void addEjb(ContextEjb ejb) { 709 710 namingResources.addEjb(ejb); 711 712 } 713 714 715 720 public void addEnvironment(ContextEnvironment environment) { 721 722 namingResources.addEnvironment(environment); 723 724 } 725 726 727 732 public void addResourceParams(ResourceParams resourceParameters) { 733 734 namingResources.addResourceParams(resourceParameters); 735 736 } 737 738 739 745 public void addInstanceListener(String listener) { 746 747 synchronized (instanceListeners) { 748 String results[] =new String [instanceListeners.length + 1]; 749 for (int i = 0; i < instanceListeners.length; i++) 750 results[i] = instanceListeners[i]; 751 results[instanceListeners.length] = listener; 752 instanceListeners = results; 753 } 754 755 } 756 757 758 769 public void addParameter(String name, String value) { 770 if ((name == null) || (value == null)) 772 throw new IllegalArgumentException 773 (sm.getString("standardContext.parameter.required")); 774 if (parameters.get(name) != null) 775 throw new IllegalArgumentException 776 (sm.getString("standardContext.parameter.duplicate", name)); 777 778 synchronized (parameters) { 780 parameters.put(name, value); 781 } 782 783 } 784 785 786 791 public void addPropertyChangeListener(PropertyChangeListener listener) { 792 793 support.addPropertyChangeListener(listener); 794 795 } 796 797 798 803 public void addResource(ContextResource resource) { 804 805 namingResources.addResource(resource); 806 807 } 808 809 810 816 public void addResourceEnvRef(String name, String type) { 817 818 namingResources.addResourceEnvRef(name, type); 819 820 } 821 822 823 828 public void addResourceLink(ContextResourceLink resourceLink) { 829 830 namingResources.addResourceLink(resourceLink); 831 832 } 833 834 835 841 public void addWrapperLifecycle(String listener) { 842 843 synchronized (wrapperLifecycles) { 844 String results[] =new String [wrapperLifecycles.length + 1]; 845 for (int i = 0; i < wrapperLifecycles.length; i++) 846 results[i] = wrapperLifecycles[i]; 847 results[wrapperLifecycles.length] = listener; 848 wrapperLifecycles = results; 849 } 850 851 } 852 853 854 860 public void addWrapperListener(String listener) { 861 862 synchronized (wrapperListeners) { 863 String results[] =new String [wrapperListeners.length + 1]; 864 for (int i = 0; i < wrapperListeners.length; i++) 865 results[i] = wrapperListeners[i]; 866 results[wrapperListeners.length] = listener; 867 wrapperListeners = results; 868 } 869 870 } 871 872 873 877 public String [] findApplicationListeners() { 878 879 return (applicationListeners); 880 881 } 882 883 884 887 public ApplicationParameter[] findApplicationParameters() { 888 889 return (applicationParameters); 890 891 } 892 893 894 900 public ContextEjb findEjb(String name) { 901 902 return namingResources.findEjb(name); 903 904 } 905 906 907 911 public ContextEjb[] findEjbs() { 912 913 return namingResources.findEjbs(); 914 915 } 916 917 918 924 public ContextEnvironment findEnvironment(String name) { 925 926 return namingResources.findEnvironment(name); 927 928 } 929 930 931 936 public ContextEnvironment[] findEnvironments() { 937 938 return namingResources.findEnvironments(); 939 940 } 941 942 943 948 public ResourceParams[] findResourceParams() { 949 950 return namingResources.findResourceParams(); 951 952 } 953 954 955 959 public String [] findInstanceListeners() { 960 961 return (instanceListeners); 962 963 } 964 965 966 972 public String findParameter(String name) { 973 974 synchronized (parameters) { 975 return ((String ) parameters.get(name)); 976 } 977 978 } 979 980 981 986 public String [] findParameters() { 987 988 synchronized (parameters) { 989 String results[] = new String [parameters.size()]; 990 return ((String []) parameters.keySet().toArray(results)); 991 } 992 993 } 994 995 996 1002 public ContextResource findResource(String name) { 1003 1004 return namingResources.findResource(name); 1005 1006 } 1007 1008 1009 1015 public String findResourceEnvRef(String name) { 1016 1017 return namingResources.findResourceEnvRef(name); 1018 1019 } 1020 1021 1022 1027 public String [] findResourceEnvRefs() { 1028 1029 return namingResources.findResourceEnvRefs(); 1030 1031 } 1032 1033 1034 1040 public ContextResourceLink findResourceLink(String name) { 1041 1042 return namingResources.findResourceLink(name); 1043 1044 } 1045 1046 1047 1051 public ContextResourceLink[] findResourceLinks() { 1052 1053 return namingResources.findResourceLinks(); 1054 1055 } 1056 1057 1058 1062 public ContextResource[] findResources() { 1063 1064 return namingResources.findResources(); 1065 1066 } 1067 1068 1069 1073 public String [] findWrapperLifecycles() { 1074 1075 return (wrapperLifecycles); 1076 1077 } 1078 1079 1080 1084 public String [] findWrapperListeners() { 1085 1086 return (wrapperListeners); 1087 1088 } 1089 1090 1091 1094 public NamingResources getNamingResources() { 1095 1096 return (this.namingResources); 1097 1098 } 1099 1100 1101 1107 public void removeApplicationListener(String listener) { 1108 1109 synchronized (applicationListeners) { 1110 1111 int n = -1; 1113 for (int i = 0; i < applicationListeners.length; i++) { 1114 if (applicationListeners[i].equals(listener)) { 1115 n = i; 1116 break; 1117 } 1118 } 1119 if (n < 0) 1120 return; 1121 1122 int j = 0; 1124 String results[] = new String [applicationListeners.length - 1]; 1125 for (int i = 0; i < applicationListeners.length; i++) { 1126 if (i != n) 1127 results[j++] = applicationListeners[i]; 1128 } 1129 applicationListeners = results; 1130 1131 } 1132 1133 1134 } 1135 1136 1137 1143 public void removeApplicationParameter(String name) { 1144 1145 synchronized (applicationParameters) { 1146 1147 int n = -1; 1149 for (int i = 0; i < applicationParameters.length; i++) { 1150 if (name.equals(applicationParameters[i].getName())) { 1151 n = i; 1152 break; 1153 } 1154 } 1155 if (n < 0) 1156 return; 1157 1158 int j = 0; 1160 ApplicationParameter results[] = 1161 new ApplicationParameter[applicationParameters.length - 1]; 1162 for (int i = 0; i < applicationParameters.length; i++) { 1163 if (i != n) 1164 results[j++] = applicationParameters[i]; 1165 } 1166 applicationParameters = results; 1167 1168 } 1169 1170 } 1171 1172 1173 1178 public void removeEjb(String name) { 1179 1180 namingResources.removeEjb(name); 1181 1182 } 1183 1184 1185 1191 public void removeInstanceListener(String listener) { 1192 1193 synchronized (instanceListeners) { 1194 1195 int n = -1; 1197 for (int i = 0; i < instanceListeners.length; i++) { 1198 if (instanceListeners[i].equals(listener)) { 1199 n = i; 1200 break; 1201 } 1202 } 1203 if (n < 0) 1204 return; 1205 1206 int j = 0; 1208 String results[] = new String [instanceListeners.length - 1]; 1209 for (int i = 0; i < instanceListeners.length; i++) { 1210 if (i != n) 1211 results[j++] = instanceListeners[i]; 1212 } 1213 instanceListeners = results; 1214 1215 } 1216 1217 } 1218 1219 1220 1226 public void removeParameter(String name) { 1227 1228 synchronized (parameters) { 1229 parameters.remove(name); 1230 } 1231 1232 } 1233 1234 1235 1240 public void removePropertyChangeListener(PropertyChangeListener listener) { 1241 1242 support.removePropertyChangeListener(listener); 1243 1244 } 1245 1246 1251 public void removeEnvironment(String envName) { 1252 1253 NamingResources nresources = getNamingResources(); 1254 if (nresources == null) { 1255 return; 1256 } 1257 ContextEnvironment env = nresources.findEnvironment(envName); 1258 if (env == null) { 1259 throw new IllegalArgumentException 1260 ("Invalid environment name '" + envName + "'"); 1261 } 1262 nresources.removeEnvironment(envName); 1263 } 1264 1265 1266 1271 public void removeResource(String resourceName) { 1272 1273 NamingResources nresources = getNamingResources(); 1276 if (nresources == null) { 1277 return; 1278 } 1279 ContextResource resource = nresources.findResource(resourceName); 1280 if (resource == null) { 1281 throw new IllegalArgumentException 1282 ("Invalid resource name '" + resourceName + "'"); 1283 } 1284 nresources.removeResource(resourceName); 1285 } 1286 1287 1288 1293 public void removeResourceLink(String resourceLinkName) { 1294 1295 NamingResources nresources = getNamingResources(); 1297 if (nresources == null) { 1298 return; 1299 } 1300 ContextResourceLink resource = nresources.findResourceLink(resourceLinkName); 1301 if (resource == null) { 1302 throw new IllegalArgumentException 1303 ("Invalid resource name '" + resourceLinkName + "'"); 1304 } 1305 nresources.removeResourceLink(resourceLinkName); 1306 } 1307 1308 1309 1314 public void removeResourceEnvRef(String name) { 1315 1316 namingResources.removeResourceEnvRef(name); 1317 1318 } 1319 1320 1326 public void removeWrapperLifecycle(String listener) { 1327 1328 1329 synchronized (wrapperLifecycles) { 1330 1331 int n = -1; 1333 for (int i = 0; i < wrapperLifecycles.length; i++) { 1334 if (wrapperLifecycles[i].equals(listener)) { 1335 n = i; 1336 break; 1337 } 1338 } 1339 if (n < 0) 1340 return; 1341 1342 int j = 0; 1344 String results[] = new String [wrapperLifecycles.length - 1]; 1345 for (int i = 0; i < wrapperLifecycles.length; i++) { 1346 if (i != n) 1347 results[j++] = wrapperLifecycles[i]; 1348 } 1349 wrapperLifecycles = results; 1350 1351 } 1352 1353 } 1354 1355 1356 1362 public void removeWrapperListener(String listener) { 1363 1364 1365 synchronized (wrapperListeners) { 1366 1367 int n = -1; 1369 for (int i = 0; i < wrapperListeners.length; i++) { 1370 if (wrapperListeners[i].equals(listener)) { 1371 n = i; 1372 break; 1373 } 1374 } 1375 if (n < 0) 1376 return; 1377 1378 int j = 0; 1380 String results[] = new String [wrapperListeners.length - 1]; 1381 for (int i = 0; i < wrapperListeners.length; i++) { 1382 if (i != n) 1383 results[j++] = wrapperListeners[i]; 1384 } 1385 wrapperListeners = results; 1386 1387 } 1388 1389 } 1390 1391 1392 1394 1395 1400 public void lifecycleEvent(LifecycleEvent event) { 1401 1402 StandardContext context = null; 1403 NamingContextListener listener = null; 1404 1405 if (event.getLifecycle() instanceof StandardContext) { 1406 context = (StandardContext) event.getLifecycle(); 1407 LifecycleListener[] listeners = context.findLifecycleListeners(); 1408 for (int i = 0; i < listeners.length; i++) { 1409 if (listeners[i] instanceof NamingContextListener) { 1410 listener = (NamingContextListener) listeners[i]; 1411 break; 1412 } 1413 } 1414 } 1415 1416 if (listener == null) { 1417 return; 1418 } 1419 1420 if ((event.getType().equals(Lifecycle.BEFORE_STOP_EVENT)) 1421 || (event.getType().equals(Context.RELOAD_EVENT))) { 1422 1423 contexts.remove(context); 1425 1426 namingResources.removePropertyChangeListener(listener); 1428 1429 if (!(event.getType().equals(Context.RELOAD_EVENT))) { 1431 context.removeLifecycleListener(this); 1432 } 1433 1434 } 1435 1436 if ((event.getType().equals(Lifecycle.AFTER_START_EVENT)) 1437 || (event.getType().equals(Context.RELOAD_EVENT))) { 1438 1439 contexts.put(context, context); 1441 1442 NamingResources contextResources = context.getNamingResources(); 1443 1444 ContextAccessController.setWritable(listener.getName(), context); 1446 1447 ContextEjb [] contextEjb = findEjbs(); 1450 for (int i = 0; i < contextEjb.length; i++) { 1451 ContextEjb contextEntry = contextEjb[i]; 1452 if (contextResources.exists(contextEntry.getName())) { 1453 listener.removeEjb(contextEntry.getName()); 1454 } 1455 listener.addEjb(contextEntry); 1456 } 1457 ContextEnvironment [] contextEnv = findEnvironments(); 1458 for (int i = 0; i < contextEnv.length; i++) { 1459 ContextEnvironment contextEntry = contextEnv[i]; 1460 if (contextResources.exists(contextEntry.getName())) { 1461 listener.removeEnvironment(contextEntry.getName()); 1462 } 1463 listener.addEnvironment(contextEntry); 1464 } 1465 ContextResource [] resources = findResources(); 1466 for (int i = 0; i < resources.length; i++) { 1467 ContextResource contextEntry = resources[i]; 1468 if (contextResources.exists(contextEntry.getName())) { 1469 listener.removeResource(contextEntry.getName()); 1470 } 1471 listener.addResource(contextEntry); 1472 } 1473 ContextResourceLink [] resourceLinks = findResourceLinks(); 1474 for (int i = 0; i < resourceLinks.length; i++) { 1475 ContextResourceLink contextEntry = resourceLinks[i]; 1476 if (contextResources.exists(contextEntry.getName())) { 1477 listener.removeResourceLink(contextEntry.getName()); 1478 } 1479 listener.addResourceLink(contextEntry); 1480 } 1481 String [] envRefs = findResourceEnvRefs(); 1482 for (int i = 0; i < envRefs.length; i++) { 1483 if (contextResources.exists(envRefs[i])) { 1484 listener.removeResourceEnvRef(envRefs[i]); 1485 } 1486 listener.addResourceEnvRef 1487 (envRefs[i], findResourceEnvRef(envRefs[i])); 1488 } 1489 1490 ContextAccessController.setReadOnly(listener.getName()); 1492 1493 namingResources.addPropertyChangeListener(listener); 1495 1496 } 1497 1498 } 1499 1500 1501 1507 public void installDefaultContext(Context context) { 1508 1509 if (context instanceof StandardContext) { 1510 ((StandardContext)context).setUseNaming(isUseNaming()); 1511 ((StandardContext)context).setSwallowOutput(getSwallowOutput()); 1512 ((StandardContext)context).setCachingAllowed(isCachingAllowed()); 1513 ((StandardContext)context).setCacheTTL(getCacheTTL()); 1514 ((StandardContext)context).setCacheMaxSize(getCacheMaxSize()); 1515 ((StandardContext)context).setAllowLinking(isAllowLinking()); 1516 ((StandardContext)context).setCaseSensitive(isCaseSensitive()); 1517 ((StandardContext)context).setManagerChecksFrequency 1518 (getManagerChecksFrequency()); 1519 if (!contexts.containsKey(context)) { 1520 ((StandardContext) context).addLifecycleListener(this); 1521 } 1522 Enumeration lifecycleListeners = lifecycle.elements(); 1523 while (lifecycleListeners.hasMoreElements()) { 1524 ((StandardContext)context).addLifecycleListener( 1525 (LifecycleListener)lifecycleListeners.nextElement()); 1526 } 1527 } 1528 1529 if (!context.getPrivileged() && loader != null) { 1530 ClassLoader parentClassLoader = context.getParent().getParentClassLoader(); 1531 Class clazz = loader.getClass(); 1532 Class types[] = { ClassLoader .class }; 1533 Object args[] = { parentClassLoader }; 1534 try { 1535 Constructor constructor = clazz.getDeclaredConstructor(types); 1536 Loader context_loader = (Loader) constructor.newInstance(args); 1537 context_loader.setDelegate(loader.getDelegate()); 1538 context_loader.setReloadable(loader.getReloadable()); 1539 if (loader instanceof WebappLoader) { 1540 ((WebappLoader)context_loader).setDebug 1541 (((WebappLoader)loader).getDebug()); 1542 ((WebappLoader)context_loader).setLoaderClass 1543 (((WebappLoader)loader).getLoaderClass()); 1544 } 1545 context.setLoader(context_loader); 1546 } catch(Exception e) { 1547 IllegalArgumentException iae = new IllegalArgumentException 1548 ("DefaultContext custom Loader install failed"); 1549 iae.initCause(e); 1550 throw iae; 1551 } 1552 } 1553 } 1554 1555 1556 1562 public void importDefaultContext(Context context) { 1563 1564 context.setCookies(getCookies()); 1565 context.setCrossContext(getCrossContext()); 1566 context.setReloadable(getReloadable()); 1567 1568 String [] listeners = findApplicationListeners(); 1569 for( int i = 0; i < listeners.length; i++ ) { 1570 context.addApplicationListener(listeners[i]); 1571 } 1572 listeners = findInstanceListeners(); 1573 for( int i = 0; i < listeners.length; i++ ) { 1574 context.addInstanceListener(listeners[i]); 1575 } 1576 String [] wrapper = findWrapperListeners(); 1577 for( int i = 0; i < wrapper.length; i++ ) { 1578 context.addWrapperListener(wrapper[i]); 1579 } 1580 wrapper = findWrapperLifecycles(); 1581 for( int i = 0; i < wrapper.length; i++ ) { 1582 context.addWrapperLifecycle(wrapper[i]); 1583 } 1584 String [] parameters = findParameters(); 1585 for( int i = 0; i < parameters.length; i++ ) { 1586 context.addParameter(parameters[i],findParameter(parameters[i])); 1587 } 1588 ApplicationParameter [] appParam = findApplicationParameters(); 1589 for( int i = 0; i < appParam.length; i++ ) { 1590 context.addApplicationParameter(appParam[i]); 1591 } 1592 1593 if (!(context instanceof StandardContext)) { 1594 ContextEjb [] contextEjb = findEjbs(); 1595 for( int i = 0; i < contextEjb.length; i++ ) { 1596 context.addEjb(contextEjb[i]); 1597 } 1598 ContextEnvironment [] contextEnv = findEnvironments(); 1599 for( int i = 0; i < contextEnv.length; i++ ) { 1600 context.addEnvironment(contextEnv[i]); 1601 } 1602 1611 ContextResource [] resources = findResources(); 1612 for( int i = 0; i < resources.length; i++ ) { 1613 context.addResource(resources[i]); 1614 } 1615 ContextResourceLink [] resourceLinks = findResourceLinks(); 1616 for( int i = 0; i < resourceLinks.length; i++ ) { 1617 context.addResourceLink(resourceLinks[i]); 1618 } 1619 String [] envRefs = findResourceEnvRefs(); 1620 for( int i = 0; i < envRefs.length; i++ ) { 1621 context.addResourceEnvRef 1622 (envRefs[i],findResourceEnvRef(envRefs[i])); 1623 } 1624 } 1625 1626 } 1627 1628 1631 public String toString() { 1632 1633 StringBuffer sb = new StringBuffer (); 1634 if (getParent() != null) { 1635 sb.append(getParent().toString()); 1636 sb.append("."); 1637 } 1638 sb.append("DefaultContext["); 1639 sb.append("]"); 1640 return (sb.toString()); 1641 1642 } 1643 1644 protected String type; 1646 protected String domain; 1647 protected String suffix; 1648 protected ObjectName oname; 1649 protected MBeanServer mserver; 1650 1651 public ObjectName getObjectName() { 1652 return oname; 1653 } 1654 1655 public String getDomain() { 1656 return domain; 1657 } 1658 1659 public String getType() { 1660 return type; 1661 } 1662 1663 protected String getJSR77Suffix() { 1664 return suffix; 1665 } 1666 1667 public ObjectName preRegister(MBeanServer server, 1668 ObjectName name) throws Exception { 1669 oname=name; 1670 mserver=server; 1671 domain=name.getDomain(); 1672 1673 type=name.getKeyProperty("type"); 1674 if( type==null ) { 1675 type=name.getKeyProperty("j2eeType"); 1676 } 1677 1678 String j2eeApp=name.getKeyProperty("J2EEApplication"); 1679 String j2eeServer=name.getKeyProperty("J2EEServer"); 1680 if( j2eeApp==null ) { 1681 j2eeApp="none"; 1682 } 1683 if( j2eeServer==null ) { 1684 j2eeServer="none"; 1685 } 1686 suffix=",J2EEApplication=" + j2eeApp + ",J2EEServer=" + j2eeServer; 1687 return name; 1688 } 1689 1690 public void postRegister(Boolean registrationDone) { 1691 } 1692 1693 public void preDeregister() throws Exception { 1694 } 1695 1696 public void postDeregister() { 1697 } 1698 1699 1703 public String [] getEnvironments() { 1704 ContextEnvironment[] envs = getNamingResources().findEnvironments(); 1705 ArrayList results = new ArrayList (); 1706 for (int i = 0; i < envs.length; i++) { 1707 try { 1708 ObjectName oname = 1709 MBeanUtils.createObjectName(this.getDomain(), envs[i]); 1710 results.add(oname.toString()); 1711 } catch (MalformedObjectNameException e) { 1712 IllegalArgumentException iae = new IllegalArgumentException 1713 ("Cannot create object name for environment " + envs[i]); 1714 iae.initCause(e); 1715 throw iae; 1716 } 1717 } 1718 return ((String []) results.toArray(new String [results.size()])); 1719 1720 } 1721 1722 1723 1728 public String [] getResourceNames() { 1729 1730 ContextResource[] resources = getNamingResources().findResources(); 1731 ArrayList results = new ArrayList (); 1732 for (int i = 0; i < resources.length; i++) { 1733 try { 1734 ObjectName oname = 1735 MBeanUtils.createObjectName(getDomain(), resources[i]); 1736 results.add(oname.toString()); 1737 } catch (MalformedObjectNameException e) { 1738 IllegalArgumentException iae = new IllegalArgumentException 1739 ("Cannot create object name for resource " + resources[i]); 1740 iae.initCause(e); 1741 throw iae; 1742 } 1743 } 1744 return ((String []) results.toArray(new String [results.size()])); 1745 1746 } 1747 1748 1749 1753 public String [] getResourceLinks() { 1754 1755 ContextResourceLink[] links = getNamingResources().findResourceLinks(); 1756 ArrayList results = new ArrayList (); 1757 for (int i = 0; i < links.length; i++) { 1758 try { 1759 ObjectName oname = 1760 MBeanUtils.createObjectName(getDomain(), links[i]); 1761 results.add(oname.toString()); 1762 } catch (MalformedObjectNameException e) { 1763 IllegalArgumentException iae = new IllegalArgumentException 1764 ("Cannot create object name for resource " + links[i]); 1765 iae.initCause(e); 1766 throw iae; 1767 } 1768 } 1769 return ((String []) results.toArray(new String [results.size()])); 1770 1771 } 1772 1773 1775 1776 1781 public String addEnvironment(String envName, String type) 1782 throws MalformedObjectNameException { 1783 1784 NamingResources nresources = getNamingResources(); 1785 if (nresources == null) { 1786 return null; 1787 } 1788 ContextEnvironment env = nresources.findEnvironment(envName); 1789 if (env != null) { 1790 throw new IllegalArgumentException 1791 ("Invalid environment name - already exists '" + envName + "'"); 1792 } 1793 env = new ContextEnvironment(); 1794 env.setName(envName); 1795 env.setType(type); 1796 nresources.addEnvironment(env); 1797 1798 ManagedBean managed = Registry.getRegistry().findManagedBean("ContextEnvironment"); 1800 ObjectName oname = 1801 MBeanUtils.createObjectName(managed.getDomain(), env); 1802 return (oname.toString()); 1803 1804 } 1805 1806 1807 1812 public String addResource(String resourceName, String type) 1813 throws MalformedObjectNameException { 1814 1815 NamingResources nresources = getNamingResources(); 1816 if (nresources == null) { 1817 return null; 1818 } 1819 ContextResource resource = nresources.findResource(resourceName); 1820 if (resource != null) { 1821 throw new IllegalArgumentException 1822 ("Invalid resource name - already exists'" + resourceName + "'"); 1823 } 1824 resource = new ContextResource(); 1825 resource.setName(resourceName); 1826 resource.setType(type); 1827 nresources.addResource(resource); 1828 1829 ManagedBean managed = Registry.getRegistry().findManagedBean("ContextResource"); 1831 ObjectName oname = 1832 MBeanUtils.createObjectName(managed.getDomain(), resource); 1833 1834 return (oname.toString()); 1835 } 1836 1837 1838 1843 public String addResourceLink(String resourceLinkName, String global, 1844 String name, String type) throws MalformedObjectNameException { 1845 1846 NamingResources nresources = getNamingResources(); 1847 if (nresources == null) { 1848 return null; 1849 } 1850 ContextResourceLink resourceLink = 1851 nresources.findResourceLink(resourceLinkName); 1852 if (resourceLink != null) { 1853 throw new IllegalArgumentException 1854 ("Invalid resource link name - already exists'" + 1855 resourceLinkName + "'"); 1856 } 1857 resourceLink = new ContextResourceLink(); 1858 resourceLink.setGlobal(global); 1859 resourceLink.setName(resourceLinkName); 1860 resourceLink.setType(type); 1861 nresources.addResourceLink(resourceLink); 1862 1863 ManagedBean managed = Registry.getRegistry().findManagedBean("ContextResourceLink"); 1865 ObjectName oname = 1866 MBeanUtils.createObjectName(managed.getDomain(), resourceLink); 1867 return (oname.toString()); 1868 } 1869} 1870 | Popular Tags |