Custom Java

In the Graphical Data Mapping editor, you can use the Custom Java™ transform to enter Java code in a message map.

Overview

You can use a Custom Java transform to process elements that have a simple type, and the type matches a native Java type.

You can use a Custom Java transform to process input and outputs that are arrays or complex types. For more information, see Processing complex or repeating elements in a Custom Java transform.

Guidelines to design a Java method to transform simple types

The Java class that you provide to the map must have a static method that returns the appropriate type for the value of the output element, and takes parameters of the appropriate type for the wired inputs.

For example, the following Java method can be used in a Custom Java transform that has three input elements, of types xs:string, xs:decimal, and xs:boolean, and the output element has a xs:decimal type:

	public static BigDecimal calSomething(String memType, BigDecimal stdCost, boolean flag) {
		BigDecimal actualCost = stdCost;
		if (flag & memType.startsWith("gold")) {
			BigDecimal discRate = new BigDecimal(0.9);
			actualCost = actualCost.multiply(discRate);
		}			
		return actualCost;
	}

Guidelines to design a Java method to transform complex or repeating types

You can use a Custom Java transform to process input and outputs that are arrays or complex types. The IBM® Integration Bus Java plugin API MbElement Java class passes data only in a Custom Java Transform. The Java DOM API passes data in Custom Java Transforms and in Transform condition expressions.

You can use the MbElement class in a Java method to pass a complex type, a wildcard xsd:any, or a wildcard xsd:anySimpleType.

Make a Java class visible

To make a Java class visible in the transform, ensure that the Java project the class is in is referenced by the project that contains the map.
  1. In the Application development page, right-click the project where the map is defined.
  2. Select Managed included projects.
  3. Select the Java project, and then click OK.
Note: Notice that if the Java project that contains the Java class does not build in Eclipse, then the Java class is not visible anywhere in the map. You must resolve all the Java errors, which you can see in the Problems tab, before you can configure the Java imports tab in the Properties page of the map.

Call a Java method

You can only configure one call to a Java method in a Custom Java transform. You can define more operations (java method or XPath expression) on the parameters that are required by the method.

You can configure the Java class, and a Java method in the General tab of the Properties page of the transform. The method that you specify defines the transformation logic that is applied by the Custom Java transform.

Note: The Java class that contains the method must be available in a Java project in your workspace. The Java class must be visible to the project that contains the map.

Shows the General tab of the properties page.

When you add a Java call in the General tab of the Properties page of a Custom Java transform in your map, an import is automatically added in the map to refer to the package qualified Java class, and a prefix based on the class name is added. All the public static methods in this Java class are then available in content assist when building expressions in other transforms.

For more information, see Mapping an element by using a Custom Java transform.

Define when the transform is applied at run time

You can define supplement connections between input elements and the Custom Java transform. You can then use any input element in a conditional expression that defines the condition under which the transform is applied. If the condition evaluates to true, the transform is applied.

To configure the conditional expression, you define an XPath expression or a Java expression in the Condition tab that is available in the Properties page of the transform.

Shows the Condition tab of the properties page.

To define the conditional expression, you can define an XPath expression or a call to a static method on an imported Java class. You can also create a complex expression that includes XPath, Java, and extension functions such as iib:getUserDefinedProperty("propertyname").  

By default, if the Java method does not provide a value, the transform creates an element with an empty value. If you do not want an element to be created, you can add a conditional expression in the Condition tab.

For more information, see Configuring the properties of a transform, Defining an XPath conditional expression for a transform, and Defining a Java conditional expression for a transform.

Mappings between the schema type, the Java type, and the IBM Integration Bus message assembly type

The following table shows the mappings between the schema type, the Java type, and the IBM Integration Bus message assembly type:

Table 1. Mapping the schema type, Java type, and message tree element type
Schema type Java type IBM Integration Bus message assembly tree element type
xs:anyURI java.lang.String CHARACTER
xs:base64Binary byte[] BLOB
xs:boolean boolean, java.lang.Boolean BOOLEAN
xs:byte byte, java.lang.Byte INTEGER
xs:date javax.xml.datatype.XMLGregorianCalendar DATE
xs:dateTime javax.xml.datatype.XMLGregorianCalendar TIMESTAMP
xs:dayTimeDuration javax.xml.datatype.Duration INTERVAL
xs:decimal java.math.BigDecimal DECIMAL
xs:double double, java.lang.Double FLOAT
xs:duration javax.xml.datatype.Duration INTERVAL
xs:float float, java.lang.Float FLOAT
xs:gDay javax.xml.datatype.XMLGregorianCalendar DATE
xs:gMonth javax.xml.datatype.XMLGregorianCalendar DATE
xs:gMonthDay javax.xml.datatype.XMLGregorianCalendar DATE
xs:gYear javax.xml.datatype.XMLGregorianCalendar DATE
xs:gYearMonth javax.xml.datatype.XMLGregorianCalendar DATE
xs:hexBinary byte[] BLOB
xs:int int, java.lang.Integer INTEGER
xs:integer java.math.BigInteger DECIMAL
xs:long long, java.lang.Long INTEGER
xs:negativeInteger java.math.BigInteger DECIMAL
xs:nonNegativeInteger java.math.BigInteger DECIMAL
xs:nonPositiveInteger java.math.BigInteger DECIMAL
xs:normalizedString java.lang.String CHARACTER
xs:positiveInteger java.math.BigInteger DECIMAL
xs:short short, java.lang.Short INTEGER
xs:string java.lang.String CHARACTER
xs:time javax.xml.datatype.XMLGregorianCalendar TIME
xs:unsignedByte Short INTEGER
xs:unsignedInt Long INTEGER
xs:unsignedLong java.math.BigInteger DECIMAL
xs:unsignedShort Int INTEGER
xs:yearMonthDuration javax.xml.datatype.Duration INTERVAL