You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@apache.org on 2001/02/01 07:22:08 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/modules/config ServerXmlReader.java ContextXmlReader.java

costin      01/01/31 22:22:08

  Added:       src/share/org/apache/tomcat/modules/config
                        ServerXmlReader.java ContextXmlReader.java
  Log:
  Split ServerXmlInterceptor in 2 modules - one deals with general
  server configuration ( reading server.xml and setting up the
  modules that compose the server ), one deals with setting
  context preferences.
  
  This helps clarify the startup process, gives more flexibility.
  
  One important thing is to try to use separate files for context
  config and server config - this will also simplify context adding
  and context configuration ( instead of editing server.xml, you'll just
  add a file - see the way RedHat deals with that - like
  /etc/xinetd.d, all the rc.x/ files, adding apps to the menu, etc )
  
  Of course, it's just a matter of style - contexts in server.xml
  will still be supported ( and this feature is available in tomcat
  3.2 also - you can use multiple server-foo.xml files and split the
  config, it's just that we don't use it by default )
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/modules/config/ServerXmlReader.java
  
  Index: ServerXmlReader.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  package org.apache.tomcat.modules.config;
  
  import java.beans.*;
  import java.io.*;
  import java.lang.reflect.*;
  import java.util.Hashtable;
  import java.util.*;
  import java.net.*;
  import org.apache.tomcat.util.*;
  import org.apache.tomcat.util.xml.*;
  import org.apache.tomcat.core.*;
  import org.apache.tomcat.modules.server.*;
  import org.apache.tomcat.util.log.*;
  import org.xml.sax.*;
  
  /**
   * This is a configuration module that will read a server.xml file
   * and dynamically configure the server by adding modules and interceptors.
   *
   * Tomcat can be configured ( and auto-configured ) in many ways, and
   * a configuration module will have access to all server events, and will
   * be able to update it's state, etc.
   *
   * @author Costin Manolache
   */
  public class ServerXmlReader extends BaseInterceptor {
      private static StringManager sm =
  	StringManager.getManager("org.apache.tomcat.resources");
  
      
      public ServerXmlReader() {
      }
  
      // -------------------- 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 -------------------- 
  
      /** When this module is added, it'll automatically load
       *  a configuration file and add all global modules.
       */
      public void addInterceptor(ContextManager cm, Context ctx,
  			       BaseInterceptor module)
  	throws TomcatException
      {
  	if( this != module ) return;
  
  	XmlMapper xh=new XmlMapper();
  	xh.setDebug( debug );
  	xh.addRule( "ContextManager", xh.setProperties() );
  	setTagRules( xh );
  	addDefaultTags(cm, xh);
  	setBackward( xh );
  	
  	// load the config file(s)
  	File f  = null;
  	if (configFile == null)
  	    configFile=DEFAULT_CONFIG;
  
  	if( configFile.startsWith( "/" ) ) 
  	    f=new File(configFile);
  	else
  	    f=new File( cm.getHome(), "/" + configFile);
  
  	if( f.exists() )
  	    loadConfigFile(xh,f,cm);
  
  	// load server-*.xml
  	Vector v = getUserConfigFiles(f);
  	for (Enumeration e = v.elements();
  	     e.hasMoreElements() ; ) {
  	    f = (File)e.nextElement();
  	    loadConfigFile(xh,f,cm);
  	}
      }
  
      // -------------------- Xml reading details --------------------
  
      public static void loadConfigFile(XmlMapper xh, File f, ContextManager cm)
  	throws TomcatException
      {
  	cm.log(sm.getString("tomcat.loading") + " " + f);
  	try {
  	    xh.readXml(f,cm);
  	} catch( Exception ex ) {
  	    cm.log( sm.getString("tomcat.fatalconfigerror"), ex );
  	    throw new TomcatException(ex);
  	}
  	cm.log(sm.getString("tomcat.loaded") + " " + f);
      }
      
      public static void setTagRules( XmlMapper xh ) {
  	xh.addRule( "module",  new XmlAction() {
  		public void end(SaxContext ctx ) throws Exception {
  		    Object elem=ctx.currentObject();
  		    AttributeList attributes = ctx.getCurrentAttributes();
  		    String name=attributes.getValue("name");
  		    String classN=attributes.getValue("javaClass");
  		    if( name==null || classN==null ) return;
  		    XmlMapper mapper=ctx.getMapper();
  		    ServerXmlReader.addTag( mapper, name, classN );
  		}
  	    });
      }
  
      // read modules.xml, if any, and load taskdefs 
      public static  void addDefaultTags( ContextManager cm, XmlMapper xh)
  	throws TomcatException
      {
  	File f=new File( cm.getHome(), "/conf/modules.xml");
  	if( f.exists() ) {
  	    loadConfigFile( xh, f, cm );
  	}
  	// load server-*.xml
  	Vector v = getUserConfigFiles(f);
  	for (Enumeration e = v.elements();
  	     e.hasMoreElements() ; ) {
  	    f = (File)e.nextElement();
  	    loadConfigFile(xh,f,cm);
  	}
      }
  
      // similar with ant's taskdef
      public static void addTag( XmlMapper xh, String tag, String classN) {
  	xh.addRule( tag ,
  		    xh.objectCreate( null, classN ));
  	xh.addRule( tag ,
  		    xh.setProperties());
  	xh.addRule( tag,
  		    xh.addChild( "addInterceptor",
  				 "org.apache.tomcat.core.BaseInterceptor"));
      }
      
  
      // -------------------- File utils --------------------
  
      // get additional files
      public static Vector getUserConfigFiles(File master) {
  	File dir = new File(master.getParent());
  	String[] names = dir.list();
  
  	String masterName=master.getAbsolutePath();
  	
  	String base=FileUtil.getBase( masterName ) + "-";
  	String ext=FileUtil.getExtension( masterName );
  	
  	Vector v = new Vector();
  	for (int i=0; i<names.length; ++i) {
  	    if( names[i].startsWith( base )
  		&& ( ext==null || names[i].endsWith( ext )) ) {
  
  		File found = new File(dir, names[i]);
  		v.addElement(found);
  	    }
  	}
  	return v;
      }
  
      // -------------------- Backward compatibility -------------------- 
  
      // Read old configuration formats
      private void setBackward( XmlMapper xh ) {
  	xh.addRule( "ContextManager/ContextInterceptor",
  		    xh.objectCreate(null, "className"));
  	xh.addRule( "ContextManager/ContextInterceptor",
  		    xh.setProperties() );
  	xh.addRule( "ContextManager/ContextInterceptor",
  		    xh.addChild( "addInterceptor",
  				 "org.apache.tomcat.core.BaseInterceptor"));
  
  	xh.addRule( "ContextManager/RequestInterceptor",
  		    xh.objectCreate(null, "className"));
  	xh.addRule( "ContextManager/RequestInterceptor",
  		    xh.setProperties() );
  	xh.addRule( "ContextManager/RequestInterceptor",
  		    xh.addChild( "addInterceptor",
  				 "org.apache.tomcat.core.BaseInterceptor"));
  
  	// old <connector>
  	xh.addRule( "ContextManager/Connector",
  		    xh.objectCreate(null, "className"));
  	xh.addRule( "ContextManager/Connector",
  		    xh.setParent( "setContextManager",
  				  "org.apache.tomcat.core.ContextManager") );
  	xh.addRule( "ContextManager/Connector",
  		    xh.addChild( "addInterceptor",
  				 "org.apache.tomcat.core.BaseInterceptor"));
  
  	xh.addRule( "ContextManager/Connector/Parameter",
  		    xh.methodSetter("setProperty",2) );
  	xh.addRule( "ContextManager/Connector/Parameter",
  		    xh.methodParam(0, "name") );
  	xh.addRule( "ContextManager/Connector/Parameter",
  		    xh.methodParam(1, "value") );
  
  	// old <Logger>
  	xh.addRule("Server/Logger",
  		   xh.objectCreate("org.apache.tomcat.util.log.QueueLogger"));
  	xh.addRule("Server/Logger", xh.setProperties());
  	xh.addRule("Server/Logger", 
  		   xh.addChild("addLogger",
  			       "org.apache.tomcat.util.log.Logger") );
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/modules/config/ContextXmlReader.java
  
  Index: ContextXmlReader.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  package org.apache.tomcat.modules.config;
  
  import java.beans.*;
  import java.io.*;
  import java.lang.reflect.*;
  import java.util.Hashtable;
  import java.util.*;
  import java.net.*;
  import org.apache.tomcat.util.*;
  import org.apache.tomcat.util.xml.*;
  import org.apache.tomcat.core.*;
  import org.apache.tomcat.modules.server.*;
  import org.apache.tomcat.util.log.*;
  import org.xml.sax.*;
  
  /**
   * 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
      {
  	XmlMapper xh=new XmlMapper();
  	xh.setDebug( debug );
  	// use the same tags for context-local modules 
  	ServerXmlReader.setTagRules( xh );
  	ServerXmlReader.addDefaultTags(cm, xh);
  	setContextRules( xh );
  	setBackward( xh );
  
  	// load the config file(s)
  	File f  = null;
  	if (configFile == null)
  	    configFile=DEFAULT_CONFIG;
  
  	if( configFile.startsWith( "/" ) ) 
  	    f=new File(configFile);
  	else
  	    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();
  	    ServerXmlReader.loadConfigFile(xh,f,cm);
  	}
      }
  
      // -------------------- Xml reading details --------------------
  
      // rules for reading teh 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 <Host>
  	xh.addRule( "Host", xh.setVariable( "current_host", "name"));
  	xh.addRule( "Host", xh.setProperties());
  
  	xh.addRule( "Context", new XmlAction() {
  		public void end( SaxContext ctx) throws Exception {
  		    Context tcCtx=(Context)ctx.currentObject();
  		    String host=(String)ctx.getVariable("current_host");
  		    
  		    if( host!=null && ! "DEFAULT".equals( host )) 
  			    tcCtx.setHost( host );
  		}
  	    });
  
  	xh.addRule( "Context",
  		    xh.addChild("addContext",
  				"org.apache.tomcat.core.Context") );
      }
      
      // -------------------- Backward compatibility -------------------- 
  
      // Read old configuration formats
      private static void setBackward( XmlMapper xh ) {
  	// Configure context interceptors
  	xh.addRule( "Context/Interceptor",
  		    xh.objectCreate(null, "className"));
  	xh.addRule( "Context/Interceptor",
  		    xh.setProperties() );
  	xh.addRule( "Context/Interceptor",
  		    xh.setParent("setContext") );
  	xh.addRule( "Context/Interceptor",
  		    xh.addChild( "addInterceptor",
  				 "org.apache.tomcat.core.BaseInterceptor"));
  	// Old style 
  	xh.addRule( "Context/RequestInterceptor",
  		    xh.objectCreate(null, "className"));
  	xh.addRule( "Context/RequestInterceptor",
  		    xh.setProperties() );
  	xh.addRule( "Context/RequestInterceptor",
  		    xh.setParent("setContext") );
  	xh.addRule( "Context/RequestInterceptor",
  		    xh.addChild( "addInterceptor",
  				 "org.apache.tomcat.core.BaseInterceptor"));
  	xh.addRule("Context/Logger",
  		   xh.objectCreate("org.apache.tomcat.util.log.QueueLogger"));
  	xh.addRule("Context/Logger", xh.setProperties());
  	xh.addRule("Context/Logger", 
  		   xh.addChild("setLogger",
  			       "org.apache.tomcat.util.log.Logger") );
  	
  	xh.addRule("Context/ServletLogger",
  		   xh.objectCreate("org.apache.tomcat.util.log.QueueLogger"));
  	xh.addRule("Context/ServletLogger", xh.setProperties());
  	xh.addRule("Context/ServletLogger", 
  		   xh.addChild("setServletLogger",
  			       "org.apache.tomcat.util.log.Logger") );
      }
  }