continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tryg...@apache.org
Subject svn commit: r188775 - in /maven/continuum/trunk/continuum-xmlrpc/src: main/java/org/apache/maven/continuum/xmlrpc/DefaultXmlRpcHelper.java main/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelper.java test/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelperTest.java
Date Tue, 07 Jun 2005 14:06:08 GMT
Author: trygvis
Date: Tue Jun  7 07:06:07 2005
New Revision: 188775

URL: http://svn.apache.org/viewcvs?rev=188775&view=rev
Log:
o Adding support for populating a bean from a Hashtable.

Modified:
    maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/DefaultXmlRpcHelper.java
    maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelper.java
    maven/continuum/trunk/continuum-xmlrpc/src/test/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelperTest.java

Modified: maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/DefaultXmlRpcHelper.java
URL: http://svn.apache.org/viewcvs/maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/DefaultXmlRpcHelper.java?rev=188775&r1=188774&r2=188775&view=diff
==============================================================================
--- maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/DefaultXmlRpcHelper.java
(original)
+++ maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/DefaultXmlRpcHelper.java
Tue Jun  7 07:06:07 2005
@@ -16,14 +16,17 @@
  * limitations under the License.
  */
 
+import java.beans.IntrospectionException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
 
@@ -32,7 +35,7 @@
 
 /**
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
- * @version $Id: DefaultXmlRpcHelper.java,v 1.1.1.1 2005/03/29 20:42:10 trygvis Exp $
+ * @version $Id$
  */
 public class DefaultXmlRpcHelper
     extends AbstractLogEnabled
@@ -182,5 +185,109 @@
         }
 
         return vector;
+    }
+
+    public void hashtableToObject( Hashtable hashtable, Object target )
+        throws IntrospectionException, IllegalAccessException, InvocationTargetException
+    {
+        for ( Iterator it = hashtable.entrySet().iterator(); it.hasNext(); )
+        {
+            Map.Entry entry = (Map.Entry) it.next();
+
+            String key = (String) entry.getKey();
+
+            Object value = entry.getValue();
+
+            if ( key == null || value == null )
+            {
+                continue;
+            }
+
+            // ----------------------------------------------------------------------
+            // Convert the key to a setter
+            // ----------------------------------------------------------------------
+
+            String setterName = "set" +
+                                Character.toUpperCase( key.charAt( 0 ) ) +
+                                key.substring( 1 );
+
+            Class clazz = target.getClass();
+
+            // ----------------------------------------------------------------------
+            //
+            // ----------------------------------------------------------------------
+
+            Method setter = getSetter( clazz, setterName, key );
+
+            if ( setter == null )
+            {
+                continue;
+            }
+
+            // TODO: Implement to give better feedback
+//            Class parameter = setter.getParameterTypes()[ 0 ];
+//
+//            if ( value.getClass().isAssignableFrom( parameter ) )
+//            {
+//            }
+
+            setter.invoke( target, new Object[] { value } );
+        }
+    }
+
+    // ----------------------------------------------------------------------
+    //
+    // ----------------------------------------------------------------------
+
+    private Method getSetter( Class clazz, String setterName, String key )
+    {
+        Map setterMap = getSetterMap( clazz );
+
+        Method setter = (Method) setterMap.get( setterName );
+
+        if ( setter == null )
+        {
+            getLogger().warn( "No setter for field '" + key + "' on the class '" + clazz.getName()
+ "'." );
+
+            return null;
+        }
+
+        if ( setter.getParameterTypes().length != 1 )
+        {
+            getLogger().warn( "No setter for field '" + key + "' on the class '" + clazz.getName()
+ "'. " +
+                              "The class has multiple setters for the field." );
+        }
+
+        return setter;
+    }
+
+    private Map getSetterMap( Class clazz )
+    {
+        // TODO: Cache the generated maps
+
+        Method[] methods = clazz.getMethods();
+
+        Map map = new HashMap();
+
+        for ( int i = 0; i < methods.length; i++ )
+        {
+            Method method = methods[ i ];
+
+            String name = method.getName();
+
+            if ( name.length() <= 3 )
+            {
+                continue;
+            }
+
+            if ( !name.startsWith( "set" ) )
+            {
+                continue;
+            }
+
+            map.put( name, method );
+        }
+
+        return map;
     }
 }

Modified: maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelper.java
URL: http://svn.apache.org/viewcvs/maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelper.java?rev=188775&r1=188774&r2=188775&view=diff
==============================================================================
--- maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelper.java
(original)
+++ maven/continuum/trunk/continuum-xmlrpc/src/main/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelper.java
Tue Jun  7 07:06:07 2005
@@ -21,10 +21,11 @@
 import java.util.Hashtable;
 import java.util.Set;
 import java.util.Vector;
+import java.beans.IntrospectionException;
 
 /**
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
- * @version $Id: XmlRpcHelper.java,v 1.1.1.1 2005/03/29 20:42:10 trygvis Exp $
+ * @version $Id$
  */
 public interface XmlRpcHelper
 {
@@ -38,4 +39,7 @@
 
     Vector collectionToVector( Collection value, boolean convertElements )
         throws IllegalAccessException, InvocationTargetException;
+
+    void hashtableToObject( Hashtable hashtable, Object object )
+        throws IntrospectionException, IllegalAccessException, InvocationTargetException;
 }

Modified: maven/continuum/trunk/continuum-xmlrpc/src/test/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelperTest.java
URL: http://svn.apache.org/viewcvs/maven/continuum/trunk/continuum-xmlrpc/src/test/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelperTest.java?rev=188775&r1=188774&r2=188775&view=diff
==============================================================================
--- maven/continuum/trunk/continuum-xmlrpc/src/test/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelperTest.java
(original)
+++ maven/continuum/trunk/continuum-xmlrpc/src/test/java/org/apache/maven/continuum/xmlrpc/XmlRpcHelperTest.java
Tue Jun  7 07:06:07 2005
@@ -22,7 +22,7 @@
 
 /**
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
- * @version $Id: XmlRpcHelperTest.java,v 1.1.1.1 2005/03/29 20:42:10 trygvis Exp $
+ * @version $Id$
  */
 public class XmlRpcHelperTest
     extends PlexusTestCase
@@ -44,16 +44,31 @@
             return foo;
         }
 
+        public void setFoo( String foo )
+        {
+            this.foo = foo;
+        }
+
         public int getBar()
         {
             return bar;
         }
 
+        public void setBar( int bar )
+        {
+            this.bar = bar;
+        }
+
         public boolean isBool()
         {
             return bool;
         }
 
+        public void setBool( boolean bool )
+        {
+            this.bool = bool;
+        }
+
         // ----------------------------------------------------------------------
         //
         // ----------------------------------------------------------------------
@@ -194,6 +209,26 @@
         assertProperty( "bar", "22", h2 );
 
         assertProperty( "bool", "false", h2 );
+    }
+
+    public void testHashtableToObject()
+        throws Exception
+    {
+        XmlRpcHelper xmlRpcHelper = (XmlRpcHelper) lookup( XmlRpcHelper.ROLE );
+
+        SimleBean bean = new SimleBean();
+
+        Hashtable hashtable = new Hashtable();
+
+        hashtable.put( "foo", "foo" );
+
+        hashtable.put( "bar", new Integer( 17 ) );
+
+        hashtable.put( "bool", Boolean.TRUE );
+
+        xmlRpcHelper.hashtableToObject( hashtable, bean );
+
+        assertEquals( "foo", bean.getFoo() );
     }
 
     // ----------------------------------------------------------------------



Mime
View raw message