[jboss-svn-commits] JBL Code SVN: r11816 - in labs/jbossesb/workspace/dbevenius/product/core/listeners: tests/src/org/jboss/soa/esb/actions/converters and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu May 10 13:23:16 EDT 2007


Author: beve
Date: 2007-05-10 13:23:16 -0400 (Thu, 10 May 2007)
New Revision: 11816

Modified:
   labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
   labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
Log:
Commit work according to JIRA : JBESB-562
The has to do with the adding of multiple aliases for the XStreamToObject class


Modified: labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java	2007-05-10 17:18:11 UTC (rev 11815)
+++ labs/jbossesb/workspace/dbevenius/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java	2007-05-10 17:23:16 UTC (rev 11816)
@@ -1,6 +1,9 @@
 package org.jboss.soa.esb.actions.converters;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import org.apache.log4j.Logger;
 import org.jboss.soa.esb.ConfigurationException;
@@ -26,6 +29,11 @@
  *     &lt;property name="class-alias" value="Customer" /&gt; &lt;!-- Optional. Class alias used in call to <a href="http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/XStream.html">XStream.alias(String, Class)</a> prior to deserialisation. --&gt;
  *     &lt;property name="incoming-type" value="CustomerProcessor" /&gt; &lt;!-- Required. Class for incoming type used to process the message after  deserialisation. --&gt;
  *     &lt;property name="exclude-package" value="false" /&gt; &lt;!-- Optional. Default "true".  Not applicable if a "class-alias" is specified. --&gt;
+ *     &lt;aliases&gt; &lt;!-- Optional list of extra aliases to add to XStream  --&gt;
+ * 		&lt;alias name="aliasName" class="className" /&gt; 
+ * 		&lt;alias name="aliasName" class="className" /&gt; 
+ * 		...
+ *     &lt;/aliases&gt;
  * &lt;/Action&gt;
  * </pre>
  * <p/>
@@ -35,6 +43,7 @@
  * This can be used with ObjectToXStream
  * 
  * @author danielmarchant
+ * @author Daniel Bevenius
  * @since Version 4.0
  */
 public class XStreamToObject  extends AbstractObjectXStream {
@@ -45,6 +54,7 @@
     private Class incomingType;
     
     // action related variables
+	private Map<String,String> aliases;
    
     /**
      * Public constructor.
@@ -53,6 +63,7 @@
      */
     public XStreamToObject(ConfigTree properties) {
     	this(properties.getName(), properties.attributesAsList());
+    	aliases = getAliases( properties );
     }
     
     /**
@@ -83,6 +94,7 @@
 		XStream xstream = new XStream(new DomDriver());
 		
         xstream.alias(getAlias(incomingType), incomingType);
+        addAliases( aliases, xstream );
         
         Object arg;
 		try {
@@ -90,13 +102,62 @@
 			arg = xstream.fromXML(xml, arg);
    		 	ActionUtils.setTaskObject(message,arg);
 		} catch (InstantiationException e) {
+			e.printStackTrace();
 			throw new ActionProcessingException("Could not invoke for Arg: " + getName(),e );
 		} catch (IllegalAccessException e) {
+			e.printStackTrace();
 			throw new ActionProcessingException("Could not access for Arg: " + getName(),e );
 		} 
 		
 		return message;
 	}
+	
+	/**
+	 * Will extract the alias elements from the passed-in conifgTree 
+	 * 
+	 * @param configTree			the configuration for this class
+	 * 
+	 * @return Map<String,String> 	either an empty map or a map containing the alias name
+	 * 								as its key and the corresponding value is the class to map
+	 * 								it to.	
+	 */
+	protected Map<String,String> getAliases( ConfigTree configTree )
+	{
+		Map<String,String> aliases = new HashMap<String,String>();
+		
+		ConfigTree firstChild = configTree.getFirstChild( "aliases" ); 
+		if ( firstChild != null ) {
+			ConfigTree[] children = firstChild.getChildren( "alias" );
+			for ( ConfigTree alias : children ) {
+				aliases.put( alias.getAttribute( "name" ), alias.getAttribute( "class" ) );
+			}
+		}
+		return aliases;
+	}
 
+	/**
+	 * Added the aliases contained in the passed-in map to the
+	 * passed-in XStream object
+	 * 
+	 * @param aliases	 Map of aliases.
+	 * @throws ActionProcessingException 
+	 */
+	protected void addAliases( Map<String, String> aliases, XStream xstream) throws ActionProcessingException
+	{
+		if ( aliases == null )
+			return;
+		
+		Set<Map.Entry<String,String>> set = aliases.entrySet();
+		for (Map.Entry me : set ) {
+			String className = (String) me.getValue();
+			try {
+				Class clazz = ClassUtil.forName( className, getClass() );
+		        xstream.alias((String)me.getKey(), clazz );
+			} catch (ClassNotFoundException e) {
+				logger.error(e);
+				throw new ActionProcessingException("Could not add alias : " + (String)me.getKey() + ", class : " + className ,e );
+			}
+		}
+	}
 	
 }

Modified: labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java	2007-05-10 17:18:11 UTC (rev 11815)
+++ labs/jbossesb/workspace/dbevenius/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java	2007-05-10 17:23:16 UTC (rev 11816)
@@ -24,19 +24,24 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import junit.framework.TestCase;
 
 import org.jboss.soa.esb.actions.ActionProcessingException;
 import org.jboss.soa.esb.actions.ActionUtils;
 import org.jboss.soa.esb.actions.TestBean;
+import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.helpers.KeyValuePair;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.message.format.MessageFactory;
 
+import com.thoughtworks.xstream.XStream;
+
 /**
  * XStreamToObject unit tests.
  * @author daniel Marchant
+ * @author Daniel Bevenius
  * @since Version 4.0
  */
 public class XStreamToObjectUnitTest extends TestCase {
@@ -108,4 +113,93 @@
         assertEquals("Tom", bean.getName());
         assertEquals("1234", bean.getPhone());
     }
-}
+    
+    public void test_getAliases()
+    {
+    	ConfigTree configTree = getConfigTreeWithAliases();
+        XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+        
+        Map<String, String> aliases = xstreamToObject.getAliases( configTree );
+        
+        assertAliases( aliases );
+    	
+    }
+    
+    public void test_addAliases() throws ActionProcessingException
+    {
+    	ConfigTree configTree = getConfigTreeWithAliases();
+        XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+        
+        Map<String, String> aliases = xstreamToObject.getAliases( configTree );
+        xstreamToObject.addAliases( aliases, new XStream() );
+        
+        assertAliases( aliases );
+    	
+    }
+    
+    public void test_addAliasesNegative() throws ActionProcessingException
+    {
+    	ConfigTree configTree = getConfigTree();
+        XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+        
+        Map<String, String> aliases = xstreamToObject.getAliases( configTree );
+        xstreamToObject.addAliases( aliases, new XStream() );
+        
+    	assertEquals( "Aliases map should have been empty!", 0,  aliases.size() );
+    	
+    }
+    
+    public void test_with_multiple_aliases() throws ActionProcessingException {
+    	
+    	ConfigTree configTree = getConfigTreeWithAliases();
+    	
+        XStreamToObject xstreamToObject = new XStreamToObject( configTree );
+
+        Message oMsg = MessageFactory.getInstance().getMessage();
+        
+        String msg = "<TomsClass><name>Tom</name><phone>1234</phone></TomsClass>";
+        
+        ActionUtils.setTaskObject(oMsg,msg);
+        oMsg = xstreamToObject.process(oMsg);
+       
+        TestBean bean = (TestBean)ActionUtils.getTaskObject(oMsg);
+  
+        assertEquals("Tom", bean.getName());
+        assertEquals("1234", bean.getPhone());
+    }
+    
+    private void assertAliases( Map<String, String> aliases )
+    {
+    	assertNotNull( "Aliases map should not have been null!",  aliases );
+        assertEquals( TestBean.class.getName(), aliases.get( "aliasName1" ) );
+        assertEquals( TestBean.class.getName(), aliases.get( "aliasName2" ) );
+    }
+    
+    private ConfigTree getConfigTree()
+	{
+		ConfigTree configTree = new ConfigTree ( "test");
+    	configTree.setAttribute( "class-alias", "TomsClass");
+    	configTree.setAttribute( "exclude-package", "false");
+    	configTree.setAttribute( "incoming-type", TestBean.class.getName());
+		return configTree;
+	}
+
+	private ConfigTree getConfigTreeWithAliases()
+	{
+		ConfigTree configTree = getConfigTree();
+		
+    	ConfigTree aliases = new ConfigTree( "aliases", configTree);
+    	ConfigTree alias1 = new ConfigTree( "alias", aliases);
+    	alias1.setAttribute( "name", "aliasName1");
+    	alias1.setAttribute( "class", TestBean.class.getName());
+    	
+    	ConfigTree alias2 = new ConfigTree( "alias", aliases);
+    	alias2.setAttribute( "name", "aliasName2");
+    	alias2.setAttribute( "class", TestBean.class.getName());
+    	
+		return configTree;
+	}
+	
+	
+}	
+	
\ No newline at end of file




More information about the jboss-svn-commits mailing list