|
What this is
Other links
The source code/* * 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.FileWriter; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; 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.Ajp12Interceptor; import org.apache.tomcat.util.io.FileUtil; import org.apache.tomcat.util.log.Log; /** Generates automatic apache mod_jserv configurations based on the Tomcat server.xml settings and the war contexts initialized during startup.
@author Costin Manolache @author Larry Isaacs @author Mel Martinez @version $Revision: 1.7 $ $Date: 2004/02/25 07:06:59 $ */ public class JservConfig extends BaseInterceptor { /** default path to JServ .conf location */ public static final String APACHE_CONFIG="conf/auto/tomcat-apache.conf"; /** default mod_jserv log file location */ public static final String JSERV_LOG_LOCATION = "logs/mod_jserv.log"; /** default location of mod_jserv Apache plug-in. */ public static String MOD_JSERV; public static final String AJPV12="ajpv12"; //set up some defaults based on OS type static{ String os = System.getProperty("os.name").toLowerCase(); if(os.indexOf("windows")>=0){ MOD_JSERV = "modules/ApacheModuleJserv.dll"; }else{ MOD_JSERV = "libexec/mod_jserv.so"; } } private File configHome = null; private File jservConfig = null; private File modJserv = null; private File jservLog = null; private String tomcatHome; private String jservDebug=null; private boolean noRoot=true; // default is true until we can map all web.xml directives // Or detect only portable directives were used. boolean forwardAll=true; Hashtable NamedVirtualHosts=null; public JservConfig() { } /** Generate the apache 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; 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 ) execute( cm ); } } //-------------------- 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 Apache's root remains intact but isn't completely servlet/JSP enabled. If the ROOT webapp can be configured with apache serving static files, there's no problem setting this option to false. If not, then setting it true means Apache 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 setJkConfig() and other path setters would be resolved against if relative. For example if ConfigHome is set to "/home/tomcat" and JservConfig is set to "conf/tomcat-apache.conf" then the resulting path used would be: "/home/tomcat/conf/tomcat-apache.conf". However, if JservConfig or other 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( "JservConfig.setConfigHome(): "+ "Configuration Home must be a directory! : "+dir); } configHome = f; } /** @return the parent directory of the conf directory or null if not set. */ public File getConfigHome(){ return configHome; } /** sets a path pointing to the output file in which to write the mod_jserv configuration. */ public void setJservConfig(String path){ setJservConfig(path==null?null:new File(path)); } /** sets a File object pointing to the output file in which to write the mod_jserv configuration. */ public void setJservConfig(File path){ jservConfig=path; } /** set the path to the Jserv Apache Module @param path String path to a file */ public void setModJserv(String path){ setModJserv(path==null?null:new File(path)); } /** set the path to the Jserv Apache Module @param path File object */ public void setModJserv(File path){ modJserv=path; } /** set the path to the mod_jserv log file @param path String path to a file */ public void setJservLog(String path){ jservLog= ( path==null?null:new File(path)); } /** Set the verbosity level for mod_jserv. ( use debug, error, etc. ) If not set, no log is written. */ public void setJservDebug( String level ) { jservDebug=null; } // -------------------- Initialize/guess defaults -------------------- /** Initialize defaults for properties that are not set explicitely */ public void initProperties(ContextManager cm) { tomcatHome = cm.getHome(); File tomcatDir = new File(tomcatHome); if(configHome==null){ configHome=tomcatDir; } jservConfig=FileUtil.getConfigFile( jservConfig, configHome, APACHE_CONFIG); if( modJserv == null ) modJserv=new File(MOD_JSERV); else modJserv=FileUtil.getConfigFile( modJserv, configHome, MOD_JSERV ); jservLog=FileUtil.getConfigFile( jservLog, configHome, JSERV_LOG_LOCATION); } // -------------------- Generate config -------------------- /** executes the JservConfig interceptor. This method generates apache configuration files for use with mod_jserv. If not already set, this method will setConfigHome() to the value returned from cm.getHome().
@param cm a ContextManager object.
*/
public void execute(ContextManager cm) throws TomcatException {
try {
initProperties(cm);
NamedVirtualHosts = new Hashtable();
PrintWriter pw=new PrintWriter(new FileWriter(jservConfig));
log("Generating apache mod_jserv config = "+jservConfig );
// generate header
generateJservHead(pw,cm);
Hashtable vhosts = new Hashtable();
// Set up contexts
// XXX deal with Virtual host configuration !!!!
Enumeration enum = cm.getContexts();
while (enum.hasMoreElements()) {
Context context = (Context)enum.nextElement();
String host = context.getHost();
if( host == null ) {
if( forwardAll )
generateStupidMappings( context, pw );
else
generateContextMappings( context, pw );
} else {
Vector vhostContexts = (Vector)vhosts.get(host);
if ( vhostContexts == null ) {
vhostContexts = new Vector();
vhosts.put(host,vhostContexts);
}
vhostContexts.addElement(context);
}
}
enum = vhosts.elements();
while( enum.hasMoreElements() ) {
Vector vhostContexts = (Vector)enum.nextElement();
for( int i = 0; i < vhostContexts.size(); i++ ) {
Context context = (Context)vhostContexts.elementAt(i);
if( i == 0 )
generateVhostHead( context, pw );
if( forwardAll )
generateStupidMappings( context, pw );
else
generateContextMappings( context, pw );
}
generateVhostTail( pw );
}
pw.close();
} catch( Exception ex ) {
Log loghelper = Log.getLog("tc_log", this);
loghelper.log("Error generating automatic apache mod_jserv configuration", ex);
}
}//end execute()
// -------------------- Config sections --------------------
/** Generate the loadModule and general options
*/
private boolean generateJservHead(PrintWriter pw, ContextManager cm)
throws TomcatException
{
//insert LoadModule calls:
pw.println(" |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 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.