|
What this is
Other links
The source code/* * Copyright 1999-2004 The Apache Software 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.jk.config; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import org.apache.catalina.Container; import org.apache.catalina.Context; import org.apache.catalina.Engine; import org.apache.catalina.Host; import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleListener; import org.apache.catalina.Server; /** Base class for automatic jk based configurations based on the Tomcat server.xml settings and the war contexts initialized during startup.
@author Costin Manolache @author Larry Isaacs @author Bill Barker @version $Revision: 1.2 $ */ public class BaseJkConfig implements LifecycleListener { protected int debug=0; protected File configHome = null; protected File workersConfig = null; protected File jkLog = null; protected String jkDebug="emerg"; protected String jkWorker = "ajp13"; protected boolean noRoot=true; protected boolean forwardAll=true; protected String tomcatHome; protected boolean regenerate=false; protected boolean append=false; protected boolean legacy=false; // -------------------- Tomcat callbacks -------------------- // 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 lifecycleEvent(LifecycleEvent evt) { if(Lifecycle.START_EVENT.equals(evt.getType())) { execute( evt ); } } /** Generate configuration files. Override with method to generate web server specific configuration. */ public void execute(LifecycleEvent evt) { initProperties(); PrintWriter mod_jk = null; try { mod_jk = getWriter(); } catch(IOException iex) { log("Unable to open config file"); return; } Lifecycle who = evt.getLifecycle(); if( who instanceof Server ) { executeServer((Server)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(); } /** Generate configuration files. Override with method to generate web server specific configuration. */ public void executeServer(Server svr, PrintWriter mod_jk) { if(! append ) { if( ! generateJkHead(mod_jk) ) return; generateSSLConfig(mod_jk); generateJkTail(mod_jk); } } /** Generate SSL options */ protected void generateSSLConfig(PrintWriter mod_jk) { } /** Generate general options */ protected boolean generateJkHead(PrintWriter mod_jk) { return true; } /** Generate general options */ protected void generateJkTail(PrintWriter mod_jk) { } /** Generate Virtual Host start */ protected void generateVhostHead(Host host, PrintWriter mod_jk) { } /** Generate Virtual Host end */ protected void generateVhostTail(Host host, PrintWriter mod_jk) { } /** Generate configuration files. Override with method to generate web server specific configuration. */ protected void executeEngine(Engine egn, PrintWriter 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); } } } /** Generate configuration files. Override with method to generate web server specific configuration. */ protected void executeHost(Host hst, PrintWriter mod_jk) { 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); } /** 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.
*/
public void executeContext(Context context, PrintWriter mod_jk){
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 void generateStupidMappings(Context context, PrintWriter mod_jk){
}
protected void generateContextMappings(Context context, PrintWriter mod_jk){
}
/** Get the output Writer. Override with method to generate
web server specific configuration.
*/
protected PrintWriter getWriter() throws IOException {
return null;
}
/** Get the host associated with this Container (if any).
*/
protected Host getHost(Container child) {
while(child != null && ! (child instanceof Host) ) {
child = child.getParent();
}
return (Host)child;
}
//-------------------- Properties --------------------
/** Append to config file.
* Set to 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; } public void setLegacy(boolean legacy) { this.legacy = legacy; } // -------------------- Initialize/guess defaults -------------------- /** Initialize defaults for properties that are not set explicitely */ protected void initProperties() { tomcatHome = System.getProperty("catalina.home"); File tomcatDir = new File(tomcatHome); if(configHome==null){ configHome=tomcatDir; } } // -------------------- Config Utils -------------------- /** Add an extension mapping. Override with method to generate web server specific configuration */ protected boolean addExtensionMapping( String ctxPath, String ext, PrintWriter pw ) { return true; } /** Add a fulling specified mapping. Override with method to generate web server specific configuration */ protected boolean addMapping( String fullPath, PrintWriter pw ) { return true; } // -------------------- General Utils -------------------- protected String getAbsoluteDocBase(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; } // ------------------ Grabbed from FileUtil ----------------- public static File getConfigFile( File base, File configDir, 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; } public static String patch(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 static boolean isAbsolute( 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; } protected void log(String msg) { System.err.println(msg); } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.