diff --git a/Makefile.in b/Makefile.in index ab4d3d26693..31513cfcd15 100644 --- a/Makefile.in +++ b/Makefile.in @@ -254,6 +254,10 @@ ifdef MOZ_PREF_EXTENSIONS tier_50_dirs += extensions/pref endif +ifdef MOZ_JAVAXPCOM +tier_50_dirs += extensions/java +endif + ifdef MOZ_XUL_APP ifndef BUILD_STATIC_LIBS tier_50_dirs += toolkit/library diff --git a/allmakefiles.sh b/allmakefiles.sh index ff640dc79a4..e085b1a3a7b 100755 --- a/allmakefiles.sh +++ b/allmakefiles.sh @@ -1356,6 +1356,17 @@ if [ "$MOZ_XMLEXTRAS" ]; then " fi +if [ "$MOZ_JAVAXPCOM" ]; then + MAKEFILES_javaxpcom=" + extensions/java/Makefile + extensions/java/xpcom/Makefile + extensions/java/xpcom/interfaces/Makefile + extensions/java/xpcom/src/Makefile + extensions/java/xpcom/glue/Makefile + extensions/java/xpcom/tools/genifaces/Makefile +" +fi + # directory/xpcom if [ "$MOZ_LDAP_XPCOM" ]; then MAKEFILES_ldap=" @@ -1420,13 +1431,6 @@ for extension in $MOZ_EXTENSIONS; do inspector ) MAKEFILES_extensions="$MAKEFILES_extensions $MAKEFILES_inspector" ;; - java ) MAKEFILES_extensions="$MAKEFILES_extensions - extensions/java/Makefile - extensions/java/xpcom/Makefile - extensions/java/xpcom/build/Makefile - extensions/java/xpcom/tests/Makefile - extensions/java/xpcom/tools/genifaces/Makefile - " ;; spatialnavigation ) MAKEFILES_extensions="$MAKEFILES_extensions $MAKEFILES_spatialnavigation" ;; @@ -1576,7 +1580,7 @@ $MAKEFILES_gfx $MAKEFILES_accessible $MAKEFILES_htmlparser $MAKEFILES_intl -$MAKEFILES_java_supplement +$MAKEFILES_javaxpcom $MAKEFILES_ldap $MAKEFILES_leaky $MAKEFILES_jpeg diff --git a/config/autoconf.mk.in b/config/autoconf.mk.in index b57505731a5..04cf6cd91fe 100644 --- a/config/autoconf.mk.in +++ b/config/autoconf.mk.in @@ -140,7 +140,7 @@ MOZ_NO_XPCOM_OBSOLETE = @MOZ_NO_XPCOM_OBSOLETE@ MOZ_NO_FAST_LOAD = @MOZ_NO_FAST_LOAD@ NS_PRINTING = @NS_PRINTING@ -MOZ_ENABLE_JAVAXPCOM = @MOZ_ENABLE_JAVAXPCOM@ +MOZ_JAVAXPCOM = @MOZ_JAVAXPCOM@ JAVA_INCLUDE_PATH=@JAVA_INCLUDE_PATH@ JAVAC=@JAVAC@ JAR=@JAR@ diff --git a/configure.in b/configure.in index d5f31e5d389..561ff94d0ae 100644 --- a/configure.in +++ b/configure.in @@ -4042,6 +4042,7 @@ MOZ_EXTENSIONS_ALL=" cookie wallet content-packs xml-rpc help p3p venkman inspec MOZ_IMG_DECODERS_DEFAULT="png gif jpeg bmp xbm icon" MOZ_IMG_ENCODERS_DEFAULT="png jpeg" MOZ_IPCD= +MOZ_JAVAXPCOM= MOZ_JSDEBUGGER=1 MOZ_JSLOADER=1 MOZ_LDAP_XPCOM= @@ -4239,6 +4240,7 @@ xulrunner) MOZ_XULRUNNER=1 MOZ_ENABLE_LIBXUL=1 MOZ_APP_VERSION=$MOZILLA_VERSION + MOZ_JAVAXPCOM=1 MOZ_EXTENSIONS_DEFAULT=" xml-rpc universalchardet webservices gnomevfs auth" AC_DEFINE(MOZ_XULRUNNER) ;; @@ -4893,6 +4895,61 @@ MOZ_ARG_DISABLE_BOOL(pref-extensions, MOZ_PREF_EXTENSIONS=, MOZ_PREF_EXTENSIONS=1 ) +dnl ======================================================== +dnl Javaconnect +dnl ======================================================== +MOZ_ARG_ENABLE_BOOL(javaconnect, +[ --enable-javaxpcom + Enable Java-XPCOM bridge], + MOZ_JAVAXPCOM=1, + MOZ_JAVAXPCOM= ) + +if test -n "${MOZ_JAVAXPCOM}"; then + case "$host_os" in + cygwin*|msvc*|mks*) + if test -n "$JAVA_HOME"; then + JAVA_HOME=`cygpath -u \`cygpath -m -s "$JAVA_HOME"\`` + fi + ;; + esac + + if test -n "${JAVA_INCLUDE_PATH}"; then + dnl Make sure jni.h exists in the given include path. + if test ! -f "$JAVA_INCLUDE_PATH/jni.h"; then + AC_MSG_ERROR([jni.h was not found in given include path $JAVA_INCLUDE_PATH.]) + fi + else + case "$target_os" in + darwin*) + dnl Default to java system location + JAVA_INCLUDE_PATH=/System/Library/Frameworks/JavaVM.framework/Headers + ;; + *) + dnl Try $JAVA_HOME + JAVA_INCLUDE_PATH="$JAVA_HOME/include" + ;; + esac + if test ! -f "$JAVA_INCLUDE_PATH/jni.h"; then + AC_MSG_ERROR([The header jni.h was not found. Set \$JAVA_HOME or use --with-java-include-path={java-include-dir}.]) + fi + fi + + if test -n "${JAVA_BIN_PATH}"; then + dnl Look for javac and jar in the specified path. + JAVA_PATH="$JAVA_BIN_PATH" + else + dnl No path specified, so look for javac and jar in $JAVA_HOME & $PATH. + JAVA_PATH="$JAVA_HOME/bin:$PATH" + fi + + AC_PATH_PROG(JAVAC, javac, :, $JAVA_PATH) + AC_PATH_PROG(JAR, jar, :, $JAVA_PATH) + if test -z "$JAVAC" || test "$JAVAC" = ":" || test -z "$JAR" || test "$JAR" = ":"; then + AC_MSG_ERROR([The programs javac and jar were not found. Set \$JAVA_HOME or use --with-java-bin-path={java-bin-dir}.]) + fi +fi + + dnl ======================================================== dnl = Enable compilation of specific extension modules dnl ======================================================== @@ -4974,54 +5031,9 @@ if test `echo "$MOZ_EXTENSIONS" | grep -c pref` -ne 0; then MOZ_EXTENSIONS=`echo $MOZ_EXTENSIONS | sed -e 's|pref||g'` fi -MOZ_ENABLE_JAVAXPCOM= -JAVAC= -JAR= if test `echo "$MOZ_EXTENSIONS" | grep -c java` -ne 0; then - MOZ_ENABLE_JAVAXPCOM=1 - - case "$host_os" in - cygwin*|msvc*|mks*) - if test -n "$JAVA_HOME"; then - JAVA_HOME=`cygpath -u \`cygpath -m -s "$JAVA_HOME"\`` - fi - ;; - esac - - if test -n "${JAVA_INCLUDE_PATH}"; then - dnl Make sure jni.h exists in the given include path. - if test ! -f "$JAVA_INCLUDE_PATH/jni.h"; then - AC_MSG_ERROR([jni.h was not found in given include path $JAVA_INCLUDE_PATH.]) - fi - else - case "$target_os" in - darwin*) - dnl Default to java system location - JAVA_INCLUDE_PATH=/System/Library/Frameworks/JavaVM.framework/Headers - ;; - *) - dnl Try $JAVA_HOME - JAVA_INCLUDE_PATH="$JAVA_HOME/include" - ;; - esac - if test ! -f "$JAVA_INCLUDE_PATH/jni.h"; then - AC_MSG_ERROR([The header jni.h was not found. Set \$JAVA_HOME or use --with-java-include-path={java-include-dir}.]) - fi - fi - - if test -n "${JAVA_BIN_PATH}"; then - dnl Look for javac and jar in the specified path. - JAVA_PATH="$JAVA_BIN_PATH" - else - dnl No path specified, so look for javac and jar are in $JAVA_HOME & $PATH. - JAVA_PATH="$JAVA_HOME/bin:$PATH" - fi - - AC_PATH_PROG(JAVAC, javac, :, $JAVA_PATH) - AC_PATH_PROG(JAR, jar, :, $JAVA_PATH) - if test -z "$JAVAC" || test "$JAVAC" = ":" || test -z "$JAR" || test "$JAR" = ":"; then - AC_MSG_ERROR([The programs javac and jar were not found. Set \$JAVA_HOME or use --with-java-bin-path={java-bin-dir}.]) - fi + AC_MSG_WARN([java is no longer an extension, use --enable-javaxpcom to enable.]) + MOZ_EXTENSIONS=`echo $MOZ_EXTENSIONS | sed -e 's|java||g'` fi dnl Remove dupes @@ -6853,7 +6865,7 @@ AC_SUBST(MOZ_PLACES) AC_SUBST(MOZ_STORAGE) AC_SUBST(NS_PRINTING) -AC_SUBST(MOZ_ENABLE_JAVAXPCOM) +AC_SUBST(MOZ_JAVAXPCOM) AC_SUBST(JAVA_INCLUDE_PATH) AC_SUBST(JAVAC) AC_SUBST(JAR) diff --git a/extensions/java/xpcom/Makefile.in b/extensions/java/xpcom/Makefile.in index 8b7ecaf3e22..5afc4d730da 100644 --- a/extensions/java/xpcom/Makefile.in +++ b/extensions/java/xpcom/Makefile.in @@ -42,7 +42,15 @@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk -DIRS = tools/genifaces interfaces src +DIRS = src + +TOOL_DIRS = \ + tools/genifaces \ + interfaces \ + glue \ + $(NULL) include $(topsrcdir)/config/rules.mk +tools:: + $(MAKE) -C src jarfile diff --git a/extensions/java/xpcom/glue/Makefile.in b/extensions/java/xpcom/glue/Makefile.in new file mode 100644 index 00000000000..a89a8b32e92 --- /dev/null +++ b/extensions/java/xpcom/glue/Makefile.in @@ -0,0 +1,72 @@ +# ***** BEGIN LICENSE BLOCK ***** +# Version: MPL 1.1/GPL 2.0/LGPL 2.1 +# +# The contents of this file are subject to the Mozilla Public License Version +# 1.1 (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.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. +# +# The Original Code is Java XPCOM Bindings. +# +# The Initial Developer of the Original Code is +# IBM Corporation. +# Portions created by the Initial Developer are Copyright (C) 2005 +# IBM Corporation. All Rights Reserved. +# +# Contributor(s): +# Javier Pedemonte (jhpedemonte@gmail.com) +# +# Alternatively, the contents of this file may be used under the terms of +# either the GNU General Public License Version 2 or later (the "GPL"), or +# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), +# in which case the provisions of the GPL or the LGPL are applicable instead +# of those above. If you wish to allow use of your version of this file only +# under the terms of either the GPL or the LGPL, and not to allow others to +# use your version of this file under the terms of the MPL, indicate your +# decision by deleting the provisions above and replace them with the notice +# and other provisions required by the GPL or the LGPL. If you do not delete +# the provisions above, a recipient may use your version of this file under +# the terms of any one of the MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +DEPTH = ../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = javaxpcomglue +LIBRARY_NAME = javaxpcomglue + +# On Mac OS X, JNI libraries must end with a '.jnilib' extension +ifeq ($(OS_ARCH),Darwin) +DLL_SUFFIX = .jnilib +endif + +CPPSRCS = nsJavaXPCOMGlue.cpp + +DEFINES += -DXPCOM_GLUE + +LOCAL_INCLUDES = -I$(JAVA_INCLUDE_PATH) + +ifeq ($(OS_ARCH),WINNT) +LOCAL_INCLUDES += -I$(JAVA_INCLUDE_PATH)/win32 +else +LOCAL_INCLUDES += -I$(JAVA_INCLUDE_PATH)/linux +endif + +LOCAL_INCLUDES += -I$(topsrcdir)/xpcom/build + +EXTRA_DSO_LDOPTS = \ + $(XPCOM_STANDALONE_GLUE_LDOPTS) \ + $(NULL) + +include $(topsrcdir)/config/rules.mk + diff --git a/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp b/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp new file mode 100644 index 00000000000..d46f70f317b --- /dev/null +++ b/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp @@ -0,0 +1,285 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (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.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Java XPCOM Bindings. + * + * The Initial Developer of the Original Code is + * IBM Corporation. + * Portions created by the Initial Developer are Copyright (C) 2005 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Javier Pedemonte (jhpedemonte@gmail.com) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "jni.h" +#include "nsXPCOMPrivate.h" // for XPCOM_DLL defines. +#include "nsXPCOMGlue.h" +#include + +#define GRE_NATIVE(func) Java_org_mozilla_xpcom_internal_GREImpl_##func +#define XPCOM_NATIVE(func) Java_org_mozilla_xpcom_internal_XPCOMImpl_##func +#define JAVAPROXY_NATIVE(func) \ + Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_##func + + +/*********************** + * JNI Load & Unload + ***********************/ + +extern "C" JNIEXPORT jint JNICALL +JNI_OnLoad(JavaVM* vm, void* reserved) +{ + // Let the JVM know that we are using JDK 1.2 JNI features. + return JNI_VERSION_1_2; +} + +extern "C" JNIEXPORT void JNICALL +JNI_OnUnload(JavaVM* vm, void* reserved) +{ +} + +/******************************** + * Javaconnect JNI interfaces + ********************************/ + +typedef void (*JX_InitEmbeddingFunc) (JNIEnv*, jobject, jobject, jobject, + jobject); +typedef void (*JX_TermEmbeddingFunc) (JNIEnv*, jobject); +typedef jobject (*JX_InitXPCOMFunc) (JNIEnv*, jobject, jobject, jobject); +typedef void (*JX_ShutdownXPCOMFunc) (JNIEnv*, jobject, jobject); +typedef jobject (*JX_NewLocalFileFunc) (JNIEnv*, jobject, jstring, jboolean); +typedef jobject (*JX_GetComponentManagerFunc) (JNIEnv*, jobject); +typedef jobject (*JX_GetComponentRegistrarFunc) (JNIEnv*, jobject); +typedef jobject (*JX_GetServiceManagerFunc) (JNIEnv*, jobject); +typedef jobject (*JX_CallXPCOMMethodFunc) (JNIEnv*, jclass, jobject, jstring, + jobjectArray); +typedef void (*JX_FinalizeProxyFunc) (JNIEnv*, jclass, jobject); + +JX_InitEmbeddingFunc InitEmbedding; +JX_TermEmbeddingFunc TermEmbedding; +JX_InitXPCOMFunc InitXPCOM; +JX_ShutdownXPCOMFunc ShutdownXPCOM; +JX_NewLocalFileFunc NewLocalFile; +JX_GetComponentManagerFunc GetComponentManager; +JX_GetComponentRegistrarFunc GetComponentRegistrar; +JX_GetServiceManagerFunc GetServiceManager; +JX_CallXPCOMMethodFunc CallXPCOMMethod; +JX_FinalizeProxyFunc FinalizeProxy; + +static nsDynamicFunctionLoad funcs[] = { + { "Java_org_mozilla_xpcom_internal_GREImpl_initEmbedding", + (NSFuncPtr*) &InitEmbedding }, + { "Java_org_mozilla_xpcom_internal_GREImpl_termEmbedding", + (NSFuncPtr*) &TermEmbedding }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_initXPCOM", + (NSFuncPtr*) &InitXPCOM }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_shutdownXPCOM", + (NSFuncPtr*) &ShutdownXPCOM }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_newLocalFile", + (NSFuncPtr*) &NewLocalFile }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentManager", + (NSFuncPtr*) &GetComponentManager }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentRegistrar", + (NSFuncPtr*) &GetComponentRegistrar }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getServiceManager", + (NSFuncPtr*) &GetServiceManager }, + { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_callXPCOMMethod", + (NSFuncPtr*) &CallXPCOMMethod }, + { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_finalizeProxy", + (NSFuncPtr*) &FinalizeProxy }, + { nsnull, nsnull } +}; + +// Get path string from java.io.File object. +jstring +GetJavaFilePath(JNIEnv* env, jobject aFile) +{ + jclass clazz = env->FindClass("java/io/File"); + if (clazz) { + jmethodID pathMID = env->GetMethodID(clazz, "getCanonicalPath", + "()Ljava/lang/String;"); + if (pathMID) { + return (jstring) env->CallObjectMethod(aFile, pathMID); + } + } + + return nsnull; +} + + +// Calls XPCOMGlueStartup using the given java.io.File object, and loads +// the Javaconnect methods from the XUL shared library. +nsresult +Initialize(JNIEnv* env, jobject aXPCOMPath) +{ + jstring pathString = GetJavaFilePath(env, aXPCOMPath); + if (!pathString) + return NS_ERROR_FAILURE; + const char* path = env->GetStringUTFChars(pathString, nsnull); + if (!path) + return NS_ERROR_OUT_OF_MEMORY; + + int len = strlen(path); + char* xpcomPath = (char*) malloc(len + sizeof(XPCOM_DLL) + + sizeof(XPCOM_FILE_PATH_SEPARATOR) + 1); + if (!xpcomPath) + return NS_ERROR_OUT_OF_MEMORY; + sprintf(xpcomPath, "%s" XPCOM_FILE_PATH_SEPARATOR XPCOM_DLL, path); + + nsresult rv = XPCOMGlueStartup(xpcomPath); + free(xpcomPath); + if (NS_FAILED(rv)) + return rv; + + rv = XPCOMGlueLoadXULFunctions(funcs); + if (NS_FAILED(rv)) + return rv; + + return NS_OK; +} + +void +ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage) +{ + // Only throw this exception if one hasn't already been thrown, so we don't + // mask a previous exception/error. + if (env->ExceptionCheck()) + return; + + // If the error code we get is for an Out Of Memory error, try to throw an + // OutOfMemoryError. The JVM may have enough memory to create this error. + if (aErrorCode == NS_ERROR_OUT_OF_MEMORY) { + jclass clazz = env->FindClass("java/lang/OutOfMemoryError"); + if (clazz) { + env->ThrowNew(clazz, aMessage); + } + env->DeleteLocalRef(clazz); + return; + } + + // If the error was not handled above, then create an XPCOMException with the + // given error code. + jthrowable throwObj = nsnull; + jclass exceptionClass = env->FindClass("org/mozilla/xpcom/XPCOMException"); + if (exceptionClass) { + jmethodID mid = env->GetMethodID(exceptionClass, "", + "(JLjava/lang/String;)V"); + if (mid) { + throwObj = (jthrowable) env->NewObject(exceptionClass, mid, + (PRInt64) aErrorCode, + env->NewStringUTF(aMessage)); + } + } + NS_ASSERTION(throwObj, "Failed to create XPCOMException object"); + + // throw exception + if (throwObj) { + env->Throw(throwObj); + } +} + +extern "C" JNIEXPORT void JNICALL +GRE_NATIVE(initEmbeddingNative) (JNIEnv* env, jobject aObject, + jobject aLibXULDirectory, + jobject aAppDirectory, jobject aAppDirProvider) +{ + nsresult rv = Initialize(env, aLibXULDirectory); + if (NS_FAILED(rv)) { + ThrowException(env, rv, "Initialization failed in initEmbeddingNative"); + return; + } + + InitEmbedding(env, aObject, aLibXULDirectory, aAppDirectory, aAppDirProvider); +} + +extern "C" JNIEXPORT void JNICALL +GRE_NATIVE(termEmbedding) (JNIEnv *env, jobject aObject) +{ + TermEmbedding(env, aObject); + XPCOMGlueShutdown(); +} + +extern "C" JNIEXPORT jobject JNICALL +XPCOM_NATIVE(initXPCOMNative) (JNIEnv* env, jobject aObject, + jobject aMozBinDirectory, + jobject aAppFileLocProvider) +{ + nsresult rv = Initialize(env, aMozBinDirectory); + if (NS_FAILED(rv)) { + ThrowException(env, rv, "Initialization failed in initXPCOMNative"); + return nsnull; + } + + return InitXPCOM(env, aObject, aMozBinDirectory, aAppFileLocProvider); +} + +extern "C" JNIEXPORT void JNICALL +XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject aObject, jobject aServMgr) +{ + ShutdownXPCOM(env, aObject, aServMgr); + XPCOMGlueShutdown(); +} + +extern "C" JNIEXPORT jobject JNICALL +XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject aObject, jstring aPath, + jboolean aFollowLinks) +{ + return NewLocalFile(env, aObject, aPath, aFollowLinks); +} + +extern "C" JNIEXPORT jobject JNICALL +XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject aObject) +{ + return GetComponentManager(env, aObject); +} + +extern "C" JNIEXPORT jobject JNICALL +XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject aObject) +{ + return GetComponentRegistrar(env, aObject); +} + +extern "C" JNIEXPORT jobject JNICALL +XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject aObject) +{ + return GetServiceManager(env, aObject); +} + +extern "C" JNIEXPORT jobject JNICALL +JAVAPROXY_NATIVE(callXPCOMMethod) (JNIEnv *env, jclass that, jobject aJavaProxy, + jstring aMethodName, jobjectArray aParams) +{ + return CallXPCOMMethod(env, that, aJavaProxy, aMethodName, aParams); +} + +extern "C" JNIEXPORT void JNICALL +JAVAPROXY_NATIVE(finalizeProxyNative) (JNIEnv *env, jclass that, + jobject aJavaProxy) +{ + FinalizeProxy(env, that, aJavaProxy); +} + diff --git a/extensions/java/xpcom/nsJavaInterfaces.cpp b/extensions/java/xpcom/nsJavaInterfaces.cpp index 54d43a36394..75a44845b9d 100644 --- a/extensions/java/xpcom/nsJavaInterfaces.cpp +++ b/extensions/java/xpcom/nsJavaInterfaces.cpp @@ -35,6 +35,7 @@ * * ***** END LICENSE BLOCK ***** */ +#include "nsJavaInterfaces.h" #include "nsJavaWrapper.h" #include "nsJavaXPCOMBindingUtils.h" #include "nsJavaXPTCStub.h" @@ -50,9 +51,6 @@ #include "nsXULAppAPI.h" #include "nsILocalFile.h" -#define GRE_NATIVE(func) Java_org_mozilla_xpcom_internal_GREImpl_##func -#define XPCOM_NATIVE(func) Java_org_mozilla_xpcom_internal_XPCOMImpl_##func - nsresult InitEmbedding_Impl(JNIEnv* env, jobject aLibXULDirectory, @@ -89,9 +87,9 @@ InitEmbedding_Impl(JNIEnv* env, jobject aLibXULDirectory, return rv; } -extern "C" JX_EXPORT void JNICALL -GRE_NATIVE(initEmbeddingNative) (JNIEnv* env, jobject, jobject aLibXULDirectory, - jobject aAppDirectory, jobject aAppDirProvider) +extern "C" NS_EXPORT void +GRE_NATIVE(initEmbedding) (JNIEnv* env, jobject, jobject aLibXULDirectory, + jobject aAppDirectory, jobject aAppDirProvider) { nsresult rv = InitEmbedding_Impl(env, aLibXULDirectory, aAppDirectory, aAppDirProvider); @@ -102,7 +100,7 @@ GRE_NATIVE(initEmbeddingNative) (JNIEnv* env, jobject, jobject aLibXULDirectory, } } -extern "C" JX_EXPORT void JNICALL +extern "C" NS_EXPORT void GRE_NATIVE(termEmbedding) (JNIEnv *env, jobject) { // Free globals before calling XRE_TermEmbedding(), since we need some @@ -158,7 +156,7 @@ InitXPCOM_Impl(JNIEnv* env, jobject aMozBinDirectory, return rv; } -extern "C" JX_EXPORT jobject JNICALL +extern "C" NS_EXPORT jobject XPCOM_NATIVE(initXPCOM) (JNIEnv* env, jobject, jobject aMozBinDirectory, jobject aAppFileLocProvider) { @@ -173,7 +171,7 @@ XPCOM_NATIVE(initXPCOM) (JNIEnv* env, jobject, jobject aMozBinDirectory, return nsnull; } -extern "C" JX_EXPORT void JNICALL +extern "C" NS_EXPORT void XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr) { nsresult rv; @@ -202,7 +200,7 @@ XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr) ThrowException(env, rv, "NS_ShutdownXPCOM failed"); } -extern "C" JX_EXPORT jobject JNICALL +extern "C" NS_EXPORT jobject XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject, jstring aPath, jboolean aFollowLinks) { @@ -233,7 +231,7 @@ XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject, jstring aPath, return nsnull; } -extern "C" JX_EXPORT jobject JNICALL +extern "C" NS_EXPORT jobject XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject) { // Call XPCOM method @@ -252,7 +250,7 @@ XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject) return nsnull; } -extern "C" JX_EXPORT jobject JNICALL +extern "C" NS_EXPORT jobject XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject) { // Call XPCOM method @@ -271,7 +269,7 @@ XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject) return nsnull; } -extern "C" JX_EXPORT jobject JNICALL +extern "C" NS_EXPORT jobject XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject) { // Call XPCOM method diff --git a/extensions/java/xpcom/nsJavaWrapper.cpp b/extensions/java/xpcom/nsJavaWrapper.cpp index 08001f2b135..33cb62055b7 100644 --- a/extensions/java/xpcom/nsJavaWrapper.cpp +++ b/extensions/java/xpcom/nsJavaWrapper.cpp @@ -35,6 +35,7 @@ * * ***** END LICENSE BLOCK ***** */ +#include "nsJavaInterfaces.h" #include "nsJavaWrapper.h" #include "nsJavaXPTCStub.h" #include "nsJavaXPCOMBindingUtils.h" @@ -46,9 +47,6 @@ #include "prmem.h" #include "nsServiceManagerUtils.h" -#define JAVAPROXY_NATIVE(func) \ - Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_##func - static nsID nullID = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}}; @@ -1383,7 +1381,7 @@ QueryMethodInfo(nsIInterfaceInfo* aIInfo, const char* aMethodName, /** * org.mozilla.xpcom.XPCOMJavaProxy.internal.callXPCOMMethod */ -extern "C" JX_EXPORT jobject JNICALL +extern "C" NS_EXPORT jobject JAVAPROXY_NATIVE(callXPCOMMethod) (JNIEnv *env, jclass that, jobject aJavaProxy, jstring aMethodName, jobjectArray aParams) { @@ -1711,11 +1709,10 @@ GetXPCOMInstFromProxy(JNIEnv* env, jobject aJavaObject, void** aResult) } /** - * org.mozilla.xpcom.internal.XPCOMJavaProxy.finalizeProxyNative + * org.mozilla.xpcom.internal.XPCOMJavaProxy.finalizeProxy */ -extern "C" JX_EXPORT void JNICALL -JAVAPROXY_NATIVE(finalizeProxyNative) (JNIEnv *env, jclass that, - jobject aJavaProxy) +extern "C" NS_EXPORT void +JAVAPROXY_NATIVE(finalizeProxy) (JNIEnv *env, jclass that, jobject aJavaProxy) { #ifdef DEBUG_JAVAXPCOM PRUint32 xpcom_addr = 0; diff --git a/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp b/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp index e7330298296..fb0a89c2789 100644 --- a/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp +++ b/extensions/java/xpcom/nsJavaXPCOMBindingUtils.cpp @@ -96,24 +96,6 @@ PRBool gJavaXPCOMInitialized = PR_FALSE; PRLock* gJavaXPCOMLock = nsnull; -/****************************** - * JNI Load & Unload - ******************************/ -extern "C" JX_EXPORT jint JNICALL -JNI_OnLoad(JavaVM* vm, void* reserved) -{ - // Save pointer to JavaVM, which is valid across threads. - gCachedJVM = vm; - - // Let the JVM know that we are using JDK 1.2 JNI features. - return JNI_VERSION_1_2; -} - -extern "C" JX_EXPORT void JNICALL -JNI_OnUnload(JavaVM* vm, void* reserved) -{ -} - /****************************** * InitializeJavaGlobals ******************************/ @@ -123,6 +105,13 @@ InitializeJavaGlobals(JNIEnv *env) if (gJavaXPCOMInitialized) return PR_TRUE; + // Save pointer to JavaVM, which is valid across threads. + jint rc = env->GetJavaVM(&gCachedJVM); + if (rc != 0) { + NS_WARNING("Failed to get JavaVM"); + goto init_error; + } + jclass clazz; if (!(clazz = env->FindClass("java/lang/Object")) || !(hashCodeMID = env->GetMethodID(clazz, "hashCode","()I"))) @@ -912,23 +901,12 @@ ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage) // Create parameters and method signature. Max of 2 params. The error code // comes before the message string. - PRUint32 index = 0; - jvalue* args = new jvalue[2]; - if (!args) { - ThrowException(env, NS_ERROR_OUT_OF_MEMORY, - "Out of memory while throwing another exception"); - return; - } - - nsCAutoString methodSig("("); - if (aErrorCode) { - args[index++].j = aErrorCode; - methodSig.Append('J'); - } + PRInt64 errorCode = aErrorCode ? aErrorCode : NS_ERROR_FAILURE; + nsCAutoString methodSig("(J"); + jstring message = nsnull; if (aMessage) { - args[index].l = env->NewStringUTF(aMessage); - if (args[index].l == nsnull) { - delete[] args; + message = env->NewStringUTF(aMessage); + if (!message) { return; } methodSig.AppendLiteral("Ljava/lang/String;"); @@ -941,8 +919,9 @@ ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage) // reset it temporarily in order to throw the appropriate exception. if (xpcomExceptionClass == nsnull) { xpcomExceptionClass = env->FindClass("org/mozilla/xpcom/XPCOMException"); - if (!xpcomExceptionClass) + if (!xpcomExceptionClass) { return; + } } // create exception object @@ -950,7 +929,8 @@ ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage) jmethodID mid = env->GetMethodID(xpcomExceptionClass, "", methodSig.get()); if (mid) { - throwObj = (jthrowable) env->NewObjectA(xpcomExceptionClass, mid, args); + throwObj = (jthrowable) env->NewObject(xpcomExceptionClass, mid, errorCode, + message); } NS_ASSERTION(throwObj, "Failed to create XPCOMException object"); @@ -958,9 +938,6 @@ ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage) if (throwObj) { env->Throw(throwObj); } - - // cleanup - delete[] args; } nsAString* diff --git a/extensions/java/xpcom/src/GREImpl.java b/extensions/java/xpcom/src/GREImpl.java index 83fc74c6734..8df7d6012a5 100644 --- a/extensions/java/xpcom/src/GREImpl.java +++ b/extensions/java/xpcom/src/GREImpl.java @@ -45,35 +45,14 @@ public class GREImpl implements IGRE { public void initEmbedding(File aLibXULDirectory, File aAppDirectory, IAppFileLocProvider aAppDirProvider) { - loadDependentLibraries(aLibXULDirectory); - initEmbeddingNative(aLibXULDirectory, aAppDirectory, aAppDirProvider); - } - - private void loadDependentLibraries(File binPath) { + // load JNI library String path = ""; - if (binPath != null) { - path = binPath + File.separator; + if (aLibXULDirectory != null) { + path = aLibXULDirectory + File.separator; } + System.load(path + System.mapLibraryName("javaxpcomglue")); - System.load(path + System.mapLibraryName("nspr4")); - System.load(path + System.mapLibraryName("plds4")); - System.load(path + System.mapLibraryName("plc4")); - try { - /* try loading Win32 DLL */ - System.load(path + System.mapLibraryName("js3250")); - } catch (UnsatisfiedLinkError e) { } - try { - /* try loading Linux DLL */ - System.load(path + System.mapLibraryName("mozjs")); - } catch (UnsatisfiedLinkError e) { } - System.load(path + System.mapLibraryName("softokn3")); - System.load(path + System.mapLibraryName("nss3")); - System.load(path + System.mapLibraryName("smime3")); - System.load(path + System.mapLibraryName("ssl3")); - System.load(path + System.mapLibraryName("xul")); - System.load(path + System.mapLibraryName("xpcom")); - - System.load(path + System.mapLibraryName("javaxpcom")); + initEmbeddingNative(aLibXULDirectory, aAppDirectory, aAppDirProvider); } public native void initEmbeddingNative(File aLibXULDirectory, diff --git a/extensions/java/xpcom/src/Makefile.in b/extensions/java/xpcom/src/Makefile.in index c99eec04491..93ba6233aed 100755 --- a/extensions/java/xpcom/src/Makefile.in +++ b/extensions/java/xpcom/src/Makefile.in @@ -44,7 +44,7 @@ include $(DEPTH)/config/autoconf.mk MODULE = javaxpcom LIBRARY_NAME = javaxpcom -MOZILLA_INTERNAL_API = 1 +LIBXUL_LIBRARY = 1 # On Mac OS X, JNI libraries must end with a '.jnilib' extension ifeq ($(OS_ARCH),Darwin) @@ -53,7 +53,6 @@ endif REQUIRES = xpcom \ string \ - embed_base \ xulapp \ $(NULL) @@ -84,6 +83,9 @@ else LOCAL_INCLUDES += -I$(JAVA_INCLUDE_PATH)/linux endif +# necessary until we do 'cvs move' to put files in correct directories +LOCAL_INCLUDES += -I$(srcdir) + EXTRA_DSO_LDOPTS = \ $(LIBS_DIR) \ $(EXTRA_DSO_LIBS) \ @@ -132,6 +134,8 @@ $(JARFILE): org/mozilla/xpcom/internal/.class_done Makefile $(JARFILE_SRC): $(JARFILE) $(JAR) cf $@ -C _javagen org -libs:: $(JARFILE) +#libs:: $(JARFILE) +# $(INSTALL) $(IFLAGS1) $^ $(DIST)/bin +jarfile:: $(JARFILE) $(INSTALL) $(IFLAGS1) $^ $(DIST)/bin diff --git a/extensions/java/xpcom/src/XPCOMImpl.java b/extensions/java/xpcom/src/XPCOMImpl.java index bdaad106db1..f5514e92d71 100644 --- a/extensions/java/xpcom/src/XPCOMImpl.java +++ b/extensions/java/xpcom/src/XPCOMImpl.java @@ -43,18 +43,30 @@ import org.mozilla.xpcom.*; public class XPCOMImpl implements IXPCOM { + public nsIServiceManager initXPCOM(File aMozBinDirectory, + IAppFileLocProvider aAppFileLocProvider) { + // load JNI library + String path = ""; + if (aMozBinDirectory != null) { + path = aMozBinDirectory + File.separator; + } + System.load(path + System.mapLibraryName("javaxpcomglue")); + + return initXPCOMNative(aMozBinDirectory, aAppFileLocProvider); + } + + public native nsIServiceManager initXPCOMNative(File aMozBinDirectory, + IAppFileLocProvider aAppFileLocProvider); + + public native void shutdownXPCOM(nsIServiceManager aServMgr); + public native nsIComponentManager getComponentManager(); public native nsIComponentRegistrar getComponentRegistrar(); public native nsIServiceManager getServiceManager(); - public native nsIServiceManager initXPCOM(File aMozBinDirectory, - IAppFileLocProvider aAppFileLocProvider); - public native nsILocalFile newLocalFile(String aPath, boolean aFollowLinks); - public native void shutdownXPCOM(nsIServiceManager aServMgr); - } diff --git a/extensions/java/xpcom/src/dlldeps-javaxpcom.cpp b/extensions/java/xpcom/src/dlldeps-javaxpcom.cpp new file mode 100644 index 00000000000..2665ac364fc --- /dev/null +++ b/extensions/java/xpcom/src/dlldeps-javaxpcom.cpp @@ -0,0 +1,63 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (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.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Java XPCOM Bindings. + * + * The Initial Developer of the Original Code is + * IBM Corporation. + * Portions created by the Initial Developer are Copyright (C) 2005 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Javier Pedemonte (jhpedemonte@gmail.com) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsJavaInterfaces.h" + + +void XXXNeverCalled_javaxpcom() +{ + GRE_NATIVE(initEmbedding) (nsnull, nsnull, nsnull, nsnull, nsnull); + + GRE_NATIVE(termEmbedding) (nsnull, nsnull); + + XPCOM_NATIVE(initXPCOM) (nsnull, nsnull, nsnull, nsnull); + + XPCOM_NATIVE(shutdownXPCOM) (nsnull, nsnull, nsnull); + + XPCOM_NATIVE(newLocalFile) (nsnull, nsnull, nsnull, nsnull); + + XPCOM_NATIVE(getComponentManager) (nsnull, nsnull); + + XPCOM_NATIVE(getComponentRegistrar) (nsnull, nsnull); + + XPCOM_NATIVE(getServiceManager) (nsnull, nsnull); + + JAVAPROXY_NATIVE(callXPCOMMethod) (nsnull, nsnull, nsnull, nsnull, nsnull); + + JAVAPROXY_NATIVE(finalizeProxy) (nsnull, nsnull, nsnull); +} + diff --git a/extensions/java/xpcom/src/nsJavaInterfaces.h b/extensions/java/xpcom/src/nsJavaInterfaces.h new file mode 100644 index 00000000000..9b7879533e0 --- /dev/null +++ b/extensions/java/xpcom/src/nsJavaInterfaces.h @@ -0,0 +1,84 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (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.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Java XPCOM Bindings. + * + * The Initial Developer of the Original Code is + * IBM Corporation. + * Portions created by the Initial Developer are Copyright (C) 2005 + * IBM Corporation. All Rights Reserved. + * + * Contributor(s): + * Javier Pedemonte (jhpedemonte@gmail.com) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef _nsJavaInterfaces_h_ +#define _nsJavaInterfaces_h_ + +#include "jni.h" +#include "nscore.h" + +#define GRE_NATIVE(func) Java_org_mozilla_xpcom_internal_GREImpl_##func +#define XPCOM_NATIVE(func) Java_org_mozilla_xpcom_internal_XPCOMImpl_##func +#define JAVAPROXY_NATIVE(func) \ + Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_##func + + +extern "C" NS_EXPORT void +GRE_NATIVE(initEmbedding) (JNIEnv* env, jobject, jobject aLibXULDirectory, + jobject aAppDirectory, jobject aAppDirProvider); + +extern "C" NS_EXPORT void +GRE_NATIVE(termEmbedding) (JNIEnv *env, jobject); + +extern "C" NS_EXPORT jobject +XPCOM_NATIVE(initXPCOM) (JNIEnv* env, jobject, jobject aMozBinDirectory, + jobject aAppFileLocProvider); + +extern "C" NS_EXPORT void +XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr); + +extern "C" NS_EXPORT jobject +XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject, jstring aPath, + jboolean aFollowLinks); + +extern "C" NS_EXPORT jobject +XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject); + +extern "C" NS_EXPORT jobject +XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject); + +extern "C" NS_EXPORT jobject +XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject); + +extern "C" NS_EXPORT jobject +JAVAPROXY_NATIVE(callXPCOMMethod) (JNIEnv *env, jclass that, jobject aJavaProxy, + jstring aMethodName, jobjectArray aParams); + +extern "C" NS_EXPORT void +JAVAPROXY_NATIVE(finalizeProxy) (JNIEnv *env, jclass that, jobject aJavaProxy); + +#endif // _nsJavaInterfaces_h_ diff --git a/toolkit/library/libxul-config.mk b/toolkit/library/libxul-config.mk index 62e4c078dca..283af6e1d6c 100644 --- a/toolkit/library/libxul-config.mk +++ b/toolkit/library/libxul-config.mk @@ -228,3 +228,18 @@ ifneq (,$(filter mac cocoa,$(MOZ_WIDGET_TOOLKIT))) EXTRA_DSO_LIBS += macmorefiles_s EXTRA_DEPS += $(DIST)/lib/$(LIB_PREFIX)macmorefiles_s.$(LIB_SUFFIX) endif + +ifdef MOZ_JAVAXPCOM +LOCAL_INCLUDES += \ + -I$(topsrcdir)/extensions/java/xpcom/src \ + -I$(JAVA_INCLUDE_PATH) \ + $(NULL) +ifeq ($(OS_ARCH),WINNT) +CPPSRCS += dlldeps-javaxpcom.cpp +LOCAL_INCLUDES += -I$(JAVA_INCLUDE_PATH)/win32 +else +LOCAL_INCLUDES += -I$(JAVA_INCLUDE_PATH)/linux +endif +STATIC_LIBS += javaxpcom +endif + diff --git a/toolkit/library/libxul-rules.mk b/toolkit/library/libxul-rules.mk index 85752bd046a..218a232c1dc 100644 --- a/toolkit/library/libxul-rules.mk +++ b/toolkit/library/libxul-rules.mk @@ -84,3 +84,11 @@ dlldeps.cpp: $(topsrcdir)/xpcom/build/dlldeps.cpp dlldeps-obs.cpp: $(topsrcdir)/xpcom/obsolete/dlldeps-obs.cpp $(INSTALL) $^ . + +ifdef MOZ_JAVAXPCOM +export:: dlldeps-javaxpcom.cpp + +dlldeps-javaxpcom.cpp: $(topsrcdir)/extensions/java/xpcom/src/dlldeps-javaxpcom.cpp + $(INSTALL) $^ . +endif +