luehe       2004/09/16 15:21:45

  Modified:    catalina/src/share/org/apache/catalina/startup Embedded.java
                        ContextConfig.java
  Log:
  Provide mechanism to override the login-method-to-authenticator mappings
  specified in Authenticators.properties, or provide custom Authenticator
  implementations.
  
  Revision  Changes    Path
  1.22      +40 -1     
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/Embedded.java
  
  Index: Embedded.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/Embedded.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- Embedded.java     14 Sep 2004 13:20:03 -0000      1.21
  +++ Embedded.java     16 Sep 2004 22:21:45 -0000      1.22
  @@ -21,7 +21,9 @@
   import java.io.File;
   import java.io.IOException;
   import java.net.InetAddress;
  +import java.util.HashMap;
   
  +import org.apache.catalina.Authenticator;
   import org.apache.catalina.Container;
   import org.apache.catalina.Context;
   import org.apache.catalina.Engine;
  @@ -31,6 +33,7 @@
   import org.apache.catalina.LifecycleListener;
   import org.apache.catalina.Loader;
   import org.apache.catalina.Realm;
  +import org.apache.catalina.Valve;
   import org.apache.catalina.connector.Connector;
   import org.apache.catalina.core.StandardContext;
   import org.apache.catalina.core.StandardEngine;
  @@ -150,6 +153,12 @@
   
   
       /**
  +     * Custom mappings of login methods to authenticators
  +     */
  +    protected HashMap authenticators;
  +
  +
  +    /**
        * Descriptive information about this server implementation.
        */
       protected static final String info =
  @@ -448,6 +457,7 @@
           context.setPath(path);
   
           ContextConfig config = new ContextConfig();
  +        config.setCustomAuthenticators(authenticators);
           ((Lifecycle) context).addLifecycleListener(config);
   
           return (context);
  @@ -687,6 +697,35 @@
               log.debug(" Removing this Host");
           host.getParent().removeChild(host);
   
  +    }
  +
  +
  +    /*
  +     * Maps the specified login method to the specified authenticator, allowing
  +     * the mappings in org/apache/catalina/startup/Authenticators.properties
  +     * to be overridden.
  +     *
  +     * @param authenticator Authenticator to handle authentication for the
  +     * specified login method
  +     * @param loginMethod Login method that maps to the specified authenticator
  +     *
  +     * @throws IllegalArgumentException if the specified authenticator does not
  +     * implement the org.apache.catalina.Valve interface
  +     */
  +    public void addAuthenticator(Authenticator authenticator,
  +                                 String loginMethod) {
  +        if (!(authenticator instanceof Valve)) {
  +            throw new IllegalArgumentException(
  +                sm.getString("authenticator.notInstanceOfValve"));
  +        }
  +        if (authenticators == null) {
  +            synchronized (this) {
  +                if (authenticators == null) {
  +                    authenticators = new HashMap();
  +                }
  +            }
  +        }
  +        authenticators.put(loginMethod, authenticator);
       }
   
   
  
  
  
  1.55      +73 -38    
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/ContextConfig.java
  
  Index: ContextConfig.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/ContextConfig.java,v
  retrieving revision 1.54
  retrieving revision 1.55
  diff -u -r1.54 -r1.55
  --- ContextConfig.java        16 Sep 2004 15:30:33 -0000      1.54
  +++ ContextConfig.java        16 Sep 2004 22:21:45 -0000      1.55
  @@ -24,6 +24,7 @@
   import java.io.InputStream;
   import java.net.URL;
   import java.util.Properties;
  +import java.util.Map;
   
   import javax.servlet.ServletContext;
   
  @@ -72,6 +73,13 @@
   
       // ----------------------------------------------------- Instance Variables
   
  +
  +    /*
  +     * Custom mappings of login methods to authenticators
  +     */
  +    private Map customAuthenticators;
  +
  +
       /**
        * The set of Authenticators that we know how to configure.  The key is
        * the name of the implemented authentication method, and the value is
  @@ -207,6 +215,17 @@
       }
   
   
  +    /**
  +     * Sets custom mappings of login methods to authenticators.
  +     *
  +     * @param customAuthenticators Custom mappings of login methods to
  +     * authenticators
  +     */
  +    public void setCustomAuthenticators(Map customAuthenticators) {
  +        this.customAuthenticators = customAuthenticators;
  +    }
  +
  +
       // --------------------------------------------------------- Public Methods
   
   
  @@ -383,54 +402,70 @@
               return;
           }
   
  -        // Load our mapping properties if necessary
  -        if (authenticators == null) {
  -            try {
  -                InputStream 
is=this.getClass().getClassLoader().getResourceAsStream("org/apache/catalina/startup/Authenticators.properties");
  -                if( is!=null ) {
  -                    authenticators = new Properties();
  -                    authenticators.load(is);
  -                } else {
  -                    log.error(sm.getString("contextConfig.authenticatorResources"));
  -                    ok=false;
  +        /*
  +         * First check to see if there is a custom mapping for the login
  +         * method. If so, use it. Otherwise, check if there is a mapping in
  +         * org/apache/catalina/startup/Authenticators.properties.
  +         */
  +        Valve authenticator = null;
  +        if (customAuthenticators != null) {
  +            authenticator = (Valve)
  +                customAuthenticators.get(loginConfig.getAuthMethod());
  +        }
  +        if (authenticator == null) {
  +            // Load our mapping properties if necessary
  +            if (authenticators == null) {
  +                try {
  +                    InputStream 
is=this.getClass().getClassLoader().getResourceAsStream("org/apache/catalina/startup/Authenticators.properties");
  +                    if( is!=null ) {
  +                        authenticators = new Properties();
  +                        authenticators.load(is);
  +                    } else {
  +                        
log.error(sm.getString("contextConfig.authenticatorResources"));
  +                        ok=false;
  +                        return;
  +                    }
  +                } catch (IOException e) {
  +                    log.error(sm.getString("contextConfig.authenticatorResources"), 
e);
  +                    ok = false;
                       return;
                   }
  -            } catch (IOException e) {
  -                log.error(sm.getString("contextConfig.authenticatorResources"), e);
  +            }
  +
  +            // Identify the class name of the Valve we should configure
  +            String authenticatorName = null;
  +            authenticatorName =
  +                    authenticators.getProperty(loginConfig.getAuthMethod());
  +            if (authenticatorName == null) {
  +                log.error(sm.getString("contextConfig.authenticatorMissing",
  +                                 loginConfig.getAuthMethod()));
                   ok = false;
                   return;
               }
  -        }
   
  -        // Identify the class name of the Valve we should configure
  -        String authenticatorName = null;
  -        authenticatorName =
  -                authenticators.getProperty(loginConfig.getAuthMethod());
  -        if (authenticatorName == null) {
  -            log.error(sm.getString("contextConfig.authenticatorMissing",
  -                             loginConfig.getAuthMethod()));
  -            ok = false;
  -            return;
  +            // Instantiate and install an Authenticator of the requested class
  +            try {
  +                Class authenticatorClass = Class.forName(authenticatorName);
  +                authenticator = (Valve) authenticatorClass.newInstance();
  +            } catch (Throwable t) {
  +                log.error(sm.getString(
  +                                    "contextConfig.authenticatorInstantiate",
  +                                    authenticatorName),
  +                          t);
  +                ok = false;
  +            }
           }
   
  -        // Instantiate and install an Authenticator of the requested class
  -        Valve authenticator = null;
  -        try {
  -            Class authenticatorClass = Class.forName(authenticatorName);
  -            authenticator = (Valve) authenticatorClass.newInstance();
  -            if (context instanceof ContainerBase) {
  -                Pipeline pipeline = ((ContainerBase) context).getPipeline();
  -                if (pipeline != null) {
  -                    ((ContainerBase) context).addValve(authenticator);
  -                    if (log.isDebugEnabled())
  -                        
log.debug(sm.getString("contextConfig.authenticatorConfigured",
  +        if (authenticator != null && context instanceof ContainerBase) {
  +            Pipeline pipeline = ((ContainerBase) context).getPipeline();
  +            if (pipeline != null) {
  +                ((ContainerBase) context).addValve(authenticator);
  +                if (log.isDebugEnabled()) {
  +                    log.debug(sm.getString(
  +                                    "contextConfig.authenticatorConfigured",
                                        loginConfig.getAuthMethod()));
                   }
               }
  -        } catch (Throwable t) {
  -            log.error(sm.getString("contextConfig.authenticatorInstantiate",
  -                             authenticatorName), t);
  -            ok = false;
           }
   
       }
  
  
  

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

Reply via email to