FileDocCategorySizeDatePackage
BaseJkConfig.javaAPI DocApache Tomcat 6.0.1418317Fri Jul 20 04:20:36 BST 2007org.apache.jk.config

BaseJkConfig

public class BaseJkConfig extends Object implements org.apache.catalina.LifecycleListener
Base class for automatic jk based configurations based on the Tomcat server.xml settings and the war contexts initialized during startup.

This config interceptor is enabled by inserting a Config element in the <ContextManager> tag body inside the server.xml file like so:

< ContextManager ... >
...
<???Config options />
...
< /ContextManager >
where options can include any of the following attributes:
  • configHome - default parent directory for the following paths. If not set, this defaults to TOMCAT_HOME. Ignored whenever any of the following paths is absolute.
  • workersConfig - path to workers.properties file used by jk connector. If not set, defaults to "conf/jk/workers.properties".
  • jkLog - path to log file to be used by jk connector.
  • jkDebug - Loglevel setting. May be debug, info, error, or emerg. If not set, defaults to emerg.
  • jkWorker The desired worker. Must be set to one of the workers defined in the workers.properties file. "ajp12", "ajp13" or "inprocess" are the workers found in the default workers.properties file. If not specified, defaults to "ajp13" if an Ajp13Interceptor is in use, otherwise it defaults to "ajp12".
  • forwardAll - If true, forward all requests to Tomcat. This helps insure that all the behavior configured in the web.xml file functions correctly. If false, let Apache serve static resources. The default is true. Warning: When false, some configuration in the web.xml may not be duplicated in Apache. Review the mod_jk conf file to see what configuration is actually being set in Apache.
  • noRoot - If true, the root context is not mapped to Tomcat. If false and forwardAll is true, all requests to the root context are mapped to Tomcat. If false and forwardAll is false, only JSP and servlets requests to the root context are mapped to Tomcat. When false, to correctly serve Tomcat's root context you may also need to modify the web server to point it's home directory to Tomcat's root context directory. Otherwise some content, such as the root index.html, may be served by the web server before the connector gets a chance to claim the request and pass it to Tomcat. The default is true.

author
Costin Manolache
author
Larry Isaacs
author
Bill Barker
version
$Revision: 467222 $

Fields Summary
private static org.apache.juli.logging.Log
log
protected File
configHome
protected File
workersConfig
protected File
jkLog
protected String
jkDebug
protected String
jkWorker
protected boolean
noRoot
protected boolean
forwardAll
protected String
tomcatHome
protected boolean
regenerate
protected boolean
append
protected boolean
legacy
Constructors Summary
Methods Summary
protected booleanaddExtensionMapping(java.lang.String ctxPath, java.lang.String ext, java.io.PrintWriter pw)
Add an extension mapping. Override with method to generate web server specific configuration

        return true;
    
protected booleanaddMapping(java.lang.String fullPath, java.io.PrintWriter pw)
Add a fulling specified mapping. Override with method to generate web server specific configuration

        return true;
    
public voidexecute(org.apache.catalina.LifecycleEvent evt)
Generate configuration files. Override with method to generate web server specific configuration.

        initProperties();
        PrintWriter mod_jk = null;
        try {
            mod_jk = getWriter();
        } catch(IOException iex) {
            log.warn("Unable to open config file");
            return;
        }
        Lifecycle who = evt.getLifecycle();
        if( who instanceof Server ) {
            executeServer((Server)who, mod_jk);
        } else if(who instanceof Engine) {
            executeEngine((Engine)who, mod_jk);
        } else if ( who instanceof Host ) {
            executeHost((Host)who, mod_jk);
        } else if( who instanceof Context ) {
            executeContext((Context)who, mod_jk);
        }
        mod_jk.close();
    
public voidexecuteContext(org.apache.catalina.Context context, java.io.PrintWriter mod_jk)
executes the ApacheConfig interceptor. This method generates apache configuration files for use with mod_jk.

param
context a Context object.
param
mod_jk Writer for output.


        if(context.getPath().length() > 0 || ! noRoot ) {
            String docRoot = context.getServletContext().getRealPath("/");
            if( forwardAll || docRoot == null)
                generateStupidMappings( context, mod_jk );
            else
                generateContextMappings( context, mod_jk);
        }
    
protected voidexecuteEngine(org.apache.catalina.Engine egn, java.io.PrintWriter mod_jk)
Generate configuration files. Override with method to generate web server specific configuration.

        if(egn.getJvmRoute() != null) {
            jkWorker = egn.getJvmRoute();
        }
        executeServer(egn.getService().getServer(), mod_jk);
        Container [] children = egn.findChildren();
        for(int ii=0; ii < children.length; ii++) {
            if( children[ii] instanceof Host ) {
                executeHost((Host)children[ii], mod_jk);
            } else if( children[ii] instanceof Context ) {
                executeContext((Context)children[ii], mod_jk);
            }
        }
    
protected voidexecuteHost(org.apache.catalina.Host hst, java.io.PrintWriter mod_jk)
Generate configuration files. Override with method to generate web server specific configuration.

        generateVhostHead(hst, mod_jk);
        Container [] children = hst.findChildren();
        for(int ii=0; ii < children.length; ii++) {
            if(children[ii] instanceof Context) {
                executeContext((Context)children[ii],mod_jk);
            }
        }
        generateVhostTail(hst, mod_jk);
    
public voidexecuteServer(org.apache.catalina.Server svr, java.io.PrintWriter mod_jk)
Generate configuration files. Override with method to generate web server specific configuration.

        if(! append ) {
            if( ! generateJkHead(mod_jk) )
                return;
            generateSSLConfig(mod_jk);
            generateJkTail(mod_jk);
        }
    
protected voidgenerateContextMappings(org.apache.catalina.Context context, java.io.PrintWriter mod_jk)

    
protected booleangenerateJkHead(java.io.PrintWriter mod_jk)
Generate general options

        return true;
    
protected voidgenerateJkTail(java.io.PrintWriter mod_jk)
Generate general options

    
protected voidgenerateSSLConfig(java.io.PrintWriter mod_jk)
Generate SSL options

    
protected voidgenerateStupidMappings(org.apache.catalina.Context context, java.io.PrintWriter mod_jk)

    
protected voidgenerateVhostHead(org.apache.catalina.Host host, java.io.PrintWriter mod_jk)
Generate Virtual Host start

    
protected voidgenerateVhostTail(org.apache.catalina.Host host, java.io.PrintWriter mod_jk)
Generate Virtual Host end

    
protected java.lang.StringgetAbsoluteDocBase(org.apache.catalina.Context context)

        // Calculate the absolute path of the document base
        String docBase = context.getServletContext().getRealPath("/");
        docBase = docBase.substring(0,docBase.length()-1);
        if (!isAbsolute(docBase)){
            docBase = tomcatHome + "/" + docBase;
        }
        docBase = patch(docBase);
        return docBase;
    
public static java.io.FilegetConfigFile(java.io.File base, java.io.File configDir, java.lang.String defaultF)

        if( base==null )
            base=new File( defaultF );
        if( ! base.isAbsolute() ) {
            if( configDir != null )
                base=new File( configDir, base.getPath());
            else
                base=new File( base.getAbsolutePath()); //??
        }
        File parent=new File(base.getParent());
        if(!parent.exists()){
            if(!parent.mkdirs()){
                throw new RuntimeException(
                    "Unable to create path to config file :"+
                    base.getAbsolutePath());
            }
        }
        return base;
    
protected org.apache.catalina.HostgetHost(org.apache.catalina.Container child)
Get the host associated with this Container (if any).

        while(child != null && ! (child instanceof Host) ) {
            child = child.getParent();
        }
        return (Host)child;
    
protected java.io.PrintWritergetWriter()
Get the output Writer. Override with method to generate web server specific configuration.

        return null;
    
protected voidinitProperties()
Initialize defaults for properties that are not set explicitely

        tomcatHome = System.getProperty("catalina.home");
        File tomcatDir = new File(tomcatHome);
        if(configHome==null){
            configHome=tomcatDir;
        }
    
public static booleanisAbsolute(java.lang.String path)

        // normal file
        if( path.startsWith("/" ) ) return true;

        if( path.startsWith(File.separator ) ) return true;

        // win c:
        if (path.length() >= 3 &&
            Character.isLetter(path.charAt(0)) &&
            path.charAt(1) == ':")
            return true;

        // NetWare volume:
        if (System.getProperty("os.name").startsWith("NetWare") &&
            path.length() >=3 &&
            path.indexOf(':") > 0)
            return true;

        return false;
    
public voidlifecycleEvent(org.apache.catalina.LifecycleEvent evt)
Generate the configuration - only when the server is completely initialized ( before starting )


    // -------------------- Tomcat callbacks --------------------


    // Auto-config should be able to react to dynamic config changes,
    // and regenerate the config.

                           
        
        if(Lifecycle.START_EVENT.equals(evt.getType())) {
           execute( evt );
        } 
    
public static java.lang.Stringpatch(java.lang.String path)

        String patchPath = path;

        // Move drive spec to the front of the path
        if (patchPath.length() >= 3 &&
            patchPath.charAt(0) == '/"  &&
            Character.isLetter(patchPath.charAt(1)) &&
            patchPath.charAt(2) == ':") {
            patchPath=patchPath.substring(1,3)+"/"+patchPath.substring(3);
        }

        // Eliminate consecutive slashes after the drive spec
        if (patchPath.length() >= 2 &&
            Character.isLetter(patchPath.charAt(0)) &&
            patchPath.charAt(1) == ':") {
            char[] ca = patchPath.replace('/", '\\").toCharArray();
            char c;
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < ca.length; i++) {
                if ((ca[i] != '\\") ||
                    (ca[i] == '\\" &&
                        i > 0 &&
                        ca[i - 1] != '\\")) {
                    if (i == 0 &&
                        Character.isLetter(ca[i]) &&
                        i < ca.length - 1 &&
                        ca[i + 1] == ':") {
                        c = Character.toUpperCase(ca[i]);
                    } else {
                        c = ca[i];
                    }

                    sb.append(c);
                }
            }

            patchPath = sb.toString();
        }

        // fix path on NetWare - all '/' become '\\' and remove duplicate '\\'
        if (System.getProperty("os.name").startsWith("NetWare") &&
            path.length() >=3 &&
            path.indexOf(':") > 0) {
            char[] ca = patchPath.replace('/", '\\").toCharArray();
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < ca.length; i++) {
                if ((ca[i] != '\\") ||
                    (ca[i] == '\\" && i > 0 && ca[i - 1] != '\\")) {
                    sb.append(ca[i]);
                }
            }
            patchPath = sb.toString();
        }

        return patchPath;
    
public voidsetAppend(boolean apnd)
Append to config file. Set to true if the config information should be appended.

        append = apnd;
    
public voidsetConfigHome(java.lang.String dir)
set a path to the parent directory of the conf folder. That is, the parent directory within which path setters would be resolved against, if relative. For example if ConfigHome is set to "/home/tomcat" and regConfig is set to "conf/mod_jk.conf" then the resulting path used would be: "/home/tomcat/conf/mod_jk.conf".

However, if the path is set to an absolute path, this attribute is ignored.

If not set, execute() will set this to TOMCAT_HOME.

param
dir - path to a directory

        if( dir==null ) return;
        File f=new File(dir);
        if(!f.isDirectory()){
            throw new IllegalArgumentException(
                "BaseConfig.setConfigHome(): "+
                "Configuration Home must be a directory! : "+dir);
        }
        configHome = f;
    
public voidsetForwardAll(boolean b)
If false, we'll try to generate a config that will let apache serve static files. The default is true, forward all requests in a context to tomcat.

        forwardAll=b;
    
public voidsetJkDebug(java.lang.String level)
Set the verbosity level ( use debug, error, etc. ) If not set, no log is written.

        jkDebug=level;
    
public voidsetJkLog(java.lang.String path)
set the path to the log file

param
path String path to a file

        jkLog = ( path==null ? null : new File(path));
    
public voidsetJkWorker(java.lang.String worker)
Sets the JK worker.

param
worker The worker

        jkWorker = worker;
    
public voidsetLegacy(boolean legacy)

        this.legacy = legacy;
    
public voidsetNoRoot(boolean b)
Special option - do not generate mappings for the ROOT context. The default is true, and will not generate the mappings, not redirecting all pages to tomcat (since /* matches everything). This means that the web server's root remains intact but isn't completely servlet/JSP enabled. If the ROOT webapp can be configured with the web server serving static files, there's no problem setting this option to false. If not, then setting it true means the web server will be out of picture for all requests.

        noRoot=b;
    
public voidsetWorkersConfig(java.lang.String path)
set a path to the workers.properties file.

param
path String path to workers.properties file

        workersConfig= (path==null?null:new File(path));