From 203d723f5f7a9dc1462ba188ae657dfb535bc2a8 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Fri, 6 Dec 2002 09:51:54 +0000 Subject: #105077# Adapted to modified javavm service (based on new jvmaccess/virtualmachine). --- stoc/source/javaloader/javaloader.cxx | 256 ++++++++++++++++------------------ stoc/source/javaloader/javaloader.xml | 3 +- stoc/source/javaloader/makefile.mk | 8 +- 3 files changed, 127 insertions(+), 140 deletions(-) (limited to 'stoc/source/javaloader') diff --git a/stoc/source/javaloader/javaloader.cxx b/stoc/source/javaloader/javaloader.cxx index 1e57c54fcf03..28f0ce9173f6 100644 --- a/stoc/source/javaloader/javaloader.cxx +++ b/stoc/source/javaloader/javaloader.cxx @@ -2,9 +2,9 @@ * * $RCSfile: javaloader.cxx,v $ * - * $Revision: 1.15 $ + * $Revision: 1.16 $ * - * last change: $Author: dbo $ $Date: 2002-10-29 09:25:58 $ + * last change: $Author: sb $ $Date: 2002-12-06 10:51:53 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -79,9 +79,8 @@ #endif #include -#include -#include +#include #include "jni.h" @@ -92,11 +91,13 @@ #include #include -#include #include #include #include +#include "jvmaccess/virtualmachine.hxx" + +namespace css = com::sun::star; using namespace ::com::sun::star::java; using namespace ::com::sun::star::lang; @@ -145,12 +146,10 @@ namespace stoc_javaloader { } class JavaComponentLoader : public WeakImplHelper2 { - Reference _xSMgr; - Reference _xCtx; - Reference _javaLoader; + css::uno::Reference _javaLoader; public: - JavaComponentLoader(const Reference & xCtx) throw(RuntimeException); + JavaComponentLoader(const css::uno::Reference & xCtx) throw(RuntimeException); virtual ~JavaComponentLoader() throw(); public: @@ -160,141 +159,137 @@ namespace stoc_javaloader { virtual Sequence SAL_CALL getSupportedServiceNames() throw(RuntimeException); // XImplementationLoader - virtual Reference SAL_CALL activate(const OUString& implementationName, const OUString& implementationLoaderUrl, const OUString& locationUrl, const Reference& xKey) throw(CannotActivateFactoryException, RuntimeException); - virtual sal_Bool SAL_CALL writeRegistryInfo(const Reference& xKey, const OUString& implementationLoaderUrl, const OUString& locationUrl) throw(CannotRegisterImplementationException, RuntimeException); + virtual css::uno::Reference SAL_CALL activate(const OUString& implementationName, const OUString& implementationLoaderUrl, const OUString& locationUrl, const css::uno::Reference& xKey) throw(CannotActivateFactoryException, RuntimeException); + virtual sal_Bool SAL_CALL writeRegistryInfo(const css::uno::Reference& xKey, const OUString& implementationLoaderUrl, const OUString& locationUrl) throw(CannotRegisterImplementationException, RuntimeException); }; - JavaComponentLoader::JavaComponentLoader(const Reference & xCtx) throw(RuntimeException) - : _xSMgr(xCtx->getServiceManager()) - , _xCtx( xCtx ) + JavaComponentLoader::JavaComponentLoader(const css::uno::Reference & xCtx) throw(RuntimeException) { sal_Int32 size = 0; - uno_Environment ** ppJava_environments = NULL; + uno_Environment * pJava_environment = NULL; uno_Environment * pUno_environment = NULL; typelib_InterfaceTypeDescription * pType_XImplementationLoader = 0; - JNIEnv * pJNIEnv = NULL; - - Reference xJavaThreadRegister_11; - Reference javaVM; - JavaVM * pJavaVM = NULL; try { // get a java vm, where we can create a loader - javaVM = _xSMgr->createInstanceWithContext( - OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine")), - xCtx ); - if(!javaVM.is()) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - could not create com.sun.star.java.JavaVirtualMachine")), Reference()); - Reference javaVM_xJavaVM(javaVM, UNO_QUERY); - if(!javaVM_xJavaVM.is()) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - could not get XJavaVM interface from JavaVirtualMachine service")), Reference()); - - Sequence processID(16); + css::uno::Reference javaVM_xJavaVM( + xCtx->getValueByName( + OUString(RTL_CONSTASCII_USTRINGPARAM( + "/singletons/" + "com.sun.star.java.theJavaVirtualMachine"))), + UNO_QUERY_THROW); + + // Use the special protocol of XJavaVM.getJavaVM: If the passed in + // process ID has an extra 17th byte of value zero, the returned any + // contains a pointer to a jvmaccess::VirtualMachine, instead of the + // underlying JavaVM pointer: + Sequence processID(17); rtl_getGlobalProcessId(reinterpret_cast(processID.getArray())); - - if(javaVM_xJavaVM->getJavaVM(processID).hasValue()) - pJavaVM = *(JavaVM **)(javaVM_xJavaVM->getJavaVM(processID).getValue()); - if(!pJavaVM) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - JavaVirtualMachine service could not provide a VM")), Reference()); - - xJavaThreadRegister_11 = Reference(javaVM, UNO_QUERY); - if(xJavaThreadRegister_11.is()) - xJavaThreadRegister_11->registerThread(); - - pJavaVM->AttachCurrentThread((void **)&pJNIEnv, (void *)NULL); - - - // instantiate the java JavaLoader - jclass jcJavaLoader = pJNIEnv->FindClass("com/sun/star/comp/loader/JavaLoader"); - if(pJNIEnv->ExceptionOccurred()) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - could not find class com/sun/star/comp/loader/JavaLoader")), Reference()); - jmethodID jmJavaLoader_init = pJNIEnv->GetMethodID(jcJavaLoader, "", "()V"); - if(pJNIEnv->ExceptionOccurred()) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")), Reference()); - jobject joJavaLoader = pJNIEnv->NewObject(jcJavaLoader, jmJavaLoader_init); - if(pJNIEnv->ExceptionOccurred()) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")), Reference()); - - - // map the java JavaLoader to this environment - OUString sJava(RTL_CONSTASCII_USTRINGPARAM("java")); - - uno_getRegisteredEnvironments(&ppJava_environments, &size, (uno_memAlloc)malloc, sJava.pData); - if(!size) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - no Java environment available")), Reference()); - - // why is there no convinient contructor? - OUString sCppu_current_lb_name(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME)); - uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, NULL); - if(!pUno_environment) + processID[16] = 0; + + // We get a non-refcounted pointer to a jvmaccess::VirtualMachine + // from the XJavaVM service (the pointer is guaranteed to be valid + // as long as our reference to the XJavaVM service lasts), and + // convert the non-refcounted pointer into a refcounted one + // immediately: + OSL_ENSURE(sizeof (sal_Int64) + >= sizeof (jvmaccess::VirtualMachine *), + "Pointer cannot be represented as sal_Int64"); + sal_Int64 nPointer = reinterpret_cast< sal_Int64 >( + static_cast< jvmaccess::VirtualMachine * >(0)); + javaVM_xJavaVM->getJavaVM(processID) >>= nPointer; + rtl::Reference< jvmaccess::VirtualMachine > xVirtualMachine( + reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer)); + if (!xVirtualMachine.is()) throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - no C++ environment available")), Reference()); + "javaloader error - JavaVirtualMachine service could not provide a VM")), css::uno::Reference()); - Mapping java_curr(ppJava_environments[0], pUno_environment); - if(!java_curr.is()) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - no mapping from java to C++ ")), Reference()); - - // release all java environments - for(sal_Int32 i = 0; i < size; ++ i) - ppJava_environments[i]->release(ppJava_environments[i]); - size = 0; - free(ppJava_environments); + try + { + jvmaccess::VirtualMachine::AttachGuard aGuard(xVirtualMachine); + JNIEnv * pJNIEnv = aGuard.getEnvironment(); + + // instantiate the java JavaLoader + jclass jcJavaLoader = pJNIEnv->FindClass("com/sun/star/comp/loader/JavaLoader"); + if(pJNIEnv->ExceptionOccurred()) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - could not find class com/sun/star/comp/loader/JavaLoader")), css::uno::Reference()); + jmethodID jmJavaLoader_init = pJNIEnv->GetMethodID(jcJavaLoader, "", "()V"); + if(pJNIEnv->ExceptionOccurred()) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")), css::uno::Reference()); + jobject joJavaLoader = pJNIEnv->NewObject(jcJavaLoader, jmJavaLoader_init); + if(pJNIEnv->ExceptionOccurred()) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")), css::uno::Reference()); + + // map the java JavaLoader to this environment + OUString sJava(RTL_CONSTASCII_USTRINGPARAM("java")); + uno_getEnvironment(&pJava_environment, sJava.pData, + xVirtualMachine.get()); + if(!pJava_environment) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - no Java environment available")), css::uno::Reference()); + + // why is there no convinient contructor? + OUString sCppu_current_lb_name(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME)); + uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, NULL); + if(!pUno_environment) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - no C++ environment available")), css::uno::Reference()); + + Mapping java_curr(pJava_environment, pUno_environment); + if(!java_curr.is()) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - no mapping from java to C++ ")), css::uno::Reference()); + + // release java environment + pJava_environment->release(pJava_environment); + pJava_environment = NULL; + + // release uno environment + pUno_environment->release(pUno_environment); + pUno_environment = NULL; - // release uno environment - pUno_environment->release(pUno_environment); - pUno_environment = NULL; + getCppuType((css::uno::Reference *) 0).getDescription((typelib_TypeDescription **) & pType_XImplementationLoader); + if(!pType_XImplementationLoader) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - no type information for XImplementationLoader")), css::uno::Reference()); - getCppuType((Reference *) 0).getDescription((typelib_TypeDescription **) & pType_XImplementationLoader); - if(!pType_XImplementationLoader) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - no type information for XImplementationLoader")), Reference()); - - _javaLoader = Reference(reinterpret_cast( + _javaLoader = css::uno::Reference(reinterpret_cast( java_curr.mapInterface(joJavaLoader, pType_XImplementationLoader))); - pJNIEnv->DeleteLocalRef( joJavaLoader ); - if(!_javaLoader.is()) - throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - mapping of java XImplementationLoader to c++ failed")), Reference()); - - typelib_typedescription_release(reinterpret_cast(pType_XImplementationLoader)); - pType_XImplementationLoader = NULL; - + pJNIEnv->DeleteLocalRef( joJavaLoader ); + if(!_javaLoader.is()) + throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( + "javaloader error - mapping of java XImplementationLoader to c++ failed")), css::uno::Reference()); - // detach from java vm - if(xJavaThreadRegister_11.is()) { - xJavaThreadRegister_11->revokeThread(); - - if(!xJavaThreadRegister_11->isThreadAttached()) - pJavaVM->DetachCurrentThread(); + typelib_typedescription_release(reinterpret_cast(pType_XImplementationLoader)); + pType_XImplementationLoader = NULL; + } + catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &) + { + throw RuntimeException( + OUString(RTL_CONSTASCII_USTRINGPARAM( + "jvmaccess::VirtualMachine::AttachGuard" + "::CreationException")), + 0); } - else - pJavaVM->DetachCurrentThread(); - - pJNIEnv = NULL; // set the service manager at the javaloader - Reference javaLoader_XInitialization(_javaLoader, UNO_QUERY); + css::uno::Reference javaLoader_XInitialization(_javaLoader, UNO_QUERY); if(!javaLoader_XInitialization.is()) throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM( - "javaloader error - initialization of java javaloader failed, no XInitialization")), Reference()); + "javaloader error - initialization of java javaloader failed, no XInitialization")), css::uno::Reference()); Any any; - any <<= _xSMgr; + any <<= css::uno::Reference( + xCtx->getServiceManager()); javaLoader_XInitialization->initialize(Sequence(&any, 1)); } - catch(RuntimeException & runtimeException) { - // release all java environments - for(sal_Int32 i = 0; i < size; ++ i) - ppJava_environments[i]->release(ppJava_environments[i]); + catch(RuntimeException &) { + if(pJava_environment) + pJava_environment->release(pJava_environment); if(pUno_environment) pUno_environment->release(pUno_environment); @@ -302,17 +297,6 @@ namespace stoc_javaloader { if(pType_XImplementationLoader) typelib_typedescription_release(reinterpret_cast(pType_XImplementationLoader)); - // detach from java vm - if(pJNIEnv) - if(xJavaThreadRegister_11.is()) { - xJavaThreadRegister_11->revokeThread(); - - if(!xJavaThreadRegister_11->isThreadAttached()) - pJavaVM->DetachCurrentThread(); - } - else - pJavaVM->DetachCurrentThread(); - throw; } OSL_TRACE("javaloader.cxx: mapped javaloader - 0x%x", _javaLoader.get()); @@ -347,17 +331,17 @@ namespace stoc_javaloader { // XImplementationLoader - sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(const Reference & xKey, const OUString & blabla, const OUString & rLibName) + sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(const css::uno::Reference & xKey, const OUString & blabla, const OUString & rLibName) throw(CannotRegisterImplementationException, RuntimeException) { return _javaLoader->writeRegistryInfo(xKey, blabla, rLibName); } - Reference SAL_CALL JavaComponentLoader::activate(const OUString & rImplName, + css::uno::Reference SAL_CALL JavaComponentLoader::activate(const OUString & rImplName, const OUString & blabla, const OUString & rLibName, - const Reference & xKey) + const css::uno::Reference & xKey) throw(CannotActivateFactoryException, RuntimeException) { return _javaLoader->activate(rImplName, blabla, rLibName, xKey); @@ -377,15 +361,15 @@ namespace stoc_javaloader { } return *pMutex; } - Reference SAL_CALL JavaComponentLoader_CreateInstance(const Reference & xCtx) throw(Exception) + css::uno::Reference SAL_CALL JavaComponentLoader_CreateInstance(const css::uno::Reference & xCtx) throw(Exception) { - Reference xRet; + css::uno::Reference xRet; try { MutexGuard guard( getInitMutex() ); // The javaloader is never destroyed and there can be only one! // Note that the first context wins .... - static Reference< XInterface > *pStaticRef = 0; + static css::uno::Reference< XInterface > *pStaticRef = 0; if( pStaticRef ) { xRet = *pStaticRef; @@ -393,7 +377,7 @@ namespace stoc_javaloader { else { xRet = *new JavaComponentLoader(xCtx); - pStaticRef = new Reference< XInterface > ( xRet ); + pStaticRef = new css::uno::Reference< XInterface > ( xRet ); } } catch(RuntimeException & runtimeException) { diff --git a/stoc/source/javaloader/javaloader.xml b/stoc/source/javaloader/javaloader.xml index 722bd9477b69..8854c40ea8e4 100644 --- a/stoc/source/javaloader/javaloader.xml +++ b/stoc/source/javaloader/javaloader.xml @@ -13,7 +13,6 @@ com.sun.star.loader.Java2 ... - com.sun.star.java.XJavaThreadRegister_11 com.sun.star.java.XJavaVM com.sun.star.lang.IllegalArgumentException com.sun.star.lang.XInitialization @@ -33,7 +32,9 @@ cppu tools sal + jvmaccess cppuhelper2$(COM) cppu2 sal2 + jvmaccess3$(COM) diff --git a/stoc/source/javaloader/makefile.mk b/stoc/source/javaloader/makefile.mk index a77aece69a26..08dc84749fd3 100644 --- a/stoc/source/javaloader/makefile.mk +++ b/stoc/source/javaloader/makefile.mk @@ -2,9 +2,9 @@ # # $RCSfile: makefile.mk,v $ # -# $Revision: 1.13 $ +# $Revision: 1.14 $ # -# last change: $Author: mh $ $Date: 2001-10-18 12:08:40 $ +# last change: $Author: sb $ $Date: 2002-12-06 10:51:54 $ # # The Contents of this file are made available subject to the terms of # either of the following licenses @@ -85,7 +85,9 @@ SHL1TARGET= $(TARGET) SHL1STDLIBS=\ $(CPPUHELPERLIB) \ $(CPPULIB) \ - $(SALLIB) + $(SALLIB) \ + $(JVMACCESSLIB) + SHL1VERSIONMAP=$(TARGET).map SHL1DEPN= SHL1IMPLIB= i$(TARGET) -- cgit