Changes

Jump to navigation Jump to search
1,149 bytes added ,  17:33, 28 March 2012
no edit summary
This article is part of the [[Querying|Query Portal]].
It demonstrates how two ways to invoke Java code can be invoked from XQuery, and (since {{Version|7.2.1}}) an extension to make Java code aware of the current context.
The Java Binding [[Querying|query]] feature is an extensibility mechanism which enables developers
to directly access Java variables and execute code from XQuery. Java classes are identified by
namespaces; the namespace URI must be of the form {{Mono|java:fully.qualified.ClassName}}.
 
=Namespace Declarations=
The following example uses Java’s {{Mono|Math}} class and returns the cosine of an angle.
as Java code cannot be pre-compiled, and will often be evaluated slower than optimized
XQuery code. Next, Java code can only be executed with [[User_Management|admin permissions]].
 
=Module Imports=
 
{{Mark|Introduced with Version 7.2.1:}}
 
Java code can also be integrated by ''importing'' classes as modules. In this case,
a new instance of the addressed class is created, which can then be accessed in the query body.
 
An example (the boolean values returned by {{Mono|set:add()}} are ignored):
 
<pre class="brush:xquery">
import module namespace set = "java:java.util.HashSet";
let $loop :=
for $i in 1 to 10000
return set:add($i)
return set:size()
</pre>
 
Advantages of this approach are:
* imported code can be executed faster than instances created at runtime via {{Mono|new()}}.
* the work on class instances ensures that queries run in parallel will not cause any concurrency issues (provided that the class contains no static variables or functions).
 
A drawback is that no arguments can be passed on to the class constructor.
This also implies that the class must provide a constructor without no arguments.
 
=Context-Awareness=
 
 
=Changelog=
 
===Version 7.2.1===
 
* Added: import of Java modules
[[Category:XQuery]]
[[Category:API]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu