[jboss-cvs] JBoss Messaging SVN: r3985 - in trunk: src/main/org/jboss/messaging/core/config/impl and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Mar 31 05:19:32 EDT 2008


Author: ataylor
Date: 2008-03-31 05:19:32 -0400 (Mon, 31 Mar 2008)
New Revision: 3985

Modified:
   trunk/src/etc/server/default/deploy/jbm-configuration.xml
   trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java
   trunk/src/main/org/jboss/messaging/core/deployers/impl/XmlDeployer.java
   trunk/src/main/org/jboss/messaging/util/XMLUtil.java
   trunk/tests/src/org/jboss/test/messaging/util/XMLUtilTest.java
Log:
added support for replacing system properties in config files

Modified: trunk/src/etc/server/default/deploy/jbm-configuration.xml
===================================================================
--- trunk/src/etc/server/default/deploy/jbm-configuration.xml	2008-03-28 21:12:11 UTC (rev 3984)
+++ trunk/src/etc/server/default/deploy/jbm-configuration.xml	2008-03-31 09:19:32 UTC (rev 3985)
@@ -49,11 +49,11 @@
       
       <!-- Storage configuration -->
                  
-      <bindings-directory>/tmp/jbm-test/data/bindings</bindings-directory>
+      <bindings-directory>${user.home}/jbm-test/data/bindings</bindings-directory>
       
       <create-bindings-dir>true</create-bindings-dir>
       
-      <journal-directory>/tmp/jbm-test/data/journal</journal-directory>
+      <journal-directory>${user.home}/jbm-test/data/journal</journal-directory>
       
       <create-journal-dir>true</create-journal-dir>
       

Modified: trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java	2008-03-28 21:12:11 UTC (rev 3984)
+++ trunk/src/main/org/jboss/messaging/core/config/impl/FileConfiguration.java	2008-03-31 09:19:32 UTC (rev 3985)
@@ -24,6 +24,8 @@
 import static org.jboss.messaging.core.remoting.TransportType.TCP;
 
 import java.io.Serializable;
+import java.io.Reader;
+import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.ArrayList;
 
@@ -50,9 +52,11 @@
    public void start() throws Exception
    {
       URL url = getClass().getClassLoader().getResource(configurationUrl);
+      Reader reader = new InputStreamReader(url.openStream());
+      String xml = XMLUtil.readerToString(reader);
+      xml = XMLUtil.replaceSystemProps(xml);
+      Element e = XMLUtil.stringToElement(xml);
       
-      Element e = XMLUtil.urlToElement(url);
-      
       messagingServerID = getInteger(e, "server-peer-id", messagingServerID);
       
       securityDomain = getString(e, "security-domain", securityDomain);

Modified: trunk/src/main/org/jboss/messaging/core/deployers/impl/XmlDeployer.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/deployers/impl/XmlDeployer.java	2008-03-28 21:12:11 UTC (rev 3984)
+++ trunk/src/main/org/jboss/messaging/core/deployers/impl/XmlDeployer.java	2008-03-31 09:19:32 UTC (rev 3985)
@@ -30,10 +30,9 @@
 import org.w3c.dom.NodeList;
 
 import java.net.URL;
-import java.util.HashMap;
-import java.util.ArrayList;
-import java.util.Set;
-import java.util.Collection;
+import java.util.*;
+import java.io.InputStreamReader;
+import java.io.Reader;
 
 /**
  * @author <a href="ataylor at redhat.com">Andy Taylor</a>
@@ -97,7 +96,7 @@
 
          }
       }
-      //now check for anything thathas been removed and undeploy
+      //now check for anything that has been removed and undeploy
       if (configuration.get(url) != null)
       {
          Set<String> keys = configuration.get(url).keySet();
@@ -118,19 +117,6 @@
       }
    }
 
-   protected Element getRootElement(URL url)
-           throws Exception
-   {
-      return XMLUtil.urlToElement(url);
-   }
-
-   private boolean hasNodeChanged(URL url, Node child, String name)
-   {
-      String newTextContent = child.getTextContent();
-      String origTextContent = configuration.get(url).get(name).getTextContent();
-      return !newTextContent.equals(origTextContent);
-   }
-
    /**
     * Undeploys a resource that has been removed
     * @param url The Resource that was deleted
@@ -243,4 +229,23 @@
    public abstract void undeploy(Node node)
            throws Exception;
 
+
+
+
+   protected Element getRootElement(URL url)
+           throws Exception
+   {
+      Reader reader = new InputStreamReader(url.openStream());
+      String xml = XMLUtil.readerToString(reader);
+      xml = XMLUtil.replaceSystemProps(xml);
+      return XMLUtil.stringToElement(xml);
+   }
+
+   private boolean hasNodeChanged(URL url, Node child, String name)
+   {
+      String newTextContent = child.getTextContent();
+      String origTextContent = configuration.get(url).get(name).getTextContent();
+      return !newTextContent.equals(origTextContent);
+   }
+
 }

Modified: trunk/src/main/org/jboss/messaging/util/XMLUtil.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/XMLUtil.java	2008-03-28 21:12:11 UTC (rev 3984)
+++ trunk/src/main/org/jboss/messaging/util/XMLUtil.java	2008-03-31 09:19:32 UTC (rev 3985)
@@ -13,6 +13,8 @@
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Properties;
+import java.util.Enumeration;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -48,16 +50,28 @@
       return readerToElement(new InputStreamReader(url.openStream()));
    }
 
+   public static String readerToString(Reader r) throws Exception
+   {
+      //Read into string
+      StringBuffer buff = new StringBuffer();
+      int c;
+      while ((c = r.read()) != -1)
+      {
+         buff.append((char)c);
+      }
+      return buff.toString();
+   }
+
    public static Element readerToElement(Reader r) throws Exception
    {
       //Read into string
       StringBuffer buff = new StringBuffer();
       int c;
-      while ((c = r.read()) != -1)            
+      while ((c = r.read()) != -1)
       {
          buff.append((char)c);
       }
-      
+
       //Quick hardcoded replace, FIXME this is a kludge - use regexp to match properly
       String s = buff.toString();
       s = doReplace(s, "jboss.messaging.groupname", "MessagingPostOffice");
@@ -67,9 +81,9 @@
       s = doReplace(s, "jboss.messaging.controlchanneludpport", "45568");
       s = doReplace(s, "jboss.messaging.ipttl", "2");
       s = doReplace(s, "jboss.messaging.ipttl", "8");
-      
+
       StringReader sreader = new StringReader(s);
-       
+
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder parser = factory.newDocumentBuilder();
       Document doc = parser.parse(new InputSource(sreader));
@@ -351,6 +365,23 @@
       return s;
    }
 
+   public static String replaceSystemProps(String xml)
+   {
+      Properties properties = System.getProperties();
+      Enumeration e = properties.propertyNames();
+      while (e.hasMoreElements())
+      {
+         String key =  (String)e.nextElement();
+         String s = "${" +  key + "}";
+         if(xml.contains(s))
+         {
+            xml = xml.replace(s, properties.getProperty(key));
+         }
+
+      }
+      return xml;
+   }
+
    // Attributes -----------------------------------------------------------------------------------
 
    // Constructors ---------------------------------------------------------------------------------
@@ -395,4 +426,6 @@
 
 
    // Inner classes --------------------------------------------------------------------------------
+
+
 }

Modified: trunk/tests/src/org/jboss/test/messaging/util/XMLUtilTest.java
===================================================================
--- trunk/tests/src/org/jboss/test/messaging/util/XMLUtilTest.java	2008-03-28 21:12:11 UTC (rev 3984)
+++ trunk/tests/src/org/jboss/test/messaging/util/XMLUtilTest.java	2008-03-31 09:19:32 UTC (rev 3985)
@@ -243,6 +243,29 @@
       XMLUtil.assertEquivalent(e, convertedAgain);
    }
 
+   public void testReplaceSystemProperties()
+   {
+      String before = "<deployment>\n" +
+           "   <test name=\"${sysprop1}\">content1</test>\n" +
+           "   <test name=\"test2\">content2</test>\n" +
+           "   <test name=\"test3\">content3</test>\n" +
+           "   <test name=\"test4\">${sysprop2}</test>\n" +
+           "   <test name=\"test5\">content5</test>\n" +
+           "   <test name=\"test6\">content6</test>\n" +
+           "</deployment>";
+      String after = "<deployment>\n" +
+           "   <test name=\"test1\">content1</test>\n" +
+           "   <test name=\"test2\">content2</test>\n" +
+           "   <test name=\"test3\">content3</test>\n" +
+           "   <test name=\"test4\">content4</test>\n" +
+           "   <test name=\"test5\">content5</test>\n" +
+           "   <test name=\"test6\">content6</test>\n" +
+           "</deployment>";
+      System.setProperty("sysprop1", "test1");
+      System.setProperty("sysprop2", "content4");
+      String replaced = XMLUtil.replaceSystemProps(before);
+      assertEquals(after, replaced);
+   }
 
 
 }




More information about the jboss-cvs-commits mailing list