JavaInterpreter

The JavaInterpreter function dynamically executes the JAVA code specified by the Source property.

The JavaInterpreter function's engine is the BeanShell Java source interpreter which is embedded in ASTERIA.

Please refer to http://www.beanshell.org/ for details on BeanShell.

Please refer to http://www.beanshell.org/manual/contents.html for a detailed explanation of the BeanShell language specification.

Input

Number of Connections: Min: 0/Max:Infinite
InputData TypeDescription
InputNAllInput data.

Output

OutputData TypeDescription
Output1AllReturned data.

Property

NameProperty TypeDescription
SourceStringThe JAVA source code to execute.

How to use the JavaInterpreter

Basically, with the exception of defining a class, the JavaInterpreter can execute any code that follows the JAVA language specification.

The interpreter can not define a class. However, standard Java classes and existing classes can be used.

Multiple input streams can be connected to this function. The function can even work without an input stream.

This function must have a connected output stream. If this function does not have a connected output stream, this function will be removed at compile time.

The in and out JAVA variables are reserved ASTERIA variables.

These variables are used to get the function's input values and set the function's output. Even if the JAVA source code ends with a return statement, the function's output can be set.

The in variable contains the input streams' values in the order in which they where connected to the function. The out variable is the output stream's value. So, setting the out variable sets the resulting output stream.

When setting the out variable, please use the setValue method.


Examples

ex1. Concatenating two String input streams

return in[0].strValue() + in[1].strValue();

ex2. Adding two integer input streams

return in[0].longValue() + in[1].longValue();

ex3. The input stream is a file name, then returning that file's size

File file = new File(in[0]);
return file.length();

Note : Because BeanShell imports the following packages, they need not be imported:

java.lang
java.io
java.util
java.net
java.awt
java.awt.event
javax.swing
javax.swing.event

ex4. JDBC

import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("...", "XXXX", "XXXX");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from XXX");
while(rs.next())
{ // some code here } stmt.close(); con.close();

The code above is a further example of the possible applications for the JavaInterpreter. Because java.sql is not imported by BeanShell, it must be imported by the code.

If the imported oracle.jdbc.driver.OracleDriver class can not be found, a ClassNotFoundException will be thrown. Exceptions that are not caught in the JavaInterpreter are thrown by the function. If the Exception property is set, the respective Exception flow is executed. If the Exception property is not set, the flow stops.

Finally, a Connection created in the JAVA code should be closed in the JAVA code. If it is not, it will remain open and become part of the Flow Engine's held resources. The code below is an example how to close a connection in the JAVA code.

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = null;
Statement stmt = null;
try
{
    con = DriverManager.getConnection("...", "XXXX", "XXXX");
    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * from XXX");
    while(rs.next())
    {
        // some code here
    }
}
finally
{
    if (stmt != null) // -- perform a null check to prevent a NullPointerException from being thrown
        stmt.close();
    if (con != null)
        con.close();
}


ex5. An example of code that does not define variable data types

The code below is taken from the BeanShell manual.

import javax.xml.parsers.*;
import org.xml.sax.InputSource;

factory = SAXParserFactory.newInstance();
saxParser = factory.newSAXParser();
parser = saxParser.getXMLReader();
parser.setContentHandler( this ); // -- Attention here ...

invoke( name, args ) // -- ... and here.
{ 
    System.out.println( name );
}

parser.parse( new InputSource("..."));

Because the JavaInterpreter function interprets the JAVA source code, data typing is determined dynamically at run time. As a result, it is not necessary to define a variable's data type in the source code. Defining a variable's data type in the source code is also acceptable.


ex6. Another calculation example

return Math.pow(in[0].doubleValue(), in[1].doubleValue());

When returning a value other than double, the casting must be done at the appropriate time.

Below is an example of integer enumeration.

return (long)Math.pow(in[0].doubleValue(), in[1].doubleValue());


Notes


The Value class

The Value class is used to express a JAVA data type as an ASTERIA data type: Binary, Boolean, DateTime, Decimal, Double, Integer and String.

This class can be used to store a value. It can also be used to convert a value.

This class's path is com.infoteria.asteria.value.

public void setValue(boolean value)Sets the value as a Boolean.
public void setValue(Boolean value)
public void setValue(byte value)Sets the value as an Integer.
public void setValue(short value)
public void setValue(int value)
public void setValue(long value)
public void setValue(Byte value)
public void setValue(Short value)
public void setValue(Integer value)
public void setValue(Long value)
public void setValue(float value)Sets the value as a Double.
public void setValue(double value)
public void setValue(Float value)
public void setValue(Double value)
public void setValue(BigInteger value)Sets the value as a Decimal.
public void setValue(BigDecimal value)
public void setValue(char value)Sets the value as a String.
public void setValue(Character value)
public void setValue(String value)
public void setValue(Calendar value)Sets the value as a DateTime.
public void setValue(Date value)
public void setValue(byte[] value)Sets the value as a Binary.
public boolean booleanValue()Returns the value as a boolean.
public int intValue()Returns the value as a int.
public long longValue()Returns the value as a long.
public double doubleValue()Returns the value as a double.
public BigDecimal decimalValue()Returns the value as a BigDecimal.
public Date dateValue()Returns the value as a Date.
public String strValue()Returns the value as a String.
public byte[] byteValue()Returns the value as a byte row in the system's default encoding.
public byte[] byteValue(String encoding)Returns the value as a byte row in the specified encoding.
public void setNull()Sets the value as null.
public boolean isNull()Returns true if the value is null, otherwise false.

The conversion rules are outlined in the ASTERIA User's Guide, section 4.3.2.