alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/* $Id: BaseJkConfig.java,v 1.9 2004/02/25 07:06:59 billbarker Exp $
 * ====================================================================
 *
 *   
 *  Copyright 1999-2004 The Apache Sofware Foundation.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.tomcat.modules.config;

import java.io.File;
import java.io.PrintWriter;

import org.apache.tomcat.core.BaseInterceptor;
import org.apache.tomcat.core.Container;
import org.apache.tomcat.core.Context;
import org.apache.tomcat.core.ContextManager;
import org.apache.tomcat.core.TomcatException;
import org.apache.tomcat.modules.server.Ajp13Interceptor;
import org.apache.tomcat.util.io.FileUtil;

/**
    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 @version $Revision: 1.9 $ */ public class BaseJkConfig extends BaseInterceptor { protected File configHome = null; protected File workersConfig = null; protected File jkLog = null; protected String jkDebug="emerg"; protected String jkWorker = null; protected boolean noRoot=true; protected boolean forwardAll=true; protected String tomcatHome; protected boolean regenerate=false; // -------------------- Tomcat callbacks -------------------- public void addInterceptor( ContextManager cm, Context ctx, BaseInterceptor bi ) throws TomcatException { if( cm.getProperty( "jkconf" ) != null ) { // we are in config generation mode - prevent tomcat // from starting. //??? cm.setNote("nostart", this ); } } // Auto-config should be able to react to dynamic config changes, // and regenerate the config. /** Generate the configuration - only when the server is * completely initialized ( before starting ) */ public void engineState( ContextManager cm, int state ) throws TomcatException { if( state != ContextManager.STATE_INIT ) return; // Generate the config only if "regenerate" property is // set on the module or if an explicit "jkconf" option has // been set on context manager. if( regenerate || cm.getProperty("jkconf") !=null) { execute( cm ); } } public void contextInit(Context ctx) throws TomcatException { ContextManager cm=ctx.getContextManager(); if( cm.getState() >= ContextManager.STATE_INIT ) { // a context has been added after the server was started. // regenerate the config ( XXX send a restart signal to // the server ) // Generate the config only if "regenerate" property is // set on the module or if an explicit "jkconf" option has // been set on context manager. if( regenerate || cm.getProperty("jkconf") !=null) { execute( cm ); } } } /** Generate configuration files. Override with method to generate web server specific configuration. */ public void execute(ContextManager cm) throws TomcatException { } //-------------------- Properties -------------------- /** 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. */ public void setForwardAll( boolean b ) { forwardAll=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. */ public void setNoRoot( boolean b ) { noRoot=b; } /** 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 */ public void setConfigHome(String dir){ 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; } /** set a path to the workers.properties file. @param path String path to workers.properties file */ public void setWorkersConfig(String path){ workersConfig= (path==null?null:new File(path)); } /** set the path to the log file @param path String path to a file */ public void setJkLog(String path){ jkLog= ( path==null?null:new File(path)); } /** Set the verbosity level ( use debug, error, etc. ) If not set, no log is written. */ public void setJkDebug( String level ) { jkDebug=level; } /** set the Ajp protocal @param protocal String protocol, "ajp12" or "ajp13" */ public void setJkWorker(String worker){ jkWorker = worker; } // -------------------- Initialize/guess defaults -------------------- /** Initialize defaults for properties that are not set explicitely */ protected void initProperties(ContextManager cm) { tomcatHome = cm.getHome(); File tomcatDir = new File(tomcatHome); if(configHome==null){ configHome=tomcatDir; } } protected void initWorker(ContextManager cm) { // Find Ajp1? connectors BaseInterceptor ci[]=cm.getContainer().getInterceptors(); for( int i=0; i

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.