From dee2288e08fadd7ad27ae2fdfb1375b392c75ea9 Mon Sep 17 00:00:00 2001 From: "pedemont%us.ibm.com" Date: Mon, 3 Apr 2006 21:16:49 +0000 Subject: [PATCH] Bug 329522 - Use RegisterNatives for associating Java native methods with their implementation. r=bsmedberg --- .../java/xpcom/glue/nsJavaXPCOMGlue.cpp | 264 ++++++++---------- extensions/java/xpcom/src/GREImpl.java | 17 +- .../java/xpcom/src/JavaXPCOMMethods.java | 58 ++++ extensions/java/xpcom/src/Makefile.in | 8 +- extensions/java/xpcom/src/XPCOMImpl.java | 15 +- 5 files changed, 194 insertions(+), 168 deletions(-) create mode 100644 extensions/java/xpcom/src/JavaXPCOMMethods.java diff --git a/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp b/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp index ae8446963e2f..5b5fdd679fc7 100644 --- a/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp +++ b/extensions/java/xpcom/glue/nsJavaXPCOMGlue.cpp @@ -13,9 +13,8 @@ * * 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 + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by the Initial Developer are Copyright (C) 2006 * IBM Corporation. All Rights Reserved. * * Contributor(s): @@ -40,11 +39,6 @@ #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 @@ -66,59 +60,25 @@ JNI_OnUnload(JavaVM* vm, void* reserved) * JavaXPCOM 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); -typedef jboolean (*JX_IsSameXPCOMObjectFunc) (JNIEnv*, jclass, jobject, - jobject); +#define JXM_NATIVE(func) Java_org_mozilla_xpcom_internal_JavaXPCOMMethods_##func -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; -JX_IsSameXPCOMObjectFunc IsSameXPCOMObject; - -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 }, - { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_isSameXPCOMObject", - (NSFuncPtr*) &IsSameXPCOMObject }, - { nsnull, nsnull } +enum { + kFunc_InitEmbedding, + kFunc_TermEmbedding, + kFunc_InitXPCOM, + kFunc_ShutdownXPCOM, + kFunc_GetComponentManager, + kFunc_GetComponentRegistrar, + kFunc_GetServiceManager, + kFunc_NewLocalFile, + kFunc_CallXPCOMMethod, + kFunc_FinalizeProxy, + kFunc_IsSameXPCOMObject }; +#define JX_NUM_FUNCS 11 + + // Get path string from java.io.File object. jstring GetJavaFilePath(JNIEnv* env, jobject aFile) @@ -135,11 +95,10 @@ GetJavaFilePath(JNIEnv* env, jobject aFile) return nsnull; } - // Calls XPCOMGlueStartup using the given java.io.File object, and loads // the JavaXPCOM methods from the XUL shared library. nsresult -Initialize(JNIEnv* env, jobject aXPCOMPath) +LoadXULMethods(JNIEnv* env, jobject aXPCOMPath, void** aFunctions) { jstring pathString = GetJavaFilePath(env, aXPCOMPath); if (!pathString) @@ -160,6 +119,32 @@ Initialize(JNIEnv* env, jobject aXPCOMPath) if (NS_FAILED(rv)) return rv; + nsDynamicFunctionLoad funcs[] = { + { "Java_org_mozilla_xpcom_internal_GREImpl_initEmbedding", + (NSFuncPtr*) &aFunctions[kFunc_InitEmbedding] }, + { "Java_org_mozilla_xpcom_internal_GREImpl_termEmbedding", + (NSFuncPtr*) &aFunctions[kFunc_TermEmbedding] }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_initXPCOM", + (NSFuncPtr*) &aFunctions[kFunc_InitXPCOM] }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_shutdownXPCOM", + (NSFuncPtr*) &aFunctions[kFunc_ShutdownXPCOM] }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentManager", + (NSFuncPtr*) &aFunctions[kFunc_GetComponentManager] }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getComponentRegistrar", + (NSFuncPtr*) &aFunctions[kFunc_GetComponentRegistrar] }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_getServiceManager", + (NSFuncPtr*) &aFunctions[kFunc_GetServiceManager] }, + { "Java_org_mozilla_xpcom_internal_XPCOMImpl_newLocalFile", + (NSFuncPtr*) &aFunctions[kFunc_NewLocalFile] }, + { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_callXPCOMMethod", + (NSFuncPtr*) &aFunctions[kFunc_CallXPCOMMethod] }, + { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_finalizeProxy", + (NSFuncPtr*) &aFunctions[kFunc_FinalizeProxy] }, + { "Java_org_mozilla_xpcom_internal_XPCOMJavaProxy_isSameXPCOMObject", + (NSFuncPtr*) &aFunctions[kFunc_IsSameXPCOMObject] }, + { nsnull, nsnull } + }; + rv = XPCOMGlueLoadXULFunctions(funcs); if (NS_FAILED(rv)) return rv; @@ -207,91 +192,88 @@ ThrowException(JNIEnv* env, const nsresult aErrorCode, const char* aMessage) } } -extern "C" JNIEXPORT void JNICALL -GRE_NATIVE(initEmbeddingNative) (JNIEnv* env, jobject aObject, - jobject aLibXULDirectory, - jobject aAppDirectory, jobject aAppDirProvider) +// Register the JavaXPCOM native methods. This associates a native Java +// method with its C implementation. +nsresult +RegisterNativeMethods(JNIEnv* env, void** aFunctions) { - nsresult rv = Initialize(env, aLibXULDirectory); - if (NS_FAILED(rv)) { - ThrowException(env, rv, "Initialization failed in initEmbeddingNative"); - return; + JNINativeMethod gre_methods[] = { + { "initEmbeddingNative", + "(Ljava/io/File;Ljava/io/File;Lorg/mozilla/xpcom/IAppFileLocProvider;)V", + (void*) aFunctions[kFunc_InitEmbedding] }, + { "termEmbedding", "()V", + (void*) aFunctions[kFunc_TermEmbedding] }, + }; + + JNINativeMethod xpcom_methods[] = { + { "initXPCOMNative", + "(Ljava/io/File;Lorg/mozilla/xpcom/IAppFileLocProvider;)Lorg/mozilla/xpcom/nsIServiceManager;", + (void*) aFunctions[kFunc_InitXPCOM] }, + { "shutdownXPCOM", "(Lorg/mozilla/xpcom/nsIServiceManager;)V", + (void*) aFunctions[kFunc_ShutdownXPCOM] }, + { "getComponentManager", "()Lorg/mozilla/xpcom/nsIComponentManager;", + (void*) aFunctions[kFunc_GetComponentManager] }, + { "getComponentRegistrar", "()Lorg/mozilla/xpcom/nsIComponentRegistrar;", + (void*) aFunctions[kFunc_GetComponentRegistrar] }, + { "getServiceManager", "()Lorg/mozilla/xpcom/nsIServiceManager;", + (void*) aFunctions[kFunc_GetServiceManager] }, + { "newLocalFile", "(Ljava/lang/String;Z)Lorg/mozilla/xpcom/nsILocalFile;", + (void*) aFunctions[kFunc_NewLocalFile] } + }; + + JNINativeMethod proxy_methods[] = { + { "callXPCOMMethod", + "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", + (void*) aFunctions[kFunc_CallXPCOMMethod] }, + { "finalizeProxyNative", "(Ljava/lang/Object;)V", + (void*) aFunctions[kFunc_FinalizeProxy] }, + { "isSameXPCOMObject", "(Ljava/lang/Object;Ljava/lang/Object;)Z", + (void*) aFunctions[kFunc_IsSameXPCOMObject] } + }; + + jint rc = -1; + jclass clazz = env->FindClass("org/mozilla/xpcom/internal/GREImpl"); + if (clazz) { + rc = env->RegisterNatives(clazz, gre_methods, + sizeof(gre_methods) / sizeof(gre_methods[0])); + } + NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE); + + rc = -1; + clazz = env->FindClass("org/mozilla/xpcom/internal/XPCOMImpl"); + if (clazz) { + rc = env->RegisterNatives(clazz, xpcom_methods, + sizeof(xpcom_methods) / sizeof(xpcom_methods[0])); + } + NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE); + + rc = -1; + clazz = env->FindClass("org/mozilla/xpcom/internal/XPCOMJavaProxy"); + if (clazz) { + rc = env->RegisterNatives(clazz, proxy_methods, + sizeof(proxy_methods) / sizeof(proxy_methods[0])); + } + NS_ENSURE_TRUE(rc == 0, NS_ERROR_FAILURE); + + return NS_OK; +} + +// Load the JavaXPCOM methods from the XUL shared library, and registers them +// as Java native methods. +extern "C" JNIEXPORT void JNICALL +JXM_NATIVE(registerJavaXPCOMMethodsNative) (JNIEnv *env, jclass that, + jobject aXPCOMPath) +{ + void* functions[JX_NUM_FUNCS]; + memset(functions, 0, JX_NUM_FUNCS * sizeof(void*)); + + nsresult rv = LoadXULMethods(env, aXPCOMPath, functions); + if (NS_SUCCEEDED(rv)) { + rv = RegisterNativeMethods(env, functions); } - 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; + ThrowException(env, rv, "Failed to register JavaXPCOM methods"); } - - 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); -} - -extern "C" JNIEXPORT jboolean JNICALL -JAVAPROXY_NATIVE(isSameXPCOMObject) (JNIEnv *env, jclass that, jobject aProxy1, - jobject aProxy2) -{ - return IsSameXPCOMObject(env, that, aProxy1, aProxy2); } diff --git a/extensions/java/xpcom/src/GREImpl.java b/extensions/java/xpcom/src/GREImpl.java index 8df7d6012a58..c23625416974 100644 --- a/extensions/java/xpcom/src/GREImpl.java +++ b/extensions/java/xpcom/src/GREImpl.java @@ -13,9 +13,8 @@ * * 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) 2004 + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by the Initial Developer are Copyright (C) 2006 * IBM Corporation. All Rights Reserved. * * Contributor(s): @@ -41,17 +40,11 @@ import java.io.*; import org.mozilla.xpcom.*; -public class GREImpl implements IGRE { +public class GREImpl extends JavaXPCOMMethods implements IGRE { public void initEmbedding(File aLibXULDirectory, File aAppDirectory, - IAppFileLocProvider aAppDirProvider) { - // load JNI library - String path = ""; - if (aLibXULDirectory != null) { - path = aLibXULDirectory + File.separator; - } - System.load(path + System.mapLibraryName("javaxpcomglue")); - + IAppFileLocProvider aAppDirProvider) { + registerJavaXPCOMMethods(aLibXULDirectory); initEmbeddingNative(aLibXULDirectory, aAppDirectory, aAppDirProvider); } diff --git a/extensions/java/xpcom/src/JavaXPCOMMethods.java b/extensions/java/xpcom/src/JavaXPCOMMethods.java new file mode 100644 index 000000000000..27d525c10f09 --- /dev/null +++ b/extensions/java/xpcom/src/JavaXPCOMMethods.java @@ -0,0 +1,58 @@ +/* ***** 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) 2006 + * 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 ***** */ + +package org.mozilla.xpcom.internal; + +import java.io.File; + + +public class JavaXPCOMMethods { + + public static void registerJavaXPCOMMethods(File aLibXULDirectory) { + // load JNI library + String path = ""; + if (aLibXULDirectory != null) { + path = aLibXULDirectory + File.separator; + } + System.load(path + System.mapLibraryName("javaxpcomglue")); + + registerJavaXPCOMMethodsNative(aLibXULDirectory); + } + + public static native void + registerJavaXPCOMMethodsNative(File aLibXULDirectory); +} + diff --git a/extensions/java/xpcom/src/Makefile.in b/extensions/java/xpcom/src/Makefile.in index 758bcdf9853c..c1431f829981 100755 --- a/extensions/java/xpcom/src/Makefile.in +++ b/extensions/java/xpcom/src/Makefile.in @@ -13,9 +13,8 @@ # # 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 +# The Initial Developer of the Original Code is IBM Corporation. +# Portions created by the Initial Developer are Copyright (C) 2006 # IBM Corporation. All Rights Reserved. # # Contributor(s): @@ -73,6 +72,7 @@ JAVA_SRCS = \ $(srcdir)/../XPCOMJavaProxyBase.java \ $(srcdir)/GREImpl.java \ $(srcdir)/XPCOMImpl.java \ + $(srcdir)/JavaXPCOMMethods.java \ $(NULL) LOCAL_INCLUDES = -I$(JAVA_INCLUDE_PATH) @@ -127,6 +127,6 @@ $(JARFILE_SRC): $(JARFILE) #libs:: $(JARFILE) # $(INSTALL) $(IFLAGS1) $^ $(DIST)/bin -jarfile:: $(JARFILE) +jarfile:: $(JARFILE) $(JARFILE_SRC) $(INSTALL) $(IFLAGS1) $^ $(DIST)/bin diff --git a/extensions/java/xpcom/src/XPCOMImpl.java b/extensions/java/xpcom/src/XPCOMImpl.java index f5514e92d71c..633d53f5e95c 100644 --- a/extensions/java/xpcom/src/XPCOMImpl.java +++ b/extensions/java/xpcom/src/XPCOMImpl.java @@ -13,9 +13,8 @@ * * 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) 2004 + * The Initial Developer of the Original Code is IBM Corporation. + * Portions created by the Initial Developer are Copyright (C) 2006 * IBM Corporation. All Rights Reserved. * * Contributor(s): @@ -41,17 +40,11 @@ import java.io.*; import org.mozilla.xpcom.*; -public class XPCOMImpl implements IXPCOM { +public class XPCOMImpl extends JavaXPCOMMethods 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")); - + registerJavaXPCOMMethods(aMozBinDirectory); return initXPCOMNative(aMozBinDirectory, aAppFileLocProvider); }