[jboss-svn-commits] JBL Code SVN: r9837 - in labs/jbossesb/trunk/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
Wed Feb 28 09:06:32 EST 2007
Author: driedtoast
Date: 2007-02-28 09:06:32 -0500 (Wed, 28 Feb 2007)
New Revision: 9837
Added:
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/ObjectInvoke.java
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/ObjectInvokeTest.java
labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
Log:
Adding a couple classes to start to basis for:
http://jira.jboss.com/jira/browse/JBESB-425
Includes:
- XstreamToObject action processor for simple xml to pojo
- ObjectInvoke action processor for simple pojo invocations
The idea would be to chain:
- XstreamToObject
- ObjectInvoke
- ObjectToXStream
To result in a pojo invocation from a transport.
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/AbstractObjectXStream.java 2007-02-28 14:06:32 UTC (rev 9837)
@@ -0,0 +1,98 @@
+package org.jboss.soa.esb.actions.converters;
+
+import java.io.Serializable;
+import java.util.List;
+
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.actions.ActionProcessor;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.message.Message;
+
+/**
+ * Abstract object and xstream action processor for common functionality.
+ *
+ *
+ * @author danielmarchant
+ *
+ */
+public abstract class AbstractObjectXStream implements ActionProcessor {
+
+
+ protected String classAlias;
+ protected boolean excludePackage = true;
+ protected String classMethod;
+ protected Class processorClass;
+ protected String classProcessor;
+ private String name;
+
+
+ /**
+ * Public constructor.
+ * @param properties Action Properties.
+ * @throws ConfigurationException Action not properly configured.
+ */
+ public AbstractObjectXStream(ConfigTree properties) {
+ this( properties.getName(), properties.attributesAsList());
+ }
+
+ /**
+ * Public constructor.
+ * @param actionName Action name.
+ * @param properties Action Properties.
+ * @throws ConfigurationException Action not properly configured.
+ */
+ public AbstractObjectXStream(String actionName, List<KeyValuePair> properties) {
+ name = actionName;
+ classAlias = KeyValuePair.getValue("class-alias", properties, null);
+ excludePackage = KeyValuePair.getBooleanValue("exclude-package", properties, true);
+ }
+
+
+
+
+ public Serializable getErrorNotification(Message message) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Serializable getOkNotification(Message message) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public abstract Message process(Message message) throws ActionProcessingException ;
+
+
+
+
+
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+
+ /**
+ * Gets the alias to use with classes.
+ *
+ * @param obj
+ * @return
+ */
+ protected String getAlias(Object obj) {
+ if(classAlias == null) {
+ if(excludePackage) {
+ return (obj instanceof Class) ? ((Class)obj).getSimpleName() : obj.getClass().getSimpleName();
+ } else {
+ return (obj instanceof Class) ? ((Class)obj).getName() : obj.getClass().getName();
+ }
+ }
+ return classAlias;
+ }
+
+}
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/ObjectInvoke.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/ObjectInvoke.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/ObjectInvoke.java 2007-02-28 14:06:32 UTC (rev 9837)
@@ -0,0 +1,210 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.actions.converters;
+
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.actions.ActionProcessor;
+import org.jboss.soa.esb.actions.ActionUtils;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.message.Message;
+
+import com.thoughtworks.xstream.XStream;
+
+
+/**
+ * Object to processor.
+ * <p/>
+ * Uses the <a href="http://xstream.codehaus.org/">XStream</a> processor to generate an XML message String from the supplied object.
+ * <p/>
+ * Sample Action Configuration:
+ * <pre>
+ * <Action name="doCustomer" processor="ObjectInvoke">
+ * <property name="class-processor" value="CustomerProcessor" /> <!-- Required. Class processor used to process the message . -->
+ * <property name="class-method" value="fooMethodName" /> <!-- Optional. Default to name of action. -->
+ *
+ * </Action>
+ * </pre>
+ * <p/>
+ *
+ * This can be used with ObjectToXStream and XStreamToObject
+ *
+ * @author danielmarchant
+ * @since Version 4.0
+ */
+public class ObjectInvoke implements ActionProcessor {
+
+ private static Logger logger = Logger.getLogger(ObjectInvoke.class);
+
+ private String classProcessor;
+
+ // class related variables
+ private Class processorClass;
+ private Method method;
+ private String classMethod;
+
+ // action related variables
+ private String name;
+
+ /**
+ * Public constructor.
+ * @param properties Action Properties.
+ * @throws ConfigurationException Action not properly configured.
+ */
+ public ObjectInvoke(ConfigTree properties) {
+ this(properties.getName(), properties.attributesAsList());
+
+ }
+
+
+
+
+ /**
+ * Public constructor.
+ * @param actionName Action name.
+ * @param properties Action Properties.
+ * @throws ConfigurationException Action not properly configured.
+ */
+ public ObjectInvoke(String actionName, List<KeyValuePair> properties) {
+
+ name = actionName;
+ classProcessor = KeyValuePair.getValue("class-processor", properties);
+ classMethod = KeyValuePair.getValue("class-method", properties);
+ if((classMethod == null) || (classMethod.length() == 0) ) {
+ classMethod = actionName;
+ }
+
+ if(classProcessor == null) {
+ return;
+ }
+
+ try {
+ processorClass = Class.forName(classProcessor);
+
+ this.setUpMethod(processorClass);
+
+ } catch (ClassNotFoundException e) {
+ logger.error("Not able to find class: " + classProcessor, e);
+ }
+ }
+
+
+
+ public Serializable getErrorNotification(Message message) {
+ return null;
+ }
+
+ public Serializable getOkNotification(Message message) {
+ return null;
+ }
+
+
+ /**
+ * Processes the message by using the giving class-processor.
+ *
+ */
+ public Message process(Message message) throws ActionProcessingException {
+ Object object = ActionUtils.getTaskObject(message);
+
+ Object processor = getObjectToInvoke();
+ Method m = this.setUpMethod(processor);
+
+ Object result;
+ try {
+ result = m.invoke(processor, new Object[] { object });
+ } catch (IllegalArgumentException e) {
+ throw new ActionProcessingException("Invalid arguments for class-processor [ " + this.classProcessor + "] for Action Processor: " + name, e );
+
+ } catch (IllegalAccessException e) {
+ throw new ActionProcessingException("No access for class-processor [ " + this.classProcessor + "] for Action Processor: " + name, e );
+
+ } catch (InvocationTargetException e) {
+ throw new ActionProcessingException("Invocation problem with class-processor [ " + this.classProcessor + "] for Action Processor: " + name, e );
+
+ }
+
+ ActionUtils.setTaskObject(message,result);
+ return message;
+ }
+
+
+ /**
+ * Gets the object that will process the message.
+ * Classes can extend this to provide the object in the way they want.
+ *
+ *
+ * @return
+ * @throws ActionProcessingException
+ */
+ protected Object getObjectToInvoke() throws ActionProcessingException {
+ Object processor = null;
+ try {
+ processor = processorClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new ActionProcessingException("Could not invoke class-processor [ " + this.classProcessor + "] for Action Processor: " + name, e );
+ } catch (IllegalAccessException e) {
+ throw new ActionProcessingException("Could not access class-processor [ " + this.classProcessor + "] for Action Processor: " + name, e );
+ }
+ return processor;
+ }
+
+
+ /**
+ * Gets the method to invoke on object.
+ *
+ * @param obj
+ * @return
+ */
+ protected Method setUpMethod(Object obj) {
+ if(method != null)
+ {
+ return method;
+ }
+
+ if(obj instanceof Class) {
+ processorClass = (Class)obj;
+ } else {
+ processorClass = obj.getClass();
+ }
+
+ Method[] methods = processorClass.getMethods();
+ for(int i=0; i<methods.length; i++) {
+ Method m = methods[i];
+ if(m.getName().equals(classMethod)) {
+ this.method = m;
+ break;
+ }
+ }
+
+ return method;
+ }
+
+
+}
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/converters/XStreamToObject.java 2007-02-28 14:06:32 UTC (rev 9837)
@@ -0,0 +1,125 @@
+package org.jboss.soa.esb.actions.converters;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.actions.ActionProcessor;
+import org.jboss.soa.esb.actions.ActionUtils;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.message.Message;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+
+/**
+ * Object to processor.
+ * <p/>
+ * Uses the <a href="http://xstream.codehaus.org/">XStream</a> processor to generate an XML message String from the supplied object.
+ * <p/>
+ * Sample Action Configuration:
+ * <pre>
+ * <Action name="doCustomer" processor="XStreamObject">
+ * <property name="class-alias" value="Customer" /> <!-- 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. -->
+ * <property name="incoming-type" value="CustomerProcessor" /> <!-- Required. Class for incoming type used to process the message after deserialisation. -->
+ * <property name="exclude-package" value="false" /> <!-- Optional. Default "true". Not applicable if a "class-alias" is specified. -->
+ * </Action>
+ * </pre>
+ * <p/>
+ * The XML root element is either set from the "class-alias" property or the classes full name. In the later case, the class package is
+ * excluded unless "exclude-package" is set to "false"/"no".
+ *
+ * This can be used with ObjectToXStream
+ *
+ * @author danielmarchant
+ * @since Version 4.0
+ */
+public class XStreamToObject extends AbstractObjectXStream implements ActionProcessor {
+
+ private static Logger logger = Logger.getLogger(XStreamToObject.class);
+
+ // class related variables
+ private Class incomingType;
+
+ // action related variables
+
+
+
+
+
+
+ /**
+ * Public constructor.
+ * @param properties Action Properties.
+ * @throws ConfigurationException Action not properly configured.
+ */
+ public XStreamToObject(ConfigTree properties) {
+ this(properties.getName(), properties.attributesAsList());
+ }
+
+
+
+
+ /**
+ * Public constructor.
+ * @param actionName Action name.
+ * @param properties Action Properties.
+ * @throws ConfigurationException Action not properly configured.
+ */
+ public XStreamToObject(String actionName, List<KeyValuePair> properties) {
+ super(actionName,properties);
+ String incomingTypeStr = KeyValuePair.getValue("incoming-type", properties);
+ try {
+ incomingType = Class.forName(incomingTypeStr);
+ } catch (ClassNotFoundException e) {
+ logger.error("Could not find : " + incomingTypeStr,e);
+ }
+ }
+
+
+
+ public Serializable getErrorNotification(Message message) {
+ return null;
+ }
+
+ public Serializable getOkNotification(Message message) {
+ return null;
+ }
+
+
+
+ /**
+ * Processes the message by using the giving class-processor.
+ *
+ */
+ public Message process(Message message) throws ActionProcessingException {
+ Object object = ActionUtils.getTaskObject(message);
+
+ String xml = object.toString();
+
+ XStream xstream = new XStream(new DomDriver());
+
+ xstream.alias(getAlias(incomingType), incomingType);
+
+ Object arg;
+ try {
+ arg = incomingType.newInstance();
+ arg = xstream.fromXML(xml, arg);
+ ActionUtils.setTaskObject(message,arg);
+ } catch (InstantiationException e) {
+ throw new ActionProcessingException("Could not invoke for Arg: " + getName(),e );
+ } catch (IllegalAccessException e) {
+ throw new ActionProcessingException("Could not access for Arg: " + getName(),e );
+ }
+
+ return message;
+ }
+
+
+}
Added: labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/ObjectInvokeTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/ObjectInvokeTest.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/ObjectInvokeTest.java 2007-02-28 14:06:32 UTC (rev 9837)
@@ -0,0 +1,77 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.actions.converters;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.soa.esb.actions.ActionUtils;
+import org.jboss.soa.esb.actions.MockPojoAction;
+import org.jboss.soa.esb.actions.TestBean;
+import org.jboss.soa.esb.actions.MockPojoAction.MockPojo;
+
+import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/**
+ *
+ * @author danielmarchant
+ *
+ */
+public class ObjectInvokeTest {
+
+ @Test
+ public void testObjectInvoke() throws Exception {
+
+ List<KeyValuePair> properties = new ArrayList<KeyValuePair>();
+ properties.add(new KeyValuePair("class-processor",MockPojoAction.class.getName()));
+ properties.add(new KeyValuePair("class-method","doPojoAction"));
+
+
+ MockPojo pojoObj = new MockPojo();
+ pojoObj.name="Hello";
+ String name = pojoObj.name;
+
+ Message oMsg = MessageFactory.getInstance().getMessage();
+ ActionUtils.setTaskObject(oMsg,pojoObj);
+
+ ObjectInvoke objectInvoke = new ObjectInvoke("ObjectInvoke",properties);
+ objectInvoke.process(oMsg);
+
+ MockPojoAction.MockPojo pojo = ( MockPojoAction.MockPojo ) ActionUtils.getTaskObject(oMsg);
+
+
+
+ Assert.assertNotNull(pojo.name);
+ Assert.assertNotSame(" Process didn't work " , name,pojo.name);
+
+
+ }
+
+
+
+}
Added: labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/tests/src/org/jboss/soa/esb/actions/converters/XStreamToObjectUnitTest.java 2007-02-28 14:06:32 UTC (rev 9837)
@@ -0,0 +1,114 @@
+/*
+ * 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.actions.converters;
+
+import java.util.ArrayList;
+import java.util.List;
+
+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.KeyValuePair;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+/**
+ * XStreamToObject unit tests.
+ * @author daniel Marchant
+ * @since Version 4.0
+ */
+public class XStreamToObjectUnitTest extends TestCase {
+
+ public void test_default() throws ActionProcessingException {
+ List<KeyValuePair> properties = new ArrayList<KeyValuePair>();
+ properties.add(new KeyValuePair("incoming-type",TestBean.class.getName() ));
+
+
+ XStreamToObject xstreamToObject = new XStreamToObject("TestBean-Serialiser", properties);
+
+
+ Message oMsg = MessageFactory.getInstance().getMessage();
+
+
+ String msg = "<" + TestBean.class.getSimpleName() + "> <name>Tom</name><phone>1234</phone></" + TestBean.class.getSimpleName() + ">";
+
+ ActionUtils.setTaskObject(oMsg,msg);
+ oMsg = xstreamToObject.process(oMsg);
+
+ TestBean bean = (TestBean)ActionUtils.getTaskObject(oMsg);
+
+ assertEquals("Tom", bean.getName());
+ assertEquals("1234", bean.getPhone());
+
+ }
+
+ public void test_with_package() throws ActionProcessingException {
+ List<KeyValuePair> properties = new ArrayList<KeyValuePair>();
+ properties.add(new KeyValuePair("exclude-package", "false"));
+ properties.add(new KeyValuePair("incoming-type",TestBean.class.getName() ));
+
+ XStreamToObject xstreamToObject = new XStreamToObject("TestBean-Serialiser", properties);
+
+
+ Message oMsg = MessageFactory.getInstance().getMessage();
+
+ String msg = "<" + TestBean.class.getName() + "> <name>Tom</name><phone>1234</phone></"+ TestBean.class.getName() +">";
+
+ ActionUtils.setTaskObject(oMsg,msg);
+ oMsg = xstreamToObject.process(oMsg);
+
+ TestBean bean = (TestBean)ActionUtils.getTaskObject(oMsg);
+
+ assertEquals("Tom", bean.getName());
+ assertEquals("1234", bean.getPhone());
+
+ }
+
+ public void test_with_alias() throws ActionProcessingException {
+ List<KeyValuePair> properties = new ArrayList<KeyValuePair>();
+
+ properties.add(new KeyValuePair("class-alias", "TomsClass"));
+ properties.add(new KeyValuePair("exclude-package", "false"));
+ properties.add(new KeyValuePair("incoming-type",TestBean.class.getName() ));
+
+ XStreamToObject xstreamToObject = new XStreamToObject("TestBean-Serialiser", properties);
+
+
+ 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());
+ }
+}
More information about the jboss-svn-commits
mailing list