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/Output

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

OutputData TypeDescription
Output1AllReturned data.

Property

NameProperty TypeDescription
SourcestringThe JAVA source code to execute.
CountintSpecifies 。

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

out.setValue(in[0].strValue() + in[1].strValue());

ex2. Adding two integer input streams

out.setValue(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].strValue());
out.setValue(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())
  {
  ...
  }
  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");
  Conneciton con = null;
  Statement stmt = null;
  try
  {
    con = DriverManager.getConnection("...", "XXXX", "XXXX");
    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * from XXX");
    while(rs.next())
    {
      ...
    }
  }
  finally
  {
    if (stmt != null) ※nullチェックをしないとNullPointerExceptionになる可能性があります
      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 ); ※ここと
  invoke( name, args ) { ※ここに注目
    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

out.setValue(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.

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

ex7. Use of exception

if (in[0].strValue().length() >= 10)
  throw new MapperException("Too long input");
else
  out.setValue(in[0]);

When throwing Exception in the interpreter code, throw MapperException.
If you do so, the engine can process the exception with specific error message has occurred in the Mapper component. It can be catched by Exception flow.
When you throw an Exception except for MapperException, it will be processed as an Exception with a message like "<Inline eval of: ...>".
MapperException can be used without import statement.

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 "Flow Designer" - "Stream" - "Data type of the field" in the Flow Service Online Manual

Testing

By clicking the "Test" button of source editor, you can confirm the results of executed on the server.
When you click the "Test" button, input dialog is displayed. Input the values, then click "OK" button.
The results are displayed when the execution is succeeded.

Notes

Creating custom functions

By launching "Create custom function" from right-click menu of JavaInterpreter component, you can create the source code set which is available to create custom Mapper function.
There are some merits when you create Mapper function from JavaInterpreter's code.

For details, please refer to SDK documents.