You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2003/08/24 12:54:43 UTC
cvs commit: incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config ConfigurationReader.java Configurator.java StringValueParser.java
jdillon 2003/08/24 03:54:43
Modified: modules/twiddle/src/conf twiddle.conf
modules/twiddle/src/java/org/apache/geronimo/twiddle/config
ConfigurationReader.java Configurator.java
StringValueParser.java
Log:
o Hooked up System props to SystemValueParser, will use JEXL later
o Implemented simple globs for config includes
o Starting to use logging
Revision Changes Path
1.2 +5 -1 incubator-geronimo/modules/twiddle/src/conf/twiddle.conf
Index: twiddle.conf
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/conf/twiddle.conf,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- twiddle.conf 24 Aug 2003 09:27:44 -0000 1.1
+++ twiddle.conf 24 Aug 2003 10:54:43 -0000 1.2
@@ -4,6 +4,10 @@
<configuration xmlns="http://geronimo.apache.org/xml/schema/Twiddle/Configuration">
+ <includes>
+ <include>${twiddle.home}/etc/twiddle/*.conf</include>
+ </includes>
+
<commands>
<command name="exit" code="org.apache.geronimo.twiddle.commands.ExitCommand"/>
</commands>
1.6 +33 -1 incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/ConfigurationReader.java
Index: ConfigurationReader.java
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/ConfigurationReader.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ConfigurationReader.java 16 Aug 2003 15:14:12 -0000 1.5
+++ ConfigurationReader.java 24 Aug 2003 10:54:43 -0000 1.6
@@ -65,9 +65,13 @@
import java.net.URL;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.MarshalException;
+import org.apache.geronimo.common.NullArgumentException;
import org.apache.geronimo.common.Strings;
/**
@@ -77,6 +81,8 @@
*/
public class ConfigurationReader
{
+ private static final Log log = LogFactory.getLog(ConfigurationReader.class);
+
/** The Castor unmarshaller used to tranform XML->Objects */
protected Unmarshaller unmarshaller;
@@ -98,6 +104,14 @@
*/
public Configuration read(final URL url) throws Exception
{
+ if (url == null) {
+ throw new NullArgumentException("url");
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Reading: " + url);
+ }
+
return doRead(new BufferedReader(new InputStreamReader(url.openStream())));
}
@@ -111,6 +125,10 @@
*/
public Configuration read(final String urlspec) throws Exception
{
+ if (urlspec == null) {
+ throw new NullArgumentException("urlspec");
+ }
+
return read(Strings.toURL(urlspec));
}
@@ -124,6 +142,14 @@
*/
public Configuration read(final File file) throws Exception
{
+ if (file == null) {
+ throw new NullArgumentException("file");
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Reading: " + file);
+ }
+
return doRead(new BufferedReader(new FileReader(file)));
}
@@ -137,6 +163,10 @@
*/
public Configuration read(final Reader reader) throws Exception
{
+ if (reader == null) {
+ throw new NullArgumentException("reader");
+ }
+
return (Configuration)unmarshaller.unmarshal(reader);
}
@@ -151,6 +181,8 @@
*/
protected Configuration doRead(final Reader reader) throws Exception
{
+ assert reader != null;
+
Configuration config = null;
try {
config = read(reader);
1.6 +76 -4 incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/Configurator.java
Index: Configurator.java
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/Configurator.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Configurator.java 16 Aug 2003 15:14:12 -0000 1.5
+++ Configurator.java 24 Aug 2003 10:54:43 -0000 1.6
@@ -57,6 +57,16 @@
package org.apache.geronimo.twiddle.config;
import java.net.URL;
+import java.net.MalformedURLException;
+
+import java.io.File;
+import java.io.FilenameFilter;
+
+import java.util.List;
+import java.util.LinkedList;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.common.NullArgumentException;
import org.apache.geronimo.common.Strings;
@@ -74,6 +84,8 @@
*/
public class Configurator
{
+ private static final Log log = LogFactory.getLog(Configurator.class);
+
protected StringValueParser valueParser;
protected Twiddle twiddle;
@@ -135,15 +147,75 @@
try {
String value = valueParser.parse(includes[i]);
- URL configURL = Strings.toURL(value);
- Configuration iconfig = reader.read(configURL);
- this.configure(iconfig);
+ URL[] urls = parseGlobURLs(value);
+ for (int j=0; j<urls.length; j++) {
+ Configuration iconfig = reader.read(urls[j]);
+ this.configure(iconfig);
+ }
}
catch (Exception e) {
throw new ConfigurationException("Failed to process include: " + includes[i], e);
}
}
}
+ }
+
+ protected URL[] parseGlobURLs(final String globspec) throws MalformedURLException
+ {
+ assert globspec != null;
+
+ boolean debug = log.isDebugEnabled();
+ if (debug) {
+ log.debug("Parsing glob URLs from spec: " + globspec);
+ }
+
+ URL baseURL = Strings.toURL(globspec);
+ if (!baseURL.getProtocol().equals("file")) {
+ // only can glob on file urls
+ return new URL[] { baseURL };
+ }
+
+ File dir = new File(baseURL.getPath());
+ String glob = dir.getName();
+ dir = dir.getParentFile();
+
+ if (debug) {
+ log.debug("Base dir: " + dir);
+ log.debug("Glob: " + glob);
+ }
+
+ int i = glob.indexOf("*");
+ final String prefix = glob.substring(0, i);
+ final String suffix = glob.substring(i + 1);
+ if (debug) {
+ log.debug("Prefix: " + prefix);
+ log.debug("Suffix: " + suffix);
+ }
+
+ File[] matches = dir.listFiles(new FilenameFilter()
+ {
+ public boolean accept(final File dir, final String name)
+ {
+ if (!name.startsWith(prefix) || !name.endsWith(suffix)) {
+ return false;
+ }
+
+ return true;
+ }
+ });
+
+ List list = new LinkedList();
+ if (matches != null) {
+ for (i=0; i < matches.length; ++i) {
+ list.add(matches[i].toURL());
+ }
+ }
+
+ if (debug) {
+ log.debug("Parsed URLs: " + list);
+ }
+
+ return (URL[])list.toArray(new URL[list.size()]);
}
protected void configureCommands(final CommandsConfig config) throws CommandException
1.5 +31 -4 incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/StringValueParser.java
Index: StringValueParser.java
===================================================================
RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/config/StringValueParser.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- StringValueParser.java 16 Aug 2003 15:14:12 -0000 1.4
+++ StringValueParser.java 24 Aug 2003 10:54:43 -0000 1.5
@@ -69,12 +69,39 @@
{
}
- public String parse(String value)
+ protected String evaluate(final String expr)
{
//
- // TODO: Implement me with JEXL or something...
+ // TODO: Hook up JEXL here
//
- return value;
+ return System.getProperty(expr);
+ }
+
+ public String parse(final String input)
+ {
+ StringBuffer buff = new StringBuffer();
+
+ int cur = 0;
+ int prefixLoc = 0;
+ int suffixLoc = 0;
+
+ while (cur < input.length()) {
+ prefixLoc = input.indexOf("${", cur);
+
+ if (prefixLoc < 0) {
+ break;
+ }
+
+ suffixLoc = input.indexOf("}", prefixLoc);
+ String expr = input.substring(prefixLoc + 2, suffixLoc);
+ buff.append(input.substring(cur, prefixLoc));
+ buff.append(evaluate(expr));
+ cur = suffixLoc + 1;
+ }
+
+ buff.append(input.substring(cur));
+
+ return buff.toString();
}
}