You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by cr...@apache.org on 2002/01/23 02:23:57 UTC

cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/users MemoryUserDatabase.java

craigmcc    02/01/22 17:23:57

  Modified:    catalina/src/conf server.xml
               catalina/src/share/org/apache/catalina/mbeans
                        MemoryUserDatabaseMBean.java
               catalina/src/share/org/apache/catalina/users
                        MemoryUserDatabase.java
  Added:       catalina/src/share/org/apache/catalina/mbeans
                        GlobalResourcesLifecycleListener.java
  Log:
  At startup time, create the MBeans for the user database, users, and groups.
  This is now ready for building the admin screens.
  
  Revision  Changes    Path
  1.51      +8 -6      jakarta-tomcat-4.0/catalina/src/conf/server.xml
  
  Index: server.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/conf/server.xml,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- server.xml	20 Jan 2002 03:16:35 -0000	1.50
  +++ server.xml	23 Jan 2002 01:23:57 -0000	1.51
  @@ -13,10 +13,12 @@
   <Server port="8005" shutdown="SHUTDOWN" debug="0">
   
   
  -  <!-- Uncomment this entry to enable JMX MBeans support -->
  +  <!-- Uncomment these entries to enable JMX MBeans support -->
   <!--
     <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"
                   debug="0"/>
  +  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
  +                debug="0"/>
   -->
   
     <!-- Global JNDI resources -->
  @@ -27,11 +29,11 @@
   
       <!-- Editable user database that can also be used by
            UserDatabaseRealm to authenticate users -->
  -    <Resource name="catalina/UserDatabase" auth="Container"
  +    <Resource name="UserDatabase" auth="Container"
                 type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved">
       </Resource>
  -    <ResourceParams name="catalina/UserDatabase">
  +    <ResourceParams name="UserDatabase">
         <parameter>
           <name>factory</name>
           <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
  @@ -160,11 +162,11 @@
         <!-- Because this Realm is here, an instance will be shared globally -->
   
         <!-- This Realm uses the UserDatabase configured in the global JNDI
  -           resources under the key "catalina/UserDatabase".  Any edits
  +           resources under the key "UserDatabase".  Any edits
              that are performed against this UserDatabase are immediately
              available for use by the Realm.  -->
         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  -                 debug="0" resourceName="catalina/UserDatabase"/>
  +                 debug="0" resourceName="UserDatabase"/>
   
         <!-- Comment out the old realm but leave here for now in case we
              need to go back quickly -->
  @@ -268,7 +270,7 @@
                       (UserDatabase) envCtx.lookup("userDatabase");
             -->
   <!--
  -          <ResourceLink name="userDatabase" global="catalina/UserDatabase"
  +          <ResourceLink name="userDatabase" global="UserDatabase"
                           type="org.apache.catalina.UserDatabase"/>
   -->
   
  
  
  
  1.2       +5 -3      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/MemoryUserDatabaseMBean.java
  
  Index: MemoryUserDatabaseMBean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/MemoryUserDatabaseMBean.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MemoryUserDatabaseMBean.java	22 Jan 2002 23:22:24 -0000	1.1
  +++ MemoryUserDatabaseMBean.java	23 Jan 2002 01:23:57 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/MemoryUserDatabaseMBean.java,v 1.1 2002/01/22 23:22:24 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2002/01/22 23:22:24 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/MemoryUserDatabaseMBean.java,v 1.2 2002/01/23 01:23:57 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/01/23 01:23:57 $
    *
    * ====================================================================
    *
  @@ -84,7 +84,7 @@
    * <code>org.apache.catalina.users.MemoryUserDatabase</code> component.</p>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2002/01/22 23:22:24 $
  + * @version $Revision: 1.2 $ $Date: 2002/01/23 01:23:57 $
    */
   
   public class MemoryUserDatabaseMBean extends BaseModelMBean {
  @@ -307,6 +307,7 @@
           }
           try {
               MBeanUtils.destroyMBean(group);
  +            database.removeGroup(group);
           } catch (Exception e) {
               throw new IllegalArgumentException("Exception destroying group " +
                                                  group + " MBean: " + e);
  @@ -329,6 +330,7 @@
           }
           try {
               MBeanUtils.destroyMBean(user);
  +            database.removeUser(user);
           } catch (Exception e) {
               throw new IllegalArgumentException("Exception destroying user " +
                                                  user + " MBean: " + e);
  
  
  
  1.1                  jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/GlobalResourcesLifecycleListener.java
  
  Index: GlobalResourcesLifecycleListener.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/mbeans/GlobalResourcesLifecycleListener.java,v 1.1 2002/01/23 01:23:57 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/01/23 01:23:57 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.catalina.mbeans;
  
  
  import java.util.Iterator;
  import javax.management.MBeanException;
  import javax.management.MBeanServer;
  import javax.management.ObjectName;
  import javax.naming.Binding;
  import javax.naming.Context;
  import javax.naming.NamingEnumeration;
  import javax.naming.NamingException;
  import org.apache.catalina.Group;
  import org.apache.catalina.Lifecycle;
  import org.apache.catalina.LifecycleEvent;
  import org.apache.catalina.LifecycleListener;
  import org.apache.catalina.Server;
  import org.apache.catalina.User;
  import org.apache.catalina.UserDatabase;
  import org.apache.catalina.core.StandardServer;
  import org.apache.commons.modeler.ManagedBean;
  import org.apache.commons.modeler.Registry;
  
  
  /**
   * Implementation of <code>LifecycleListener</code> that instantiates the
   * set of MBeans associated with global JNDI resources that are subject to
   * management.
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/01/23 01:23:57 $
   * @since 4.1
   */
  
  public class GlobalResourcesLifecycleListener
      implements LifecycleListener {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * The owning Catalina component that we are attached to.
       */
      protected Lifecycle component = null;
  
  
      /**
       * The configuration information registry for our managed beans.
       */
      protected static Registry registry = MBeanUtils.createRegistry();
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * The debugging detail level for this component.
       */
      protected int debug = 0;
  
      public int getDebug() {
          return (this.debug);
      }
  
      public void setDebug(int debug) {
          this.debug = debug;
      }
  
  
      // ---------------------------------------------- LifecycleListener Methods
  
  
      /**
       * Primary entry point for startup and shutdown events.
       *
       * @param event The event that has occurred
       */
      public void lifecycleEvent(LifecycleEvent event) {
  
          if (Lifecycle.START_EVENT.equals(event.getType())) {
              component = event.getLifecycle();
              createMBeans();
          } else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
              destroyMBeans();
              component = null;
          }
  
      }
  
  
      // ------------------------------------------------------ Protected Methods
  
  
      /**
       * Create the MBeans for the interesting global JNDI resources.
       */
      protected void createMBeans() {
  
          // Look up our global naming context
          // FIXME - getGlobalNamingContext should be on Server
          if (!(component instanceof StandardServer)) {
              throw new IllegalArgumentException
                  ("Cannot create global resource MBeans for server " +
                   component);
          }
          StandardServer server = (StandardServer) component;
          Context context = server.getGlobalNamingContext();
          if (context == null) {
              throw new IllegalStateException
                  ("No global naming context defined for server " + server);
          }
  
          // Recurse through the defined global JNDI resources context
          try {
              createMBeans("", context);
          } catch (NamingException e) {
              log("Exception processing Global JNDI Resources", e);
          }
  
      }
  
  
      /**
       * Create the MBeans for the interesting global JNDI resources in
       * the specified naming context.
       *
       * @param prefix Prefix for complete object name paths
       * @param context Context to be scanned
       *
       * @exception NamingException if a JNDI exception occurs
       */
      protected void createMBeans(String prefix, Context context)
          throws NamingException {
  
          if (debug >= 1) {
              log("Creating MBeans for Global JNDI Resources in Context '" +
                  prefix + "'");
          }
  
          NamingEnumeration bindings = context.listBindings("");
          while (bindings.hasMore()) {
              Binding binding = (Binding) bindings.next();
              String name = prefix + binding.getName();
              Object value = context.lookup(binding.getName());
              if (debug >= 2) {
                  log("Checking resource " + name);
              }
              if (value instanceof Context) {
                  createMBeans(name + "/", (Context) value);
              } else if (value instanceof UserDatabase) {
                  try {
                      createMBeans(name, (UserDatabase) value);
                  } catch (Exception e) {
                      log("Exception creating UserDatabase MBeans for " + name,
                          e);
                  }
              }
          }
  
      }
  
  
      /**
       * Create the MBeans for the specified UserDatabase and its contents.
       *
       * @param name Complete resource name of this UserDatabase
       * @param database The UserDatabase to be processed
       *
       * @exception Exception if an exception occurs while creating MBeans
       */
      protected void createMBeans(String name, UserDatabase database)
          throws Exception {
  
          // Create the MBean for the UserDatabase itself
          if (debug >= 2) {
              log("Creating UserDatabase MBeans for resource " + name);
              log("Database=" + database);
          }
          if (MBeanUtils.createMBean(database) == null) {
              throw new IllegalArgumentException
                  ("Cannot create UserDatabase MBean for resource " + name);
          }
  
          // Create the MBeans for each defined Group
          Iterator groups = database.getGroups();
          while (groups.hasNext()) {
              Group group = (Group) groups.next();
              if (debug >= 3) {
                  log("  Creating Group MBean for group " + group);
              }
              if (MBeanUtils.createMBean(group) == null) {
                  throw new IllegalArgumentException
                      ("Cannot create Group MBean for group " + group);
              }
          }
  
          // Create the MBeans for each defined User
          Iterator users = database.getUsers();
          while (users.hasNext()) {
              User user = (User) users.next();
              if (debug >= 3) {
                  log("  Creating User MBean for user " + user);
              }
              if (MBeanUtils.createMBean(user) == null) {
                  throw new IllegalArgumentException
                      ("Cannot create User MBean for user " + user);
              }
          }
  
      }
  
  
      /**
       * Destroy the MBeans for the interesting global JNDI resources.
       */
      protected void destroyMBeans() {
  
          if (debug >= 1) {
              log("Destroying MBeans for Global JNDI Resources");
          }
  
      }
  
  
      /**
       * Log a message.
       *
       * @param message The message to be logged
       */
      protected void log(String message) {
  
          System.out.print("GlobalResourcesLifecycleListener: ");
          System.out.println(message);
  
      }
  
  
      /**
       * Log a message and associated exception.
       *
       * @param message The message to be logged
       * @param throwable The exception to be logged
       */
      protected void log(String message, Throwable throwable) {
  
          log(message);
          throwable.printStackTrace(System.out);
  
      }
  
  
  }
  
  
  
  1.4       +23 -4     jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/users/MemoryUserDatabase.java
  
  Index: MemoryUserDatabase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/users/MemoryUserDatabase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MemoryUserDatabase.java	22 Jan 2002 23:41:38 -0000	1.3
  +++ MemoryUserDatabase.java	23 Jan 2002 01:23:57 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/users/MemoryUserDatabase.java,v 1.3 2002/01/22 23:41:38 craigmcc Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/01/22 23:41:38 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/users/MemoryUserDatabase.java,v 1.4 2002/01/23 01:23:57 craigmcc Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/01/23 01:23:57 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -87,7 +87,7 @@
    * a specified XML file for its persistent storage.</p>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.3 $ $Date: 2002/01/22 23:41:38 $
  + * @version $Revision: 1.4 $ $Date: 2002/01/23 01:23:57 $
    * @since 4.1
    */
   
  @@ -494,6 +494,25 @@
                                 fileOrig.getAbsolutePath()));
           }
           fileOld.delete();
  +
  +    }
  +
  +
  +    /**
  +     * Return a String representation of this UserDatabase.
  +     */
  +    public String toString() {
  +
  +        StringBuffer sb = new StringBuffer("MemoryUserDatabase[id=");
  +        sb.append(this.id);
  +        sb.append(",pathname=");
  +        sb.append(pathname);
  +        sb.append(",groupCount=");
  +        sb.append(this.groups.size());
  +        sb.append(",userCount=");
  +        sb.append(this.users.size());
  +        sb.append("]");
  +        return (sb.toString());
   
       }
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>