JavaClass

The JavaClass component executes the specified external JAVA class.

Stream Information

InputFormatAll
Number of Inputs1
DescriptionAny stream type can be input.
OutputFormatAll
Description

If the StreamPassThrough property is True, the input stream is copied to the output stream.
If the StreamPassThrough property is False, the external Java class must set the output stream's data.

Component Properties

NameData Type/SelectionMappingDescription
ClassNamestringYes

Specifies the class name of the external Java class to be executed.
The Java class specified here must extend the com.infoteria.asteria.flowlibrary2.component.system.UserJavaClass class and implement the execute method.

StreamPassThroughbooleanNone

Specifies whether this component's input stream is to be copied to the output stream.
True - the input stream is copied to the output stream
False - the external Java class must set the output stream and field values

UserPropertiescategoryYes

Specifies the properties to be set by the external Java class.
The default values set for these properties can be retrieved with the UserJavaClass#getProperty method.

Loop Management

This component cannot be the starting point for a loop.

Transaction Management

Commit

Do Nothing

Rollback

Do Nothing

Exceptions

TypeParametersFlow Input StreamError CodeCause
Exception none

This component's input stream.

none The ClassName class can not be found.
none The external Java class throws an exception.

About External Java classes

An external Java class is executed much like a component is executed.

However, external java classes:

External Java classes must be placed in the ASTERIA_HOME/lib/userlib directory.
FlowService must be restarted after the external Java classes are placed there.

Refer to the Developer's SDK Guide for details on the UserJavaClass, retrieving input stream data, getting variables and setting output streams.


Sample

Code Sample 1

In the code sample below, the sum of the UserProperties variables a and b is calculated and the result is set to the UserProperties variable c.
The StreamPassThrough property is set to True. The UserProperties variables a, b and c must be defined.

import com.infoteria.asteria.flowengine2.execute.ExecuteContext;
import com.infoteria.asteria.flowlibrary2.FlowException;
import com.infoteria.asteria.flowlibrary2.component.system.UserJavaClass;
import com.infoteria.asteria.value.Value;

/**
 * This class must extend the base class 
 * com.infoteria.asteria.flowlibrary2.component.system.UserJavaClass.
 */
public class UserClassTest1 extends UserJavaClass {
    
    /** This class must implement the execute method.
     */
    public void execute(ExecuteContext context) throws FlowException {
        context.info("UserClassTest1 execute");//log entries can be written 
                                               //using the ExecuteContext#info method
        
        // UserProperties variables can be retrieved using the
        // getProperty method. This method returns Value objects. 
        // A Value object has various methods used to retrieve different 
        // formats of the data. For example:
        //    String strValue()
        //    int intValue()
        Value v1 = getProperty("a");
        Value v2 = getProperty("b");
        if (v1 == null || v2 == null)
            throw new FlowException("Required property not found");
        
        // Use the setProperty method to set the value of a
        // UserProperties variable.
        setProperty("c", new Value(v1.intValue() + v2.intValue()));
    }
    
}

Code Sample 2

In the code sample below, the UserProperties variables pre and post defined strings are appended to the left and right of the input stream, and this new string is set to the output stream.
The StreamPassThrough property is set to False. The UserProperties variables pre and post must be defined.

import com.infoteria.asteria.flowengine2.execute.ExecuteContext;
import com.infoteria.asteria.flowlibrary2.FlowException;
import com.infoteria.asteria.flowlibrary2.component.system.UserJavaClass;
import com.infoteria.asteria.flowlibrary2.stream.StreamFactoryText;
import com.infoteria.asteria.value.Value;

public class UserClassTest2 extends UserJavaClass {
    
    public void execute(ExecuteContext context) throws FlowException {
        context.info("UserClassTest2 execute");
        
        Value v1 = getProperty("pre");
        Value v2 = getProperty("post");
        if (v1 == null || v2 == null)
            throw new FlowException("Required property not found");
        
        // Use the getInputStream method to retrieve the input stream.
        // This method returns a StreamDataObject object.
        // The StreamDataObject has various methods that can be
        // used to get the input stream's data. For example,
        //     String strValue()   - returns the input string as a string
        //     byte[] byteValue()  - returns the input string binary data
        StreamDataObject is = getInputStream();
        
        // Use the StreamFactory to create a stream.
        // Refer to the SDK developer's guide for further details.
        // Use the setOutputStream method to set the output stream.
        // The StreamPassThrough property must be set to False.
        StreamFactoryText factory = (StreamFactoryText)getStreamFactory();
        setOutputStream(factory.create(v1.strValue() + is.strValue() + v2.strValue()));
    }
    
}