Difference between revisions of "Scriptlet"

From PresenceWiki
Jump to: navigation, search
(Available methods)
(Available methods)
Line 79: Line 79:
 
This method allows you to perform dynamic lookups on the current data table and variables set. For example, if you have a data table in the Presence Context which contains the columns FORENAME and SURNAME and you wish to refer to the value of SURNAME, you could use:
 
This method allows you to perform dynamic lookups on the current data table and variables set. For example, if you have a data table in the Presence Context which contains the columns FORENAME and SURNAME and you wish to refer to the value of SURNAME, you could use:
  
Object surname = getValue(":var{SURNAME}");
+
Object surname = getValue(":var{SURNAME}");
  
 
This acts on the current row, and the scriptlet will be invoked multiple times where multiple records are present and referenced in the scriptlet where appropriate - see [[Data_Table#Iteration_Strategies|Iteration Strategies]].
 
This acts on the current row, and the scriptlet will be invoked multiple times where multiple records are present and referenced in the scriptlet where appropriate - see [[Data_Table#Iteration_Strategies|Iteration Strategies]].

Revision as of 16:36, 3 February 2011

Scriptlets

http://www.international-presence.com/wikidocs/images/java_logo.png

Scriptlets are bits of Java code which can appear in a dynamic text area which will be compiled and executed at runtime. They provide similar functionality to Presence Functions but unlike functions are dynamically compiled. To summarize, you can use Scriptlets where you want complex functionality within a Task element but do not want to write a re-usable function.

Scriptlets are identified by surrounding <java> tags, for example:

<java> .... some Java code ... </java>

In order to correctly compile and run, Presence must know the location of the Java compiler. This can be set in presenceconfig.xml:

http://www.international-presence.com/wikidocs/images/javac_config.png

The first "part" element should contain the location of the javac executable, or equivalent. Note that you must have a Java compiler installed in order for this to work - this can be downloaded here:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Purpose

Scriptlets allow you to:

  • Link Presence to your Java libraries
  • Easily include complex code and logic in a single Node
  • Perform tasks which would be lengthy or difficult to do using only Task nodes.

How Scriptlets Work

When Presence finds the special <java> tags in a Task Element it knows that whatever is between the tags needs to be compiled by the Java compiler (usually javac.exe). A new class is created which extends com.internationalpresence.script.ScriptAbstract, and the abstract method public void userCode() is dynamically replaced with the scriptlet contents.

If compilation is successful the new Class is imported into Presence for use by any servers or clients and is stored in the Presence database.

Note that you can view the compiled class in the Jar Manager under "Loose Classes" - the class name will be something along the lines of com.internationalpresence.tmp.Tempxxxxxxxxxxxxxx.

Available methods

You can make calls to the following methods within your scriptlet code.

public void write (Object o)

This method writes the String value of "o" back to the calling Task Element, in the place where the scriptlet tags were found.

For example:

<java>
write ("Hello Presence");
</java>

Will result in:

Hello Presence

And:

<java>
write ("2+2="+(2+2));
</java>

Will result in:

2+2=4

public void writeln (Object o)

The writeln method does the same as the write method, but appends a newline after the String that is written.

protected PresenceContext getContext()

Returns the current Presence Context, from which you can perform many operations. Since the PresenceContext class is not publicly documented however, we do not support this usage.

protected StringWriter getWriter()

This method returns the StringWriter that is used to write back to the invoking Task Element, should you need a reference to it.

protected Object getValue (String key) throws ScriptletException

This method allows you to perform dynamic lookups on the current data table and variables set. For example, if you have a data table in the Presence Context which contains the columns FORENAME and SURNAME and you wish to refer to the value of SURNAME, you could use:

Object surname = getValue(":var{SURNAME}");

This acts on the current row, and the scriptlet will be invoked multiple times where multiple records are present and referenced in the scriptlet where appropriate - see Iteration Strategies.