You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2010/03/04 23:16:50 UTC

svn commit: r919208 - in /tomcat/trunk/java/org/apache/catalina/realm: CombinedRealm.java DataSourceRealm.java JAASRealm.java JDBCRealm.java JNDIRealm.java LockOutRealm.java MemoryRealm.java RealmBase.java UserDatabaseRealm.java

Author: markt
Date: Thu Mar  4 22:16:50 2010
New Revision: 919208

URL: http://svn.apache.org/viewvc?rev=919208&view=rev
Log:
Lifecycle refactoring
Realm: Note most implementations were firing the start event too early

Modified:
    tomcat/trunk/java/org/apache/catalina/realm/CombinedRealm.java
    tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java
    tomcat/trunk/java/org/apache/catalina/realm/JAASRealm.java
    tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java
    tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java
    tomcat/trunk/java/org/apache/catalina/realm/LockOutRealm.java
    tomcat/trunk/java/org/apache/catalina/realm/MemoryRealm.java
    tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java
    tomcat/trunk/java/org/apache/catalina/realm/UserDatabaseRealm.java

Modified: tomcat/trunk/java/org/apache/catalina/realm/CombinedRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/CombinedRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/CombinedRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/CombinedRealm.java Thu Mar  4 22:16:50 2010
@@ -30,6 +30,7 @@
 import org.apache.catalina.Lifecycle;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.Realm;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
@@ -218,15 +219,14 @@
 
     /**
      * Prepare for the beginning of active use of the public methods of this
-     * component.  This method should be called before any of the public
-     * methods of this component are utilized.  It should also send a
-     * LifecycleEvent of type START_EVENT to any registered listeners.
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that prevents this component from being used
      */
     @Override
-    public void start() throws LifecycleException {
+    protected void startInternal() throws LifecycleException {
         // Start 'sub-realms' then this one
     	Iterator<Realm> iter = realms.iterator();
     	
@@ -243,23 +243,22 @@
             	}
             }
         }
-        super.start();
+        super.startInternal();
     }
 
 
     /**
      * Gracefully terminate the active use of the public methods of this
-     * component.  This method should be the last one called on a given
-     * instance of this component.  It should also send a LifecycleEvent
-     * of type STOP_EVENT to any registered listeners.
+     * component and implement the requirements of
+     * {@link LifecycleBase#stopInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that needs to be reported
      */
      @Override
-    public void stop() throws LifecycleException {
+    protected void stopInternal() throws LifecycleException {
         // Stop this realm, then the sub-realms (reverse order to start)
-        super.stop();
+        super.stopInternal();
         for (Realm realm : realms) {
             if (realm instanceof Lifecycle) {
                 ((Lifecycle) realm).stop();

Modified: tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/DataSourceRealm.java Thu Mar  4 22:16:50 2010
@@ -32,6 +32,7 @@
 import org.apache.naming.ContextBindings;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.core.StandardServer;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.tomcat.util.res.StringManager;
 
 /**
@@ -617,17 +618,15 @@
 
 
     /**
-     *
-     * Prepare for active use of the public methods of this Component.
+     * Prepare for the beginning of active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
-     *  that prevents it from being started
+     *  that prevents this component from being used
      */
     @Override
-    public void start() throws LifecycleException {
-
-        // Perform normal superclass initialization
-        super.start();
+    protected void startInternal() throws LifecycleException {
 
         // Create the roles PreparedStatement string
         StringBuilder temp = new StringBuilder("SELECT ");
@@ -648,22 +647,7 @@
         temp.append(userNameCol);
         temp.append(" = ?");
         preparedCredentials = temp.toString();
+        
+        super.startInternal();
     }
-
-
-    /**
-     * Gracefully shut down active use of the public methods of this Component.
-     *
-     * @exception LifecycleException if this component detects a fatal error
-     *  that needs to be reported
-     */
-    @Override
-    public void stop() throws LifecycleException {
-
-        // Perform normal superclass finalization
-        super.stop();
-
-    }
-
-
 }

Modified: tomcat/trunk/java/org/apache/catalina/realm/JAASRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/JAASRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/JAASRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/JAASRealm.java Thu Mar  4 22:16:50 2010
@@ -35,6 +35,7 @@
 import org.apache.catalina.Container;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.authenticator.Constants;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
@@ -591,39 +592,22 @@
     // ------------------------------------------------------ Lifecycle Methods
 
 
-    /**
-     *
-     * Prepare for active use of the public methods of this <code>Component</code>.
-     *
-     * @exception LifecycleException if this component detects a fatal error
-     *  that prevents it from being started
-     */
-    @Override
-    public void start() throws LifecycleException {
-
-        // Perform normal superclass initialization
-        super.start();
+     /**
+      * Prepare for the beginning of active use of the public methods of this
+      * component and implement the requirements of
+      * {@link LifecycleBase#startInternal()}.
+      *
+      * @exception LifecycleException if this component detects a fatal error
+      *  that prevents this component from being used
+      */
+     @Override
+     protected void startInternal() throws LifecycleException {
 
         // These need to be called after loading configuration, in case
         // useContextClassLoader appears after them in xml config
         parseClassNames(userClassNames, userClasses);
         parseClassNames(roleClassNames, roleClasses);
-    }
-
-
-    /**
-     * Gracefully shut down active use of the public methods of this <code>Component</code>.
-     *
-     * @exception LifecycleException if this component detects a fatal error
-     *  that needs to be reported
-     */
-    @Override
-    public void stop() throws LifecycleException {
-
-        // Perform normal superclass finalization
-        super.stop();
-
-    }
-
 
+        super.startInternal();
+     }
 }

Modified: tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/JDBCRealm.java Thu Mar  4 22:16:50 2010
@@ -29,6 +29,7 @@
 import java.util.Properties;
 
 import org.apache.catalina.LifecycleException;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.tomcat.util.res.StringManager;
 
 
@@ -772,17 +773,15 @@
 
 
     /**
-     *
-     * Prepare for active use of the public methods of this Component.
+     * Prepare for the beginning of active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
-     *  that prevents it from being started
+     *  that prevents this component from being used
      */
     @Override
-    public void start() throws LifecycleException {
-
-        // Perform normal superclass initialization
-        super.start();
+    protected void startInternal() throws LifecycleException {
 
         // Validate that we can open our connection - but let tomcat
         // startup in case the database is temporarily unavailable
@@ -792,20 +791,22 @@
             containerLog.error(sm.getString("jdbcRealm.open"), e);
         }
 
+        super.startInternal();
     }
 
 
     /**
-     * Gracefully shut down active use of the public methods of this Component.
+     * Gracefully terminate the active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#stopInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that needs to be reported
      */
-    @Override
-    public void stop() throws LifecycleException {
+     @Override
+    protected void stopInternal() throws LifecycleException {
 
-        // Perform normal superclass finalization
-        super.stop();
+        super.stopInternal();
 
         // Close any open DB connection
         close(this.dbConnection);

Modified: tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/JNDIRealm.java Thu Mar  4 22:16:50 2010
@@ -52,6 +52,7 @@
 import javax.naming.directory.SearchResult;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.util.Base64;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.tomcat.util.buf.ByteChunk;
 import org.apache.tomcat.util.buf.CharChunk;
 
@@ -2032,16 +2033,15 @@
 
 
     /**
-     * Prepare for active use of the public methods of this Component.
+     * Prepare for the beginning of active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
-     *  that prevents it from being started
+     *  that prevents this component from being used
      */
     @Override
-    public void start() throws LifecycleException {
-
-        // Perform normal superclass initialization
-        super.start();
+    protected void startInternal() throws LifecycleException {
 
         // Validate that we can open our connection
         try {
@@ -2050,20 +2050,22 @@
             throw new LifecycleException(sm.getString("jndiRealm.open"), e);
         }
 
+        super.startInternal();
     }
 
 
     /**
-     * Gracefully shut down active use of the public methods of this Component.
+     * Gracefully terminate the active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#stopInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that needs to be reported
      */
-    @Override
-    public void stop() throws LifecycleException {
+     @Override
+    protected void stopInternal() throws LifecycleException {
 
-        // Perform normal superclass finalization
-        super.stop();
+        super.stopInternal();
 
         // Close any open directory server connection
         close(this.context);

Modified: tomcat/trunk/java/org/apache/catalina/realm/LockOutRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/LockOutRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/LockOutRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/LockOutRealm.java Thu Mar  4 22:16:50 2010
@@ -24,6 +24,7 @@
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.catalina.LifecycleException;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 
@@ -78,15 +79,14 @@
 
     /**
      * Prepare for the beginning of active use of the public methods of this
-     * component.  This method should be called before any of the public
-     * methods of this component are utilized.  It should also send a
-     * LifecycleEvent of type START_EVENT to any registered listeners.
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that prevents this component from being used
      */
     @Override
-    public void start() throws LifecycleException {
+    protected void startInternal() throws LifecycleException {
         // Configure the list of failed users to delete the oldest entry once it
         // exceeds the specified size
         failedUsers = new LinkedHashMap<String, LockRecord>(cacheSize, 0.75f,
@@ -109,7 +109,7 @@
             }
         };
 
-        super.start();
+        super.startInternal();
     }
 
 

Modified: tomcat/trunk/java/org/apache/catalina/realm/MemoryRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/MemoryRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/MemoryRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/MemoryRealm.java Thu Mar  4 22:16:50 2010
@@ -25,6 +25,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.catalina.LifecycleException;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
@@ -282,16 +283,15 @@
 
 
     /**
-     * Prepare for active use of the public methods of this Component.
+     * Prepare for the beginning of active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
-     *  that prevents it from being started
+     *  that prevents this component from being used
      */
     @Override
-    public synchronized void start() throws LifecycleException {
-
-        // Perform normal superclass initialization
-        super.start();
+    protected void startInternal() throws LifecycleException {
 
         // Validate the existence of our database file
         File file = new File(pathname);
@@ -319,24 +319,6 @@
             digester.reset();
         }
 
+        super.startInternal();
     }
-
-
-    /**
-     * Gracefully shut down active use of the public methods of this Component.
-     *
-     * @exception LifecycleException if this component detects a fatal error
-     *  that needs to be reported
-     */
-    @Override
-    public synchronized void stop() throws LifecycleException {
-
-        // Perform normal superclass finalization
-        super.stop();
-
-        // No shutdown activities required
-
-    }
-
-
 }

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=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/RealmBase.java Thu Mar  4 22:16:50 2010
@@ -40,9 +40,8 @@
 import org.apache.catalina.Engine;
 import org.apache.catalina.Globals;
 import org.apache.catalina.Host;
-import org.apache.catalina.Lifecycle;
 import org.apache.catalina.LifecycleException;
-import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.LifecycleState;
 import org.apache.catalina.Realm;
 import org.apache.catalina.Server;
 import org.apache.catalina.Service;
@@ -53,7 +52,7 @@
 import org.apache.catalina.deploy.SecurityConstraint;
 import org.apache.catalina.deploy.SecurityCollection;
 import org.apache.catalina.util.HexUtils;
-import org.apache.catalina.util.LifecycleSupport;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.catalina.util.MD5Encoder;
 import org.apache.tomcat.util.res.StringManager;
 import org.apache.juli.logging.Log;
@@ -69,8 +68,8 @@
  * @version $Revision$ $Date$
  */
 
-public abstract class RealmBase
-    implements Lifecycle, Realm, MBeanRegistration {
+public abstract class RealmBase extends LifecycleBase
+    implements Realm, MBeanRegistration {
 
     private static final Log log = LogFactory.getLog(RealmBase.class);
 
@@ -111,12 +110,6 @@
 
 
     /**
-     * The lifecycle event support for this component.
-     */
-    protected LifecycleSupport lifecycle = new LifecycleSupport(this);
-
-
-    /**
      * The MessageDigest object for digesting user credentials (passwords).
      */
     protected MessageDigest md = null;
@@ -142,12 +135,6 @@
 
 
     /**
-     * Has this component been started?
-     */
-    protected boolean started = false;
-
-
-    /**
      * The property change support for this component.
      */
     protected PropertyChangeSupport support = new PropertyChangeSupport(this);
@@ -985,65 +972,20 @@
     }
 
 
-    // ------------------------------------------------------ Lifecycle Methods
-
-
-    /**
-     * Add a lifecycle event listener to this component.
-     *
-     * @param listener The listener to add
-     */
-    public void addLifecycleListener(LifecycleListener listener) {
-
-        lifecycle.addLifecycleListener(listener);
-
-    }
-
-
-    /**
-     * Get the lifecycle listeners associated with this lifecycle. If this 
-     * Lifecycle has no listeners registered, a zero-length array is returned.
-     */
-    public LifecycleListener[] findLifecycleListeners() {
-
-        return lifecycle.findLifecycleListeners();
-
-    }
-
-
-    /**
-     * Remove a lifecycle event listener from this component.
-     *
-     * @param listener The listener to remove
-     */
-    public void removeLifecycleListener(LifecycleListener listener) {
-
-        lifecycle.removeLifecycleListener(listener);
-
-    }
-
     /**
      * Prepare for the beginning of active use of the public methods of this
-     * component.  This method should be called before any of the public
-     * methods of this component are utilized.  It should also send a
-     * LifecycleEvent of type START_EVENT to any registered listeners.
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that prevents this component from being used
      */
-    public void start() throws LifecycleException {
+    @Override
+    protected void startInternal() throws LifecycleException {
 
-        // Validate and update our current component state
-        if (started) {
-            if(log.isInfoEnabled())
-                log.info(sm.getString("realmBase.alreadyStarted"));
-            return;
-        }
         if( !initialized ) {
             init();
         }
-        lifecycle.fireLifecycleEvent(START_EVENT, null);
-        started = true;
 
         // Create a MessageDigest instance for credentials, if desired
         if (digest != null) {
@@ -1055,37 +997,42 @@
             }
         }
 
+        setState(LifecycleState.STARTING);
     }
 
 
     /**
      * Gracefully terminate the active use of the public methods of this
-     * component.  This method should be the last one called on a given
-     * instance of this component.  It should also send a LifecycleEvent
-     * of type STOP_EVENT to any registered listeners.
+     * component and implement the requirements of
+     * {@link LifecycleBase#stopInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that needs to be reported
      */
-    public void stop()
-        throws LifecycleException {
-
-        // Validate and update our current component state
-        if (!started) {
-            if(log.isInfoEnabled())
-                log.info(sm.getString("realmBase.notStarted"));
-            return;
-        }
-        lifecycle.fireLifecycleEvent(STOP_EVENT, null);
-        started = false;
+    @Override
+    protected void stopInternal() throws LifecycleException {
 
+        setState(LifecycleState.STOPPING);
+        
         // Clean up allocated resources
         md = null;
         
         destroy();
+    }
+    
     
+    /**
+     * Return a String representation of this component.
+     */
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("Realm[");
+        sb.append(getName());
+        sb.append(']');
+        return sb.toString();
     }
     
+    
     public void destroy() {
     
         // unregister this realm

Modified: tomcat/trunk/java/org/apache/catalina/realm/UserDatabaseRealm.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/realm/UserDatabaseRealm.java?rev=919208&r1=919207&r2=919208&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/realm/UserDatabaseRealm.java (original)
+++ tomcat/trunk/java/org/apache/catalina/realm/UserDatabaseRealm.java Thu Mar  4 22:16:50 2010
@@ -32,6 +32,7 @@
 import org.apache.catalina.User;
 import org.apache.catalina.UserDatabase;
 import org.apache.catalina.core.StandardServer;
+import org.apache.catalina.util.LifecycleBase;
 import org.apache.tomcat.util.res.StringManager;
 
 
@@ -241,16 +242,15 @@
 
 
     /**
-     * Prepare for active use of the public methods of this Component.
+     * Prepare for the beginning of active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#startInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
-     *  that prevents it from being started
+     *  that prevents this component from being used
      */
     @Override
-    public synchronized void start() throws LifecycleException {
-
-        // Perform normal superclass initialization
-        super.start();
+    protected void startInternal() throws LifecycleException {
 
         try {
             Context context =
@@ -267,25 +267,26 @@
                 (sm.getString("userDatabaseRealm.noDatabase", resourceName));
         }
 
+        super.startInternal();
     }
 
 
     /**
-     * Gracefully shut down active use of the public methods of this Component.
+     * Gracefully terminate the active use of the public methods of this
+     * component and implement the requirements of
+     * {@link LifecycleBase#stopInternal()}.
      *
      * @exception LifecycleException if this component detects a fatal error
      *  that needs to be reported
      */
     @Override
-    public synchronized void stop() throws LifecycleException {
+    protected void stopInternal() throws LifecycleException {
 
         // Perform normal superclass finalization
-        super.stop();
+        super.stopInternal();
 
         // Release reference to our user database
         database = null;
 
     }
-
-
 }



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