|
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.util.Enumeration; import java.util.Hashtable; import java.util.Vector; import org.apache.tomcat.core.BaseInterceptor; import org.apache.tomcat.core.Context; import org.apache.tomcat.core.ContextManager; import org.apache.tomcat.core.TomcatException; import org.apache.tomcat.util.IntrospectionUtils; import org.apache.tomcat.util.res.StringManager; import org.apache.tomcat.util.xml.SaxContext; import org.apache.tomcat.util.xml.XmlAction; import org.apache.tomcat.util.xml.XmlMapper; import org.xml.sax.AttributeList; /** * This is a configuration module that will read context configuration files, * including server.xml, and configure the server by adding the contexts. * * @author Costin Manolache */ public class ContextXmlReader extends BaseInterceptor { private static StringManager sm = StringManager.getManager("org.apache.tomcat.resources"); public ContextXmlReader() { } // -------------------- Properties -------------------- String configFile=null; static final String DEFAULT_CONFIG="conf/server.xml"; public void setConfig( String s ) { configFile=s; } public void setHome( String h ) { System.getProperties().put("tomcat.home", h); } // -------------------- Hooks -------------------- public void engineInit(ContextManager cm) throws TomcatException { if( configFile==null ) configFile=(String)cm.getNote("configFile"); XmlMapper xh=new XmlMapper(); xh.setDebug( debug ); // use the same tags for context-local modules addTagRules(cm, xh); setContextRules( xh ); setPropertiesRules( cm, xh ); setBackward( xh ); // load the config file(s) File f = null; if (configFile == null) configFile=DEFAULT_CONFIG; if (File.separatorChar != '/') configFile=configFile.replace('/',File.separatorChar); f=new File(configFile); if( !f.isAbsolute()) f=new File( cm.getHome(), configFile); if( f.exists() ) ServerXmlReader.loadConfigFile(xh,f,cm); // load server-*.xml Vector v = ServerXmlReader.getUserConfigFiles(f); for (Enumeration e = v.elements(); e.hasMoreElements() ; ) { f = (File)e.nextElement(); if( f.exists() ) { String s=f.getAbsolutePath(); if( s.startsWith( cm.getHome())) s="$TOMCAT_HOME" + s.substring( cm.getHome().length()); log( "Context config=" + s); } ServerXmlReader.loadConfigFile(xh,f,cm); } } // -------------------- Xml reading details -------------------- static class ContextPropertySource implements IntrospectionUtils.PropertySource { ContextManager cm; Context ctx=null; ContextPropertySource( ContextManager cm ) { this.cm=cm; } public void setContext(Context ctx) { this.ctx=ctx; } public String getProperty( String key ) { // XXX add other "predefined" properties String s=null; if( ctx != null ) s=ctx.getProperty( key ); if( s == null ) s=cm.getProperty( key ); if( s == null ) s=System.getProperty( key ); return s; } } public static void setPropertiesRules( ContextManager cm, XmlMapper xh ) throws TomcatException { ContextPropertySource propS=new ContextPropertySource( cm ); xh.setPropertySource( propS ); xh.addRule( "Context/Property", new XmlAction() { public void start(SaxContext ctx ) throws Exception { AttributeList attributes = ctx.getCurrentAttributes(); String name=attributes.getValue("name"); String value=attributes.getValue("value"); if( name==null || value==null ) return; XmlMapper xm=ctx.getMapper(); Context context=(Context)ctx.currentObject(); // replace ${foo} in value value=xm.replaceProperties( value ); if( context.getDebug() > 0 ) context.log("Setting " + name + "=" + value); context.setProperty( name, value ); } }); } // rules for reading the context config public static void setContextRules( XmlMapper xh ) { // Default host xh.addRule( "Context", xh.objectCreate("org.apache.tomcat.core.Context")); xh.addRule( "Context", xh.setProperties() ); xh.addRule( "Context", xh.setParent("setContextManager") ); // Virtual host support - if Context is inside a |
... 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.