From 758520c83da256a26089ee398f2bae091b81e3aa Mon Sep 17 00:00:00 2001 From: markt Date: Thu, 4 Mar 2010 22:16:50 +0000 Subject: [PATCH] Lifecycle refactoring Realm: Note most implementations were firing the start event too early git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@919208 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/realm/CombinedRealm.java | 19 ++-- .../org/apache/catalina/realm/DataSourceRealm.java | 32 ++---- java/org/apache/catalina/realm/JAASRealm.java | 42 +++----- java/org/apache/catalina/realm/JDBCRealm.java | 25 ++--- java/org/apache/catalina/realm/JNDIRealm.java | 24 ++--- java/org/apache/catalina/realm/LockOutRealm.java | 10 +- java/org/apache/catalina/realm/MemoryRealm.java | 32 ++---- java/org/apache/catalina/realm/RealmBase.java | 107 ++++++--------------- .../apache/catalina/realm/UserDatabaseRealm.java | 23 ++--- 9 files changed, 107 insertions(+), 207 deletions(-) diff --git a/java/org/apache/catalina/realm/CombinedRealm.java b/java/org/apache/catalina/realm/CombinedRealm.java index 65ec2af41..64e9affe0 100644 --- a/java/org/apache/catalina/realm/CombinedRealm.java +++ b/java/org/apache/catalina/realm/CombinedRealm.java @@ -30,6 +30,7 @@ import org.apache.catalina.Container; 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 @@ public class CombinedRealm extends RealmBase { /** * 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 iter = realms.iterator(); @@ -243,23 +243,22 @@ public class CombinedRealm extends RealmBase { } } } - 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(); diff --git a/java/org/apache/catalina/realm/DataSourceRealm.java b/java/org/apache/catalina/realm/DataSourceRealm.java index 8deb93ac1..239184d57 100644 --- a/java/org/apache/catalina/realm/DataSourceRealm.java +++ b/java/org/apache/catalina/realm/DataSourceRealm.java @@ -32,6 +32,7 @@ import javax.sql.DataSource; 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 @@ public class DataSourceRealm /** - * - * 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 @@ public class DataSourceRealm 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(); - - } - - } diff --git a/java/org/apache/catalina/realm/JAASRealm.java b/java/org/apache/catalina/realm/JAASRealm.java index 9c8411fb1..ad4e6170d 100644 --- a/java/org/apache/catalina/realm/JAASRealm.java +++ b/java/org/apache/catalina/realm/JAASRealm.java @@ -35,6 +35,7 @@ import javax.security.auth.login.LoginException; 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 @@ public class JAASRealm // ------------------------------------------------------ Lifecycle Methods - /** - * - * Prepare for active use of the public methods of this Component. - * - * @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 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(); - - } - + super.startInternal(); + } } diff --git a/java/org/apache/catalina/realm/JDBCRealm.java b/java/org/apache/catalina/realm/JDBCRealm.java index 391e0a118..b5b558ced 100644 --- a/java/org/apache/catalina/realm/JDBCRealm.java +++ b/java/org/apache/catalina/realm/JDBCRealm.java @@ -29,6 +29,7 @@ import java.util.ArrayList; 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 @@ public class JDBCRealm /** - * - * 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 @@ public class JDBCRealm 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); diff --git a/java/org/apache/catalina/realm/JNDIRealm.java b/java/org/apache/catalina/realm/JNDIRealm.java index 902cd90da..d4f26af63 100644 --- a/java/org/apache/catalina/realm/JNDIRealm.java +++ b/java/org/apache/catalina/realm/JNDIRealm.java @@ -52,6 +52,7 @@ import javax.naming.directory.SearchControls; 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 @@ public class JNDIRealm extends RealmBase { /** - * 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 @@ public class JNDIRealm extends RealmBase { 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); diff --git a/java/org/apache/catalina/realm/LockOutRealm.java b/java/org/apache/catalina/realm/LockOutRealm.java index 744ba96c6..24b6143ff 100644 --- a/java/org/apache/catalina/realm/LockOutRealm.java +++ b/java/org/apache/catalina/realm/LockOutRealm.java @@ -24,6 +24,7 @@ import java.util.Map; 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 @@ public class LockOutRealm extends CombinedRealm { /** * 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(cacheSize, 0.75f, @@ -109,7 +109,7 @@ public class LockOutRealm extends CombinedRealm { } }; - super.start(); + super.startInternal(); } diff --git a/java/org/apache/catalina/realm/MemoryRealm.java b/java/org/apache/catalina/realm/MemoryRealm.java index 0ba1147aa..ff320872b 100644 --- a/java/org/apache/catalina/realm/MemoryRealm.java +++ b/java/org/apache/catalina/realm/MemoryRealm.java @@ -25,6 +25,7 @@ import java.util.ArrayList; 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 @@ public class MemoryRealm extends RealmBase { /** - * 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 @@ public class MemoryRealm extends RealmBase { 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 - - } - - } diff --git a/java/org/apache/catalina/realm/RealmBase.java b/java/org/apache/catalina/realm/RealmBase.java index 6efa922db..a0d3cb8f9 100644 --- a/java/org/apache/catalina/realm/RealmBase.java +++ b/java/org/apache/catalina/realm/RealmBase.java @@ -40,9 +40,8 @@ import org.apache.catalina.Context; 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.LoginConfig; 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 @@ import org.apache.tomcat.util.modeler.Registry; * @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 @@ public abstract class RealmBase /** - * 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 @@ public abstract class RealmBase /** - * 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 @@ public abstract class RealmBase } - // ------------------------------------------------------ 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 @@ public abstract class RealmBase } } + 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 diff --git a/java/org/apache/catalina/realm/UserDatabaseRealm.java b/java/org/apache/catalina/realm/UserDatabaseRealm.java index e271eeef3..3b50dbe64 100644 --- a/java/org/apache/catalina/realm/UserDatabaseRealm.java +++ b/java/org/apache/catalina/realm/UserDatabaseRealm.java @@ -32,6 +32,7 @@ import org.apache.catalina.Role; 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 @@ public class UserDatabaseRealm /** - * 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 @@ public class UserDatabaseRealm (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; } - - } -- 2.11.0