← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/util-jvm_core_module into lp:zorba

 

Chris Hillery has proposed merging lp:~zorba-coders/zorba/util-jvm_core_module into lp:zorba.

Requested reviews:
  Chris Hillery (ceejatec)
  Cezar Andrei (cezar-andrei)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/util-jvm_core_module/+merge/161854

Moves the util-jvm module into the core.
-- 
https://code.launchpad.net/~zorba-coders/zorba/util-jvm_core_module/+merge/161854
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'modules/CMakeLists.txt'
--- modules/CMakeLists.txt	2013-02-07 17:24:36 +0000
+++ modules/CMakeLists.txt	2013-05-01 15:42:31 +0000
@@ -16,6 +16,7 @@
 ADD_SUBDIRECTORY(org)
 ADD_SUBDIRECTORY(functx)
 ADD_SUBDIRECTORY(xqxq)
+ADD_SUBDIRECTORY(util-jvm)
 ADD_SUBDIRECTORY(w3c)
 
 # Add external module projects - any subdirectories of a directory

=== added directory 'modules/util-jvm'
=== added file 'modules/util-jvm/CMakeLists.txt'
--- modules/util-jvm/CMakeLists.txt	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/CMakeLists.txt	2013-05-01 15:42:31 +0000
@@ -0,0 +1,88 @@
+# Copyright 2006-2010 The FLWOR Foundation.
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+# http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# missing 
+# share use file for other modules
+
+#
+# JAVA
+#
+IF (ZORBA_SUPPRESS_JAVA)
+  MESSAGE (STATUS "ZORBA_SUPPRESS_JAVA is true - not searching for Java")
+ELSE (ZORBA_SUPPRESS_JAVA)
+  #include util-jvm macros
+  INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/config/UtilJavaUse.cmake)
+  MESSAGE(STATUS "-- util-jvm module --")
+  #find packages
+  MESSAGE (STATUS "Looking for JNI")
+  ZORBA_FIND_JNI()
+  MESSAGE (STATUS "Looking for Java")
+  FIND_PACKAGE(Java)
+
+  IF (JNI_FOUND)
+    SET (UTIL_JVM_VERSION 1.0)
+
+    # Export this include directory by setting ZORBA_PROJECT_INCLUDE_DIRS
+    SET (ZORBA_PROJECT_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
+    INCLUDE_DIRECTORIES ("${ZORBA_PROJECT_INCLUDE_DIRS}")
+    INSTALL (FILES include/JavaVMSingleton.h DESTINATION include)
+    
+    INCLUDE (CMakeJavaInformation )
+    MESSAGE(STATUS "Using Java API")
+    EXECUTE_PROCESS (
+      COMMAND ${JAVA_RUNTIME} -version
+      RESULT_VARIABLE JAVA_VERSION
+      OUTPUT_QUIET
+      ERROR_QUIET)
+
+    MESSAGE (STATUS "JNI found at    :${JAVA_INCLUDE_PATH}")
+    MESSAGE (STATUS "Java Library at :${JAVA_JVM_LIBRARY}")
+
+    INCLUDE_DIRECTORIES (${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
+    INCLUDE_DIRECTORIES (${JAVA_INCLUDE_PATH})
+
+    # For now, we still have a module, but it actually does nothing at all.
+    DECLARE_ZORBA_MODULE (URI "http://www.zorba-xquery.com/modules/util-jvm";
+      VERSION 1.0 FILE "util-jvm.xq")
+
+    # The important stuff is the library, which we install in Zorba's
+    # default lib directory.
+    ADD_LIBRARY(util-jvm SHARED util-jvm.cpp JavaVMSingleton.cpp)
+    TARGET_LINK_LIBRARIES(util-jvm "${JAVA_JVM_LIBRARY}" zorba_${ZORBA_STORE_NAME})
+    INSTALL(TARGETS util-jvm 
+      RUNTIME DESTINATION bin
+      LIBRARY DESTINATION lib${LIB_SUFFIX}
+      ARCHIVE DESTINATION lib)
+
+    ADD_SUBDIRECTORY(config)
+    # MAC OS X only property
+    # this is required to make sure that the library has the correct install name after
+    # installation such that dependent modules always find it
+    #SET_TARGET_PROPERTIES(util-jvm PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
+
+    # Set this in the parent scope so it will be put into our Config.cmake file.
+    # As with many other things, this doesn't work when the module is installed.
+    #SET(ZORBA_PROJECT_LIBRARIES util-jvm PARENT_SCOPE)
+
+  ELSE (JNI_FOUND)
+    MESSAGE(STATUS "Building Zorba without util-jvm module.")
+  ENDIF (JNI_FOUND)
+  MESSAGE(STATUS "---")
+  MESSAGE(STATUS "")
+ENDIF(ZORBA_SUPPRESS_JAVA)
+
+# Cache these variables for ease of packages depending on us
+SET (JAVA_FOUND "${JAVA_FOUND}" CACHE BOOL "Whether Java was found" FORCE)
+SET (JNI_FOUND "${JNI_FOUND}" CACHE BOOL "Whether JNI was found" FORCE)
+

=== added file 'modules/util-jvm/JavaVMSingleton.cpp'
--- modules/util-jvm/JavaVMSingleton.cpp	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/JavaVMSingleton.cpp	2013-05-01 15:42:31 +0000
@@ -0,0 +1,267 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "JavaVMSingleton.h"
+
+#include <fstream>
+#include <iostream>
+#include <istream>
+#include <string>
+#include <cstdlib>
+#include <cstring>
+#include <sstream>
+
+#include <zorba/util/path.h>
+#include <zorba/util/file.h>
+#include <zorba/zorba.h>
+
+
+namespace zorba { namespace jvm {
+JavaVMSingleton* JavaVMSingleton::instance = NULL;
+
+JavaVMSingleton::JavaVMSingleton(const char* classPath, const char* javaLibPath)
+{
+  memset(&args, 0, sizeof(args));
+  jint r;
+  jint nOptions = NO_OF_JVM_OPTIONS;
+
+  std::string classpathOption;
+  std::ostringstream os;
+  os << "-Djava.class.path=" << classPath;
+  classpathOption = os.str();
+  classPathOption = new char[classpathOption.size() + 1];
+  memset(classPathOption, 0, sizeof(char) * (classpathOption.size() + 1));
+  memcpy(classPathOption, classpathOption.c_str(), classpathOption.size() * sizeof(char));
+
+  std::string lAwtArgStr = "-Djava.awt.headless=true";
+  awtOption = new char[lAwtArgStr.size() + 1];
+  memset(awtOption, 0, sizeof(char) * (lAwtArgStr.size() + 1));
+  memcpy(awtOption, lAwtArgStr.c_str(), sizeof(char) * lAwtArgStr.size());
+  awtOption[lAwtArgStr.size()] = 0;
+
+  // javaLibPath are only base pathes, the full path will be computed at runtime in the Java class
+  std::string jlpStr = "-Djava.library.path=" + std::string(javaLibPath);
+  jlpOption = new char[jlpStr.size() + 1];
+  memset(jlpOption, 0, sizeof(char) * (jlpStr.size() + 1));
+  memcpy(jlpOption, jlpStr.c_str(), sizeof(char) * jlpStr.size());
+  jlpOption[jlpStr.size()] = 0;
+
+  options[0].optionString = classPathOption;
+  options[0].extraInfo = NULL;
+  options[1].optionString = awtOption;
+  options[1].extraInfo = NULL;
+  options[2].optionString = jlpOption;
+  options[2].extraInfo = NULL;
+
+  memset(&args, 0, sizeof(args));
+  args.version  = JNI_VERSION_1_2;
+  args.nOptions = nOptions;
+  args.options  = options;
+  args.ignoreUnrecognized = JNI_FALSE;
+
+  r = JNI_CreateJavaVM(&m_vm, (void **)&m_env, &args);
+  if (r != JNI_OK) {
+    throw VMOpenException();
+  }
+}
+
+JavaVMSingleton::~JavaVMSingleton()
+{
+  if (instance) {
+    delete instance;
+    instance = NULL;
+  }
+  m_vm->DestroyJavaVM();
+  if (awtOption)
+    delete[] awtOption;
+  if (classPathOption)
+    delete[] classPathOption;
+}
+
+/*JavaVMSingleton* JavaVMSingleton::getInstance(const char* classPath)
+{
+  return getInstance(classPath, "");
+}*/
+
+JavaVMSingleton* JavaVMSingleton::getInstance(const char* classPath, const char* javaLibPath)
+{
+//#ifdef WIN32
+//  // If pointer to instance of JavaVMSingleton exists (true) then return instance pointer else look for
+//  // instance pointer in memory mapped pointer. If the instance pointer does not exist in
+//  // memory mapped pointer, return a newly created pointer to an instance of Abc.
+
+//  return instance ?
+//     instance : (instance = (JavaVMSingleton*) MemoryMappedPointers::getPointer("JavaVMSingleton")) ?
+//     instance : (instance = (JavaVMSingleton*) MemoryMappedPointers::createEntry("JavaVMSingleton",(void*)new JavaVMSingleton(classPath)));
+//#else
+
+
+  // If pointer to instance of JavaVMSingleton exists (true) then return instance pointer
+  // else return a newly created pointer to an instance of JavaVMSingleton.
+  if (instance == NULL)
+  {
+    JavaVM *jvms;
+    jsize nVMs;
+    if ( JNI_GetCreatedJavaVMs(&jvms, 1, &nVMs)==0 )
+    {
+      //std::cout << "Got JVMs " << nVMs << "\n"; std::cout.flush();
+      if (nVMs == 1)
+      {
+        JavaVM *jvm = jvms;
+        JNIEnv *env;
+        if( jvm->AttachCurrentThread((void **)&env, NULL) ==0 )
+        {
+          // if there is a jvm opened already by a diffrent dynamic lib
+          // make a singleton for this lib with that jvm
+          instance = new JavaVMSingleton(jvm, env);
+        }
+      }
+    }
+
+    if (instance == NULL)
+    {
+      instance = new JavaVMSingleton(classPath, javaLibPath);
+    }
+  }
+
+  return instance;
+}
+
+
+
+JavaVMSingleton* JavaVMSingleton::getInstance(const zorba::StaticContext* aStaticContext)
+{
+  if (instance == NULL)
+  {
+    String cp = computeClassPath(aStaticContext);
+    String lp = computeLibPath(aStaticContext);
+    return getInstance(cp.c_str(), lp.c_str());
+  }
+
+  return instance;
+}
+
+JavaVM* JavaVMSingleton::getVM()
+{
+  return m_vm;
+}
+
+JNIEnv* JavaVMSingleton::getEnv()
+{
+  return m_env;
+}
+
+
+String JavaVMSingleton::computeClassPath(const zorba::StaticContext* aStaticContext)
+{
+  String cp;
+
+  // get classpath from global Properties
+  PropertiesGlobal * properties = Zorba::getInstance(NULL)->getPropertiesGlobal();
+  std::string globalClassPath;
+  properties->getJVMClassPath(globalClassPath);
+  cp += globalClassPath;
+
+  std::vector<String> lCPV;
+  aStaticContext->getFullLibPath(lCPV);
+
+  String pathSeparator(filesystem_path::get_path_separator());
+  String dirSeparator(filesystem_path::get_directory_separator());
+
+  for (std::vector<String>::iterator lIter = lCPV.begin();
+       lIter != lCPV.end(); ++lIter)
+  {
+    // verify it contains a jars dir
+    const filesystem_path baseFsPath((*lIter).str());
+    const filesystem_path jarsFsPath(std::string("jars"));
+    filesystem_path jarsDirPath(baseFsPath, jarsFsPath);
+
+    file jarsDir(jarsDirPath);
+
+    if ( jarsDir.exists() && jarsDir.is_directory())
+    {
+      std::vector<std::string> list;
+      jarsDir.lsdir(list);
+
+      for (std::vector<std::string>::iterator itemIter = list.begin();
+           itemIter != list.end(); ++itemIter)
+      {
+        filesystem_path itemLocalFS(*itemIter);
+        filesystem_path itemFS(jarsDirPath, itemLocalFS);
+        file itemFile(itemFS);
+        if ( itemFile.exists() && itemFile.is_file() )
+        {
+          std::string itemName = itemFile.get_path();
+          std::string suffix = "-classpath.txt";
+          size_t found;
+          found = itemName.rfind(suffix);
+          if (found!=std::string::npos &&
+              found + suffix.length() == itemName.length() )
+          {
+            std::auto_ptr<std::istream> pathFile;
+            pathFile.reset(new std::ifstream (itemName.c_str ()));
+            if (!pathFile->good() || pathFile->eof() )
+            {
+              std::cerr << "file {" << itemName << "} not found or not readable." << std::endl;
+              throw itemName;
+            }
+
+            // read file
+            char line[1024];
+            while( !pathFile->eof() && !pathFile->bad() && !pathFile->fail())
+            {
+              pathFile->getline(line, sizeof(line));
+              std::string lineStr(line);
+
+              if ( lineStr.size() == 0 )
+                continue;
+
+              const std::string normalizedPath =
+                  filesystem_path::normalize_path( lineStr, jarsDirPath.get_path());
+
+              cp += pathSeparator + normalizedPath;
+            }
+          }
+        }
+      }
+    }
+  }
+
+  properties->setJVMClassPath(cp.str());
+
+  return cp;
+}
+
+
+String JavaVMSingleton::computeLibPath(const zorba::StaticContext* aStaticContext)
+{
+  String lp;
+  std::vector<String> lCPV;
+  String pathSeparator(filesystem_path::get_path_separator());
+
+  aStaticContext->getFullLibPath(lCPV);
+  for (std::vector<String>::iterator lIter = lCPV.begin();
+       lIter != lCPV.end(); ++lIter)
+  {
+    String p = *lIter;
+    lp += pathSeparator + p;
+  }
+
+  return lp;
+}
+
+
+}} // namespace zorba, jvm

=== added directory 'modules/util-jvm/config'
=== added file 'modules/util-jvm/config/CMakeLists.txt'
--- modules/util-jvm/config/CMakeLists.txt	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/config/CMakeLists.txt	2013-05-01 15:42:31 +0000
@@ -0,0 +1,44 @@
+# Copyright 2006-2012 The FLWOR Foundation.
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+# http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# We need to be able to install util-jvm and then build module projects
+# against that. The "auto-config" approach Zorba provides does not
+# support this, so we roll our own here. This should probably be
+# re-thought in the future.
+
+IF (WIN32)
+  SET (UTILJVM_CMAKE_DIR "cmake" CACHE STRING
+    "Relative path to CMake files in util-jvm installation")
+ELSE (WIN32)
+  SET(UTILJVM_CMAKE_DIR "share/cmake/zorba_util-jvm_module" CACHE STRING
+    "Relative path to CMake files in util-jvm installation")
+ENDIF (WIN32)
+
+
+# First, install the Use file. Doesn't need to be configured since it
+# just contains a macro.
+INSTALL (FILES UtilJavaUse.cmake DESTINATION "${UTILJVM_CMAKE_DIR}")
+
+# Anyway, we set all the same variables to what they should be in the 
+# install image, and re-config Zorba's ExternalModuleConfig.cmake.in.
+SET (ZORBA_PROJECT_INCLUDE_DIRS "${CMAKE_INSTALL_PREFIX}/include")
+SET (ZORBA_PROJECT_USE_FILE "${CMAKE_INSTALL_PREFIX}/${UTILJVM_CMAKE_DIR}/UtilJavaUse.cmake")
+GET_TARGET_PROPERTY(_libpath util-jvm LOCATION)
+GET_FILENAME_COMPONENT(_libname "${_libpath}"  NAME)
+SET (ZORBA_PROJECT_LIBRARIES "${CMAKE_INSTALL_PREFIX}/lib/${_libname}")
+
+CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/ExternalModuleConfig.cmake.in"
+  "${PROJECT_BINARY_DIR}/install/zorba_util-jvm_moduleConfig.cmake" @ONLY)
+INSTALL(FILES "${PROJECT_BINARY_DIR}/install/zorba_util-jvm_moduleConfig.cmake"
+  DESTINATION "${UTILJVM_CMAKE_DIR}")

=== added file 'modules/util-jvm/config/ExternalModuleConfig.cmake.in'
--- modules/util-jvm/config/ExternalModuleConfig.cmake.in	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/config/ExternalModuleConfig.cmake.in	2013-05-01 15:42:31 +0000
@@ -0,0 +1,40 @@
+# Copyright 2006-2010 The FLWOR Foundation.
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+# http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+IF (WIN32)
+  SET (PATH_SEP ",")
+ELSE (WIN32)
+  SET (PATH_SEP ":")
+ENDIF (WIN32)
+
+# Probably should have separate URI and LIB paths here someday; will
+# require testdriver to accept --uri-path and --lib-path args
+# Note that this path is meaningless if the module is installed; see
+# bug 966999.
+SET (DEPENDENCY_MODULE_PATH 
+  "${DEPENDENCY_MODULE_PATH}${PATH_SEP}@PROJECT_BINARY_DIR@/URI_PATH/${PATH_SEP}${DEPENDENCY_LIB_PATH}${PATH_SEP}@PROJECT_BINARY_DIR@/LIB_PATH/")
+
+# Dynamic libraries created by this project to link against
+SET (zorba_util-jvm_module_LIBRARIES @ZORBA_PROJECT_LIBRARIES@ @ZORBA_MODULE_LIBRARIES@)
+
+# Include directories exported by this project
+SET (zorba_util-jvm_module_INCLUDE_DIRS @ZORBA_PROJECT_INCLUDE_DIRS@)
+
+# Offer a "use file" to the user of this module. For most module
+# packages, this is unnecessary. However it can be utilized by
+# advanced packages which wish to, for example, export a C++ header
+# file to dependent packages. As above, note that this currently is
+# non-functional if this module package is installed; this will only
+# work from a project build directory.
+SET (zorba_util-jvm_module_USE_FILE "@ZORBA_PROJECT_USE_FILE@")

=== added file 'modules/util-jvm/config/UtilJavaUse.cmake'
--- modules/util-jvm/config/UtilJavaUse.cmake	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/config/UtilJavaUse.cmake	2013-05-01 15:42:31 +0000
@@ -0,0 +1,129 @@
+# Copyright 2006-2010 The FLWOR Foundation.
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+# 
+# http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Macro to find a Jar file whose filename possibly includes a version
+# number. Some Apache distributions, such as FOP, include dependent jars
+# with version numbers, and CMake's FIND_FILE() doesn't support glob
+# patterns for the filename. This macro will find .jar files with optional
+# patterns like "-1.0" in the basename.
+#
+# This macro operates the similarly to FIND_FILE(), except it only supports
+# NAMES, PATHS, PATH_SUFFIXES, DOC, and NO_CMAKE_PATH. Other differences:
+#   1. NAMES is required, and it should be passed the basename of the
+#     jarfiles without the version number or ".jar" extension.
+#   2. The only search directories will be those on CMAKE_PREFIX_PATH and
+#     those specified by PATHS.
+#
+# For example:
+#  ZORBA_FIND_JAR (FOP_JAR NAMES fop PATHS ${XSL_FOP_HOME} PATH_SUFFIXES build)
+#
+# will find fop.jar, fop-1.0.jar, or fop-23453.1234.jar, in any directory
+# on CMAKE_PREFIX_PATH or in XSL_FOP_HOME, or in any subdirectory named
+# "build" of any of those directories.
+#
+# For the moment this macro does not check the version number or enforce
+# a particular/minimum version.
+MACRO (ZORBA_FIND_JAR JARVARNAME)
+  IF (NOT COMMAND PARSE_ARGUMENTS)
+    MESSAGE (FATAL_ERROR
+      "Please INCLUDE(\${Zorba_USE_FILE}) file "
+      "prior to calling ZORBA_FIND_JAR().")
+  ENDIF (NOT COMMAND PARSE_ARGUMENTS)
+
+  PARSE_ARGUMENTS (ZFINDJAR "PATHS;NAMES;PATH_SUFFIXES" "DOC"
+    "NO_CMAKE_PATH" ${ARGN})
+
+  IF (NOT ZFINDJAR_NAMES)
+    MESSAGE (FATAL_ERROR "'NAMES' argument is required for ZORBA_FIND_JAR")
+  ENDIF (NOT ZFINDJAR_NAMES)
+
+  # If the value is already cached, don't do anything
+  IF (NOT ${JARVARNAME})
+
+    # Form up the set of directories to search in.
+    SET (_paths)
+    IF (NOT ZFINDJAR_NO_CMAKE_PATH)
+      SET (_paths ${CMAKE_PREFIX_PATH})
+    ENDIF (NOT ZFINDJAR_NO_CMAKE_PATH)
+    IF (ZFINDJAR_PATHS)
+      LIST (APPEND _paths ${ZFINDJAR_PATHS})
+    ENDIF (ZFINDJAR_PATHS)
+    IF (NOT _paths)
+      MESSAGE (WARNING "No place to search for ${ZFINDJAR_NAMES} jars! "
+	"Set either CMAKE_PREFIX_PATH or pass PATHS")
+    ENDIF (NOT _paths)
+
+    # Iterate through each directory looking for each filename
+    SET (_jarpath "${JARVARNAME}-NOTFOUND")
+    FOREACH (_path ${_paths})
+      IF (_jarpath)
+	BREAK ()
+      ENDIF (_jarpath)
+
+      # Iterate through each potential jarname
+      FOREACH (_name ${ZFINDJAR_NAMES})
+	IF (_jarpath)
+	  BREAK ()
+	ENDIF (_jarpath)
+
+	# Iterate through current directory and each suffix
+	FOREACH (_dir "" ${ZFINDJAR_PATH_SUFFIXES})
+	  # Form up the final full path
+	  IF (_dir)
+	    SET (_path2 "${_path}/${_dir}")
+	  ELSE (_dir)
+	    SET (_path2 "${_path}")
+	  ENDIF (_dir)
+
+	  # First see if the exact filename exists
+	  IF (EXISTS "${_path2}/${_name}.jar")
+	    SET (_jarpath "${_path2}/${_name}.jar")
+	    BREAK ()
+	  ENDIF (EXISTS "${_path2}/${_name}.jar")
+
+	  # Finally, glob for version variants
+	  FILE (GLOB _jarfiles "${_path2}/${_name}-*.jar")
+	  IF (_jarfiles)
+	    LIST (GET _jarfiles 0 _jarpath)
+	    BREAK ()
+	  ENDIF (_jarfiles)
+
+	ENDFOREACH (_dir)
+      ENDFOREACH (_name)
+    ENDFOREACH (_path)
+
+    # Cache the results
+    SET (${JARVARNAME} "${_jarpath}" CACHE PATH "${ZFINDJAR_DOC}" FORCE)
+
+  ENDIF (NOT ${JARVARNAME})
+
+ENDMACRO (ZORBA_FIND_JAR)
+
+MACRO (ZORBA_FIND_JNI)
+  IF (UNIX AND NOT APPLE)
+    # setting JAVA_HOME to bypass issues in FindJNI.cmake  
+    FIND_FILE(_JAVA_BIN_LOC NAMES java PATHS ENV PATH)
+    IF (_JAVA_BIN_LOC)
+      GET_FILENAME_COMPONENT(_JAVA_BIN_LOC "${_JAVA_BIN_LOC}" REALPATH) 
+      GET_FILENAME_COMPONENT(_JAVA_BIN_DIR ${_JAVA_BIN_LOC} PATH)
+      GET_FILENAME_COMPONENT(_JAVA_HOME "${_JAVA_BIN_DIR}/../.." ABSOLUTE)
+      MESSAGE(
+        STATUS "Generated JAVA_HOME to support FindJNI.cmake: " 
+        ${_JAVA_HOME})
+      SET(ENV{JAVA_HOME} ${_JAVA_HOME})
+    ENDIF()
+  ENDIF()
+
+  FIND_PACKAGE(JNI)
+ENDMACRO (ZORBA_FIND_JNI)

=== added directory 'modules/util-jvm/include'
=== added file 'modules/util-jvm/include/JavaVMSingleton.h'
--- modules/util-jvm/include/JavaVMSingleton.h	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/include/JavaVMSingleton.h	2013-05-01 15:42:31 +0000
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef JAVA_VM_SINGLETON
+#define JAVA_VM_SINGLETON
+
+#include <jni.h>
+#include <zorba/static_context.h>
+
+# if defined WIN32 || defined CYGWIN
+#   ifdef util_jvm_EXPORTS
+#     ifdef __GNUC__
+#       define UTIL_JVM_DLL_PUBLIC __attribute__((dllexport))
+#     else
+#       define UTIL_JVM_DLL_PUBLIC __declspec(dllexport)
+#     endif /* __GNUC__ */
+#   else /* util_jvm_EXPORTS */
+#     ifdef __GNUC__
+#       define UTIL_JVM_DLL_PUBLIC __attribute__((dllimport))
+#     else
+#       define UTIL_JVM_DLL_PUBLIC __declspec(dllimport)
+#     endif /* __GNUC__ */
+#   endif /* util_jvm_EXPORTS */
+# else
+#   if __GNUC__ >= 4
+#     define UTIL_JVM_DLL_PUBLIC __attribute__ ((visibility("default")))
+#   else
+#     define UTIL_JVM_DLL_PUBLIC
+#   endif
+# endif
+
+namespace zorba { namespace jvm {
+
+#define NO_OF_JVM_OPTIONS 3
+
+
+class VMOpenException {};
+
+class UTIL_JVM_DLL_PUBLIC JavaVMSingleton
+{
+public:
+  //static JavaVMSingleton* getInstance(const char* classPath);
+  static JavaVMSingleton* getInstance(const char* classPath, const char* javaLibPath);
+  static JavaVMSingleton* getInstance(const zorba::StaticContext* aStaticContext);
+
+  virtual ~JavaVMSingleton();
+  JavaVM* getVM();
+  JNIEnv* getEnv();
+
+protected:
+  JavaVMSingleton(const char* classPath, const char* javaLibPath);
+  JavaVMSingleton(JavaVM *jvm, JNIEnv *env) : m_vm(jvm), m_env(env) {}
+  static String computeClassPath(const zorba::StaticContext* aStaticContext);
+  static String computeLibPath(const zorba::StaticContext* aStaticContext);
+
+  static JavaVMSingleton* instance;
+  JavaVM* m_vm;
+  JNIEnv* m_env;
+  JavaVMInitArgs args;
+  JavaVMOption options[NO_OF_JVM_OPTIONS];
+
+  char* classPathOption;
+  char* awtOption;
+  char* jlpOption;
+};
+
+
+}} //namespace zorba, jvm
+
+#endif // JAVA_VM_SINGLETON
+
+
+
+

=== added file 'modules/util-jvm/util-jvm.cpp'
--- modules/util-jvm/util-jvm.cpp	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/util-jvm.cpp	2013-05-01 15:42:31 +0000
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <sstream>
+#include <iostream>
+#include <cstdlib>
+#include <list>
+
+#include <zorba/base64.h>
+#include <zorba/empty_sequence.h>
+#include <zorba/diagnostic_list.h>
+#include <zorba/function.h>
+#include <zorba/external_module.h>
+#include <zorba/user_exception.h>
+#include <zorba/file.h>
+#include <zorba/item_factory.h>
+#include <zorba/serializer.h>
+#include <zorba/singleton_item_sequence.h>
+#include <zorba/vector_item_sequence.h>
+#include <zorba/zorba.h>
+
+#include "JavaVMSingleton.h"
+
+#define UTILJVM_MODULE_NAMESPACE "http://www.zorba-xquery.com/modules/util-jvm";
+#define UTILJVM_OPTIONS_NAMESPACE "http://www.zorba-xquery.com/modules/util-jvm/util-jvm-options";
+
+class JavaException {
+};
+
+#define CHECK_EXCEPTION(env)  if ((lException = env->ExceptionOccurred())) throw JavaException()
+
+namespace zorba
+{
+  namespace utiljvm
+  {
+
+    class UtilJvmModule;
+
+    class UtilJvmModule : public ExternalModule {
+    private:
+
+    public:
+      UtilJvmModule() {}
+
+      virtual ~UtilJvmModule()
+      {}
+
+      virtual String getURI() const
+      {
+	return UTILJVM_MODULE_NAMESPACE;
+      }
+
+      virtual ExternalFunction*
+      getExternalFunction(const String& localName);
+
+      virtual void destroy()
+      {
+	delete this;
+      }
+    };
+
+
+    ExternalFunction*
+    UtilJvmModule::getExternalFunction(const String& localName)
+    {
+      return 0;
+    }
+
+  }}; // namespace zorba, utiljvm
+
+#ifdef WIN32
+#  define DLL_EXPORT __declspec(dllexport)
+#else
+#  define DLL_EXPORT __attribute__ ((visibility("default")))
+#endif
+
+extern "C" DLL_EXPORT zorba::ExternalModule* createModule()
+{
+  return new zorba::utiljvm::UtilJvmModule();
+}

=== added file 'modules/util-jvm/util-jvm.xq'
--- modules/util-jvm/util-jvm.xq	1970-01-01 00:00:00 +0000
+++ modules/util-jvm/util-jvm.xq	2013-05-01 15:42:31 +0000
@@ -0,0 +1,40 @@
+xquery version "1.0";
+
+(:
+ : Copyright 2006-2009 The FLWOR Foundation.
+ :
+ : Licensed under the Apache License, Version 2.0 (the "License");
+ : you may not use this file except in compliance with the License.
+ : You may obtain a copy of the License at
+ :
+ : http://www.apache.org/licenses/LICENSE-2.0
+ :
+ : Unless required by applicable law or agreed to in writing, software
+ : distributed under the License is distributed on an "AS IS" BASIS,
+ : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ : See the License for the specific language governing permissions and
+ : limitations under the License.
+ :)
+
+(:~
+ : This module provides common functionality for modules that use java
+ : implementations. 
+ : <br/>
+ : <br/>
+ : Modules using java implementations must import this module 
+ : to specify the dependency.
+ : <br />
+ : <br />
+ : <b>Note:</b> Since this module has a Java library dependency a JVM is required
+ : to be installed on the system. For Windows: jvm.dll is required on the system
+ : path (usually located in "C:\Program Files\Java\jre\bin\client").
+ :
+ : @author Cezar Andrei
+ : @library <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html";>JDK - Java Development Kit</a>
+ : @project utils/util jvm
+ :)
+module namespace util-jvm = "http://www.zorba-xquery.com/modules/util-jvm";;
+
+declare namespace ver = "http://www.zorba-xquery.com/options/versioning";;
+declare option ver:module-version "1.0";
+


Follow ups