1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.deploy;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.naming.NamingException;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.JmxEnabled;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Server;
import org.apache.catalina.mbeans.MBeanUtils;
import org.apache.catalina.util.Introspection;
import org.apache.catalina.util.LifecycleMBeanBase;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.naming.ContextBindings;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.descriptor.web.ContextEjb;
import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
import org.apache.tomcat.util.descriptor.web.ContextLocalEjb;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef;
import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
import org.apache.tomcat.util.descriptor.web.ContextService;
import org.apache.tomcat.util.descriptor.web.ContextTransaction;
import org.apache.tomcat.util.descriptor.web.InjectionTarget;
import org.apache.tomcat.util.descriptor.web.MessageDestinationRef;
import org.apache.tomcat.util.descriptor.web.NamingResources;
import org.apache.tomcat.util.descriptor.web.ResourceBase;
import org.apache.tomcat.util.res.StringManager;
/**
* Holds and manages the naming resources defined in the Jakarta EE Naming Context and their associated JNDI context.
*
* @author Remy Maucherat
*/
public class NamingResourcesImpl extends LifecycleMBeanBase implements Serializable, NamingResources {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(NamingResourcesImpl.class);
private static final StringManager sm = StringManager.getManager(NamingResourcesImpl.class);
private volatile boolean resourceRequireExplicitRegistration = false;
// ----------------------------------------------------------- Constructors
/**
* Create a new NamingResources instance.
*/
public NamingResourcesImpl() {
// NOOP
}
// ----------------------------------------------------- Instance Variables
/**
* Associated container object.
*/
private Object container = null;
/**
* Set of naming entries, keyed by name.
*/
private final Set<String> entries = new HashSet<>();
/**
* The EJB resource references for this web application, keyed by name.
*/
private final Map<String,ContextEjb> ejbs = new HashMap<>();
/**
* The environment entries for this web application, keyed by name.
*/
private final Map<String,ContextEnvironment> envs = new HashMap<>();
/**
* The local EJB resource references for this web application, keyed by name.
*/
private final Map<String,ContextLocalEjb> localEjbs = new HashMap<>();
/**
* The message destination references for this web application, keyed by name.
*/
private final Map<String,MessageDestinationRef> mdrs = new HashMap<>();
/**
* The resource environment references for this web application, keyed by name.
*/
private final HashMap<String,ContextResourceEnvRef> resourceEnvRefs = new HashMap<>();
/**
* The resource references for this web application, keyed by name.
*/
private final HashMap<String,ContextResource> resources = new HashMap<>();
/**
* The resource links for this web application, keyed by name.
*/
private final HashMap<String,ContextResourceLink> resourceLinks = new HashMap<>();
/**
* The web service references for this web application, keyed by name.
*/
private final HashMap<String,ContextService> services = new HashMap<>();
/**
* The transaction for this webapp.
*/
private ContextTransaction transaction = null;
/**
* The property change support for this component.
*/
protected final PropertyChangeSupport support = new PropertyChangeSupport(this);
// ------------------------------------------------------------- Properties
@Override
public Object getContainer() {
return container;
}
/**
* Set the container with which the naming resources are associated.
*
* @param container the associated with the resources
*/
public void setContainer(Object container) {
this.container = container;
}
/**
* Set the transaction object.
*
* @param transaction the transaction descriptor
*/
public void setTransaction(ContextTransaction transaction) {
this.transaction = transaction;
}
/**
* @return the transaction object.
*/
public ContextTransaction getTransaction() {
return transaction;
}
/**
* Add an EJB resource reference for this web application.
*
* @param ejb New EJB resource reference
*/
public void addEjb(ContextEjb ejb) {
// Entries with lookup-name and ejb-link are an error (EE.5.5.2 / EE.5.5.3)
String ejbLink = ejb.getLink();
String lookupName = ejb.getLookupName();
if (ejbLink != null && !ejbLink.isEmpty() && lookupName != null && !lookupName.isEmpty()) {
throw new IllegalArgumentException(sm.getString("namingResources.ejbLookupLink", ejb.getName()));
}
if (entries.contains(ejb.getName())) {
return;
} else {
entries.add(ejb.getName());
}
synchronized (ejbs) {
ejb.setNamingResources(this);
ejbs.put(ejb.getName(), ejb);
}
support.firePropertyChange("ejb", null, ejb);
}
@Override
public void addEnvironment(ContextEnvironment environment) {
if (entries.contains(environment.getName())) {
ContextEnvironment ce = findEnvironment(environment.getName());
ContextResourceLink rl = findResourceLink(environment.getName());
if (ce != null) {
if (ce.getOverride()) {
removeEnvironment(environment.getName());
} else {
return;
}
} else if (rl != null) {
// Link. Need to look at the global resources
NamingResourcesImpl global = getServer().getGlobalNamingResources();
if (global.findEnvironment(rl.getGlobal()) != null) {
if (global.findEnvironment(rl.getGlobal()).getOverride()) {
removeResourceLink(environment.getName());
} else {
return;
}
}
} else {
// It exists but it isn't an env or a res link...
return;
}
}
List<InjectionTarget> injectionTargets = environment.getInjectionTargets();
String value = environment.getValue();
String lookupName = environment.getLookupName();
// Entries with injection targets but no value are effectively ignored
if (injectionTargets != null && !injectionTargets.isEmpty() && (value == null || value.isEmpty())) {
return;
}
// Entries with lookup-name and value are an error (EE.5.4.1.3)
if (value != null && !value.isEmpty() && lookupName != null && !lookupName.isEmpty()) {
throw new IllegalArgumentException(
sm.getString("namingResources.envEntryLookupValue", environment.getName()));
}
if (!checkResourceType(environment)) {
throw new IllegalArgumentException(
sm.getString("namingResources.resourceTypeFail", environment.getName(), environment.getType()));
}
entries.add(environment.getName());
synchronized (envs) {
environment.setNamingResources(this);
envs.put(environment.getName(), environment);
}
support.firePropertyChange("environment", null, environment);
// Register with JMX
if (resourceRequireExplicitRegistration) {
try {
MBeanUtils.createMBean(environment);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanCreateFail", environment.getName()), e);
}
}
}
// Container should be an instance of Server or Context. If it is anything
// else, return null which will trigger a NPE.
private Server getServer() {
if (container instanceof Server) {
return (Server) container;
}
if (container instanceof Context) {
// Could do this in one go. Lots of casts so split out for clarity
Engine engine = (Engine) ((Context) container).getParent().getParent();
return engine.getService().getServer();
}
return null;
}
/**
* Add a local EJB resource reference for this web application.
*
* @param ejb New EJB resource reference
*/
public void addLocalEjb(ContextLocalEjb ejb) {
if (entries.contains(ejb.getName())) {
return;
} else {
entries.add(ejb.getName());
}
synchronized (localEjbs) {
ejb.setNamingResources(this);
localEjbs.put(ejb.getName(), ejb);
}
support.firePropertyChange("localEjb", null, ejb);
}
/**
* Add a message destination reference for this web application.
*
* @param mdr New message destination reference
*/
public void addMessageDestinationRef(MessageDestinationRef mdr) {
if (entries.contains(mdr.getName())) {
return;
} else {
if (!checkResourceType(mdr)) {
throw new IllegalArgumentException(
sm.getString("namingResources.resourceTypeFail", mdr.getName(), mdr.getType()));
}
entries.add(mdr.getName());
}
synchronized (mdrs) {
mdr.setNamingResources(this);
mdrs.put(mdr.getName(), mdr);
}
support.firePropertyChange("messageDestinationRef", null, mdr);
}
/**
* Add a property change listener to this component.
*
* @param listener The listener to add
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void addResource(ContextResource resource) {
if (entries.contains(resource.getName())) {
return;
} else {
if (!checkResourceType(resource)) {
throw new IllegalArgumentException(
sm.getString("namingResources.resourceTypeFail", resource.getName(), resource.getType()));
}
entries.add(resource.getName());
}
synchronized (resources) {
resource.setNamingResources(this);
resources.put(resource.getName(), resource);
}
support.firePropertyChange("resource", null, resource);
// Register with JMX
if (resourceRequireExplicitRegistration) {
try {
MBeanUtils.createMBean(resource);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanCreateFail", resource.getName()), e);
}
}
}
/**
* Add a resource environment reference for this web application.
*
* @param resource The resource
*/
public void addResourceEnvRef(ContextResourceEnvRef resource) {
if (entries.contains(resource.getName())) {
return;
} else {
if (!checkResourceType(resource)) {
throw new IllegalArgumentException(
sm.getString("namingResources.resourceTypeFail", resource.getName(), resource.getType()));
}
entries.add(resource.getName());
}
synchronized (resourceEnvRefs) {
resource.setNamingResources(this);
resourceEnvRefs.put(resource.getName(), resource);
}
support.firePropertyChange("resourceEnvRef", null, resource);
}
@Override
public void addResourceLink(ContextResourceLink resourceLink) {
if (entries.contains(resourceLink.getName())) {
return;
} else {
entries.add(resourceLink.getName());
}
synchronized (resourceLinks) {
resourceLink.setNamingResources(this);
resourceLinks.put(resourceLink.getName(), resourceLink);
}
support.firePropertyChange("resourceLink", null, resourceLink);
// Register with JMX
if (resourceRequireExplicitRegistration) {
try {
MBeanUtils.createMBean(resourceLink);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanCreateFail", resourceLink.getName()), e);
}
}
}
/**
* Add a web service reference for this web application.
*
* @param service New web service reference
*/
public void addService(ContextService service) {
if (entries.contains(service.getName())) {
return;
} else {
entries.add(service.getName());
}
synchronized (services) {
service.setNamingResources(this);
services.put(service.getName(), service);
}
support.firePropertyChange("service", null, service);
}
/**
* @return the EJB resource reference with the specified name, if any; otherwise, return <code>null</code>.
*
* @param name Name of the desired EJB resource reference
*/
public ContextEjb findEjb(String name) {
synchronized (ejbs) {
return ejbs.get(name);
}
}
/**
* @return the defined EJB resource references for this application. If there are none, a zero-length array is
* returned.
*/
public ContextEjb[] findEjbs() {
synchronized (ejbs) {
return ejbs.values().toArray(new ContextEjb[0]);
}
}
/**
* @return the environment entry with the specified name, if any; otherwise, return <code>null</code>.
*
* @param name Name of the desired environment entry
*/
public ContextEnvironment findEnvironment(String name) {
synchronized (envs) {
return envs.get(name);
}
}
/**
* @return the array of defined environment entries for this web application. If none have been defined, a zero-length
* array is returned.
*/
public ContextEnvironment[] findEnvironments() {
synchronized (envs) {
return envs.values().toArray(new ContextEnvironment[0]);
}
}
/**
* @return the local EJB resource reference with the specified name, if any; otherwise, return <code>null</code>.
*
* @param name Name of the desired EJB resource reference
*/
public ContextLocalEjb findLocalEjb(String name) {
synchronized (localEjbs) {
return localEjbs.get(name);
}
}
/**
* @return the defined local EJB resource references for this application. If there are none, a zero-length array is
* returned.
*/
public ContextLocalEjb[] findLocalEjbs() {
synchronized (localEjbs) {
return localEjbs.values().toArray(new ContextLocalEjb[0]);
}
}
/**
* @return the message destination reference with the specified name, if any; otherwise, return <code>null</code>.
*
* @param name Name of the desired message destination reference
*/
public MessageDestinationRef findMessageDestinationRef(String name) {
synchronized (mdrs) {
return mdrs.get(name);
}
}
/**
* @return the defined message destination references for this application. If there are none, a zero-length array
* is returned.
*/
public MessageDestinationRef[] findMessageDestinationRefs() {
synchronized (mdrs) {
return mdrs.values().toArray(new MessageDestinationRef[0]);
}
}
/**
* @return the resource reference with the specified name, if any; otherwise return <code>null</code>.
*
* @param name Name of the desired resource reference
*/
public ContextResource findResource(String name) {
synchronized (resources) {
return resources.get(name);
}
}
/**
* @return the resource link with the specified name, if any; otherwise return <code>null</code>.
*
* @param name Name of the desired resource link
*/
public ContextResourceLink findResourceLink(String name) {
synchronized (resourceLinks) {
return resourceLinks.get(name);
}
}
/**
* @return the defined resource links for this application. If none have been defined, a zero-length array is
* returned.
*/
public ContextResourceLink[] findResourceLinks() {
synchronized (resourceLinks) {
return resourceLinks.values().toArray(new ContextResourceLink[0]);
}
}
/**
* @return the defined resource references for this application. If none have been defined, a zero-length array is
* returned.
*/
public ContextResource[] findResources() {
synchronized (resources) {
return resources.values().toArray(new ContextResource[0]);
}
}
/**
* @return the resource environment reference type for the specified name, if any; otherwise return
* <code>null</code>.
*
* @param name Name of the desired resource environment reference
*/
public ContextResourceEnvRef findResourceEnvRef(String name) {
synchronized (resourceEnvRefs) {
return resourceEnvRefs.get(name);
}
}
/**
* @return the array of resource environment reference names for this web application. If none have been specified, a
* zero-length array is returned.
*/
public ContextResourceEnvRef[] findResourceEnvRefs() {
synchronized (resourceEnvRefs) {
return resourceEnvRefs.values().toArray(new ContextResourceEnvRef[0]);
}
}
/**
* @return the web service reference for the specified name, if any; otherwise return <code>null</code>.
*
* @param name Name of the desired web service
*/
public ContextService findService(String name) {
synchronized (services) {
return services.get(name);
}
}
/**
* @return the defined web service references for this application. If none have been defined, a zero-length array
* is returned.
*/
public ContextService[] findServices() {
synchronized (services) {
return services.values().toArray(new ContextService[0]);
}
}
/**
* Remove any EJB resource reference with the specified name.
*
* @param name Name of the EJB resource reference to remove
*/
public void removeEjb(String name) {
entries.remove(name);
ContextEjb ejb;
synchronized (ejbs) {
ejb = ejbs.remove(name);
}
if (ejb != null) {
support.firePropertyChange("ejb", ejb, null);
ejb.setNamingResources(null);
}
}
@Override
public void removeEnvironment(String name) {
entries.remove(name);
ContextEnvironment environment;
synchronized (envs) {
environment = envs.remove(name);
}
if (environment != null) {
support.firePropertyChange("environment", environment, null);
// De-register with JMX
if (resourceRequireExplicitRegistration) {
try {
MBeanUtils.destroyMBean(environment);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", environment.getName()), e);
}
}
environment.setNamingResources(null);
}
}
/**
* Remove any local EJB resource reference with the specified name.
*
* @param name Name of the EJB resource reference to remove
*/
public void removeLocalEjb(String name) {
entries.remove(name);
ContextLocalEjb localEjb;
synchronized (localEjbs) {
localEjb = localEjbs.remove(name);
}
if (localEjb != null) {
support.firePropertyChange("localEjb", localEjb, null);
localEjb.setNamingResources(null);
}
}
/**
* Remove any message destination reference with the specified name.
*
* @param name Name of the message destination resource reference to remove
*/
public void removeMessageDestinationRef(String name) {
entries.remove(name);
MessageDestinationRef mdr;
synchronized (mdrs) {
mdr = mdrs.remove(name);
}
if (mdr != null) {
support.firePropertyChange("messageDestinationRef", mdr, null);
mdr.setNamingResources(null);
}
}
/**
* Remove a property change listener from this component.
*
* @param listener The listener to remove
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}
@Override
public void removeResource(String name) {
entries.remove(name);
ContextResource resource;
synchronized (resources) {
resource = resources.remove(name);
}
if (resource != null) {
support.firePropertyChange("resource", resource, null);
// De-register with JMX
if (resourceRequireExplicitRegistration) {
try {
MBeanUtils.destroyMBean(resource);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", resource.getName()), e);
}
}
resource.setNamingResources(null);
}
}
/**
* Remove any resource environment reference with the specified name.
*
* @param name Name of the resource environment reference to remove
*/
public void removeResourceEnvRef(String name) {
entries.remove(name);
ContextResourceEnvRef resourceEnvRef;
synchronized (resourceEnvRefs) {
resourceEnvRef = resourceEnvRefs.remove(name);
}
if (resourceEnvRef != null) {
support.firePropertyChange("resourceEnvRef", resourceEnvRef, null);
resourceEnvRef.setNamingResources(null);
}
}
@Override
public void removeResourceLink(String name) {
entries.remove(name);
ContextResourceLink resourceLink;
synchronized (resourceLinks) {
resourceLink = resourceLinks.remove(name);
}
if (resourceLink != null) {
support.firePropertyChange("resourceLink", resourceLink, null);
// De-register with JMX
if (resourceRequireExplicitRegistration) {
try {
MBeanUtils.destroyMBean(resourceLink);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", resourceLink.getName()), e);
}
}
resourceLink.setNamingResources(null);
}
}
/**
* Remove any web service reference with the specified name.
*
* @param name Name of the web service reference to remove
*/
public void removeService(String name) {
entries.remove(name);
ContextService service;
synchronized (services) {
service = services.remove(name);
}
if (service != null) {
support.firePropertyChange("service", service, null);
service.setNamingResources(null);
}
}
// ------------------------------------------------------- Lifecycle methods
@Override
protected void initInternal() throws LifecycleException {
super.initInternal();
// Set this before we register currently known naming resources to avoid
// timing issues. Duplication registration is not an issue.
resourceRequireExplicitRegistration = true;
for (ContextResource cr : resources.values()) {
try {
MBeanUtils.createMBean(cr);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanCreateFail", cr.getName()), e);
}
}
for (ContextEnvironment ce : envs.values()) {
try {
MBeanUtils.createMBean(ce);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanCreateFail", ce.getName()), e);
}
}
for (ContextResourceLink crl : resourceLinks.values()) {
try {
MBeanUtils.createMBean(crl);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanCreateFail", crl.getName()), e);
}
}
}
@Override
protected void startInternal() throws LifecycleException {
fireLifecycleEvent(CONFIGURE_START_EVENT, null);
setState(LifecycleState.STARTING);
}
@Override
protected void stopInternal() throws LifecycleException {
cleanUp();
setState(LifecycleState.STOPPING);
fireLifecycleEvent(CONFIGURE_STOP_EVENT, null);
}
/**
* Close those resources that an explicit close may help clean-up faster.
*/
private void cleanUp() {
if (resources.isEmpty()) {
return;
}
javax.naming.Context ctxt;
try {
if (container instanceof Server) {
ctxt = ((Server) container).getGlobalNamingContext();
} else {
ctxt = ContextBindings.getClassLoader();
ctxt = (javax.naming.Context) ctxt.lookup("comp/env");
}
} catch (NamingException e) {
log.warn(sm.getString("namingResources.cleanupNoContext", container), e);
return;
}
for (ContextResource cr : resources.values()) {
if (cr.getSingleton()) {
String closeMethod = cr.getCloseMethod();
if (closeMethod != null && !closeMethod.isEmpty()) {
String name = cr.getName();
Object resource;
try {
resource = ctxt.lookup(name);
} catch (NamingException e) {
log.warn(sm.getString("namingResources.cleanupNoResource", cr.getName(), container), e);
continue;
}
cleanUp(resource, name, closeMethod);
}
}
}
}
/**
* Clean up a resource by calling the defined close method. For example, closing a database connection pool will
* close it's open connections. This will happen on GC but that leaves db connections open that may cause issues.
*
* @param resource The resource to close.
*/
private void cleanUp(Object resource, String name, String closeMethod) {
// Look for a zero-arg close() method
Method m;
try {
m = resource.getClass().getMethod(closeMethod, (Class<?>[]) null);
} catch (SecurityException e) {
log.debug(sm.getString("namingResources.cleanupCloseSecurity", closeMethod, name, container));
return;
} catch (NoSuchMethodException e) {
log.debug(sm.getString("namingResources.cleanupNoClose", name, container, closeMethod));
return;
}
try {
m.invoke(resource, (Object[]) null);
} catch (IllegalArgumentException | IllegalAccessException e) {
log.warn(sm.getString("namingResources.cleanupCloseFailed", closeMethod, name, container), e);
} catch (InvocationTargetException e) {
Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
ExceptionUtils.handleThrowable(t);
log.warn(sm.getString("namingResources.cleanupCloseFailed", closeMethod, name, container), t);
}
}
@Override
protected void destroyInternal() throws LifecycleException {
// Set this before we de-register currently known naming resources to
// avoid timing issues. Duplication de-registration is not an issue.
resourceRequireExplicitRegistration = false;
// Destroy in reverse order to create, although it should not matter
for (ContextResourceLink crl : resourceLinks.values()) {
try {
MBeanUtils.destroyMBean(crl);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", crl.getName()), e);
}
}
for (ContextEnvironment ce : envs.values()) {
try {
MBeanUtils.destroyMBean(ce);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", ce.getName()), e);
}
}
for (ContextResource cr : resources.values()) {
try {
MBeanUtils.destroyMBean(cr);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", cr.getName()), e);
}
}
super.destroyInternal();
}
@Override
protected String getDomainInternal() {
// Use the same domain as our associated container if we have one
Object c = getContainer();
if (c instanceof JmxEnabled) {
return ((JmxEnabled) c).getDomain();
}
return null;
}
@Override
protected String getObjectNameKeyProperties() {
Object c = getContainer();
if (c instanceof Container) {
return "type=NamingResources" + ((Container) c).getMBeanKeyProperties();
}
// Server or just unknown
return "type=NamingResources";
}
/**
* Checks that the configuration of the type for the specified resource is consistent with any injection targets and
* if the type is not specified, tries to configure the type based on the injection targets
*
* @param resource The resource to check
*
* @return <code>true</code> if the type for the resource is now valid (if previously <code>null</code> this means
* it is now set) or <code>false</code> if the current resource type is inconsistent with the injection
* targets and/or cannot be determined
*/
private boolean checkResourceType(ResourceBase resource) {
if (!(container instanceof Context)) {
// Only Context's will have injection targets
return true;
}
if (resource.getInjectionTargets() == null || resource.getInjectionTargets().isEmpty()) {
// No injection targets so use the defined type for the resource
return true;
}
Context context = (Context) container;
String typeName = resource.getType();
Class<?> typeClass = null;
if (typeName != null) {
typeClass = Introspection.loadClass(context, typeName);
if (typeClass == null) {
// Can't load the type - will trigger a failure later so don't
// fail here
return true;
}
}
Class<?> compatibleClass = getCompatibleType(context, resource, typeClass);
if (compatibleClass == null) {
// Indicates that a compatible type could not be identified that
// worked for all injection targets
return false;
}
resource.setType(compatibleClass.getCanonicalName());
return true;
}
private Class<?> getCompatibleType(Context context, ResourceBase resource, Class<?> typeClass) {
Class<?> result = null;
for (InjectionTarget injectionTarget : resource.getInjectionTargets()) {
Class<?> clazz = Introspection.loadClass(context, injectionTarget.getTargetClass());
if (clazz == null) {
// Can't load class - therefore ignore this target
continue;
}
// Look for a match
String targetName = injectionTarget.getTargetName();
// Look for a setter match first
Class<?> targetType = getSetterType(clazz, targetName);
if (targetType == null) {
// Try a field match if no setter match
targetType = getFieldType(clazz, targetName);
}
if (targetType == null) {
// No match - ignore this injection target
continue;
}
targetType = Introspection.convertPrimitiveType(targetType);
if (typeClass == null) {
// Need to find a common type amongst the injection targets
if (result == null) {
result = targetType;
} else if (targetType.isAssignableFrom(result)) {
// NO-OP - This will work
} else if (result.isAssignableFrom(targetType)) {
// Need to use more specific type
result = targetType;
} else {
// Incompatible types
return null;
}
} else {
// Each injection target needs to be consistent with the defined
// type
if (targetType.isAssignableFrom(typeClass)) {
result = typeClass;
} else {
// Incompatible types
return null;
}
}
}
return result;
}
private Class<?> getSetterType(Class<?> clazz, String name) {
Method[] methods = Introspection.getDeclaredMethods(clazz);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
if (Introspection.isValidSetter(method) && Introspection.getPropertyName(method).equals(name)) {
return method.getParameterTypes()[0];
}
}
}
return null;
}
private Class<?> getFieldType(Class<?> clazz, String name) {
Field[] fields = Introspection.getDeclaredFields(clazz);
if (fields != null && fields.length > 0) {
for (Field field : fields) {
if (field.getName().equals(name)) {
return field.getType();
}
}
}
return null;
}
}
|