Implment Filip's idea for a configurable close method
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 30 Mar 2011 13:35:12 +0000 (13:35 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 30 Mar 2011 13:35:12 +0000 (13:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1086950 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/deploy/ContextResource.java
java/org/apache/catalina/deploy/LocalStrings.properties
java/org/apache/catalina/deploy/NamingResources.java
webapps/docs/changelog.xml
webapps/docs/config/context.xml

index 237e583..dbe8526 100644 (file)
@@ -80,6 +80,24 @@ public class ContextResource extends ResourceBase {
         this.singleton = singleton;
     }
 
+
+    /**
+     * The name of the zero argument method to be called when the resource is
+     * no longer required to clean-up resources. This method must only speed up
+     * the clean-up of resources that would otherwise happen via garbage
+     * collection.
+     */
+    private String closeMethod = "close";
+    
+    public String getCloseMethod() {
+        return closeMethod;
+    }
+
+    public void setCloseMethod(String closeMethod) {
+        this.closeMethod = closeMethod;
+    }
+
+
     // --------------------------------------------------------- Public Methods
 
 
index 5f2b8cd..c225757 100644 (file)
@@ -43,9 +43,9 @@ webxml.unrecognisedPublicId=The public ID [{0}] did not match any of the known p
 webXml.version.nfe=Unable to parse [{0}] from the version string [{1}]. This component of the version string will be ignored. 
 webXml.wrongFragmentName=Used a wrong fragment name {0} at web.xml absolute-ordering tag!
 
-namingResources.cleanupCloseFailed=Failed to invoke close method for resource [{0}] in container [{1}] so no cleanup was performed for that resource
-namingResources.cleanupCloseSecurity=Unable to retrieve close method for resource [{0}] in container [{1}] so no cleanup was performed for that resource
-namingResources.cleanupNoClose=Resource [{0}] in container [{1}] does not have a close method so no cleanup was performed for that resource
+namingResources.cleanupCloseFailed=Failed to invoke method [{0}] for resource [{1}] in container [{2}] so no cleanup was performed for that resource
+namingResources.cleanupCloseSecurity=Unable to retrieve method [{0}] for resource [{1}] in container [{2}] so no cleanup was performed for that resource
+namingResources.cleanupNoClose=Resource [{0}] in container [{1}] does not have a [{2}] method so no cleanup was performed for that resource
 namingResources.cleanupNoContext=Failed to retrieve JNDI naming context for container [{0}] so no cleanup was performed for that container
 namingResources.cleanupNoResource=Failed to retrieve JNDI resource [{0}] for container [{1}] so no cleanup was performed for that resource
 namingResources.mbeanCreateFail=Failed to create MBean for naming resource [{0}]
index 726a680..58a66c0 100644 (file)
@@ -28,7 +28,6 @@ import java.util.HashMap;
 import java.util.Hashtable;
 
 import javax.naming.NamingException;
-import javax.sql.DataSource;
 
 import org.apache.catalina.Container;
 import org.apache.catalina.Context;
@@ -981,54 +980,57 @@ public class NamingResources extends LifecycleMBeanBase implements Serializable
             return;
         }
         for (ContextResource cr: resources.values()) {
-            if (DataSource.class.getName().equals(cr.getType())) {
+            String closeMethod = cr.getCloseMethod(); 
+            if (closeMethod != null && closeMethod.length() > 0) {
                 String name = cr.getName();
-                DataSource ds;
+                Object resource;
                 try {
-                     ds = (DataSource) ctxt.lookup(name);
+                     resource = ctxt.lookup(name);
                 } catch (NamingException e) {
                     log.warn(sm.getString("namingResources.cleanupNoResource",
                             cr.getName(), container), e);
                     continue;
                 }
-                cleanUp(ds, name);
+                cleanUp(resource, name, cr.getCloseMethod());
             }
         }
     }
 
     
     /**
-     * Closing a database connection pool will close it's open connections. This
+     * 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 ds    The DataSource to close.
+     * 
+     * @param resource  The resource to close.
      */
-    private void cleanUp(DataSource ds, String name) {
+    private void cleanUp(Object resource, String name, String closeMethod) {
         // Look for a zero-arg close() method
         Method m = null;
         try {
-            m = ds.getClass().getMethod("close", (Class<?>[]) null);
+            m = resource.getClass().getMethod(closeMethod, (Class<?>[]) null);
         } catch (SecurityException e) {
-            log.debug(sm.getString("namingResources.cleanupCloseSecurity", name,
-                    container));
+            log.debug(sm.getString("namingResources.cleanupCloseSecurity",
+                    closeMethod, name, container));
             return;
         } catch (NoSuchMethodException e) {
-            log.debug(sm.getString("namingResources.cleanupNoClose", name,
-                    container));
+            log.debug(sm.getString("namingResources.cleanupNoClose",
+                    name, container, closeMethod));
             return;
         }
         if (m != null) {
             try {
-                m.invoke(ds, (Object[]) null);
+                m.invoke(resource, (Object[]) null);
             } catch (IllegalArgumentException e) {
                 log.warn(sm.getString("namingResources.cleanupCloseFailed",
-                        name, container), e);
+                        closeMethod, name, container), e);
             } catch (IllegalAccessException e) {
                 log.warn(sm.getString("namingResources.cleanupCloseFailed",
-                        name, container), e);
+                        closeMethod, name, container), e);
             } catch (InvocationTargetException e) {
                 log.warn(sm.getString("namingResources.cleanupCloseFailed",
-                        name, container), e);
+                        closeMethod, name, container), e);
             }
         }
     }
index 3c73025..6f702d2 100644 (file)
         close on a JNDI resource while it was still available to the
         application. (markt)
       </fix>
+      <add>
+        Provide a configuration option that lets the close method to be used for
+        a JNDI Resource to be defined by the user. (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">
index 5996763..678c172 100644 (file)
         application uses a <code>&lt;resource-env-ref&gt;</code> instead.</p>
       </attribute>
 
+      <attribute name="closeMethod" required="false">
+        <p>Name of zero-argument method to call on the resource when it is no
+        longer required to spped up clean-up of resources that would otherwise
+        happen as part of garbage collection. If not specificed, the default
+        value of <code>close</code> is used.</p>
+      </attribute>
+
       <attribute name="description" required="false">
         <p>Optional, human-readable description of this resource.</p>
       </attribute>