Java component creation, step by step

Now you have read the illustrated theory about component structure and TOS code generation model in the component creation guide, let's have a step by step component creation tutorial. A very simple component which prints a list of email addresses at each iteration in a row flow. We'll create the tTutorialRow. By convention, we name it Row because it works in row mode.

Create required files

Appart from the very first component all components are created by duplicating the nearest component. For the tTutorialRow, we'll have a TABLE parameter type to handle a list of email addresses. The tRowGenerator takes a TABLE as input, maybe we can duplicate and rename this one, or create it manually.

We go to the components directory :

plugins/org.talend.designer.components.localprovider_<version>/components

create a directory tTutorialRow with all required files inside it like this:

all required files

XML description

Open the tTutorialRow_java.xml file, edit is like this:

<COMPONENT>
  <HEADER
    PLATEFORM="ALL"
    SERIAL=""
    VERSION="2.0"
    STATUS="ALPHA"
 
    COMPATIBILITY="ALL"
    AUTHOR="Talend"
    RELEASE_DATE="20070525A"
    STARTABLE="false"
  >
    <SIGNATURE/>
  </HEADER>
 
  <FAMILIES> 
    <FAMILY>tutorial</FAMILY> 
  </FAMILIES> 
 
  <DOCUMENTATION>
    <URL/>
  </DOCUMENTATION>
 
  <CONNECTORS>
    <CONNECTOR CTYPE="FLOW" MAX_INPUT="1"/>
    <CONNECTOR CTYPE="ITERATE" MAX_OUTPUT="1" MAX_INPUT="1"/>
    <CONNECTOR CTYPE="SUBJOB_OK" MAX_INPUT="1" />
    <CONNECTOR CTYPE="SUBJOB_ERROR" MAX_INPUT="1" />
    <CONNECTOR CTYPE="COMPONENT_OK" />
    <CONNECTOR CTYPE="COMPONENT_ERROR" />
    <CONNECTOR CTYPE="RUN_IF" />
  </CONNECTORS>
 
  <PARAMETERS>
    <PARAMETER NAME="ADDRESSES" FIELD="TABLE" REQUIRED="true" NUM_ROW="3" NB_LINES="5" SHOW="true">
      <ITEMS BASED_ON_SCHEMA="false">
        <ITEM NAME="USERNAME" />
        <ITEM NAME="DOMAIN" />
      </ITEMS>
    </PARAMETER>
  </PARAMETERS>
 
  <CODEGENERATION/>
 
  <RETURNS>
    <RETURN NAME="NB_LINE" TYPE="id_Integer" AVAILABILITY="AFTER"/>
  </RETURNS>
 
</COMPONENT>

Labels

We want some default labels in the GUI. Open tTutorialRow_messages.properties file, edit it like this:

LONG_NAME=Tutorial component
HELP=org.talend.help.TutorialRow

NB_LINE.NAME=Number of line

ADDRESSES.ITEM.USERNAME=Username
ADDRESSES.ITEM.DOMAIN=Domain
ADDRESSES.NAME=Addresses

Template

Begin

<%@ jet 
imports="
    	org.talend.core.model.process.INode    
		org.talend.designer.codegen.config.CodeGeneratorArgument
		org.talend.core.model.process.ElementParameterParser 
		java.util.List
		java.util.Map
		" 
%>

<%
	CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
	INode node = (INode)codeGenArgument.getArgument();
	String cid = node.getUniqueName();
	List<Map<String, String>> lines = (List<Map<String,String>>)ElementParameterParser.getObjectValue(node, "__ADDRESSES__");
%> 

java.util.List<String> addresses_<%=cid %> = new java.util.ArrayList<String>();
<%
  for (int i=0; i<lines.size(); i++) {
    Map<String, String> line = lines.get(i);
%>
    addresses_<%=cid %>.add(<%= line.get("USERNAME") %> + "@" + <%= line.get("DOMAIN") %>);
<%
  }
%>

int nb_line_<%=cid %> = 0;

In the “lines” Java TOS variable, we store all lines filled in the GUI. In the addresses Java Jet list, we store concatenations of column USERNAME and column DOMAIN. The user is supposed to fill the table as illustrated in the following screenshot.

Addresses table filled

We also have a nb_line Java Jet int variable to know the current line number for the tTutorialRow.

Main

<%@ jet 
imports="
    	org.talend.core.model.process.INode    
	org.talend.designer.codegen.config.CodeGeneratorArgument
	" 
%>

<%
	CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
	INode node = (INode)codeGenArgument.getArgument();
	String cid = node.getUniqueName();
%> 

    String[] adresses_<%=cid %> = addresses_<%=cid %>.toArray(new String[] {});
    
    System.out.print(nb_line_<%=cid %>++ + ": ");
    for (int i_<%=cid %> = 0; i_<%=cid %> < adresses_<%=cid %>.length; i_<%=cid %>++ )
    {
      System.out.print(adresses_<%=cid %>[i_<%=cid %>]);
      if (i_<%=cid %> < adresses_<%=cid %>.length-1) System.out.print(",");
    }   
    System.out.println();

As described in the section “Code generation model”, the Java output code of this template will be executed at each row iteration.

It will display the line number and the list of email addresses.

End

<%@ jet 
imports="
    	org.talend.core.model.process.INode    
	org.talend.designer.codegen.config.CodeGeneratorArgument
	" 
%>
<%
	CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
	INode node = (INode)codeGenArgument.getArgument();
	String cid = node.getUniqueName();
%>  	
 	globalMap.put("<%=cid %>_NB_LINE",nb_line_<%=cid %>);  

Here, we put the nb_line variable in the globalMap, maybe the next components will use it.

notice: the suffix of the key is NB_LINE, it is just the same in tTutorialRow_java.xml.

<RETURN NAME="NB_LINE" TYPE="id_Integer" AVAILABILITY="AFTER"/>

Component execution

Once the component described and templates written, restart TOS and see your new component in the Palette. Create a simple Job using tTutorialRow, with a tRowGenerator as input of 10 lines.

simple job using tTutorialRow

In my “run job” output, I have:

Starting job tutorial at 15:46 25/05/2007.
0: foo@foo.com,bar@bar.com,foobar@foobar.com
1: foo@foo.com,bar@bar.com,foobar@foobar.com
2: foo@foo.com,bar@bar.com,foobar@foobar.com
3: foo@foo.com,bar@bar.com,foobar@foobar.com
4: foo@foo.com,bar@bar.com,foobar@foobar.com
5: foo@foo.com,bar@bar.com,foobar@foobar.com
6: foo@foo.com,bar@bar.com,foobar@foobar.com
7: foo@foo.com,bar@bar.com,foobar@foobar.com
8: foo@foo.com,bar@bar.com,foobar@foobar.com
9: foo@foo.com,bar@bar.com,foobar@foobar.com
Job tutorial ended at 15:46 25/05/2007. [exit code=0]
 
doc/component_creation_example_java.txt · Last modified: 2011/12/17 03:52 (external edit)
 
 
Recent changes RSS feed Driven by DokuWiki