|
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.Date; import java.util.Enumeration; 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.util.io.FileUtil; import org.apache.tomcat.util.log.Log; /** Generates automatic IIS isapi_redirect configurations based on the Tomcat server.xml settings and the war contexts initialized during startup.
@author Costin Manolache @author Larry Isaacs @author Gal Shachor @version $Revision: 1.15 $ */ public class IISConfig extends BaseJkConfig { public static final String WORKERS_CONFIG = "conf/jk/workers.properties"; public static final String URI_WORKERS_MAP_CONFIG = "conf/auto/uriworkermap.properties"; public static final String ISAPI_LOG_LOCATION = "logs/iis_redirect.log"; public static final String ISAPI_REG_FILE = "conf/auto/iis_redirect.reg"; public static final String ISAPI_PROP_FILE = "conf/auto/isapi_redirect.properties"; public static final String ISAPI_REDIRECTOR = "isapi_redirect.dll"; private File regConfig = null; private File propConfig = null; private File uriConfig = null; private String isapiRedirector = null; public IISConfig() { } //-------------------- Properties -------------------- private void updatePropFile() { propConfig=null; if( isapiRedirector != null ) { int idx=isapiRedirector.lastIndexOf('.'); if( idx > 0 ) { String dir=(regConfig!=null) ? regConfig.toString().replace(File.separatorChar,'/') : ISAPI_REG_FILE; int idx2=dir.lastIndexOf('/'); if( idx2 > 0 ) { dir = dir.substring(0,idx2 + 1); } else { dir = ""; } propConfig = new File(dir + isapiRedirector.substring(0,idx) + ".properties"); } } } /** set the path to the output file for the auto-generated isapi_redirect registry file. If this path is relative then getRegConfig() will resolve it absolutely against the getConfigHome() path. @param path String path to a file */ public void setRegConfig(String path){ regConfig= (path==null)?null:new File(path); updatePropFile(); } /** set a path to the uriworkermap.properties file. @param path String path to uriworkermap.properties file */ public void setUriConfig(String path){ uriConfig= (path==null?null:new File(path)); } public void setIsapiRedirector(String s) { isapiRedirector=s; updatePropFile(); } // -------------------- Initialize/guess defaults -------------------- /** Initialize defaults for properties that are not set explicitely */ protected void initProperties(ContextManager cm) { super.initProperties(cm); regConfig=FileUtil.getConfigFile( regConfig, configHome, ISAPI_REG_FILE); propConfig=FileUtil.getConfigFile( propConfig, configHome, ISAPI_PROP_FILE); workersConfig=FileUtil.getConfigFile( workersConfig, configHome, WORKERS_CONFIG); uriConfig=FileUtil.getConfigFile( uriConfig, configHome, URI_WORKERS_MAP_CONFIG); jkLog=FileUtil.getConfigFile( jkLog, configHome, ISAPI_LOG_LOCATION); } // -------------------- Generate config -------------------- /** executes the IISConfig interceptor. This method generates IIS configuration files for use with isapi_redirect. 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); initWorker(cm); PrintWriter regfile = new PrintWriter(new FileWriter(regConfig)); PrintWriter propfile = new PrintWriter(new FileWriter(propConfig)); PrintWriter uri_worker = new PrintWriter(new FileWriter(uriConfig)); log("Generating IIS registry file = "+regConfig ); log("Generating IIS properties file = "+propConfig ); log("Generating IIS URI worker map file = "+uriConfig ); generateRegistrySettings(regfile); generatePropertySettings(propfile); generateUriWorkerHeader(uri_worker); // Set up contexts // XXX deal with Virtual host configuration !!!! Enumeration enum = cm.getContexts(); while (enum.hasMoreElements()) { Context context = (Context)enum.nextElement(); String vhost = context.getHost(); if(vhost != null) { // Vhosts are not supported yet for IIS continue; } if( forwardAll ) generateStupidMappings( context, uri_worker ); else generateContextMappings( context, uri_worker ); } regfile.close(); propfile.close(); uri_worker.close(); } catch(Exception ex) { Log loghelper = Log.getLog("tc_log", this); loghelper.log("Error generating automatic IIS configuration", ex); } } // -------------------- Config sections -------------------- /** Writes the registry settings required by the IIS connector */ private void generateRegistrySettings(PrintWriter regfile) { regfile.println("REGEDIT4"); regfile.println(); regfile.println("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Apache Software Foundation\\Jakarta Isapi Redirector\\1.0]"); regfile.println("\"extension_uri\"=\"/jakarta/" + (isapiRedirector!=null?isapiRedirector:ISAPI_REDIRECTOR) + "\""); regfile.println("\"log_file\"=\"" + dubleSlash(jkLog.toString()) +"\""); regfile.println("\"log_level\"=\"" + jkDebug + "\""); regfile.println("\"worker_file\"=\"" + dubleSlash(workersConfig.toString()) +"\""); regfile.println("\"worker_mount_file\"=\"" + dubleSlash(uriConfig.toString()) +"\""); regfile.println("\"uri_select\"=\"parsed\""); } /** Writes the registry settings required by the IIS connector */ private void generatePropertySettings(PrintWriter propfile) { propfile.println("extension_uri=/jakarta/" + (isapiRedirector!=null?isapiRedirector:ISAPI_REDIRECTOR)); propfile.println("log_file=" + jkLog.toString()); propfile.println("log_level=" + jkDebug); propfile.println("worker_file=" + workersConfig.toString()); propfile.println("worker_mount_file=" + uriConfig.toString()); propfile.println("uri_select=parsed"); } /** Writes the header information to the uriworkermap file */ private void generateUriWorkerHeader(PrintWriter uri_worker) { uri_worker.println("###################################################################"); uri_worker.println("# Auto generated configuration. Dated: " + new Date()); uri_worker.println("###################################################################"); uri_worker.println(); uri_worker.println("#"); uri_worker.println("# Default worker to be used through our mappings"); uri_worker.println("#"); uri_worker.println("default.worker=" + jkWorker); uri_worker.println(); } /** Forward all requests for a context to tomcat. The default. */ private void generateStupidMappings(Context context, PrintWriter uri_worker ) { String ctxPath = context.getPath(); String nPath=("".equals(ctxPath)) ? "/" : ctxPath; if( noRoot && "".equals(ctxPath) ) { log("Ignoring root context in forward-all mode "); return; } // map all requests for this context to Tomcat uri_worker.println(nPath +"=$(default.worker)"); if( "".equals(ctxPath) ) { uri_worker.println(nPath +"*=$(default.worker)"); uri_worker.println( "# Note: To correctly serve the Tomcat's root context, IIS's Home Directory must"); uri_worker.println( "# must be set to: \"" + getAbsoluteDocBase(context) + "\""); } else uri_worker.println(nPath +"/*=$(default.worker)"); } private void generateContextMappings(Context context, PrintWriter uri_worker ) { String ctxPath = context.getPath(); String nPath=("".equals(ctxPath)) ? "/" : ctxPath; if( noRoot && "".equals(ctxPath) ) { log("Ignoring root context in forward-all mode "); return; } // Static files will be served by IIS uri_worker.println(); uri_worker.println("#########################################################"); uri_worker.println("# Auto configuration for the " + nPath + " context."); uri_worker.println("#########################################################"); uri_worker.println(); // Static mappings are not set in uriworkermap, but must be set with IIS admin. // InvokerInterceptor - it doesn't have a container, // but it's implemented using a special module. // XXX we need to better collect all mappings addMapping( ctxPath + "/servlet/*", uri_worker ); Enumeration servletMaps=context.getContainers(); while( servletMaps.hasMoreElements() ) { Container ct=(Container)servletMaps.nextElement(); addMapping( context, ct , uri_worker ); } // XXX ErrorDocument // Security and filter mappings } /** Add an IIS extension mapping. */ protected boolean addExtensionMapping( String ctxPath, String ext, PrintWriter uri_worker ) { if( debug > 0 ) log( "Adding extension map for " + ctxPath + "/*." + ext ); uri_worker.println(ctxPath + "/*." + ext + "=$(default.worker)"); return true; } /** Add a fulling specified IIS mapping. */ protected boolean addMapping( String fullPath, PrintWriter uri_worker ) { if( debug > 0 ) log( "Adding map for " + fullPath ); if( fullPath.endsWith("/*") ) { uri_worker.println(fullPath.substring(0, fullPath.length() - 2) + "=$(default.worker)" ); } uri_worker.println(fullPath + "=$(default.worker)" ); return true; } // -------------------- Utils -------------------- private String dubleSlash(String in) { StringBuffer sb = new StringBuffer(); for(int i = 0 ; i < in.length() ; i++) { char ch = in.charAt(i); if('\\' == ch) { sb.append("\\\\"); } else { sb.append(ch); } } return sb.toString(); } } |
... 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.