remm        2004/10/08 02:34:02

  Modified:    catalina/src/share/org/apache/catalina/core
                        NamingContextListener.java LocalStrings.properties
  Log:
  - Register datasource with JMX.
  - With DBCP, this is enough to provide JMX management and monitoring. It might work 
well with many other data sources which might not
    register themselves in JMX and expose their stuff in a java bean fashion. Note: I 
didn't test it a lot, let me know how well it works.
  
  Revision  Changes    Path
  1.10      +77 -1     
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/NamingContextListener.java
  
  Index: NamingContextListener.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/NamingContextListener.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- NamingContextListener.java        26 Jul 2004 16:04:01 -0000      1.9
  +++ NamingContextListener.java        8 Oct 2004 09:34:02 -0000       1.10
  @@ -20,10 +20,13 @@
   
   import java.beans.PropertyChangeEvent;
   import java.beans.PropertyChangeListener;
  +import java.util.HashMap;
   import java.util.Hashtable;
   import java.util.Iterator;
   import java.util.StringTokenizer;
   
  +import javax.management.MalformedObjectNameException;
  +import javax.management.ObjectName;
   import javax.naming.NameAlreadyBoundException;
   import javax.naming.NamingException;
   import javax.naming.Reference;
  @@ -33,10 +36,13 @@
   import org.apache.catalina.ContainerEvent;
   import org.apache.catalina.ContainerListener;
   import org.apache.catalina.Context;
  +import org.apache.catalina.Engine;
  +import org.apache.catalina.Host;
   import org.apache.catalina.Lifecycle;
   import org.apache.catalina.LifecycleEvent;
   import org.apache.catalina.LifecycleListener;
   import org.apache.catalina.Server;
  +import org.apache.catalina.Service;
   import org.apache.catalina.deploy.ContextEjb;
   import org.apache.catalina.deploy.ContextEnvironment;
   import org.apache.catalina.deploy.ContextLocalEjb;
  @@ -48,6 +54,7 @@
   import org.apache.catalina.util.StringManager;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  +import org.apache.commons.modeler.Registry;
   import org.apache.naming.ContextAccessController;
   import org.apache.naming.ContextBindings;
   import org.apache.naming.EjbRef;
  @@ -119,6 +126,12 @@
        */
       protected javax.naming.Context envCtx = null;
   
  +    
  +    /**
  +     * Objectnames hashtable.
  +     */
  +    protected HashMap objectNames = new HashMap();
  +    
   
       /**
        * The string manager for this package.
  @@ -631,6 +644,53 @@
   
   
       /**
  +     * Create an <code>ObjectName</code> for this
  +     * <code>ContextResource</code> object.
  +     *
  +     * @param domain Domain in which this name is to be created
  +     * @param resource The ContextResource to be named
  +     *
  +     * @exception MalformedObjectNameException if a name cannot be created
  +     */
  +    protected ObjectName createObjectName(ContextResource resource)
  +        throws MalformedObjectNameException {
  +
  +        String domain = null;
  +        if (container instanceof StandardServer) {
  +            domain = ((StandardServer) container).getDomain();
  +        } else if (container instanceof ContainerBase) {
  +            domain = ((ContainerBase) container).getDomain();
  +        }
  +        if (domain == null) {
  +            domain = "Catalina";
  +        }
  +        
  +        ObjectName name = null;
  +        String quotedResourceName = ObjectName.quote(resource.getName());
  +        if (container instanceof Server) {        
  +            name = new ObjectName(domain + ":type=DataSource" +
  +                        ",class=" + resource.getType() + 
  +                        ",name=" + quotedResourceName);
  +        } else if (container instanceof Context) {                    
  +            String path = ((Context)container).getPath();
  +            if (path.length() < 1)
  +                path = "/";
  +            Host host = (Host) ((Context)container).getParent();
  +            Engine engine = (Engine) host.getParent();
  +            Service service = engine.getService();
  +            name = new ObjectName(domain + ":type=DataSource" +
  +                        ",path=" + path + 
  +                        ",host=" + host.getName() +
  +                        ",class=" + resource.getType() +
  +                        ",name=" + quotedResourceName);
  +        }
  +        
  +        return (name);
  +
  +    }
  +
  +    
  +    /**
        * Set the specified EJBs in the naming context.
        */
       public void addEjb(ContextEjb ejb) {
  @@ -778,6 +838,17 @@
               logger.error(sm.getString("naming.bindFailed", e));
           }
   
  +        if ("javax.sql.DataSource".equals(ref.getClassName())) {
  +            try {
  +                ObjectName on = createObjectName(resource);
  +                Object actualResource = envCtx.lookup(resource.getName());
  +                Registry.getRegistry(null, null).registerComponent(actualResource, 
on, null);
  +                objectNames.put(resource.getName(), on);
  +            } catch (Exception e) {
  +                logger.warn(sm.getString("naming.jmxRegistrationFailed", e));
  +            }
  +        }
  +        
       }
   
   
  @@ -882,6 +953,11 @@
               envCtx.unbind(name);
           } catch (NamingException e) {
               logger.error(sm.getString("naming.unbindFailed", e));
  +        }
  +
  +        ObjectName on = (ObjectName) objectNames.get(name);
  +        if (on != null) {
  +            Registry.getRegistry(null, null).unregisterComponent(on);
           }
   
       }
  
  
  
  1.15      +1 -0      
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/LocalStrings.properties,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- LocalStrings.properties   5 Oct 2004 17:12:51 -0000       1.14
  +++ LocalStrings.properties   8 Oct 2004 09:34:02 -0000       1.15
  @@ -32,6 +32,7 @@
   interceptorValve.alreadyStarted=InterceptorValve has already been started
   interceptorValve.notStarted=InterceptorValve has not yet been started
   naming.bindFailed=Failed to bind object: {0}
  +naming.jmxRegistrationFailed=Failed to register in JMX: {0}
   naming.unbindFailed=Failed to unbind object: {0}
   naming.invalidEnvEntryType=Environment entry {0} has an invalid type
   naming.invalidEnvEntryValue=Environment entry {0} has an invalid value
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to