From 6190b10148c7076eb97f2fec73849e98f5e1fb87 Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Thu, 22 Jun 2023 20:21:32 +0100 Subject: [PATCH] Code cleanup - formatting. No functional change. --- .../catalina/deploy/NamingResourcesImpl.java | 235 +++++++----------- 1 file changed, 90 insertions(+), 145 deletions(-) diff --git a/java/org/apache/catalina/deploy/NamingResourcesImpl.java b/java/org/apache/catalina/deploy/NamingResourcesImpl.java index 37f90217e29e..39fcb2853b06 100644 --- a/java/org/apache/catalina/deploy/NamingResourcesImpl.java +++ b/java/org/apache/catalina/deploy/NamingResourcesImpl.java @@ -60,13 +60,11 @@ /** - * Holds and manages the naming resources defined in the Jakarta EE - * Naming Context and their associated JNDI context. + * 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 { +public class NamingResourcesImpl extends LifecycleMBeanBase implements Serializable, NamingResources { private static final long serialVersionUID = 1L; @@ -105,56 +103,49 @@ public NamingResourcesImpl() { /** * The EJB resource references for this web application, keyed by name. */ - private final Map ejbs = new HashMap<>(); + private final Map ejbs = new HashMap<>(); /** * The environment entries for this web application, keyed by name. */ - private final Map envs = new HashMap<>(); + private final Map envs = new HashMap<>(); /** - * The local EJB resource references for this web application, keyed by - * name. + * The local EJB resource references for this web application, keyed by name. */ - private final Map localEjbs = new HashMap<>(); + private final Map localEjbs = new HashMap<>(); /** - * The message destination references for this web application, - * keyed by name. + * The message destination references for this web application, keyed by name. */ - private final Map mdrs = new HashMap<>(); + private final Map mdrs = new HashMap<>(); /** - * The resource environment references for this web application, - * keyed by name. + * The resource environment references for this web application, keyed by name. */ - private final HashMap resourceEnvRefs = - new HashMap<>(); + private final HashMap resourceEnvRefs = new HashMap<>(); /** * The resource references for this web application, keyed by name. */ - private final HashMap resources = - new HashMap<>(); + private final HashMap resources = new HashMap<>(); /** * The resource links for this web application, keyed by name. */ - private final HashMap resourceLinks = - new HashMap<>(); + private final HashMap resourceLinks = new HashMap<>(); /** * The web service references for this web application, keyed by name. */ - private final HashMap services = - new HashMap<>(); + private final HashMap services = new HashMap<>(); /** @@ -166,8 +157,7 @@ public NamingResourcesImpl() { /** * The property change support for this component. */ - protected final PropertyChangeSupport support = - new PropertyChangeSupport(this); + protected final PropertyChangeSupport support = new PropertyChangeSupport(this); // ------------------------------------------------------------- Properties @@ -184,6 +174,7 @@ public Object getContainer() { /** * Set the container with which the naming resources are associated. + * * @param container the associated with the resources */ public void setContainer(Object container) { @@ -193,6 +184,7 @@ public void setContainer(Object container) { /** * Set the transaction object. + * * @param transaction the transaction descriptor */ public void setTransaction(ContextTransaction transaction) { @@ -220,8 +212,7 @@ public void addEjb(ContextEjb ejb) { String lookupName = ejb.getLookupName(); if (ejbLink != null && ejbLink.length() > 0 && lookupName != null && lookupName.length() > 0) { - throw new IllegalArgumentException( - sm.getString("namingResources.ejbLookupLink", ejb.getName())); + throw new IllegalArgumentException(sm.getString("namingResources.ejbLookupLink", ejb.getName())); } if (entries.contains(ejb.getName())) { @@ -277,8 +268,7 @@ public void addEnvironment(ContextEnvironment environment) { String lookupName = environment.getLookupName(); // Entries with injection targets but no value are effectively ignored - if (injectionTargets != null && injectionTargets.size() > 0 && - (value == null || value.length() == 0)) { + if (injectionTargets != null && injectionTargets.size() > 0 && (value == null || value.length() == 0)) { return; } @@ -289,9 +279,8 @@ public void addEnvironment(ContextEnvironment environment) { } if (!checkResourceType(environment)) { - throw new IllegalArgumentException(sm.getString( - "namingResources.resourceTypeFail", environment.getName(), - environment.getType())); + throw new IllegalArgumentException( + sm.getString("namingResources.resourceTypeFail", environment.getName(), environment.getType())); } entries.add(environment.getName()); @@ -307,8 +296,7 @@ public void addEnvironment(ContextEnvironment environment) { try { MBeanUtils.createMBean(environment); } catch (Exception e) { - log.warn(sm.getString("namingResources.mbeanCreateFail", - environment.getName()), e); + log.warn(sm.getString("namingResources.mbeanCreateFail", environment.getName()), e); } } } @@ -321,8 +309,7 @@ private Server getServer() { } 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(); + Engine engine = (Engine) ((Context) container).getParent().getParent(); return engine.getService().getServer(); } return null; @@ -361,9 +348,8 @@ public void addMessageDestinationRef(MessageDestinationRef mdr) { return; } else { if (!checkResourceType(mdr)) { - throw new IllegalArgumentException(sm.getString( - "namingResources.resourceTypeFail", mdr.getName(), - mdr.getType())); + throw new IllegalArgumentException( + sm.getString("namingResources.resourceTypeFail", mdr.getName(), mdr.getType())); } entries.add(mdr.getName()); } @@ -401,9 +387,8 @@ public void addResource(ContextResource resource) { return; } else { if (!checkResourceType(resource)) { - throw new IllegalArgumentException(sm.getString( - "namingResources.resourceTypeFail", resource.getName(), - resource.getType())); + throw new IllegalArgumentException( + sm.getString("namingResources.resourceTypeFail", resource.getName(), resource.getType())); } entries.add(resource.getName()); } @@ -419,8 +404,7 @@ public void addResource(ContextResource resource) { try { MBeanUtils.createMBean(resource); } catch (Exception e) { - log.warn(sm.getString("namingResources.mbeanCreateFail", - resource.getName()), e); + log.warn(sm.getString("namingResources.mbeanCreateFail", resource.getName()), e); } } } @@ -437,9 +421,8 @@ public void addResourceEnvRef(ContextResourceEnvRef resource) { return; } else { if (!checkResourceType(resource)) { - throw new IllegalArgumentException(sm.getString( - "namingResources.resourceTypeFail", resource.getName(), - resource.getType())); + throw new IllegalArgumentException( + sm.getString("namingResources.resourceTypeFail", resource.getName(), resource.getType())); } entries.add(resource.getName()); } @@ -478,8 +461,7 @@ public void addResourceLink(ContextResourceLink resourceLink) { try { MBeanUtils.createMBean(resourceLink); } catch (Exception e) { - log.warn(sm.getString("namingResources.mbeanCreateFail", - resourceLink.getName()), e); + log.warn(sm.getString("namingResources.mbeanCreateFail", resourceLink.getName()), e); } } } @@ -508,8 +490,7 @@ public void addService(ContextService service) { /** - * @return the EJB resource reference with the specified name, if any; - * otherwise, return null. + * @return the EJB resource reference with the specified name, if any; otherwise, return null. * * @param name Name of the desired EJB resource reference */ @@ -523,8 +504,8 @@ public ContextEjb findEjb(String name) { /** - * @return the defined EJB resource references for this application. - * If there are none, a zero-length array is returned. + * @return the defined EJB resource references for this application. If there are none, a zero-length array is + * returned. */ public ContextEjb[] findEjbs() { @@ -536,8 +517,7 @@ public ContextEjb[] findEjbs() { /** - * @return the environment entry with the specified name, if any; - * otherwise, return null. + * @return the environment entry with the specified name, if any; otherwise, return null. * * @param name Name of the desired environment entry */ @@ -551,9 +531,8 @@ public ContextEnvironment findEnvironment(String name) { /** - * @return the set of defined environment entries for this web - * application. If none have been defined, a zero-length array - * is returned. + * @return the set of defined environment entries for this web application. If none have been defined, a zero-length + * array is returned. */ public ContextEnvironment[] findEnvironments() { @@ -565,8 +544,7 @@ public ContextEnvironment[] findEnvironments() { /** - * @return the local EJB resource reference with the specified name, if any; - * otherwise, return null. + * @return the local EJB resource reference with the specified name, if any; otherwise, return null. * * @param name Name of the desired EJB resource reference */ @@ -580,8 +558,8 @@ public ContextLocalEjb findLocalEjb(String name) { /** - * @return the defined local EJB resource references for this application. - * If there are none, a zero-length array is returned. + * @return the defined local EJB resource references for this application. If there are none, a zero-length array is + * returned. */ public ContextLocalEjb[] findLocalEjbs() { @@ -593,8 +571,7 @@ public ContextLocalEjb[] findLocalEjbs() { /** - * @return the message destination reference with the specified name, - * if any; otherwise, return null. + * @return the message destination reference with the specified name, if any; otherwise, return null. * * @param name Name of the desired message destination reference */ @@ -608,8 +585,8 @@ public MessageDestinationRef findMessageDestinationRef(String name) { /** - * @return the defined message destination references for this application. - * If there are none, a zero-length array is returned. + * @return the defined message destination references for this application. If there are none, a zero-length array + * is returned. */ public MessageDestinationRef[] findMessageDestinationRefs() { @@ -621,8 +598,7 @@ public MessageDestinationRef[] findMessageDestinationRefs() { /** - * @return the resource reference with the specified name, if any; - * otherwise return null. + * @return the resource reference with the specified name, if any; otherwise return null. * * @param name Name of the desired resource reference */ @@ -636,8 +612,7 @@ public ContextResource findResource(String name) { /** - * @return the resource link with the specified name, if any; - * otherwise return null. + * @return the resource link with the specified name, if any; otherwise return null. * * @param name Name of the desired resource link */ @@ -651,8 +626,8 @@ public ContextResourceLink findResourceLink(String name) { /** - * @return the defined resource links for this application. If - * none have been defined, a zero-length array is returned. + * @return the defined resource links for this application. If none have been defined, a zero-length array is + * returned. */ public ContextResourceLink[] findResourceLinks() { @@ -664,8 +639,8 @@ public ContextResourceLink[] findResourceLinks() { /** - * @return the defined resource references for this application. If - * none have been defined, a zero-length array is returned. + * @return the defined resource references for this application. If none have been defined, a zero-length array is + * returned. */ public ContextResource[] findResources() { @@ -677,8 +652,8 @@ public ContextResource[] findResources() { /** - * @return the resource environment reference type for the specified - * name, if any; otherwise return null. + * @return the resource environment reference type for the specified name, if any; otherwise return + * null. * * @param name Name of the desired resource environment reference */ @@ -692,9 +667,8 @@ public ContextResourceEnvRef findResourceEnvRef(String name) { /** - * @return the set of resource environment reference names for this - * web application. If none have been specified, a zero-length - * array is returned. + * @return the set of resource environment reference names for this web application. If none have been specified, a + * zero-length array is returned. */ public ContextResourceEnvRef[] findResourceEnvRefs() { @@ -706,8 +680,7 @@ public ContextResourceEnvRef[] findResourceEnvRefs() { /** - * @return the web service reference for the specified - * name, if any; otherwise return null. + * @return the web service reference for the specified name, if any; otherwise return null. * * @param name Name of the desired web service */ @@ -721,8 +694,8 @@ public ContextService findService(String name) { /** - * @return the defined web service references for this application. If - * none have been defined, a zero-length array is returned. + * @return the defined web service references for this application. If none have been defined, a zero-length array + * is returned. */ public ContextService[] findServices() { @@ -775,8 +748,7 @@ public void removeEnvironment(String name) { try { MBeanUtils.destroyMBean(environment); } catch (Exception e) { - log.warn(sm.getString("namingResources.mbeanDestroyFail", - environment.getName()), e); + log.warn(sm.getString("namingResources.mbeanDestroyFail", environment.getName()), e); } } environment.setNamingResources(null); @@ -819,8 +791,7 @@ public void removeMessageDestinationRef(String name) { mdr = mdrs.remove(name); } if (mdr != null) { - support.firePropertyChange("messageDestinationRef", - mdr, null); + support.firePropertyChange("messageDestinationRef", mdr, null); mdr.setNamingResources(null); } @@ -860,8 +831,7 @@ public void removeResource(String name) { try { MBeanUtils.destroyMBean(resource); } catch (Exception e) { - log.warn(sm.getString("namingResources.mbeanDestroyFail", - resource.getName()), e); + log.warn(sm.getString("namingResources.mbeanDestroyFail", resource.getName()), e); } } resource.setNamingResources(null); @@ -880,8 +850,7 @@ public void removeResourceEnvRef(String name) { ContextResourceEnvRef resourceEnvRef = null; synchronized (resourceEnvRefs) { - resourceEnvRef = - resourceEnvRefs.remove(name); + resourceEnvRef = resourceEnvRefs.remove(name); } if (resourceEnvRef != null) { support.firePropertyChange("resourceEnvRef", resourceEnvRef, null); @@ -912,8 +881,7 @@ public void removeResourceLink(String name) { try { MBeanUtils.destroyMBean(resourceLink); } catch (Exception e) { - log.warn(sm.getString("namingResources.mbeanDestroyFail", - resourceLink.getName()), e); + log.warn(sm.getString("namingResources.mbeanDestroyFail", resourceLink.getName()), e); } } resourceLink.setNamingResources(null); @@ -956,8 +924,7 @@ protected void initInternal() throws LifecycleException { try { MBeanUtils.createMBean(cr); } catch (Exception e) { - log.warn(sm.getString( - "namingResources.mbeanCreateFail", cr.getName()), e); + log.warn(sm.getString("namingResources.mbeanCreateFail", cr.getName()), e); } } @@ -965,8 +932,7 @@ protected void initInternal() throws LifecycleException { try { MBeanUtils.createMBean(ce); } catch (Exception e) { - log.warn(sm.getString( - "namingResources.mbeanCreateFail", ce.getName()), e); + log.warn(sm.getString("namingResources.mbeanCreateFail", ce.getName()), e); } } @@ -974,8 +940,7 @@ protected void initInternal() throws LifecycleException { try { MBeanUtils.createMBean(crl); } catch (Exception e) { - log.warn(sm.getString( - "namingResources.mbeanCreateFail", crl.getName()), e); + log.warn(sm.getString("namingResources.mbeanCreateFail", crl.getName()), e); } } } @@ -1011,22 +976,19 @@ private void cleanUp() { ctxt = (javax.naming.Context) ctxt.lookup("comp/env"); } } catch (NamingException e) { - log.warn(sm.getString("namingResources.cleanupNoContext", - container), e); + log.warn(sm.getString("namingResources.cleanupNoContext", container), e); return; } - for (ContextResource cr: resources.values()) { + for (ContextResource cr : resources.values()) { if (cr.getSingleton()) { String closeMethod = cr.getCloseMethod(); if (closeMethod != null && closeMethod.length() > 0) { String name = cr.getName(); Object resource; try { - resource = ctxt.lookup(name); + resource = ctxt.lookup(name); } catch (NamingException e) { - log.warn(sm.getString( - "namingResources.cleanupNoResource", - cr.getName(), container), e); + log.warn(sm.getString("namingResources.cleanupNoResource", cr.getName(), container), e); continue; } cleanUp(resource, name, closeMethod); @@ -1037,12 +999,10 @@ private void cleanUp() { /** - * 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. + * 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. + * @param resource The resource to close. */ private void cleanUp(Object resource, String name, String closeMethod) { // Look for a zero-arg close() method @@ -1050,24 +1010,20 @@ private void cleanUp(Object resource, String name, String closeMethod) { try { m = resource.getClass().getMethod(closeMethod, (Class[]) null); } catch (SecurityException e) { - log.debug(sm.getString("namingResources.cleanupCloseSecurity", - closeMethod, name, container)); + log.debug(sm.getString("namingResources.cleanupCloseSecurity", closeMethod, name, container)); return; } catch (NoSuchMethodException e) { - log.debug(sm.getString("namingResources.cleanupNoClose", - name, container, closeMethod)); + 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); + 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); + log.warn(sm.getString("namingResources.cleanupCloseFailed", closeMethod, name, container), t); } } @@ -1083,8 +1039,7 @@ protected void destroyInternal() throws LifecycleException { try { MBeanUtils.destroyMBean(crl); } catch (Exception e) { - log.warn(sm.getString( - "namingResources.mbeanDestroyFail", crl.getName()), e); + log.warn(sm.getString("namingResources.mbeanDestroyFail", crl.getName()), e); } } @@ -1092,8 +1047,7 @@ protected void destroyInternal() throws LifecycleException { try { MBeanUtils.destroyMBean(ce); } catch (Exception e) { - log.warn(sm.getString( - "namingResources.mbeanDestroyFail", ce.getName()), e); + log.warn(sm.getString("namingResources.mbeanDestroyFail", ce.getName()), e); } } @@ -1101,8 +1055,7 @@ protected void destroyInternal() throws LifecycleException { try { MBeanUtils.destroyMBean(cr); } catch (Exception e) { - log.warn(sm.getString( - "namingResources.mbeanDestroyFail", cr.getName()), e); + log.warn(sm.getString("namingResources.mbeanDestroyFail", cr.getName()), e); } } @@ -1127,24 +1080,21 @@ protected String getDomainInternal() { protected String getObjectNameKeyProperties() { Object c = getContainer(); if (c instanceof Container) { - return "type=NamingResources" + - ((Container) c).getMBeanKeyProperties(); + 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 + * 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 + * @param resource The resource to check * - * @return true if the type for the resource is now valid (if - * previously null this means it is now set) or - * false if the current resource type is inconsistent - * with the injection targets and/or cannot be determined + * @return true if the type for the resource is now valid (if previously null this means + * it is now set) or false 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)) { @@ -1152,8 +1102,7 @@ private boolean checkResourceType(ResourceBase resource) { return true; } - if (resource.getInjectionTargets() == null || - resource.getInjectionTargets().size() == 0) { + if (resource.getInjectionTargets() == null || resource.getInjectionTargets().size() == 0) { // No injection targets so use the defined type for the resource return true; } @@ -1171,8 +1120,7 @@ private boolean checkResourceType(ResourceBase resource) { } } - Class compatibleClass = - getCompatibleType(context, resource, typeClass); + Class compatibleClass = getCompatibleType(context, resource, typeClass); if (compatibleClass == null) { // Indicates that a compatible type could not be identified that // worked for all injection targets @@ -1183,14 +1131,12 @@ private boolean checkResourceType(ResourceBase resource) { return true; } - private Class getCompatibleType(Context context, - ResourceBase resource, Class typeClass) { + private Class getCompatibleType(Context context, ResourceBase resource, Class typeClass) { Class result = null; for (InjectionTarget injectionTarget : resource.getInjectionTargets()) { - Class clazz = Introspection.loadClass( - context, injectionTarget.getTargetClass()); + Class clazz = Introspection.loadClass(context, injectionTarget.getTargetClass()); if (clazz == null) { // Can't load class - therefore ignore this target continue; @@ -1202,7 +1148,7 @@ private Class getCompatibleType(Context context, Class targetType = getSetterType(clazz, targetName); if (targetType == null) { // Try a field match if no setter match - targetType = getFieldType(clazz,targetName); + targetType = getFieldType(clazz, targetName); } if (targetType == null) { // No match - ignore this injection target @@ -1241,8 +1187,7 @@ private Class getSetterType(Class clazz, String name) { Method[] methods = clazz.getDeclaredMethods(); if (methods != null && methods.length > 0) { for (Method method : methods) { - if (Introspection.isValidSetter(method) && - Introspection.getPropertyName(method).equals(name)) { + if (Introspection.isValidSetter(method) && Introspection.getPropertyName(method).equals(name)) { return method.getParameterTypes()[0]; } }