tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r941064 - in /tomcat/trunk/java/org/apache/catalina: mbeans/MBeanUtils.java realm/RealmBase.java valves/ValveBase.java
Date Tue, 04 May 2010 21:51:37 GMT
Author: markt
Date: Tue May  4 21:51:37 2010
New Revision: 941064

URL: http://svn.apache.org/viewvc?rev=941064&view=rev
Log:
Re-factor Realms to use LifecycleMBeanBase

Modified:
    tomcat/trunk/java/org/apache/catalina/mbeans/MBeanUtils.java
    tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java
    tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java

Modified: tomcat/trunk/java/org/apache/catalina/mbeans/MBeanUtils.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/mbeans/MBeanUtils.java?rev=941064&r1=941063&r2=941064&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/mbeans/MBeanUtils.java (original)
+++ tomcat/trunk/java/org/apache/catalina/mbeans/MBeanUtils.java Tue May  4 21:51:37 2010
@@ -42,6 +42,7 @@ import org.apache.catalina.Service;
 import org.apache.catalina.User;
 import org.apache.catalina.UserDatabase;
 import org.apache.catalina.Valve;
+import org.apache.catalina.Wrapper;
 import org.apache.catalina.connector.Connector;
 import org.apache.catalina.deploy.ContextEnvironment;
 import org.apache.catalina.deploy.ContextResource;
@@ -1654,7 +1655,7 @@ public class MBeanUtils {
      * Determine the name of the domain to register MBeans for from a given
      * Container.
      * 
-     * @param container 
+     * @param container
      * @return
      */
     public static String getDomain(Container container) {
@@ -1674,4 +1675,46 @@ public class MBeanUtils {
         return domain;
     }
 
+    
+    /**
+     * Calculate the key properties string to be added to an object's
+     * {@link ObjectName} to indicate that it is associated with that container.
+     * 
+     * @param container The container the object is associated with 
+     * @return          A string suitable for appending to the ObjectName
+     */
+    public static String getContainerKeyProperties(Container container) {
+        
+        Container c = container;
+        StringBuilder keyProperties = new StringBuilder();
+        int unknown = 0;
+        
+        // Work up container hierarchy, add a component to the name for
+        // each container
+        while (!(c instanceof Engine)) {
+            if (c instanceof Wrapper) {
+                keyProperties.append(",servlet=");
+                keyProperties.append(c.getName());
+            } else if (c instanceof Context) {
+                String path = ((Context)c).getPath();
+                if (path.length() < 1) {
+                    path = "/";
+                }
+                keyProperties.append(",path=");
+                keyProperties.append(path);
+            } else if (c instanceof Host) {
+                keyProperties.append(",host=");
+                keyProperties.append(c.getName());
+            } else {
+                // Should never happen...
+                keyProperties.append(",unknown");
+                keyProperties.append(unknown++);
+                keyProperties.append('=');
+                keyProperties.append(c.getName());
+            }
+            c = c.getParent();
+        }
+
+        return keyProperties.toString();
+    }
 }

Modified: tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java?rev=941064&r1=941063&r2=941064&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java Tue May  4 21:51:37 2010
@@ -29,10 +29,6 @@ import java.security.Principal;
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 
-import javax.management.Attribute;
-import javax.management.MBeanRegistration;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.catalina.Container;
@@ -47,17 +43,17 @@ import org.apache.catalina.Server;
 import org.apache.catalina.Service;
 import org.apache.catalina.connector.Request;
 import org.apache.catalina.connector.Response;
-import org.apache.catalina.core.ContainerBase;
 import org.apache.catalina.deploy.LoginConfig;
 import org.apache.catalina.deploy.SecurityConstraint;
 import org.apache.catalina.deploy.SecurityCollection;
+import org.apache.catalina.mbeans.MBeanUtils;
 import org.apache.catalina.util.HexUtils;
 import org.apache.catalina.util.LifecycleBase;
+import org.apache.catalina.util.LifecycleMBeanBase;
 import org.apache.catalina.util.MD5Encoder;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
-import org.apache.tomcat.util.modeler.Registry;
 
 /**
  * Simple implementation of <b>Realm</b> that reads an XML file to configure
@@ -68,8 +64,7 @@ import org.apache.tomcat.util.modeler.Re
  * @version $Id$
  */
 
-public abstract class RealmBase extends LifecycleBase
-    implements Realm, MBeanRegistration {
+public abstract class RealmBase extends LifecycleMBeanBase implements Realm {
 
     private static final Log log = LogFactory.getLog(RealmBase.class);
 
@@ -957,6 +952,17 @@ public abstract class RealmBase extends 
     }
 
 
+    @Override
+    protected void initInternal() throws LifecycleException {
+
+        super.initInternal();
+
+        // We want logger as soon as possible
+        if (container != null) {
+            this.containerLog = container.getLogger();
+        }
+    }
+        
     /**
      * Prepare for the beginning of active use of the public methods of this
      * component and implement the requirements of
@@ -1012,22 +1018,6 @@ public abstract class RealmBase extends 
     }
     
     
-    @Override
-    protected void destroyInternal() {
-    
-        // unregister this realm
-        if ( oname!=null ) {   
-            try {   
-                Registry.getRegistry(null, null).unregisterComponent(oname); 
-                if(log.isDebugEnabled())
-                    log.debug( "unregistering realm " + oname );   
-            } catch( Exception ex ) {   
-                log.error( "Can't unregister realm " + oname, ex);   
-            }      
-        }
-          
-    }
-
     // ------------------------------------------------------ Protected Methods
 
 
@@ -1238,34 +1228,23 @@ public abstract class RealmBase extends 
 
 
     // -------------------- JMX and Registration  --------------------
-    protected String type;
-    protected String domain;
-    protected String host;
-    protected String path;
-    protected String realmPath = "/realm0";
-    protected ObjectName oname;
-    protected ObjectName controller;
-    protected MBeanServer mserver;
 
-    public ObjectName getController() {
-        return controller;
-    }
-
-    public void setController(ObjectName controller) {
-        this.controller = controller;
-    }
+    @Override
+    public String getObjectNameKeyProperties() {
+        
+        StringBuilder keyProperties = new StringBuilder("type=Realm");
+        keyProperties.append(getRealmSuffix());
+        keyProperties.append(MBeanUtils.getContainerKeyProperties(container));
 
-    public ObjectName getObjectName() {
-        return oname;
+        return keyProperties.toString();
     }
 
-    public String getDomain() {
-        return domain;
+    @Override
+    public String getDomainInternal() {
+        return MBeanUtils.getDomain(container);
     }
 
-    public String getType() {
-        return type;
-    }
+    protected String realmPath = "/realm0";
 
     public String getRealmPath() {
         return realmPath;
@@ -1275,80 +1254,6 @@ public abstract class RealmBase extends 
         realmPath = theRealmPath;
     }
 
-    public ObjectName preRegister(MBeanServer server,
-                                  ObjectName name) throws Exception {
-        oname=name;
-        mserver=server;
-        domain=name.getDomain();
-
-        type=name.getKeyProperty("type");
-        host=name.getKeyProperty("host");
-        path=name.getKeyProperty("path");
-
-        return name;
-    }
-
-    public void postRegister(Boolean registrationDone) {
-        // NOOP in base class
-    }
-
-    public void preDeregister() throws Exception {
-        // NOOP in base class
-    }
-
-    public void postDeregister() {
-        // NOOP in base class
-    }
-
-    @Override
-    protected void initInternal() {
-
-        // We want logger as soon as possible
-        if (container != null) {
-            this.containerLog = container.getLogger();
-        }
-        
-        if( container== null ) {
-            ObjectName parent=null;
-            // Register with the parent
-            try {
-                if( host == null ) {
-                    // global
-                    parent=new ObjectName(domain +":type=Engine");
-                } else if( path==null ) {
-                    parent=new ObjectName(domain +
-                            ":type=Host,host=" + host);
-                } else {
-                    parent=new ObjectName(domain +":j2eeType=WebModule,name=//" +
-                            host + path);
-                }
-                if( mserver.isRegistered(parent ))  {
-                    if(log.isDebugEnabled())
-                        log.debug("Register with " + parent);
-                    mserver.setAttribute(parent, new Attribute("realm", this));
-                }
-            } catch (Exception e) {
-                log.error("Parent not available yet: " + parent);  
-            }
-        }
-        
-        if( oname==null ) {
-            // register
-            try {
-                ContainerBase cb=(ContainerBase)container;
-                oname=new ObjectName(cb.getDomain()+":type=Realm" +
-                        getRealmSuffix() + cb.getContainerSuffix());
-                Registry.getRegistry(null, null).registerComponent(this, oname, null );
-                if(log.isDebugEnabled())
-                    log.debug("Register Realm "+oname);
-            } catch (Throwable e) {
-                log.error( "Can't register " + oname, e);
-            }
-        }
-
-    }
-
-
     protected String getRealmSuffix() {
         return ",realmPath=" + getRealmPath();
     }

Modified: tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java?rev=941064&r1=941063&r2=941064&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java (original)
+++ tomcat/trunk/java/org/apache/catalina/valves/ValveBase.java Tue May  4 21:51:37 2010
@@ -25,13 +25,9 @@ import javax.servlet.ServletException;
 
 import org.apache.catalina.Contained;
 import org.apache.catalina.Container;
-import org.apache.catalina.Context;
-import org.apache.catalina.Engine;
-import org.apache.catalina.Host;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.LifecycleState;
 import org.apache.catalina.Valve;
-import org.apache.catalina.Wrapper;
 import org.apache.catalina.comet.CometEvent;
 import org.apache.catalina.connector.Request;
 import org.apache.catalina.connector.Response;
@@ -273,34 +269,9 @@ public abstract class ValveBase extends 
         StringBuilder name = new StringBuilder("type=Valve");
         
         Container container = getContainer();
-        int unknown = 0;
-
-        // Work up container hierarchy, add a component to the name for
-        // each container
-        while (!(container instanceof Engine)) {
-            if (container instanceof Wrapper) {
-                name.append(",servlet=");
-                name.append(container.getName());
-            } else if (container instanceof Context) {
-                String path = ((Context)container).getPath();
-                if (path.length() < 1) {
-                    path = "/";
-                }
-                name.append(",path=");
-                name.append(path);
-            } else if (container instanceof Host) {
-                name.append(",host=");
-                name.append(container.getName());
-            } else {
-                // Should never happen...
-                name.append(",unknown");
-                name.append(unknown++);
-                name.append('=');
-                name.append(container.getName());
-            }
-            container = container.getParent();
-        }
 
+        name.append(MBeanUtils.getContainerKeyProperties(container));
+        
         int seq = 0;
         for (Valve valve : container.getPipeline().getValves()) {
             // Skip null valves



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


Mime
View raw message