Author: markt
Date: Mon Aug 10 10:28:16 2009
New Revision: 802727

URL: http://svn.apache.org/viewvc?rev=802727&view=rev
Log:
Expose filters via JMX. Filter config is now available via JMX in read-only 
form.
Based on a patch provided by Xie Xiaodong as part of GSOC2009.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java
    tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties
    tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml

Modified: 
tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java?rev=802727&r1=802726&r2=802727&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationFilterConfig.java Mon 
Aug 10 10:28:16 2009
@@ -22,10 +22,12 @@
 import java.io.Serializable;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.management.ObjectName;
 import javax.naming.NamingException;
 import javax.servlet.Filter;
 import javax.servlet.FilterConfig;
@@ -37,9 +39,11 @@
 import org.apache.catalina.deploy.FilterDef;
 import org.apache.catalina.security.SecurityUtil;
 import org.apache.catalina.util.Enumerator;
+import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.tomcat.InstanceManager;
 import org.apache.tomcat.util.log.SystemLogHandler;
+import org.apache.tomcat.util.modeler.Registry;
 
 
 /**
@@ -51,12 +55,15 @@
  * @version $Revision$ $Date$
  */
 
-final class ApplicationFilterConfig implements FilterConfig, Serializable {
+public final class ApplicationFilterConfig implements FilterConfig, 
Serializable {
 
 
     protected static StringManager sm =
         StringManager.getManager(Constants.Package);
 
+    private static org.apache.juli.logging.Log log =
+        LogFactory.getLog(ApplicationFilterConfig.class);
+
     // ----------------------------------------------------------- Constructors
 
 
@@ -79,7 +86,7 @@
      * @throws NamingException
      * @throws InvocationTargetException
      */
-    public ApplicationFilterConfig(Context context, FilterDef filterDef)
+    ApplicationFilterConfig(Context context, FilterDef filterDef)
         throws ClassCastException, ClassNotFoundException,
                IllegalAccessException, InstantiationException,
                ServletException, InvocationTargetException, NamingException {
@@ -117,6 +124,10 @@
      */
     private transient InstanceManager instanceManager;
 
+    /**
+     * JMX registration name
+     */
+    private ObjectName oname;
 
     // --------------------------------------------------- FilterConfig Methods
 
@@ -125,11 +136,15 @@
      * Return the name of the filter we are configuring.
      */
     public String getFilterName() {
-
         return (filterDef.getFilterName());
-
     }
 
+    /**
+     * Return the class of the filter we are configuring.
+     */
+    public String getFilterClass() {
+        return filterDef.getFilterClass();
+    }
 
     /**
      * Return a <code>String</code> containing the value of the named
@@ -154,8 +169,8 @@
      * parameters for this Filter.
      */
     public Enumeration<String> getInitParameterNames() {
-
         Map<String,String> map = filterDef.getParameterMap();
+        
         if (map == null)
             return (new Enumerator<String>(new ArrayList<String>()));
         else
@@ -189,6 +204,11 @@
 
     }
 
+    // --------------------------------------------------------- Public Methods
+
+    public Map<String, String> getFilterInitParameterMap() {
+        return Collections.unmodifiableMap(filterDef.getParameterMap());
+    }
 
     // -------------------------------------------------------- Package Methods
 
@@ -233,9 +253,12 @@
         } else {
             filter.init(this);
         }
+        
+        // Expose filter via JMX
+        registerJMX();
+        
         return (this.filter);
 
-
     }
 
 
@@ -254,6 +277,8 @@
      */
     void release() {
 
+        unregsiterJMX();
+        
         if (this.filter != null)
         {
             if (Globals.IS_SECURITY_ENABLED) {
@@ -352,4 +377,54 @@
         return instanceManager;
     }
 
+    private void registerJMX() {
+        String parentName = context.getName();
+        parentName = ("".equals(parentName)) ? "/" : parentName;
+
+        String hostName = context.getParent().getName();
+        hostName = (hostName == null) ? "DEFAULT" : hostName;
+
+        // domain == engine name
+        String domain = context.getParent().getParent().getName();
+
+        String webMod = "//" + hostName + parentName;
+        String onameStr = null;
+        if (context instanceof StandardContext) {
+            StandardContext standardContext = (StandardContext) context;
+            onameStr = domain + ":j2eeType=Filter,name=" +
+                 filterDef.getFilterName() + ",WebModule=" + webMod +
+                 ",J2EEApplication=" +
+                 standardContext.getJ2EEApplication() + ",J2EEServer=" +
+                 standardContext.getJ2EEServer();
+        } else {
+            onameStr = domain + ":j2eeType=Filter,name=" +
+                 filterDef.getFilterName() + ",WebModule=" + webMod;
+        }
+        try {
+            oname = new ObjectName(onameStr);
+            Registry.getRegistry(null, null).registerComponent(this, oname,
+                    null);
+        } catch (Exception ex) {
+            log.info(sm.getString("applicationFilterConfig.jmxRegsiterFail",
+                    getFilterClass(), getFilterName()), ex);
+        }
+    }
+    
+    private void unregsiterJMX() {
+        // unregister this component
+        if (oname != null) {
+            try {
+                Registry.getRegistry(null, null).unregisterComponent(oname);
+                if(log.isDebugEnabled())
+                    log.debug(sm.getString(
+                            "applicationFilterConfig.jmxUnregsiter",
+                            getFilterClass(), getFilterName()));
+            } catch(Exception ex) {
+                log.error(sm.getString(
+                        "applicationFilterConfig.jmxUnregsiterFail",
+                        getFilterClass(), getFilterName()), ex);
+            }
+        }
+
+    }
 }

Modified: tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties?rev=802727&r1=802726&r2=802727&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/LocalStrings.properties Mon Aug 
10 10:28:16 2009
@@ -33,6 +33,9 @@
 applicationDispatcher.serviceException=Servlet.service() for servlet {0} threw 
exception
 applicationDispatcher.specViolation.request=Original SevletRequest or wrapped 
original ServletRequest not passed to RequestDispatcher in violation of SRV.8.2 
and SRV.14.2.5.1
 applicationDispatcher.specViolation.response=Original SevletResponse or 
wrapped original ServletResponse not passed to RequestDispatcher in violation 
of SRV.8.2 and SRV.14.2.5.1
+applicationFilterConfig.jmxRegisterFail=JMX registration failed for filter of 
type [{0}] and name [{1}]
+applicationFilterConfig.jmxUnregister=JMX de-registration complete for filter 
of type [{0}] and name [{1}]
+applicationFilterConfig.jmxUnregisterFail=JMX de-registration failed for 
filter of type [{0}] and name [{1}]
 applicationRequest.badParent=Cannot locate parent Request implementation
 applicationRequest.badRequest=Request is not a 
javax.servlet.ServletRequestWrapper
 applicationResponse.badParent=Cannot locate parent Response implementation

Modified: tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml?rev=802727&r1=802726&r2=802727&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml (original)
+++ tomcat/trunk/java/org/apache/catalina/core/mbeans-descriptors.xml Mon Aug 
10 10:28:16 2009
@@ -17,6 +17,29 @@
 -->
 <mbeans-descriptors>
 
+  <mbean name="ApplicationFilterConfig"
+         description="Wrapper that represents an individual servlet-filter 
definition"
+         domain="Catalina"
+         group="Filter"
+         type="org.apache.catalina.core.ApplicationFilterConfig">
+                
+     <attribute name="filterName"
+                description="The name used to reference the filter in web.xml"
+                type="java.lang.String"
+                writeable="false"/>    
+
+     <attribute name="filterClass"
+                description="Fully qualified class name of the filter object"
+                type="java.lang.String"
+                writeable="false"/>                               
+
+     <attribute name="filterInitParameterMap"
+                description="Return the initiaization parameters associated 
with this filter"
+                type="java.util.Map"
+                writeable="false" />
+                    
+  </mbean> 
+
   <mbean name="NamingContextListener"
          description="Helper class used to initialize and populate the JNDI 
context associated with each context and server"
          domain="Catalina"



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to