Java Bindings

From BaseX Documentation
Jump to navigation Jump to search

This article is part of the XQuery Portal. It demonstrates different ways to invoke Java code from XQuery, and it presents extensions to access the current query context from Java.

The Java Binding feature is an extensibility mechanism which enables developers to directly access Java variables and execute code from XQuery. Addressed Java code must either be contained in the Java classpath, or it must be located in the Repository.

Please bear in mind that the execution of Java code may cause side effects that conflict with the functional nature of XQuery, or may introduce new security risks to your project.

Identification

Classes

A Java class is identified by a namespace URI. The original URI is rewritten as follows:

  1. The URI Rewriting steps are applied to the URI.
  2. Slashes in the resulting URI are replaced with dots.
  3. The last path segment of the URI is capitalized and rewritten to CamelCase.

The normalization steps are skipped if the URI is prefixed with java::

  • http://basex.org/modules/meta-dataorg.basex.modules.MetaData
  • java:java.lang.Stringjava.lang.String

Functions and Variables

Java functions and variables can be referenced and evaluated by the existing XQuery function syntax:

  • The namespace of the function name identifies the Java class.
  • The local part of the name, which is rewritten to camel case, identifies a variable or function of that class.
  • The middle dot character · (·, a valid character in XQuery names, but not in Java) can be used to append exact Java parameter types to the function name. Class types must be referenced by their full path.
Type XQuery Java
Variable Q{java.lang.Integer}MIN_VALUE() Integer.MIN_VALUE
Function Q{java.lang.Object}hash-code($object) object.hashCode()
Function with types Q{java.lang.String}split·java.lang.String·int($string, ';', xs:int(3)) string.split(";", 3)

As XQuery and Java have different type systems, XQuery arguments are converted to equivalent Java values, and the result of a Java function is converted back to an XQuery value (see Data Types).

If the Java function you want to address is not detected, you may need to cast your values to the target type. For example, if a Java function expects a primitive int value, you will need to convert your XQuery integers to xs:int.

Namespace Declarations

In the following example, Java’s Math class is referenced. When executed, the query returns the cosine of an angle by calling the static method cos(), and the value of π by addressing the static variable via PI():

<syntaxhighlight lang="xquery"> declare namespace math = "java:java.lang.Math"; math:cos(xs:double(0)), math:PI() </syntaxhighlight>

With the Expanded QName notation of XQuery 3.0, the namespace can directly be embedded in the function call:

<syntaxhighlight lang="xquery"> Q{java:java.lang.Math}cos(xs:double(0)) </syntaxhighlight>

The constructor of a class can be invoked by calling the virtual function new(). Instance methods can then called by passing on the resulting Java object as first argument. In the following example, 256 bytes are written to the file output.txt. First, a new FileWriter instance is created, and its write() function is called in the next step:

<syntaxhighlight lang="xquery"> declare namespace fw = "java.io.FileWriter"; let $file := fw:new('output.txt') return (

 for $i in 0 to 255
 return fw:write($file, xs:int($i)),
 fw:close($file)

) </syntaxhighlight>

If the result of a Java call contains invalid XML characters, it will be rejected. The validity check can be disabled by setting CHECKSTRINGS to false. In the example below, a file with a single 00 byte is written, and this file will then be accessed by via Java functions:

<syntaxhighlight lang="xquery"> declare namespace br = 'java.io.BufferedReader'; declare namespace fr = 'java.io.FileReader';

declare option db:checkstrings 'false';

file:write-binary('00.bin', xs:hexBinary('00')), br:new(fr:new('00.bin')) ! (br:readLine(.), br:close(.)) </syntaxhighlight>

Module Imports

A Java classes can also be instantiated by importing them as a module: A new instance of the addressed class will be constructed, which can then be referenced in the query body.

In the (side-effecting) example below, a HashSet instance is created, values are added, and the size of the set is returned. As set:add() returns boolean values, prof:void is used to swallow the values:

<syntaxhighlight lang="xquery"> import module namespace set = "java:java.util.HashSet"; prof:void(

 for $s in ("one", "two", "one")
 return set:add($s)

), set:size() </syntaxhighlight>

The execution of imported classes is more efficient than the execution of instances that have been created via new(). In turn, no arguments can be supplied in the import statement, and the construction will only be successful if the class can be instantiated without arguments.

Integration

Java classes can be coupled more closely to BaseX. If a class inherits the abstract QueryModule class, the two variables queryContext and staticContext get available, which provide access to the global and static context of a query.

The QueryResource interface can be implemented to enforce finalizing operations, such as the closing of opened connections or resources in a module. Its close() method will be called after the XQuery expression has been fully evaluated.

Annotations

The internal properties of functions can be assigned via annotations:

  • Java functions can only be executed by users with Admin permissions. You can annotate a function with @Requires(<Permission>) to also make it accessible to users with fewer privileges.
  • Java code is treated as non-deterministic, as its behavior cannot be predicted by the XQuery processor. You may annotate a function as @Deterministic if you know that it will have no side effects and will always yield the same result.
  • Java code is treated as context-independent. If a function accesses the query context, it should be annotated as @ContextDependent
  • Java code is treated as focus-independent. If a function accesses the current context item, position or size, it should be annotated as @FocusDependent

In the following code, information from the static query context is returned by the first function, and a query exception is raised by the second function:

<syntaxhighlight lang="xquery"> import module namespace context = 'org.basex.examples.query.ContextModule';

element user {

 context:user()

}, try {

 element to-int { context:to-int('abc') }

} catch basex:error {

 element error { $err:description }

} </syntaxhighlight>

The imported Java class is shown below:

<syntaxhighlight lang="java"> package org.basex.examples.query;

import org.basex.query.*; import org.basex.query.value.item.*; import org.basex.util.*;

/**

* This example inherits the {@link QueryModule} class and
* implements the QueryResource interface.
*/

public class ContextModule extends QueryModule implements QueryResource {

 /**
  * Returns the name of the logged-in user.
  * @return user string
  */
 @Requires(Permission.NONE)
 @Deterministic
 @ContextDependent
 public String user() {
   return queryContext.context.user.name;
 }
 /**
  * Converts the specified string to an integer.
  * @param value string to be converted
  * @return resulting integer
  * @throws QueryException query exception
  */
 @Requires(Permission.NONE)
 @Deterministic
 public int toInt(final String value) throws QueryException {
   try {
     return Integer.parseInt(value);
   } catch(NumberFormatException ex) {
     throw new QueryException("Integer conversion failed: " + value);
   }
 }
 @Override
 public void close() {
   // defined in QueryResource interface, will be called after query evaluation
 }

} </syntaxhighlight>

The result will look as follows:

<syntaxhighlight lang="xml"> <user>admin</admin> <error>Integer conversion failed: abc</error> </syntaxhighlight>

Please visit the XQuery 3.0 specification if you want to get more insight into function properties.

Updates

The @Updating annotation can be applied to mark Java functions that perform write or update operations:

<syntaxhighlight lang="java">

 @Updating
 public void backup() {
   // ...
 }

</syntaxhighlight>

An XQuery expression will be handled as an updating expression if it calls an updating Java function. In contrast to XQuery update operations, the Java code will immediately be executed, but the result will be cached as if update:output was called.

The annotation is particularly helpful if combined with a lock annotation.

Locking

By default, a Java function will be executed in parallel with other code. If a Java function performs sensitive operations, it is advisable to explicitly lock the code.

Java Locks

Java provides a handful of mechanism to control the execution of code. The concurrent execution of functions can be avoided with the synchronized keyword. For more complex scenarios, the Lock, Semaphore and Atomic classes can be brought into play.

XQuery Locks

If you want to synchronize the execution of your code with BaseX locks, you can take advantage of the @Lock annotation:

<syntaxhighlight lang="java">

 @Lock("HEAVYIO")
 public void read() {
   // ...
 }
 @Updating
 @Lock("HEAVYIO")
 public void write() {
   // ...
 }

</syntaxhighlight>

If an XQuery expression invokes write(), any other query that calls write() or read() needs to wait for the query to be finished. The read() function can be run in parallel; whereas queries will be queued if write() is called.

More details on concurrent querying can be found in the article on Transaction Management.

Data Types

Conversion to Java

Before Java code is executed, the arguments are converted to Java values, depending on the addressed function or constructor parameters. The accepted Java types and the original XQuery types are depicted in the second and first column of the table below.

Conversion to XQuery

The result of a Java call is converted back to XQuery, as depicted in the second and third column of the table.

As there are too many differences between XQuery and Java types, no bidirectional mapping is possible. The chosen mapping is a compromise between usability and conformity.

XQuery input Expected or returned Java type XQuery output
empty-sequence() null empty-sequence()
item()* (no conversion) org.basex.query.value.Value item()* (no conversion)
xs:string, xs:untypedAtomic String xs:string
xs:string with single character, xs:unsignedShort char, Character xs:string
xs:boolean boolean, Boolean xs:boolean
xs:byte byte, Byte xs:byte
xs:short short, Short xs:short
xs:int int, Integer xs:int
xs:integer, xs:long long, Long xs:integer
xs:unsignedLong java.math.BigInteger xs:unsignedLong (lossy)
xs:decimal java.math.BigDecimal xs:decimal
xs:float float, Float xs:float
xs:double double, Double xs:double
xs:QName javax.xml.namespace.QName xs:QName
xs:anyURI java.net.URI, java.net.URL xs:anyURI
xs:date javax.xml.datatype.XMLGregorianCalendar xs:date
xs:duration javax.xml.datatype.Duration xs:duration
node() org.w3c.dom.Node node()
array(xs:boolean) boolean[] xs:boolean*
array(xs:string) String[] xs:string*
array(xs:unsignedShort) char[] xs:string
array(xs:short) short[] xs:short*
array(xs:int) int[] xs:int*
array(xs:integer), array(xs:long) long[] xs:integer*
array(xs:float) float[] xs:float*
array(xs:double) double[] xs:double*
Object[] (others) item()* array(*) (others)
java.util.HashMap Wrapped Java object map(*)
char[][] xs:string*

URI Rewriting

Before a Java class or module is accessed, its namespace URI will be normalized:

  1. If the URI is a URL:
    1. colons will be replaced with slashes,
    2. in the URI authority, the order of all substrings separated by dots is reversed, and
    3. dots in the authority and the path are replaced by slashes. If no path exists, a single slash is appended.
  2. Otherwise, if the URI is a URN, colons will be replaced with slashes.
  3. Characters other than letters, dots and slashes will be replaced with dashes.
  4. If the resulting string ends with a slash, the index string is appended.

If the resulting path has no file suffix, it may point to either an XQuery module or a Java archive:

  • http://basex.org/modules/hello/Worldorg/basex/modules/hello/World
  • http://www.example.comcom/example/www/index
  • a/little/examplea/little/example
  • a:b:ca/b/c

Changelog

Version 9.4
  • Added: Annotation for updating functions.
  • Updated: Single annotation for read and write locks.
Version 8.4
  • Updated: Rewriting rules
Version 8.2
Version 8.0
  • Added: QueryResource interface, called after a query has been fully evaluated.
Version 7.8
  • Added: Java locking annotations
  • Updated: context variable has been split into queryContext and staticContext.
Version 7.2.1