
|
Re: [mule-scm] [mule] [11723] branches/mule-2.0.x/core/src/main/java/org/mule/util: Add some simple elements to be used by the new scripting example
Hi Travis, you've added the sample code to core Cheers,
Ross Mason CTO, Co-Founder MuleSource Inc.
- Revision
- 11723
- Author
- tcarlson
- Date
- 2008-05-12 12:24:54 -0500 (Mon, 12 May 2008)
Log MessageAdd some simple elements to be used by the new scripting example Modified PathsAdded PathsDiffAdded: branches/mule-2.0.x/core/src/main/java/org/mule/component/simple/AccumulatorComponent.java (0 => 11723)--- branches/mule-2.0.x/core/src/main/java/org/mule/component/simple/AccumulatorComponent.java (rev 0)
+++ branches/mule-2.0.x/core/src/main/java/org/mule/component/simple/AccumulatorComponent.java 2008-05-12 17:24:54 UTC (rev 11723)
@@ -0,0 +1,72 @@
+/*
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+
+package org.mule.component.simple;
+
+import org.mule.api.MuleEventContext;
+import org.mule.api.lifecycle.Callable;
+import org.mule.api.transformer.TransformerException;
+import org.mule.config.i18n.MessageFactory;
+import org.mule.util.NumberUtils;
+
+/**
+ * Component which accumulates successive numbers, by adding/subtracting/multiplying/dividing.
+ */
+public class AccumulatorComponent implements Callable
+{
+ /** Operation to perform: "add", "subtract", "multiply", "divide" */
+ private String operation = "add";
+
+ private double accumulatedValue = 0;
+
+ public Object onCall(MuleEventContext context) throws Exception
+ {
+ Object msg = context.transformMessage();
+
+ double data = NumberUtils.toDouble(msg);
+ if (data == NumberUtils.DOUBLE_ERROR)
+ {
+ throw new TransformerException(MessageFactory.createStaticMessage("Unable to convert message to double: " + msg));
+ }
+
+ if (operation.equalsIgnoreCase("add"))
+ {
+ accumulatedValue += data;
+ }
+ else if (operation.equalsIgnoreCase("subtract"))
+ {
+ accumulatedValue -= data;
+ }
+ else if (operation.equalsIgnoreCase("multiply"))
+ {
+ accumulatedValue *= data;
+ }
+ else if (operation.equalsIgnoreCase("divide"))
+ {
+ accumulatedValue /= data;
+ }
+ else
+ {
+ throw new TransformerException(MessageFactory.createStaticMessage("Operation " + operation + " not recognized"));
+ }
+
+ return accumulatedValue;
+ }
+
+ public String getOperation()
+ {
+ return operation;
+ }
+
+ public void setOperation(String operation)
+ {
+ this.operation = operation;
+ }
+}
Property changes on: branches/mule-2.0.x/core/src/main/java/org/mule/component/simple/AccumulatorComponent.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/SimpleMathTransformer.java (0 => 11723)--- branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/SimpleMathTransformer.java (rev 0)
+++ branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/SimpleMathTransformer.java 2008-05-12 17:24:54 UTC (rev 11723)
@@ -0,0 +1,84 @@
+/*
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+
+package org.mule.transformer.simple;
+
+import org.mule.api.transformer.TransformerException;
+import org.mule.config.i18n.MessageFactory;
+import org.mule.transformer.AbstractTransformer;
+import org.mule.util.NumberUtils;
+
+/**
+ * A simple transformer which adds/subtracts/multiplies/divides a constant factor to integer messages.
+ */
+public class SimpleMathTransformer extends AbstractTransformer
+{
+ /** Operation to perform: "add", "subtract", "multiply", "divide" */
+ private String operation = "add";
+
+ /** Factor to be applied */
+ private int factor;
+
+ public SimpleMathTransformer()
+ {
+ registerSourceType(Number.class);
+ setReturnClass(Number.class);
+ }
+
+ public Object doTransform(Object src, String encoding) throws TransformerException
+ {
+ double data = NumberUtils.toDouble(src);
+ if (data == NumberUtils.DOUBLE_ERROR)
+ {
+ throw new TransformerException(MessageFactory.createStaticMessage("Unable to convert message to double: " + src));
+ }
+
+ if (operation.equalsIgnoreCase("add"))
+ {
+ return data + factor;
+ }
+ else if (operation.equalsIgnoreCase("subtract"))
+ {
+ return data - factor;
+ }
+ else if (operation.equalsIgnoreCase("multiply"))
+ {
+ return data * factor;
+ }
+ else if (operation.equalsIgnoreCase("divide"))
+ {
+ return data / factor;
+ }
+ else
+ {
+ throw new TransformerException(MessageFactory.createStaticMessage("Operation " + operation + " not recognized"));
+ }
+ }
+
+ public String getOperation()
+ {
+ return operation;
+ }
+
+ public void setOperation(String operation)
+ {
+ this.operation = operation;
+ }
+
+ public int getFactor()
+ {
+ return factor;
+ }
+
+ public void setFactor(int factor)
+ {
+ this.factor = factor;
+ }
+}
Property changes on: branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/SimpleMathTransformer.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Added: branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/StringToNumber.java (0 => 11723)--- branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/StringToNumber.java (rev 0)
+++ branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/StringToNumber.java 2008-05-12 17:24:54 UTC (rev 11723)
@@ -0,0 +1,52 @@
+/*
+ * $Id$
+ * --------------------------------------------------------------------------------------
+ * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
+ *
+ * The software in this package is published under the terms of the CPAL v1.0
+ * license, a copy of which has been included with this distribution in the
+ * LICENSE.txt file.
+ */
+
+package org.mule.transformer.simple;
+
+import org.mule.api.transformer.TransformerException;
+import org.mule.transformer.AbstractTransformer;
+import org.mule.util.NumberUtils;
+
+/**
+ * Converts a string to a number.
+ */
+public class StringToNumber extends AbstractTransformer
+{
+ /** Convert the string to an integer (by default it will convert it to a double) */
+ private boolean integer = false;
+
+ public StringToNumber()
+ {
+ registerSourceType(String.class);
+ setReturnClass(Number.class);
+ }
+
+ public Object doTransform(Object src, String encoding) throws TransformerException
+ {
+ if (integer)
+ {
+ return NumberUtils.toInt(src);
+ }
+ else
+ {
+ return NumberUtils.toDouble(src);
+ }
+ }
+
+ public boolean isInteger()
+ {
+ return integer;
+ }
+
+ public void setInteger(boolean integer)
+ {
+ this.integer = integer;
+ }
+}
Property changes on: branches/mule-2.0.x/core/src/main/java/org/mule/transformer/simple/StringToNumber.java
___________________________________________________________________
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Modified: branches/mule-2.0.x/core/src/main/java/org/mule/util/NumberUtils.java (11722 => 11723)--- branches/mule-2.0.x/core/src/main/java/org/mule/util/NumberUtils.java 2008-05-12 17:20:49 UTC (rev 11722)
+++ branches/mule-2.0.x/core/src/main/java/org/mule/util/NumberUtils.java 2008-05-12 17:24:54 UTC (rev 11723)
@@ -16,6 +16,10 @@
// @ThreadSafe
public class NumberUtils extends org.apache.commons.lang.math.NumberUtils
{
+ public static final int INTEGER_ERROR = -999999999;
+ public static final long LONG_ERROR = -999999999;
+ public static final float FLOAT_ERROR = -999999999;
+ public static final double DOUBLE_ERROR = -999999999;
public static long toLong(Object obj)
{
@@ -59,4 +63,65 @@
}
}
+ public static float toFloat(Object obj)
+ {
+ if (obj == null)
+ {
+ throw new IllegalArgumentException("Unable to convert null object to float");
+ }
+ else if (obj instanceof String)
+ {
+ return toFloat((String) obj);
+ }
+ else if (obj instanceof Number)
+ {
+ return ((Number) obj).floatValue();
+ }
+ else
+ {
+ throw new IllegalArgumentException("Unable to convert object of type: "
+ + obj.getClass().getName() + " to float.");
+ }
+ }
+
+ public static double toDouble(Object obj)
+ {
+ if (obj == null)
+ {
+ throw new IllegalArgumentException("Unable to convert null object to double");
+ }
+ else if (obj instanceof String)
+ {
+ return toDouble((String) obj);
+ }
+ else if (obj instanceof Number)
+ {
+ return ((Number) obj).doubleValue();
+ }
+ else
+ {
+ throw new IllegalArgumentException("Unable to convert object of type: "
+ + obj.getClass().getName() + " to double.");
+ }
+ }
+
+ //@Override
+ public static int toInt(String str) {
+ return toInt(str, INTEGER_ERROR);
+ }
+
+ //@Override
+ public static long toLong(String str) {
+ return toLong(str, LONG_ERROR);
+ }
+
+ //@Override
+ public static float toFloat(String str) {
+ return toFloat(str, FLOAT_ERROR);
+ }
+
+ //@Override
+ public static double toDouble(String str) {
+ return toDouble(str, DOUBLE_ERROR);
+ }
}
Modified: branches/mule-2.0.x/modules/spring-config/src/main/java/org/mule/config/spring/handlers/MuleNamespaceHandler.java (11722 => 11723)--- branches/mule-2.0.x/modules/spring-config/src/main/java/org/mule/config/spring/handlers/MuleNamespaceHandler.java 2008-05-12 17:20:49 UTC (rev 11722)
+++ branches/mule-2.0.x/modules/spring-config/src/main/java/org/mule/config/spring/handlers/MuleNamespaceHandler.java 2008-05-12 17:24:54 UTC (rev 11723)
@@ -14,6 +14,7 @@
import org.mule.component.DefaultJavaComponent;
import org.mule.component.PooledJavaComponent;
import org.mule.component.SimpleCallableJavaComponent;
+import org.mule.component.simple.AccumulatorComponent;
import org.mule.component.simple.EchoComponent;
import org.mule.component.simple.LogComponent;
import org.mule.component.simple.NullComponent;
@@ -56,8 +57,8 @@
import org.mule.config.spring.parsers.specific.TransactionManagerDefinitionParser;
import org.mule.config.spring.parsers.specific.TransformerDefinitionParser;
import org.mule.config.spring.parsers.specific.TransformerRefDefinitionParser;
+import org.mule.config.spring.parsers.specific.endpoint.EndpointRefParser;
import org.mule.config.spring.parsers.specific.endpoint.GenericEndpointDefinitionParser;
-import org.mule.config.spring.parsers.specific.endpoint.EndpointRefParser;
import org.mule.config.spring.parsers.specific.endpoint.support.OrphanEndpointDefinitionParser;
import org.mule.config.spring.util.SpringBeanLookup;
import org.mule.context.notification.ListenerSubscriptionPair;
@@ -141,7 +142,9 @@
import org.mule.transformer.simple.ObjectToByteArray;
import org.mule.transformer.simple.ObjectToString;
import org.mule.transformer.simple.SerializableToByteArray;
+import org.mule.transformer.simple.SimpleMathTransformer;
import org.mule.transformer.simple.StringAppendTransformer;
+import org.mule.transformer.simple.StringToNumber;
/**
* This is the core namespace handler for Mule and configures all Mule configuration elements under the
@@ -215,8 +218,10 @@
registerBeanDefinitionParser("serializable-to-byte-array-transformer", new TransformerDefinitionParser(SerializableToByteArray.class));
registerBeanDefinitionParser("byte-array-to-string-transformer", new TransformerDefinitionParser(ObjectToString.class));
registerBeanDefinitionParser("string-to-byte-array-transformer", new TransformerDefinitionParser(ObjectToByteArray.class));
+ registerBeanDefinitionParser("string-to-number-transformer", new TransformerDefinitionParser(StringToNumber.class));
registerBeanDefinitionParser("append-string-transformer", new TransformerDefinitionParser(StringAppendTransformer.class));
+ registerBeanDefinitionParser("math-transformer", new TransformerDefinitionParser(SimpleMathTransformer.class));
//Transaction Managers
registerBeanDefinitionParser("custom-transaction-manager", new TransactionManagerDefinitionParser());
@@ -268,6 +273,7 @@
registerBeanDefinitionParser("pass-through-component", new ComponentDefinitionParser(PassThroughComponent.class));
registerBeanDefinitionParser("log-component", new SimpleComponentDefinitionParser(SimpleCallableJavaComponent.class, LogComponent.class));
registerBeanDefinitionParser("null-component",new SimpleComponentDefinitionParser(SimpleCallableJavaComponent.class, NullComponent.class));
+ registerBeanDefinitionParser("accumulator-component",new SimpleComponentDefinitionParser(SimpleCallableJavaComponent.class, AccumulatorComponent.class));
// We need to use DefaultJavaComponent for the echo comonent because some tests invoke EchoComponent with method name and therefore we need an entry point resolver
registerBeanDefinitionParser("echo-component", new SimpleComponentDefinitionParser(DefaultJavaComponent.class, EchoComponent.class));
Modified: branches/mule-2.0.x/modules/spring-config/src/main/resources/META-INF/mule.xsd (11722 => 11723)--- branches/mule-2.0.x/modules/spring-config/src/main/resources/META-INF/mule.xsd 2008-05-12 17:20:49 UTC (rev 11722)
+++ branches/mule-2.0.x/modules/spring-config/src/main/resources/META-INF/mule.xsd 2008-05-12 17:24:54 UTC (rev 11723)
@@ -1328,7 +1328,15 @@
</xsd:annotation>
</xsd:element>
+ <xsd:element name="accumulator-component" type="defaultComponentType" substitutionGroup="abstract-component">
+ <xsd:annotation>
+ <xsd:documentation>
+ Component which accumulates successive integers by adding them together.
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+
<!--==============================================================-->
<!-- Transactions -->
<!--==============================================================-->
@@ -2322,6 +2330,55 @@
</xsd:complexType>
</xsd:element>
+ <xsd:element name="string-to-number-transformer" substitutionGroup="abstract-transformer">
+ <xsd:annotation>
+ <xsd:documentation>
+ Converts a string to a number.
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:complexType>
+ <xsd:complexContent>
+ <xsd:extension base="abstractTransformerType">
+ <xsd:attribute name="integer" type="substitutableBoolean" use="optional">
+ <xsd:annotation>
+ <xsd:documentation>
+ Convert the string to an integer (by default it will convert it to a double).
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+ </xsd:element>
+
+ <xsd:element name="math-transformer" substitutionGroup="abstract-transformer">
+ <xsd:annotation>
+ <xsd:documentation>
+ A simple transformer which adds/subtracts/multiplies/divides a constant factor to integer messages.
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:complexType>
+ <xsd:complexContent>
+ <xsd:extension base="abstractTransformerType">
+ <xsd:attribute name="operation" type="mathematicalOperation" use="required">
+ <xsd:annotation>
+ <xsd:documentation>
+ The mathematical operation.
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ <xsd:attribute name="factor" type="xsd:integer" use="required">
+ <xsd:annotation>
+ <xsd:documentation>
+ Integer factor to be applied to each message.
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ </xsd:extension>
+ </xsd:complexContent>
+ </xsd:complexType>
+ </xsd:element>
+
<xsd:element name="encrypt-transformer" type="encryptionTransformerType" substitutionGroup="abstract-transformer">
<xsd:annotation>
<xsd:documentation>
@@ -3669,6 +3726,14 @@
</xsd:complexContent>
</xsd:complexType>
+ <xsd:simpleType name="mathematicalOperation">
+ <xsd:restriction base="xsd:NMTOKEN">
+ <xsd:enumeration value="add"/>
+ <xsd:enumeration value="subtract"/>
+ <xsd:enumeration value="multiply"/>
+ <xsd:enumeration value="divide"/>
+ </xsd:restriction>
+ </xsd:simpleType>
<!--==============================================================-->
<!-- Connector support -->
To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email
|