[jboss-svn-commits] JBL Code SVN: r34029 - in labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta: tests/src/org/jboss/soa/esb/listeners/message and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jul 19 11:01:52 EDT 2010


Author: tfennelly
Date: 2010-07-19 11:01:51 -0400 (Mon, 19 Jul 2010)
New Revision: 34029

Added:
   labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/PropertyDecodeException.java
Modified:
   labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/Configurator.java
   labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/BeanContainerActionUnitTest.java
Log:
added decoding using single-arg constructors

Modified: labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/Configurator.java
===================================================================
--- labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/Configurator.java	2010-07-19 14:18:30 UTC (rev 34028)
+++ labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/Configurator.java	2010-07-19 15:01:51 UTC (rev 34029)
@@ -22,8 +22,6 @@
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.lifecycle.annotation.Destroy;
 import org.jboss.soa.esb.lifecycle.annotation.Initialize;
-import org.milyn.javabean.DataDecodeException;
-import org.milyn.javabean.DataDecoder;
 import org.milyn.util.ClassUtil;
 
 import java.lang.annotation.*;
@@ -41,7 +39,19 @@
 public class Configurator {
 
     private static Log logger = LogFactory.getLog(Configurator.class);
+    private static final Map<Class<?>, Class<?>> primitiveToObjectMap;
 
+    static {
+        primitiveToObjectMap = new HashMap<Class<?>, Class<?>>();
+        primitiveToObjectMap.put(Integer.TYPE, Integer.class);
+        primitiveToObjectMap.put(Long.TYPE, Long.class);
+        primitiveToObjectMap.put(Boolean.TYPE, Boolean.class);
+        primitiveToObjectMap.put(Float.TYPE, Float.class);
+        primitiveToObjectMap.put(Double.TYPE, Double.class);
+        primitiveToObjectMap.put(Character.TYPE, Character.class);
+        primitiveToObjectMap.put(Short.TYPE, Short.class);
+    }
+
     /**
      * Configure the supplied object instance using the supplied
      * {@link ConfigTree} instance.
@@ -136,13 +146,16 @@
                     }
 
                     // Decode the default and set it on the property...
-                    // No decoder specified via annotation.  Infer from the field type...
-                    DataDecoder decoder = DataDecoder.Factory.create(field.getType());
-                    if(decoder == null) {
-                        throw new ConfigurationException("Class member '" + getLongMemberName(field) + "' is annotated with the @ConfigParam annotation.  Cannot find a DataDecoder from member type '" + field.getType().getName() + "'.  Define as a String and manually decode in an @Initialize method.");
+                    Object value;
+                    try {
+                    	value = decodeValue(defaultVal, field.getType());
+                    } catch (PropertyDecodeException e) {
+                    	e.setConfiguredClass(instance.getClass());
+                    	e.setPropertyName(fieldName);
+                    	throw e;
                     }
                     try {
-                        ClassUtil.setField(field, instance, decoder.decode(defaultVal));
+                        ClassUtil.setField(field, instance, value);
                     } catch (IllegalAccessException e) {
                         throw new ConfigurationException("Unable to set property field value for '" + getLongMemberName(field) + "'.", e);
                     }
@@ -151,7 +164,7 @@
         }
     }
 
-    private static <U> void processMethodConfigAnnotations(U instance, ConfigTree config) throws ConfigurationException {
+	private static <U> void processMethodConfigAnnotations(U instance, ConfigTree config) throws ConfigurationException {
         Method[] methods = instance.getClass().getMethods();
 
         for (Method method : methods) {
@@ -209,16 +222,15 @@
 
             assertValidChoice(choices, name, paramValue);
 
-            DataDecoder decoder = DataDecoder.Factory.create(type);
-            if(decoder == null) {
-                throw new ConfigurationException("Class member '" + getLongMemberName(member) + "' is annotated with the @ConfigParam annotation.  Cannot find a DataDecoder from member type '" + type + "'.  Define as a String and manually decode in an @Initialize method.");
-            }
-
+            Object value;
             try {
-                setMember(member, instance, decoder.decode(paramValue));
-            } catch (DataDecodeException e) {
-                throw new ConfigurationException("Failed to set property configuration value on '" + getLongMemberName(member) + "'.", e);
+            	value = decodeValue(paramValue, type);
+            } catch (PropertyDecodeException e) {
+            	e.setConfiguredClass(instance.getClass());
+            	e.setPropertyName(name);
+            	throw e;
             }
+            setMember(member, instance, value);
         } else if(configParam.use() == ConfigProperty.Use.REQUIRED) {
             throw new ConfigurationException("Property '" + name + "' not specified on configuration:\n" + config);
         }
@@ -326,4 +338,28 @@
 
         return methodName.toString();
     }
+
+	private static Object decodeValue(String value, Class<?> type) throws PropertyDecodeException {
+		if(primitiveToObjectMap.containsKey(type)) {
+			type = primitiveToObjectMap.get(type);
+		}
+		
+		try {
+			Constructor<?> constructor = type.getConstructor(String.class);			
+			return constructor.newInstance(value);
+		} catch (SecurityException e) {
+			throw new PropertyDecodeException("Security exception accessing single-arg String Constructor for property type '" + type.getName() + "'.", e);
+		} catch (NoSuchMethodException e) {
+			throw new PropertyDecodeException("No single-arg String Constructor for property type '" + type.getName() + "'.  You may need to perform decoding of this property from inside an @Initialize method.", e);
+		} catch (IllegalArgumentException e) {
+			throw new PropertyDecodeException("Exception invoking single-arg String Constructor for property type '" + type.getName() + "'.", e);
+		} catch (InstantiationException e) {
+			throw new PropertyDecodeException("Exception invoking single-arg String Constructor for property type '" + type.getName() + "'.", e);
+		} catch (IllegalAccessException e) {
+			throw new PropertyDecodeException("Exception invoking single-arg String Constructor for property type '" + type.getName() + "'.", e);
+		} catch (InvocationTargetException e) {
+			throw new PropertyDecodeException("Exception invoking single-arg String Constructor for property type '" + type.getName() + "'.", e.getTargetException());
+		}
+	}
+
 }

Added: labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/PropertyDecodeException.java
===================================================================
--- labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/PropertyDecodeException.java	                        (rev 0)
+++ labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/PropertyDecodeException.java	2010-07-19 15:01:51 UTC (rev 34029)
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY 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 along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.configure;
+
+import org.jboss.soa.esb.ConfigurationException;
+
+/**
+ * Configuration Property decode exception.
+ * 
+ * @author <a href="mailto:tom.fennelly at gmail.com">tom.fennelly at gmail.com</a>
+ */
+public class PropertyDecodeException extends ConfigurationException {
+
+	private static final long serialVersionUID = 1L;
+
+	private Class<? extends Object> configuredClass;
+	private String propertyName;
+
+	public PropertyDecodeException(String message, Throwable cause) {
+		super(message, cause);
+	}
+
+	public Class<? extends Object> getConfiguredClass() {
+		return configuredClass;
+	}
+
+	public void setConfiguredClass(Class<? extends Object> configuredClass) {
+		this.configuredClass = configuredClass;
+	}
+
+	public String getPropertyName() {
+		return propertyName;
+	}
+
+	public void setPropertyName(String propertyName) {
+		this.propertyName = propertyName;
+	}
+
+	@Override
+	public String getMessage() {
+		if(propertyName != null && configuredClass != null) {
+			return "Exception decoding configuration value for property '" + propertyName + "' on class '" + configuredClass.getName() + "': " + super.getMessage();
+		} else {
+			return super.getMessage();
+		}
+	}
+}
\ No newline at end of file


Property changes on: labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/src/org/jboss/soa/esb/configure/PropertyDecodeException.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/BeanContainerActionUnitTest.java
===================================================================
--- labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/BeanContainerActionUnitTest.java	2010-07-19 14:18:30 UTC (rev 34028)
+++ labs/jbossesb/workspace/tfennelly/annotated_components/product/rosetta/tests/src/org/jboss/soa/esb/listeners/message/BeanContainerActionUnitTest.java	2010-07-19 15:01:51 UTC (rev 34029)
@@ -28,11 +28,9 @@
 import org.jboss.soa.esb.ConfigurationException;
 import org.jboss.soa.esb.actions.ActionProcessingException;
 import org.jboss.soa.esb.actions.annotation.AttachmentParam;
-import org.jboss.soa.esb.actions.annotation.BodyParam;
 import org.jboss.soa.esb.actions.annotation.OnException;
 import org.jboss.soa.esb.actions.annotation.OnSuccess;
 import org.jboss.soa.esb.actions.annotation.Process;
-import org.jboss.soa.esb.actions.annotation.PropertyParam;
 import org.jboss.soa.esb.configure.ConfigProperty;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.lifecycle.annotation.Destroy;
@@ -65,7 +63,18 @@
 		
 		configTree.setAttribute("targetFile", "./somefile.xml");
 		
-		BeanContainerAction containerAction = new BeanContainerAction(action, configTree);
+		BeanContainerAction containerAction = null;
+		
+		try {
+			containerAction = new BeanContainerAction(action, configTree);
+			fail("Expected ConfigurationException");
+		} catch(ConfigurationException e) {
+			assertTrue(e.getMessage().startsWith("Property 'intDecodedProperty' not specified on configuration:"));
+		}
+
+		configTree.setAttribute("intDecodedProperty", "9876");		
+		containerAction = new BeanContainerAction(action, configTree);
+		
 		Message message = MessageFactory.getInstance().getMessage();
 		
 		containerAction.initialise();
@@ -240,6 +249,8 @@
 	public static class ActionWithoutLifecycleWithProperty {
 		@ConfigProperty
 		private File targetFile;
+		@ConfigProperty
+		private int intDecodedProperty;
 		
 		@Process
 		public void processMethod(Message m) {



More information about the jboss-svn-commits mailing list