[jboss-svn-commits] JBL Code SVN: r22321 - in labs/jbossesb/workspace/skeagh/commons/src: main/java/org/jboss/esb/properties and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 2 09:24:15 EDT 2008


Author: tfennelly
Date: 2008-09-02 09:24:15 -0400 (Tue, 02 Sep 2008)
New Revision: 22321

Added:
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/ApplicationProperties.java
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/PropertiesPropertyTokenReplacer.java
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/StreamTokenReplacer.java
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/SystemPropertyTokenReplacer.java
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/TokenReplacer.java
   labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/package.html
   labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/
   labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/PropertyReplacementTest.java
   labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.expected.xml
   labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.properties
   labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.xml
Log:
Added ApplicationProperties and Token replacement code.

Added: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/ApplicationProperties.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/ApplicationProperties.java	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/ApplicationProperties.java	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.properties;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
+
+/**
+ * ApplicationProperties class.
+ * <p/>
+ * Provides an application with a single point of access for all configuration
+ * properties through a {@link Properties} implementation.  This means that an
+ * application doesn't need to worry
+ * about where an actual configuration property value is really coming from.
+ * The applications just loads it's properties config using
+ * this class and asks it for all config properties.  The actual value could
+ * be coming from any number of places e.g. {@link java.lang.System java.lang.System},
+ * a database etc.
+ * <p/>
+ * <h3>Installed TokenReplacers</h3>
+ * The {@link TokenReplacer Default Token Replacer} used by this class is the
+ * {@link PropertiesPropertyTokenReplacer}.  This means that if the TokenReplacer lookup fails (see {@link TokenReplacer}),
+ * the replacement values will come from the {@link Properties } instance itself.  See example below.
+ * <p/>
+ * The {@link org.jboss.esb.properties.SystemPropertyTokenReplacer} is installed under the
+ * TokenReplacer ID of "env".  This meaning that properties can be injected from the System using
+ * ${<b>env</b>.XXX} type syntax.
+ * <p/>
+ * Additional/Custom TokenReplacers can be registered through the {@link #register(TokenReplacer)} method.
+ * <p/>
+ * <h3>Example .properties file</h3>
+ * <pre style="background-color: RGB(234,234,234)">
+ * # Replace ${env.HOST_NAME} with the "HOST_NAME" environment variable, and ${env.HOST_PORT} with the
+ * # "HOST_PORT" environment variable.
+ * spring.config=http://${{@link SystemPropertyTokenReplacer env}.HOST_NAME}:${{@link SystemPropertyTokenReplacer env}.HOST_PORT}/spring-config.xml
+ * <p/>
+ * provider.jndi.protocol=t3
+ * <p/>
+ * # Replace ${provider.jndi.protocol} with the value of the "provider.jndi.protocol" variable defined
+ * # above i.e. use the Default Token Replacer.
+ * XService.providerURL=${provider.jndi.protocol}://xservicehost:xserviceport</pre>
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class ApplicationProperties extends Properties
+{
+
+    /**
+     * List of token replacers for this instance.
+     */
+    private List<TokenReplacer> tokenReplacers = new ArrayList<TokenReplacer>();
+
+    /**
+     * Public default constructor.
+     */
+    public ApplicationProperties()
+    {
+        register(new PropertiesPropertyTokenReplacer(this));
+        register(new SystemPropertyTokenReplacer());
+    }
+
+    /**
+     * Register a TokenReplacer with the supplied <code>id</code>.  The <code>id</code>
+     * will be matched against the 1st "word" token in the token replacement
+     * processes.  See {@link TokenReplacer#PATTERN_EXPRESSION}.
+     *
+     * @param replacer The TokenReplacer to be used to get the replacement tokens.
+     */
+    public final void register(final TokenReplacer replacer)
+    {
+        // Delegating param checking to Hashtable
+        tokenReplacers.add(replacer);
+    }
+
+    /**
+     * Load the properties from the supplied input stream.
+     * <p/>
+     * Performs pattern replacement on the properties.
+     *
+     * @param propertiesStream The property stream.
+     * @throws IOException Error reading stream.
+     */
+    public final synchronized void load(final InputStream propertiesStream) throws IOException
+    {
+        super.load(propertiesStream);
+
+        // Iterate over the property values and perform PATTERN replacements.
+        Iterator properties = entrySet().iterator();
+        while (properties.hasNext())
+        {
+            Map.Entry property = (Map.Entry) properties.next();
+            String value = (String) property.getValue();
+
+            // Perform replacements and reset the value to new value.
+            value = TokenReplacer.Replacer.replaceTokens(value, tokenReplacers);
+            property.setValue(value);
+        }
+    }
+}


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/ApplicationProperties.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/PropertiesPropertyTokenReplacer.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/PropertiesPropertyTokenReplacer.java	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/PropertiesPropertyTokenReplacer.java	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,74 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.properties;
+
+import java.util.Properties;
+
+/**
+ * {@link java.util.Properties} property TokenReplacer.
+ * <p/>
+ * Uses the {@link java.util.Properties} class for replacement tokens.
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class PropertiesPropertyTokenReplacer implements TokenReplacer
+{
+
+    /**
+     * Properties lookup for replacement tokens.
+     */
+    private Properties properties;
+
+    /**
+     * Public constructor.
+     *
+     * @param properties Properties lookup for replacement tokens.
+     */
+    public PropertiesPropertyTokenReplacer(final Properties properties)
+    {
+        if (properties == null)
+        {
+            throw new IllegalArgumentException("null 'properties' arg in constructor call.");
+        }
+        this.properties = properties;
+    }
+
+    /**
+     * Get the identifier for the TokenReplacer.
+     *
+     * @return The TokenReplacer's identifier..
+     */
+    public final String getId()
+    {
+        return "local";
+    }
+
+    /**
+     * Get the replacement value for the supplied token.
+     *
+     * @param token The name of the token whose replacement is required.
+     * @return The replacement value for the supplied token, or whatever the implemention
+     *         deems appropriate if there is no replacement.
+     */
+    public final String getReplacement(final String token)
+    {
+        return properties.getProperty(token);
+    }
+}


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/PropertiesPropertyTokenReplacer.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/StreamTokenReplacer.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/StreamTokenReplacer.java	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/StreamTokenReplacer.java	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.properties;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Stream token replacer.
+ * <p/>
+ * Works in a similar fashion to the {@link org.jboss.esb.properties.ApplicationProperties}
+ * class, accept it provides stream replacement functionality i.e. the caller supplies input
+ * and output streams and this class will perform token replacements for the input going to
+ * the output.
+ * <p/>
+ * <h3>Installed TokenReplacers</h3>
+ * The {@link TokenReplacer Default Token Replacer} used by this class is the
+ * {@link org.jboss.esb.properties.SystemPropertyTokenReplacer}.  This means that if the TokenReplacer
+ * lookup fails (see {@link TokenReplacer}), the token replacement values will come from the
+ * System properties.
+ * <p/>
+ * Additional/Custom TokenReplacers can be registered through the {@link #register(TokenReplacer)} method.
+ * <h3>Example Input Stream/File</h3>
+ * In this example, both "HOST.NAME" and "PORT.NUM" will come from the System properties.  "${env.HOST.NAME}"
+ * is just the explicit equivalent of "${HOST.NAME}".  This is because the System TokenReplacer ("env") is the
+ * default (see above).
+ * <pre style="background-color: RGB(234,234,234)">
+ * &lt;config&gt;
+ * &lt;entry name="index.system.url"&gt;http://${env.HOST.NAME}:${PORT.NUM}/myapp/index.html&lt;/entry&gt;
+ * &lt;entry name="HOST_NAME"&gt;localhost&lt;/entry&gt;
+ * &lt;/config&gt;
+ * </pre>
+ * <h3>Example Code</h3>
+ * <pre style="background-color: RGB(234,234,234)">
+ * StreamTokenReplacer tokenReplacer = new StreamTokenReplacer();
+ * <p/>
+ * tokenReplacer.{@link #replace(java.io.Reader, java.io.Writer) replace}(myInputConfigStream, myOutputConfigStream);
+ * </pre>
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ * @see org.jboss.esb.properties.ApplicationProperties
+ */
+public class StreamTokenReplacer
+{
+
+    /**
+     * List of token replacers for this instance.
+     */
+    private List<TokenReplacer> tokenReplacers = new ArrayList<TokenReplacer>();
+
+    /**
+     * Public default constructor.
+     */
+    public StreamTokenReplacer()
+    {
+        register(new SystemPropertyTokenReplacer());
+    }
+
+    /**
+     * Register a TokenReplacer with the supplied <code>id</code>.  The <code>id</code>
+     * will be matched against the 1st "word" token in the token replacement
+     * processes.  See {@link TokenReplacer#PATTERN_EXPRESSION}.
+     *
+     * @param replacer The TokenReplacer to be used to get the replacement tokens.
+     */
+    public final void register(final TokenReplacer replacer)
+    {
+        // Delegating param checking to Hashtable
+        tokenReplacers.add(replacer);
+    }
+
+    /**
+     * Perform token replace on the input stream, outputting to the
+     * output stream.
+     *
+     * @param input  The input stream.
+     * @param output The output stream.
+     * @throws IOException Error reading or writing streams.
+     */
+    public final void replace(final Reader input, final Writer output) throws IOException
+    {
+        BufferedReader inputReader = new BufferedReader(input);
+        String line = inputReader.readLine();
+
+        while (line != null)
+        {
+            output.write(TokenReplacer.Replacer.replaceTokens(line, tokenReplacers));
+            output.write("\n");
+            line = inputReader.readLine();
+        }
+    }
+}


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/StreamTokenReplacer.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/SystemPropertyTokenReplacer.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/SystemPropertyTokenReplacer.java	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/SystemPropertyTokenReplacer.java	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.properties;
+
+/**
+ * System property TokenReplacer.
+ * <p/>
+ * Uses {@link java.lang.System#getProperty(java.lang.String)} to get replacement
+ * tokens.
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class SystemPropertyTokenReplacer implements TokenReplacer
+{
+
+    /**
+     * Get the identifier for the TokenReplacer.
+     *
+     * @return The TokenReplacer's identifier..
+     */
+    public final String getId()
+    {
+        return "env";
+    }
+
+    /**
+     * Get the replacement value for the supplied token.
+     *
+     * @param token The name of the token whose replacement is required.
+     * @return The replacement value for the supplied token, or whatever the implemention
+     *         deems appropriate if there is no replacement.
+     */
+    public final String getReplacement(final String token)
+    {
+        return System.getProperty(token);
+    }
+}


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/SystemPropertyTokenReplacer.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/TokenReplacer.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/TokenReplacer.java	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/TokenReplacer.java	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,170 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.properties;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Token Replacer interface.
+ * <p/>
+ * Implementations should use the {@link Replacer} class.  This class matches
+ * <b>${['X'.]'X'}</b> tokens where 'X' is one or more "word" characters.
+ * <p/>
+ * Word characters are defined as any of:
+ * <ul>
+ * <li>'a' to 'z'</li>
+ * <li>'A' to 'Z'</li>
+ * <li>'_'</li>
+ * <li>'0' to '9'</li>
+ * </ul>
+ * So the overall pattern matches "${", followed by 1+ "word" characters and then optionally
+ * followed by the '.' character plus 1+ "word" characters, followed by the '}' character.
+ * <p/>
+ * If there are 2+ sequences of word characters separated by ".",
+ * then the first sequence of word characters is used to lookup the token replacer to be used e.g.
+ * ${env.XXX} means "replace with the <i>XXX</i> property from the env (System)".
+ * <h3>Default Token Replacer</h3>
+ * If the lookup of the {@link TokenReplacer} (see above) fails, the 1st TokenReplacer in the list
+ * supplied in the call to {@link Replacer#replaceTokens(String, java.util.List)} is used.
+ * We call this {@link TokenReplacer} the "Default Token Replacer".
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public interface TokenReplacer
+{
+
+    /**
+     * Regular expression replacement token pattern.
+     */
+    String PATTERN_EXPRESSION = "\\$\\{[.\\w]+\\}";
+
+    /**
+     * Get the identifier for the TokenReplacer.
+     *
+     * @return The TokenReplacer's identifier..
+     */
+    String getId();
+
+    /**
+     * Get the replacement value for the supplied token.
+     *
+     * @param token The name of the token whose replacement is required.
+     * @return The replacement value for the supplied token, or whatever the implemention
+     *         deems appropriate if there is no replacement.
+     */
+    String getReplacement(String token);
+
+    /**
+     * Replacer Class.
+     */
+    public final class Replacer
+    {
+        /**
+         * Private default constructor.
+         */
+        private Replacer()
+        {
+        }
+
+        /**
+         * Static compiled Pattern instance.
+         */
+        private static final Pattern PATTERN_MATCHER = Pattern.compile(PATTERN_EXPRESSION);
+
+        /**
+         * Replace {@link TokenReplacer#PATTERN_EXPRESSION} tokens in the provided String.
+         *
+         * @param string The string on which the operation is to be performed.
+         * @param tokenReplacers The list of token replacers to use.
+         * @return The resultant string after token replacement as happened.
+         */
+        public static String replaceTokens(final String string, final List<TokenReplacer> tokenReplacers)
+        {
+            Matcher m = PATTERN_MATCHER.matcher(string);
+            int lastPos = 0;
+            StringBuffer rebuild = new StringBuffer();
+            TokenReplacer defaultReplacer = tokenReplacers.get(0);
+
+            while (m.find())
+            {
+                int start = m.start();
+                int end = m.end();
+                String fullToken = string.substring((start + 2), (end - 1));
+                TokenReplacer replacer = null;
+                int wordSeperatorIndex = fullToken.indexOf('.');
+
+                if (wordSeperatorIndex != -1)
+                {
+                    String firstWord = fullToken.substring(0, wordSeperatorIndex);
+                    replacer = findReplacer(firstWord, tokenReplacers);
+                }
+
+                if (replacer != null)
+                {
+                    String secondWord = fullToken.substring(wordSeperatorIndex + 1);
+                    rebuild.append(string.substring(lastPos, start));
+                    rebuild.append(replacer.getReplacement(secondWord));
+                } else
+                {
+                    String defaultReplacement = defaultReplacer.getReplacement(fullToken);
+
+                    if (defaultReplacement != null)
+                    {
+                        rebuild.append(string.substring(lastPos, start));
+                        rebuild.append(defaultReplacement);
+                    } else
+                    {
+                        // Leave unchanged.
+                        rebuild.append(string.substring(lastPos, start));
+                        rebuild.append("${").append(fullToken).append('}');
+                    }
+                }
+
+                lastPos = end;
+            }
+            rebuild.append(string.substring(lastPos));
+
+            return rebuild.toString();
+        }
+
+        /**
+         * Find the specified TokenReplacer instance from the list of TokenReplacer
+         * instances.
+         *
+         * @param id             The TokenReplacer ID.
+         * @param tokenReplacers The list of TokenReplacers.
+         * @return The TokenReplacer instance, or null if not found.
+         */
+        private static TokenReplacer findReplacer(final String id, final List<TokenReplacer> tokenReplacers)
+        {
+            for (TokenReplacer tokenReplacer : tokenReplacers)
+            {
+                if (tokenReplacer.getId().equals(id))
+                {
+                    return tokenReplacer;
+                }
+            }
+
+            return null;
+        }
+    }
+}


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/TokenReplacer.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/package.html
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/package.html	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/main/java/org/jboss/esb/properties/package.html	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,17 @@
+<html>
+    <head></head>
+    <body>
+		Application properties utility classes.
+
+		<h2>Package Specification</h2>
+        Performs ${} type character token replacement.
+        <p/>
+        See:
+        <ol>
+            <li>{@link org.jboss.esb.properties.TokenReplacer}</li>
+            <li>{@link org.jboss.esb.properties.ApplicationProperties}: Use this class in place of {@link java.util.Properties}.</li>
+            <li>{@link org.jboss.esb.properties.StreamTokenReplacer}: Use this class for performing Input to Output Stream token replacement.</li>
+        </ol>
+
+    </body>
+</html>
\ No newline at end of file

Added: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/PropertyReplacementTest.java
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/PropertyReplacementTest.java	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/PropertyReplacementTest.java	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.properties;
+
+import java.io.*;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public class PropertyReplacementTest extends TestCase {
+	
+	public void test_ApplicationProperties() {
+		ApplicationProperties properties = new ApplicationProperties();
+		
+		System.setProperty("HOST", "www.x.com");
+		System.setProperty("PORT.NUM", "7001");
+		try {
+			properties.load(getClass().getResourceAsStream("props.properties"));
+		} catch (IOException e) {
+			e.printStackTrace();
+			fail("Unexpected IOException.");
+		}
+		
+		assertEquals("http://www.x.com:7001/myapp/index.html", properties.getProperty("index.system.url"));
+		assertEquals("http://localhost:8080/myapp/index.html", properties.getProperty("index.prop.url"));
+		assertEquals("localhost", properties.getProperty("HOST_NAME"));
+		assertEquals("8080", properties.getProperty("HOST_PORT"));
+		assertEquals("xxxx${x.y}", properties.getProperty("someotherprop"));		
+	}
+
+    public void test_StreamTokeReplacer() throws IOException {
+        StreamTokenReplacer replacer = new StreamTokenReplacer();
+
+        System.setProperty("HOST", "www.x.com");
+        System.setProperty("PORT", "7001");
+
+        StringReader reader = new StringReader(readStream(getClass().getResourceAsStream("props.xml")));
+        StringWriter writer = new StringWriter();
+
+        replacer.replace(reader, writer);
+        String expected = readStream(getClass().getResourceAsStream("props.expected.xml"));
+        String actual = writer.toString();
+        assertEquals(clean(expected), clean(actual));
+    }
+
+    private String clean(String s) {
+        StringBuffer stringOut = new StringBuffer();
+        char[] stringIn = s.toCharArray();
+
+        for (int i = 0; i < stringIn.length; i++) {
+            char c = stringIn[i];
+
+            if(!Character.isWhitespace(c)) {
+                stringOut.append(c);
+            }
+        }
+
+        return stringOut.toString();
+    }
+
+    public static String readStream(InputStream stream) {
+		if(stream == null) {
+			throw new IllegalArgumentException("null 'stream' arg passed in method call.");
+		}
+
+		ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
+		byte[] buffer = new byte[256];
+		int readCount = 0;
+
+		try {
+			while((readCount = stream.read(buffer)) != -1) {
+				outBuffer.write(buffer, 0, readCount);
+			}
+		} catch (IOException e) {
+			throw new IllegalStateException("Error reading stream.", e);
+		}
+
+		return outBuffer.toString();
+	}
+}
+
+
+
+
+


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/PropertyReplacementTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.expected.xml
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.expected.xml	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.expected.xml	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,4 @@
+<config>
+    <entry name="index.system.url">http://www.x.com:7001/myapp/index.html</entry>
+    <entry name="HOST_NAME">localhost</entry>
+</config>


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.expected.xml
___________________________________________________________________
Name: svn:mime-type
   + text/xml
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.properties
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.properties	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.properties	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,5 @@
+index.system.url=http://${env.HOST}:${env.PORT.NUM}/myapp/index.html
+HOST_NAME=localhost
+index.prop.url=http://${local.HOST_NAME}:${HOST_PORT}/myapp/index.html
+HOST_PORT=8080
+someotherprop=xxxx${x.y}
\ No newline at end of file


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.properties
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:eol-style
   + native

Added: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.xml
===================================================================
--- labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.xml	                        (rev 0)
+++ labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.xml	2008-09-02 13:24:15 UTC (rev 22321)
@@ -0,0 +1,4 @@
+<config>
+    <entry name="index.system.url">http://${env.HOST}:${PORT}/myapp/index.html</entry>
+    <entry name="HOST_NAME">localhost</entry>
+</config>
\ No newline at end of file


Property changes on: labs/jbossesb/workspace/skeagh/commons/src/test/java/org/jboss/esb/properties/props.xml
___________________________________________________________________
Name: svn:mime-type
   + text/xml
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list