You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Alex Chaffee <gu...@edamame.stinky.com> on 2000/07/13 13:31:16 UTC

Multiple server.xml config files

I got sick of having to copy-and-paste my contexts into server.xml
every time I unpacked a new distribution.  So I hacked in the
following feature:

On startup, Tomcat.java loads server.xml, then loads all files named
server-*.xml in the same directory.

(If the user specifies a -config foo.xml on the command line, it looks
for foo-*.xml.)

Sample conf/server-purple.xml :

<Server>
   <ContextManager>
        <Context path="/purple" 
                 docBase="d:/dev/projects/web/sites/www.purpletech.com" 
                 debug="0" 
                 reloadable="true" > 
        </Context>
   </ContextManager>
</Server>

It's not pretty, it's not robust, but it works, sort of.  It only
works for -new- objects, like contexts; if you try to reconfigure old
objects, like Loggers, the behavior is undefined.

I'll put the patch in this email.  Is there any interest whatsoever in
checking it into the main source tree?  The only good thing is, if you
don't know about it, it won't hurt you.

 - Alex

-- 
Alex Chaffee                       mailto:alex@jguru.com
jGuru - Java News and FAQs         http://www.jguru.com/alex/
Creator of Gamelan                 http://www.gamelan.com/
Founder of Purple Technology       http://www.purpletech.com/
Curator of Stinky Art Collective   http://www.stinky.com/


Index: src/share/org/apache/tomcat/startup/Tomcat.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/startup/Tomcat.java,v
retrieving revision 1.34
diff -u -r1.34 Tomcat.java
--- src/share/org/apache/tomcat/startup/Tomcat.java	2000/07/11 08:09:31	1.34
+++ src/share/org/apache/tomcat/startup/Tomcat.java	2000/07/13 18:36:29
@@ -196,16 +196,18 @@
 	setConnectorHelper( xh );
 	setLogHelper( xh );
 
+	// load server.xml
 	File f = getConfigFile(cm);
-	log(sm.getString("tomcat.loading") + " " + f);
-	try {
-	    xh.readXml(f,cm);
-	} catch( Exception ex ) {
-	    log( sm.getString("tomcat.fatalconfigerror"), ex );
-	    throw ex;
-	}
-	log(sm.getString("tomcat.loaded") + " " + f);
+	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);
+	}
+	
 	// by now, we should know where the log file is
 	String path = cm.getLogger().getPath();
 	if (path == null)
@@ -236,6 +238,50 @@
 	}
     }
 
+    void loadConfigFile(XmlMapper xh, File f, ContextManager cm) throws Exception {
+	log(sm.getString("tomcat.loading") + " " + f);
+	try {
+	    xh.readXml(f,cm);
+	} catch( Exception ex ) {
+	    log( sm.getString("tomcat.fatalconfigerror"), ex );
+	    throw ex;
+	}
+	log(sm.getString("tomcat.loaded") + " " + f);
+    }
+
+    Vector getUserConfigFiles(File master) {
+	File dir = new File(master.getParent());
+	String[] names = dir.list( new ConfigFilter(master) );
+	Vector v = new Vector(names.length);
+	for (int i=0; i<names.length; ++i) {
+	    File found = new File(dir, names[i]);
+	    v.addElement(found);
+	}
+	return v;
+    }
+
+    class ConfigFilter implements FilenameFilter {
+	String start;
+	String end;
+	public ConfigFilter(File master) {
+	    String name = master.getName();
+	    int dot = name.indexOf(".");
+	    if (dot==-1) return;
+	    start = name.substring(0,dot) + "-";
+	    end = name.substring(dot);
+	}
+	public boolean accept(File dir, String name) {
+	    if (start == null || end == null) return false;
+	    if (name.startsWith(start) &&
+		name.endsWith(end))
+	    {
+		return true;
+	    }
+	    return false;
+	}
+    }
+
+    
     /** This method will generate Server config files that
 	reflect the existing cm settings. It is called
 	at startup, and may be called when a new context is