Difference between revisions of "Java Bindings"

From BaseX Documentation
Jump to navigation Jump to search
(48 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
The Java Binding feature is an extensibility mechanism which enables developers
 
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]].
 
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=
 
=Identification=
Line 8: Line 10:
 
==Classes==
 
==Classes==
  
{{Mark|Updated with Version 8.4}}: A Java class is identified by a namespace URI. The original URI is rewritten as follows:
+
A Java class is identified by a namespace URI. The original URI is rewritten as follows:
  
# The [[Repository#URI_Rewriting|URI Rewriting]] steps are applied to the URI.
+
# The [[#URI Rewriting|URI Rewriting]] steps are applied to the URI.
 
# Slashes in the resulting URI are replaced with dots.
 
# Slashes in the resulting URI are replaced with dots.
# The last path segment of the URI is capitalized and rewritten to [https://en.wikipedia.org/wiki/CamelCase camel case].
+
# The last path segment of the URI is capitalized and rewritten to [https://en.wikipedia.org/wiki/CamelCase CamelCase].
  
The normalization steps are skipped if the URI is prefixed with {{Code|java:}}. See the following examples:
+
The normalization steps are skipped if the URI is prefixed with {{Code|java:}}:
  
 
* <code><nowiki>http://basex.org/modules/meta-data</nowiki></code> → <code>org.basex.modules.MetaData</code>
 
* <code><nowiki>http://basex.org/modules/meta-data</nowiki></code> → <code>org.basex.modules.MetaData</code>
Line 21: Line 23:
 
==Functions and Variables==
 
==Functions and Variables==
  
A Java variable is retrieved, and a Java function is invoked, by a usual XQuery function call. The namespace of its QName identifies the class, and the local part, which is rewritten to camel case, identifies a variable or function of that class:
+
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 <code>[https://www.fileformat.info/info/unicode/char/b7/index.htm ·]</code> (&amp;#xB7;, 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.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 30: Line 36:
 
|- valign="top"
 
|- valign="top"
 
| Variable
 
| Variable
| <code>Q{java.lang.Integer}MIN_VALUE#0</code>
+
| <code>Q{java.lang.Integer}MIN_VALUE()</code>
| <code>Integer#MIN_VALUE</code>
+
| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#MAX_VALUE Integer.MIN_VALUE]</code>
 
|- valign="top"
 
|- valign="top"
 
| Function
 
| Function
| <code>Q{java.lang.Object}hash-code#0</code>
+
| <code>Q{java.lang.Object}hash-code($object)</code>
| <code>Object#hashCode()</code>
+
| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode() object.hashCode()]</code>
 +
|- valign="top"
 +
| Function with types
 +
| <code>Q{java.lang.String}split·java.lang.String·int($string, ';', xs:int(3))</code>
 +
| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int- string.split(";", 3)]</code>
 
|}
 
|}
  
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. A table with all mappings is listed [[#Data Types|at the end]] of this article.
+
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|Data Types]]).
  
If a Java function is not found, it may help to explicitly cast your XQuery values. For example, if a Java function expects a primitive {{Code|int}} value, you will need to convert your XQuery integers to {{Code|xs:int}}.
+
If a Java function is not found, XQuery values may need to be cast the target type. For example, if a Java function expects a primitive {{Code|int}} value, you will need to convert your XQuery integers to {{Code|xs:int}}.
  
 
=Namespace Declarations=
 
=Namespace Declarations=
Line 46: Line 56:
 
In the following example, Java’s {{Code|Math}} class is referenced. When executed, the query returns the cosine of an angle by calling the static method {{Code|cos()}}, and the value of π by addressing the static variable via {{Code|PI()}}:
 
In the following example, Java’s {{Code|Math}} class is referenced. When executed, the query returns the cosine of an angle by calling the static method {{Code|cos()}}, and the value of π by addressing the static variable via {{Code|PI()}}:
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
declare namespace math = "java:java.lang.Math";
 
declare namespace math = "java:java.lang.Math";
 
math:cos(xs:double(0)), math:PI()
 
math:cos(xs:double(0)), math:PI()
</pre>
+
</syntaxhighlight>
  
 
With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0,
 
With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0,
 
the namespace can directly be embedded in the function call:
 
the namespace can directly be embedded in the function call:
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
Q{java:java.lang.Math}cos(xs:double(0))
 
Q{java:java.lang.Math}cos(xs:double(0))
</pre>
+
</syntaxhighlight>
  
 
The constructor of a class can be invoked by calling the virtual function {{Code|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 {{Code|output.txt}}. First, a new {{Code|FileWriter}} instance is created, and its {{Code|write()}} function is called in the next step:
 
The constructor of a class can be invoked by calling the virtual function {{Code|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 {{Code|output.txt}}. First, a new {{Code|FileWriter}} instance is created, and its {{Code|write()}} function is called in the next step:
  
<pre class="brush:xquery">declare namespace fw = "java.io.FileWriter";
+
<syntaxhighlight lang="xquery">
 +
declare namespace fw = "java.io.FileWriter";
 
let $file := fw:new('output.txt')
 
let $file := fw:new('output.txt')
 
return (
 
return (
Line 67: Line 78:
 
   fw:close($file)
 
   fw:close($file)
 
)
 
)
</pre>
+
</syntaxhighlight>
  
If the result of a Java call contains invalid XML characters, it will be rejected. The validity check can be disabled by setting the [[Options#CHECKSTRINGS|CHECKSTRINGS]] option to false. The following query writes a file with a single 00-byte, which will then be successfully read via Java functions:
+
If the result of a Java call contains invalid XML characters, it will be rejected. The validity check can be disabled by setting {{Option|CHECKSTRINGS}} to false. In the example below, a file with a single {{Code|00}} byte is written, and this file will then be accessed by via Java functions:
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
declare namespace br = 'java.io.BufferedReader';
 
declare namespace br = 'java.io.BufferedReader';
 
declare namespace fr = 'java.io.FileReader';
 
declare namespace fr = 'java.io.FileReader';
Line 79: Line 90:
 
file:write-binary('00.bin', xs:hexBinary('00')),
 
file:write-binary('00.bin', xs:hexBinary('00')),
 
br:new(fr:new('00.bin')) ! (br:readLine(.), br:close(.))
 
br:new(fr:new('00.bin')) ! (br:readLine(.), br:close(.))
</pre>
+
</syntaxhighlight>
  
 
Note that Java code cannot be pre-compiled, and will as such be evaluated slower than optimized XQuery code.
 
Note that Java code cannot be pre-compiled, and will as such be evaluated slower than optimized XQuery code.
Line 85: Line 96:
 
=Module Imports=
 
=Module Imports=
  
Java code can also be integrated by ''importing'' classes as modules. A new instance of the addressed class is created, which can then be accessed in the query body.
+
An alternative solution is to access Java code by ''importing'' classes as modules. A new instance of the addressed class will be created, which can then be referenced in the query body.
  
The following, side-effecting example returns the number of distinct values added to a hash set (the boolean values returned by {{Code|set:add()}} will be swallowed):
+
In the (side-effecting) example below, the size of a Java hash set is returned. The boolean values that are returned by {{Code|set:add()}} are swallowed:
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
import module namespace set = "java.util.HashSet";
+
import module namespace set = "java:java.util.HashSet";
 
prof:void(
 
prof:void(
 
   for $s in ("one", "two", "one")
 
   for $s in ("one", "two", "one")
Line 96: Line 107:
 
),
 
),
 
set:size()
 
set:size()
</pre>
+
</syntaxhighlight>
 +
 
 +
The execution of imported classes is more efficient than the execution of instances that are created at runtime via {{Code|new()}}. A drawback is that no arguments can be passed on to the class constructor. As a consequence, the import fails if the addressed class has no default constructor, but at least one constructor with arguments.
 +
 
 +
=Integration=
 +
 
 +
Java classes can be coupled even more closely to the BaseX core library.
 +
If a class inherits the abstract [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryModule.java QueryModule] class, the two variables [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryContext.java queryContext] and [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/StaticContext.java staticContext] get available, which provide access to the global and static context of a query.
  
The advantages of this approach is that imported code is executed faster than instances created at runtime via {{Code|new()}}. A drawback is that no arguments can be passed on to the class constructor. As a consequence, the import only works if the class provides a constructor with no arguments.
+
The [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryResource.java QueryResource] interface can be implemented to enforce finalizing operations, such as the closing of opened connections or resources in a module. Its {{Code|close()}} method will be called after the XQuery expression has been fully evaluated.
  
=Context-Awareness=
+
==Annotations==
  
Java classes can be coupled more closely to the BaseX core library.
+
The internal properties of functions can be assigned via annotations:
If a class inherits the abstract [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryModule.java QueryModule] class, the two variables [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryContext.java queryContext] and [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/StaticContext.java staticContext] get available, which provide access to the global and static context of a query. Additionally, the default properties of functions can be changed via annotations:
 
  
 
* Java functions can only be executed by users with [[User_Management|Admin permissions]]. You may annotate a function with {{Code|@Requires(<Permission>)}} to also make it accessible to users with less privileges.
 
* Java functions can only be executed by users with [[User_Management|Admin permissions]]. You may annotate a function with {{Code|@Requires(<Permission>)}} to also make it accessible to users with less privileges.
Line 110: Line 127:
 
* Java code is treated as ''focus-independent''. If a function accesses the current context item, position or size, it should be annotated as {{Code|@FocusDependent}}
 
* Java code is treated as ''focus-independent''. If a function accesses the current context item, position or size, it should be annotated as {{Code|@FocusDependent}}
  
The [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryResource.java QueryResource] interface can be implemented to enforce finalizing operations, such as the closing of opened connections or resources in a module. Its {{Code|close()}} method will be called after a query has been fully evaluated.
+
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:
 
 
The following XQuery code invokes two Java methods. The first Java function retrieves information from the static query context, and the second one throws a query exception:
 
  
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
import module namespace context = 'org.basex.examples.query.ContextModule';
 
import module namespace context = 'org.basex.examples.query.ContextModule';
  
Line 120: Line 135:
 
   context:user()
 
   context:user()
 
},
 
},
element to-int {
+
try {
  try { context:to-int('abc') }
+
  element to-int { context:to-int('abc') }
   catch * { 'Error in line', $err:line-number }
+
} catch basex:error {
 +
   element error { $err:description }
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
The imported Java class is shown below:
 
The imported Java class is shown below:
  
<pre class="brush:java">
+
<syntaxhighlight lang="java">
 
package org.basex.examples.query;
 
package org.basex.examples.query;
  
Line 142: Line 158:
 
   /**
 
   /**
 
   * Returns the name of the logged in user.
 
   * Returns the name of the logged in user.
   * @return user
+
   * @return user string
 
   */
 
   */
 
   @Requires(Permission.NONE)
 
   @Requires(Permission.NONE)
Line 153: Line 169:
 
   /**
 
   /**
 
   * Converts the specified string to an integer.
 
   * Converts the specified string to an integer.
   * @param value string representation
+
   * @param value string to be converted
   * @return integer
+
   * @return resulting integer
 
   * @throws QueryException query exception
 
   * @throws QueryException query exception
 
   */
 
   */
Line 163: Line 179:
 
       return Integer.parseInt(value);
 
       return Integer.parseInt(value);
 
     } catch(NumberFormatException ex) {
 
     } catch(NumberFormatException ex) {
       throw new QueryException(ex.getMessage());
+
       throw new QueryException("Integer conversion failed: " + value);
 
     }
 
     }
 
   }
 
   }
Line 169: Line 185:
 
   @Override
 
   @Override
 
   public void close() {
 
   public void close() {
     // see description above
+
     // defined in QueryResource interface, will be called after query evaluation
 
   }
 
   }
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
The result will look as follows:
 
The result will look as follows:
  
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<user>admin</admin>
 
<user>admin</admin>
<to-int>Error in line 6</to-int>
+
<error>Integer conversion failed: abc</error>
</pre>
+
</syntaxhighlight>
  
 
Please visit the XQuery 3.0 specification if you want to get more insight into
 
Please visit the XQuery 3.0 specification if you want to get more insight into
[http://www.w3.org/TR/xpath-functions-30/#properties-of-functions function properties].
+
[https://www.w3.org/TR/xpath-functions-31/#properties-of-functions function properties].
  
=Locking=
+
==Updates==
  
By default, a Java function will be executed in parallel with other code. However, if a Java function performs sensitive write operations, it is advisable to explicitly lock the code. This can be realized via locking annotations:
+
The {{Code|@Updating}} annotation can be applied to mark Java functions that perform write or update operations:
  
<pre class="brush:java">
+
<syntaxhighlight lang="java">
   @Lock(write = { "HEAVYIO" })
+
   @Updating
   public void write() {
+
   public void backup() {
 
     // ...
 
     // ...
 
   }
 
   }
 +
</syntaxhighlight>
 +
 +
An XQuery expression will be handled as an [[XQuery Update#Updating Expressions|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 {{Function|Update|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 {{Code|synchronized}} keyword. For more complex scenarios, Lock and Semaphor and Atomic classes exist.
 +
 +
===XQuery Locks===
 +
 +
If you want to synchronize the execution of your code with BaseX locks, you can take advantage of the {{Code|@Lock}} annotation:
  
   @Lock(read = { "HEAVYIO" })
+
<syntaxhighlight lang="java">
 +
   @Lock("HEAVYIO")
 
   public void read() {
 
   public void read() {
 
     // ...
 
     // ...
 
   }
 
   }
</pre>
 
  
If an XQuery expression is run which calls the Java {{Code|write()}} function, every other query that calls {{Code|write()}} or {{Code|read()}} needs to wait for the query to be finished. If a query calls the {{Code|read()}} function, only those queries are queued that call {{Code|write()}}, because this function is only annotated with a {{Code|read}} lock. More details on parallel query execution can be found in the article on [[Transaction Management]].
+
  @Updating
 +
  @Lock("HEAVYIO")
 +
  public void write() {
 +
    // ...
 +
  }
 +
</syntaxhighlight>
 +
 
 +
If an XQuery expression invokes {{Code|write()}}, any other query that call {{Code|write()}} or {{Code|read()}} needs to wait for the query to be finished. The {{Code|read()}} function can be run in parallel; only those queries are queued that call {{Code|write()}}.  
 +
 
 +
More details on concurrent querying can be found in the article on [[Transaction Management]].
  
=Data Types=
+
==Data Types==
  
The following table lists the mappings of XQuery and Java types:
+
XQuery and Java types are mapped as follows:
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 250: Line 292:
 
| <code>null</code>
 
| <code>null</code>
 
|}
 
|}
 +
 +
==URI Rewriting==
 +
 +
Before a Java class or module is accessed, its namespace URI will be normalized:
 +
 +
# If the URI is a URL:
 +
## colons will be replaced with slashes,
 +
## in the URI authority, the order of all substrings separated by dots is reversed, and
 +
## dots in the authority and the path are replaced by slashes. If no path exists, a single slash is appended.
 +
# Otherwise, if the URI is a URN, colons will be replaced with slashes.
 +
# Characters other than letters, dots and slashes will be replaced with dashes.
 +
# If the resulting string ends with a slash, the {{Code|index}} string is appended.
 +
 +
If the resulting path has no file suffix, it may point to either an XQuery module or a Java archive:
 +
 +
* {{Code|<nowiki>http://basex.org/modules/hello/World</nowiki>}} → {{Code|org/basex/modules/hello/World}}
 +
* {{Code|<nowiki>http://www.example.com</nowiki>}} → {{Code|com/example/www/index}}
 +
* {{Code|a/little/example}} → {{Code|a/little/example}}
 +
* {{Code|a:b:c}} → {{Code|a/b/c}}
  
 
=Changelog=
 
=Changelog=
 +
 +
; Version 9.4
 +
* Added: Annotation for [[#Updates|updating functions]].
 +
* Updated: Single annotation for read and write locks.
  
 
; Version 8.4
 
; Version 8.4
 +
* Updated: Rewriting rules
  
* Updates: Rewriting rules
+
;Version 8.2
 +
* Added: [[#URI Rewriting|URI Rewriting]]: support for URNs
  
 
; Version 8.0
 
; Version 8.0
 
 
* Added: {{Code|QueryResource}} interface, called after a query has been fully evaluated.
 
* Added: {{Code|QueryResource}} interface, called after a query has been fully evaluated.
  
 
; Version 7.8
 
; Version 7.8
 
 
* Added: Java locking annotations
 
* Added: Java locking annotations
 
* Updated: {{Code|context}} variable has been split into {{Code|queryContext}} and {{Code|staticContext}}.
 
* Updated: {{Code|context}} variable has been split into {{Code|queryContext}} and {{Code|staticContext}}.
  
 
; Version 7.2.1
 
; Version 7.2.1
 
 
* Added: import of Java modules, context awareness
 
* Added: import of Java modules, context awareness
 +
* Added: [[#Packaging|Packaging]], [[#URI Rewriting|URI Rewriting]]

Revision as of 17:52, 18 November 2020

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 · (&#xB7;, 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 a Java function is not found, XQuery values may need to be cast 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>

Note that Java code cannot be pre-compiled, and will as such be evaluated slower than optimized XQuery code.

Module Imports

An alternative solution is to access Java code by importing classes as modules. A new instance of the addressed class will be created, which can then be referenced in the query body.

In the (side-effecting) example below, the size of a Java hash set is returned. The boolean values that are returned by set:add() are swallowed:

<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 are created at runtime via new(). A drawback is that no arguments can be passed on to the class constructor. As a consequence, the import fails if the addressed class has no default constructor, but at least one constructor with arguments.

Integration

Java classes can be coupled even more closely to the BaseX core library. 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 may annotate a function with @Requires(<Permission>) to also make it accessible to users with less 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, Lock and Semaphor and Atomic classes exist.

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 call write() or read() needs to wait for the query to be finished. The read() function can be run in parallel; only those queries are queued that call write().

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

Data Types

XQuery and Java types are mapped as follows:

XQuery Type Java Type
xs:string String, char, Character
xs:boolean boolean, Boolean
xs:byte byte, Byte
xs:short short, Short
xs:int int, Integer
xs:long long, Long
xs:float float, Float
xs:double double, Double
xs:decimal java.math.BigDecimal
xs:integer java.math.BigInteger
xs:QName javax.xml.namespace.QName
xs:anyURI java.net.URI, java.net.URL
empty sequence null

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