[jbosscache-commits] JBoss Cache SVN: r5497 - in core/trunk/src: test/java/org/jboss/cache/config and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Wed Apr 2 18:15:14 EDT 2008


Author: bstansberry at jboss.com
Date: 2008-04-02 18:15:14 -0400 (Wed, 02 Apr 2008)
New Revision: 5497

Added:
   core/trunk/src/test/java/org/jboss/cache/config/StringPropertyReplacementTest.java
   core/trunk/src/test/resources/META-INF/conf-test/string-property-replaced-service.xml
Modified:
   core/trunk/src/main/java/org/jboss/cache/xml/XmlHelper.java
Log:
[JBCACHE-1218] Add system property substitution in config file parsing

Modified: core/trunk/src/main/java/org/jboss/cache/xml/XmlHelper.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/xml/XmlHelper.java	2008-04-02 20:04:30 UTC (rev 5496)
+++ core/trunk/src/main/java/org/jboss/cache/xml/XmlHelper.java	2008-04-02 22:15:14 UTC (rev 5497)
@@ -8,6 +8,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.jboss.util.StringPropertyReplacer;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -139,7 +140,8 @@
             continue;
 
          Element element = (Element) node;
-         return element.getAttribute(attributeName);
+         String value = element.getAttribute(attributeName);
+         return value == null ? null : StringPropertyReplacer.replaceProperties(value);
 
       }
       return null;
@@ -196,7 +198,7 @@
          Node n = nl.item(i);
          if (n instanceof Text)
          {
-            attributeText += ((Text) n).getData();
+            attributeText += StringPropertyReplacer.replaceProperties(((Text) n).getData());
          }
       } // end of for ()
       if (trim)
@@ -227,7 +229,7 @@
             String value = node2.getNodeValue();
             if (value == null)
                return "";
-            return value.trim();
+            return StringPropertyReplacer.replaceProperties(value.trim());
          }
          else
          {

Added: core/trunk/src/test/java/org/jboss/cache/config/StringPropertyReplacementTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/config/StringPropertyReplacementTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/config/StringPropertyReplacementTest.java	2008-04-02 22:15:14 UTC (rev 5497)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.cache.config;
+
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertTrue;
+
+import org.jboss.cache.config.BuddyReplicationConfig.BuddyLocatorConfig;
+import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
+import org.jboss.cache.config.Configuration.CacheMode;
+import org.jboss.cache.config.Configuration.NodeLockingScheme;
+import org.jboss.cache.eviction.LRUConfiguration;
+import org.jboss.cache.eviction.LRUPolicy;
+import org.jboss.cache.factories.XmlConfigurationParser;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Tests that string property replacement works properly when parsing
+ * a config file.  JBCACHE-1218
+ * 
+ * @author Brian Stansberry
+ */
+ at Test(groups = {"functional"})
+public class StringPropertyReplacementTest
+{
+   public static final String STRING_REPLACED_FILE = "META-INF/conf-test/string-property-replaced-service.xml";
+   
+   private static final String PROP_BASE = "test.property.";
+   private static final String CACHE_MODE_PROP = PROP_BASE + "CacheMode";
+   private static final String SYNC_COMMIT_PROP = PROP_BASE + "SyncCommitPhase";
+   private static final String NUM_BUDDIES_PROP = PROP_BASE + "BuddyReplicationConfig.numBuddies";
+   private static final String MAX_NODES_PROP = PROP_BASE + "EvictionPolicyConfig.maxNodes";
+   private static final String BUDDY_POOL_PROP = PROP_BASE + "BuddyReplicationConfig.buddyPoolName";
+   
+   private String cacheMode;
+   private String numBuddies;
+   private String syncCommitPhase;
+   private String maxNodes;
+   private String buddyPoolName;
+   
+   @BeforeMethod(alwaysRun = true)
+   public void setUp() throws Exception
+   {
+      cacheMode = System.getProperty(CACHE_MODE_PROP);
+      numBuddies = System.getProperty(NUM_BUDDIES_PROP);
+      syncCommitPhase = System.getProperty(SYNC_COMMIT_PROP);
+      maxNodes = System.getProperty(MAX_NODES_PROP);
+      buddyPoolName = System.getProperty(BUDDY_POOL_PROP);
+   }
+   
+   @AfterMethod(alwaysRun = true)
+   public void tearDown() throws Exception
+   {
+      if (cacheMode == null)
+         System.clearProperty(CACHE_MODE_PROP);
+      else
+         System.setProperty(CACHE_MODE_PROP, cacheMode);
+
+      if (numBuddies == null)
+         System.clearProperty(NUM_BUDDIES_PROP);
+      else
+         System.setProperty(NUM_BUDDIES_PROP, numBuddies);
+
+      if (syncCommitPhase == null)
+         System.clearProperty(SYNC_COMMIT_PROP);
+      else
+         System.setProperty(SYNC_COMMIT_PROP, syncCommitPhase);
+
+      if (maxNodes == null)
+         System.clearProperty(MAX_NODES_PROP);
+      else
+         System.setProperty(MAX_NODES_PROP, maxNodes);
+
+      if (buddyPoolName == null)
+         System.clearProperty(BUDDY_POOL_PROP);
+      else
+         System.setProperty(BUDDY_POOL_PROP, buddyPoolName);      
+   }
+   
+   public void testStringPropertyReplacement() throws Exception
+   {
+      System.setProperty(CACHE_MODE_PROP, "REPL_SYNC");
+      System.setProperty(NUM_BUDDIES_PROP, "3");
+      System.setProperty(SYNC_COMMIT_PROP, "false");
+      System.setProperty(MAX_NODES_PROP, "1000");
+      System.setProperty(BUDDY_POOL_PROP, "replaced");
+      
+      Configuration cfg = new XmlConfigurationParser().parseFile(STRING_REPLACED_FILE);
+      
+      assertEquals(NodeLockingScheme.OPTIMISTIC, cfg.getNodeLockingScheme());
+      assertEquals(CacheMode.REPL_SYNC, cfg.getCacheMode());
+      assertFalse(cfg.isSyncCommitPhase());
+      assertTrue(cfg.isSyncRollbackPhase());
+      assertEquals(15000, cfg.getLockAcquisitionTimeout());
+      String clusterCfg = cfg.getClusterConfig();
+      assertTrue(clusterCfg == null || clusterCfg.length() == 0);
+      
+      EvictionConfig ec = cfg.getEvictionConfig();
+      assertEquals(LRUPolicy.class.getName(), ec.getDefaultEvictionPolicyClass());
+      EvictionRegionConfig erc = ec.getEvictionRegionConfigs().get(0);
+      LRUConfiguration epc = (LRUConfiguration) erc.getEvictionPolicyConfig();
+      assertEquals(1000, epc.getMaxNodes());
+      
+      CacheLoaderConfig clc = cfg.getCacheLoaderConfig();
+      IndividualCacheLoaderConfig iclc = clc.getFirstCacheLoaderConfig();
+      assertEquals(System.getProperty("java.io.tmpdir"), iclc.getProperties().get("location"));
+      
+      BuddyReplicationConfig brc = cfg.getBuddyReplicationConfig();
+      assertTrue(brc.isEnabled());
+      assertEquals("replaced", brc.getBuddyPoolName());
+      BuddyLocatorConfig blc = brc.getBuddyLocatorConfig();
+      assertEquals("3", blc.getBuddyLocatorProperties().get("numBuddies"));
+   }
+
+}

Added: core/trunk/src/test/resources/META-INF/conf-test/string-property-replaced-service.xml
===================================================================
--- core/trunk/src/test/resources/META-INF/conf-test/string-property-replaced-service.xml	                        (rev 0)
+++ core/trunk/src/test/resources/META-INF/conf-test/string-property-replaced-service.xml	2008-04-02 22:15:14 UTC (rev 5497)
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- ===================================================================== -->
+<!--                                                                       -->
+<!--  Sample Service Configuration to test string property replacement     -->
+<!--                                                                       -->
+<!-- ===================================================================== -->
+
+<server>
+
+   <classpath codebase="./lib" archives="jboss-cache.jar, jgroups.jar"/>
+
+   <mbean code="org.jboss.cache.jmx.CacheJmxWrapper"
+          name="jboss.cache:service=testTreeCache">
+
+      <depends>jboss:service=Naming</depends>
+      <depends>jboss:service=TransactionManager</depends>
+    
+
+        <!-- Node locking scheme -->
+        <attribute name="NodeLockingScheme">${test.property.NodeLockingScheme:OPTIMISTIC}</attribute>
+
+        <attribute name="CacheMode">${test.property.CacheMode}</attribute>
+        
+        <attribute name="SyncCommitPhase">${test.property.SyncCommitPhase:true}</attribute>
+        <attribute name="SyncRollbackPhase">true</attribute>
+
+        <attribute name="ClusterName">optimistic-entity</attribute>
+        
+        <attribute name="MultiplexerStack">udp-sync</attribute>
+
+        <!-- Whether or not to fetch state on joining a cluster. -->
+        <attribute name="FetchInMemoryState">false</attribute>
+
+        <attribute name="StateRetrievalTimeout">20000</attribute>
+
+        <attribute name="SyncReplTimeout">20000</attribute>
+
+        <attribute name="LockAcquisitionTimeout">${test.property.LockAcquisitionTimeout:15000}</attribute>
+
+        <attribute name="UseRegionBasedMarshalling">true</attribute>
+        <attribute name="InactiveOnStartup">true</attribute>
+      
+      <!--  Make sure an empty element doesn't blow us up -->
+      <attribute name="ClusterConfig"></attribute>
+
+      <!--  Specific eviction policy configurations. This is LRU -->
+      <attribute name="EvictionPolicyConfig">
+        <config>
+          <attribute name="wakeUpIntervalSeconds">5</attribute>
+          <attribute name="policyClass">${test.property.EvictionPolicyConfig.policyClass:org.jboss.cache.eviction.LRUPolicy}</attribute>
+          <region name="/_default_">
+            <attribute name="maxNodes">${test.property.EvictionPolicyConfig.maxNodes:5000}</attribute>
+            <attribute name="timeToLiveSeconds">1000</attribute>
+          </region>
+        </config>
+     </attribute>
+
+      <attribute name="CacheLoaderConfiguration">
+         <config>
+            <passivation>true</passivation>
+            <preload>/</preload>
+            <shared>false</shared>
+
+            <!-- we can now have multiple cache loaders, which get chained -->
+            <cacheloader>
+               <class>org.jboss.cache.loader.FileCacheLoader</class>
+               <properties>
+                  location=${test.property.CacheLoaderConfiguration.location,java.io.tmpdir:/tmp}
+               </properties>
+               <async>false</async>
+               <fetchPersistentState>true</fetchPersistentState>
+               <ignoreModifications>false</ignoreModifications>
+            </cacheloader>
+
+         </config>
+      </attribute>      
+      
+      <attribute name="BuddyReplicationConfig">
+         <config>
+            <buddyReplicationEnabled>${test.property.BuddyReplicationConfig.enabled:true}</buddyReplicationEnabled>
+            <buddyLocatorClass>org.jboss.cache.buddyreplication.NextMemberBuddyLocator</buddyLocatorClass>
+            <!-- NOTE: we deliberately put the property being replaced on the 2nd line to ensure that's handled -->
+            <buddyLocatorProperties>
+               ignoreColocatedBuddies = true
+               numBuddies = ${test.property.BuddyReplicationConfig.numBuddies:1}
+            </buddyLocatorProperties>
+
+            <buddyPoolName>${test.property.BuddyReplicationConfig.buddyPoolName:default}</buddyPoolName>
+            <buddyCommunicationTimeout>2000</buddyCommunicationTimeout>
+
+            <autoDataGravitation>false</autoDataGravitation>
+            <dataGravitationRemoveOnFind>true</dataGravitationRemoveOnFind>
+            <dataGravitationSearchBackupTrees>true</dataGravitationSearchBackupTrees>
+
+         </config>
+      </attribute>
+   </mbean>
+
+
+</server>




More information about the jbosscache-commits mailing list