LCOV - code coverage report
Current view: top level - libreoffice/stoc/source/javaloader - javaloader.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 138 0.0 %
Date: 2012-12-17 Functions: 0 14 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <cstdarg>
      21             : #include <osl/diagnose.h>
      22             : #include <osl/process.h>
      23             : 
      24             : #include <rtl/process.h>
      25             : #include <rtl/ustrbuf.hxx>
      26             : 
      27             : #include <uno/environment.h>
      28             : #include <uno/mapping.hxx>
      29             : #include "com/sun/star/uno/RuntimeException.hpp"
      30             : 
      31             : #include <cppuhelper/servicefactory.hxx>
      32             : 
      33             : #ifdef LINUX
      34             : #undef minor
      35             : #undef major
      36             : #endif
      37             : 
      38             : #include <com/sun/star/java/XJavaVM.hpp>
      39             : 
      40             : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
      41             : 
      42             : #include "jni.h"
      43             : 
      44             : #include <cppuhelper/factory.hxx>
      45             : #include <cppuhelper/implementationentry.hxx>
      46             : 
      47             : #include <cppuhelper/implbase2.hxx>
      48             : 
      49             : #include <com/sun/star/loader/XImplementationLoader.hpp>
      50             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      51             : #include <com/sun/star/lang/XServiceInfo.hpp>
      52             : #include <com/sun/star/lang/XInitialization.hpp>
      53             : #include <com/sun/star/registry/XRegistryKey.hpp>
      54             : 
      55             : #include "jvmaccess/unovirtualmachine.hxx"
      56             : #include "jvmaccess/virtualmachine.hxx"
      57             : 
      58             : using namespace ::com::sun::star::java;
      59             : using namespace ::com::sun::star::lang;
      60             : using namespace ::com::sun::star::loader;
      61             : using namespace ::com::sun::star::uno;
      62             : using namespace ::com::sun::star::registry;
      63             : 
      64             : using namespace ::cppu;
      65             : using namespace ::rtl;
      66             : using namespace ::osl;
      67             : 
      68             : namespace stoc_javaloader {
      69             : 
      70             : static Mutex & getInitMutex();
      71             : 
      72           0 : static Sequence< OUString > loader_getSupportedServiceNames()
      73             : {
      74           0 :     Sequence< OUString > seqNames(2);
      75             :     seqNames.getArray()[0] = OUString(
      76           0 :         RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Java") );
      77           0 :     seqNames.getArray()[1] = OUString(
      78           0 :         RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Java2") );
      79           0 :     return seqNames;
      80             : }
      81             : 
      82           0 : static OUString loader_getImplementationName()
      83             : {
      84             :     return OUString(
      85           0 :         RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.stoc.JavaComponentLoader" ) );
      86             : }
      87             : 
      88             : class JavaComponentLoader : public WeakImplHelper2<XImplementationLoader, XServiceInfo>
      89             : {
      90             :     css::uno::Reference<XComponentContext> m_xComponentContext;
      91             :     /** Do not use m_javaLoader directly. Instead use getJavaLoader.
      92             :      */
      93             :     css::uno::Reference<XImplementationLoader> m_javaLoader;
      94             :     /** The retured Reference contains a null pointer if the office is not configured
      95             :         to run java.
      96             : 
      97             :         @exception com::sun::star::uno::RuntimeException
      98             :         If the Java implementation of the loader could not be obtained, for reasons other
      99             :         then that java was not configured the RuntimeException is thrown.
     100             :      */
     101             :     const css::uno::Reference<XImplementationLoader> & getJavaLoader();
     102             : 
     103             : 
     104             : public:
     105             :     JavaComponentLoader(const css::uno::Reference<XComponentContext> & xCtx)
     106             :         throw(RuntimeException);
     107             :     virtual ~JavaComponentLoader() throw();
     108             : 
     109             : public:
     110             :     // XServiceInfo
     111             :     virtual OUString SAL_CALL getImplementationName() throw(RuntimeException);
     112             :     virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
     113             :         throw(RuntimeException);
     114             :     virtual Sequence<OUString> SAL_CALL getSupportedServiceNames()
     115             :         throw(RuntimeException);
     116             : 
     117             :     // XImplementationLoader
     118             :     virtual css::uno::Reference<XInterface> SAL_CALL activate(
     119             :         const OUString& implementationName, const OUString& implementationLoaderUrl,
     120             :         const OUString& locationUrl, const css::uno::Reference<XRegistryKey>& xKey)
     121             :         throw(CannotActivateFactoryException, RuntimeException);
     122             :     virtual sal_Bool SAL_CALL writeRegistryInfo(
     123             :         const css::uno::Reference<XRegistryKey>& xKey,
     124             :         const OUString& implementationLoaderUrl, const OUString& locationUrl)
     125             :         throw(CannotRegisterImplementationException, RuntimeException);
     126             : };
     127             : 
     128           0 : const css::uno::Reference<XImplementationLoader> & JavaComponentLoader::getJavaLoader()
     129             : {
     130           0 :     MutexGuard aGuard(getInitMutex());
     131             : 
     132           0 :     if (m_javaLoader.is())
     133           0 :         return m_javaLoader;
     134             : 
     135           0 :     uno_Environment * pJava_environment = NULL;
     136           0 :     uno_Environment * pUno_environment = NULL;
     137           0 :     typelib_InterfaceTypeDescription * pType_XImplementationLoader = 0;
     138             : 
     139             :     try {
     140             :         // get a java vm, where we can create a loader
     141             :         css::uno::Reference<XJavaVM> javaVM_xJavaVM(
     142           0 :             m_xComponentContext->getValueByName(
     143             :                 OUString(RTL_CONSTASCII_USTRINGPARAM(
     144             :                              "/singletons/"
     145           0 :                              "com.sun.star.java.theJavaVirtualMachine"))),
     146           0 :             UNO_QUERY_THROW);
     147             : 
     148             :         // Use the special protocol of XJavaVM.getJavaVM:  If the passed in
     149             :         // process ID has an extra 17th byte of value one, the returned any
     150             :         // contains a pointer to a jvmaccess::UnoVirtualMachine, instead of the
     151             :         // underlying JavaVM pointer:
     152           0 :         Sequence<sal_Int8> processID(17);
     153           0 :         rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8 *>(processID.getArray()));
     154           0 :         processID[16] = 1;
     155             : 
     156             :         // We get a non-refcounted pointer to a jvmaccess::UnoVirtualMachine
     157             :         // from the XJavaVM service (the pointer is guaranteed to be valid
     158             :         // as long as our reference to the XJavaVM service lasts), and
     159             :         // convert the non-refcounted pointer into a refcounted one
     160             :         // immediately:
     161             :         OSL_ENSURE(sizeof (sal_Int64)
     162             :                         >= sizeof (jvmaccess::UnoVirtualMachine *),
     163             :                     "Pointer cannot be represented as sal_Int64");
     164             :         sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
     165           0 :             static_cast< jvmaccess::UnoVirtualMachine * >(0));
     166           0 :         javaVM_xJavaVM->getJavaVM(processID) >>= nPointer;
     167             :         rtl::Reference< jvmaccess::UnoVirtualMachine > xVirtualMachine(
     168           0 :             reinterpret_cast< jvmaccess::UnoVirtualMachine * >(nPointer));
     169           0 :         if (!xVirtualMachine.is())
     170             :         {
     171             :             //throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     172             :             //   "javaloader error - JavaVirtualMachine service could not provide a VM")),
     173             :             //   css::uno::Reference<XInterface>());
     174             :             // We must not throw a RuntimeException, because this might end the applications.
     175             :             // It is ok if java components
     176             :             // are not working because the office can be installed without Java support.
     177             :             SAL_WARN("stoc", "getJavaVM returned null");
     178           0 :             return m_javaLoader; // null-ref
     179             :         }
     180             : 
     181             :         try
     182             :         {
     183             :             jvmaccess::VirtualMachine::AttachGuard aGuard2(
     184           0 :                 xVirtualMachine->getVirtualMachine());
     185           0 :             JNIEnv * pJNIEnv = aGuard2.getEnvironment();
     186             : 
     187             :             // instantiate the java JavaLoader
     188           0 :             jclass jcClassLoader = pJNIEnv->FindClass("java/lang/ClassLoader");
     189           0 :             if(pJNIEnv->ExceptionOccurred())
     190             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     191             :                     "javaloader error - could not find class java/lang/ClassLoader")),
     192           0 :                     css::uno::Reference<XInterface>());
     193             :             jmethodID jmLoadClass = pJNIEnv->GetMethodID(
     194             :                 jcClassLoader, "loadClass",
     195           0 :                 "(Ljava/lang/String;)Ljava/lang/Class;");
     196           0 :             if(pJNIEnv->ExceptionOccurred())
     197             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     198             :                     "javaloader error - could not find method java/lang/ClassLoader.loadClass")),
     199           0 :                     css::uno::Reference<XInterface>());
     200             :             jvalue arg;
     201             :             arg.l = pJNIEnv->NewStringUTF(
     202           0 :                 "com.sun.star.comp.loader.JavaLoader");
     203           0 :             if(pJNIEnv->ExceptionOccurred())
     204             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     205             :                     "javaloader error - could not create string")),
     206           0 :                     css::uno::Reference<XInterface>());
     207             :             jclass jcJavaLoader = static_cast< jclass >(
     208             :                 pJNIEnv->CallObjectMethodA(
     209           0 :                     static_cast< jobject >(xVirtualMachine->getClassLoader()),
     210           0 :                     jmLoadClass, &arg));
     211           0 :             if(pJNIEnv->ExceptionOccurred())
     212             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     213             :                     "javaloader error - could not find class com/sun/star/comp/loader/JavaLoader")),
     214           0 :                     css::uno::Reference<XInterface>());
     215           0 :             jmethodID jmJavaLoader_init = pJNIEnv->GetMethodID(jcJavaLoader, "<init>", "()V");
     216           0 :             if(pJNIEnv->ExceptionOccurred())
     217             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     218             :                     "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")),
     219           0 :                     css::uno::Reference<XInterface>());
     220           0 :             jobject joJavaLoader = pJNIEnv->NewObject(jcJavaLoader, jmJavaLoader_init);
     221           0 :             if(pJNIEnv->ExceptionOccurred())
     222             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     223             :                     "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")),
     224           0 :                     css::uno::Reference<XInterface>());
     225             : 
     226             :             // map the java JavaLoader to this environment
     227           0 :             OUString sJava(RTL_CONSTASCII_USTRINGPARAM("java"));
     228             :             uno_getEnvironment(&pJava_environment, sJava.pData,
     229           0 :                                 xVirtualMachine.get());
     230           0 :             if(!pJava_environment)
     231             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     232           0 :                     "javaloader error - no Java environment available")), css::uno::Reference<XInterface>());
     233             : 
     234             :             // why is there no convinient contructor?
     235           0 :             OUString sCppu_current_lb_name(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
     236           0 :             uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, NULL);
     237           0 :             if(!pUno_environment)
     238             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     239           0 :                     "javaloader error - no C++ environment available")), css::uno::Reference<XInterface>());
     240             : 
     241           0 :             Mapping java_curr(pJava_environment, pUno_environment);
     242           0 :             if(!java_curr.is())
     243             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     244           0 :                     "javaloader error - no mapping from java to C++ ")), css::uno::Reference<XInterface>());
     245             : 
     246             :             // release java environment
     247           0 :             pJava_environment->release(pJava_environment);
     248           0 :             pJava_environment = NULL;
     249             : 
     250             :             // release uno environment
     251           0 :             pUno_environment->release(pUno_environment);
     252           0 :             pUno_environment = NULL;
     253             : 
     254           0 :             getCppuType((css::uno::Reference<XImplementationLoader> *) 0).
     255           0 :                 getDescription((typelib_TypeDescription **) & pType_XImplementationLoader);
     256           0 :             if(!pType_XImplementationLoader)
     257             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     258             :                     "javaloader error - no type information for XImplementationLoader")),
     259           0 :                     css::uno::Reference<XInterface>());
     260             : 
     261             :             m_javaLoader = css::uno::Reference<XImplementationLoader>(reinterpret_cast<XImplementationLoader *>(
     262           0 :                             java_curr.mapInterface(joJavaLoader, pType_XImplementationLoader)));
     263           0 :             pJNIEnv->DeleteLocalRef( joJavaLoader );
     264           0 :             if(!m_javaLoader.is())
     265             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     266             :                     "javaloader error - mapping of java XImplementationLoader to c++ failed")),
     267           0 :                     css::uno::Reference<XInterface>());
     268             : 
     269           0 :             typelib_typedescription_release(reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
     270           0 :             pType_XImplementationLoader = NULL;
     271             :         }
     272           0 :         catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
     273             :         {
     274             :             throw RuntimeException(
     275             :                 OUString(RTL_CONSTASCII_USTRINGPARAM(
     276             :                                 "jvmaccess::VirtualMachine::AttachGuard"
     277           0 :                                 "::CreationException")),0);
     278             :         }
     279             : 
     280             :         // set the service manager at the javaloader
     281           0 :         css::uno::Reference<XInitialization> javaLoader_XInitialization(m_javaLoader, UNO_QUERY);
     282           0 :         if(!javaLoader_XInitialization.is())
     283             :             throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     284             :                 "javaloader error - initialization of java javaloader failed, no XInitialization")),
     285           0 :                 css::uno::Reference<XInterface>());
     286             : 
     287           0 :         Any any;
     288             :         any <<= css::uno::Reference<XMultiComponentFactory>(
     289           0 :             m_xComponentContext->getServiceManager());
     290             : 
     291           0 :         javaLoader_XInitialization->initialize(Sequence<Any>(&any, 1));
     292             :     }
     293           0 :     catch(RuntimeException &) {
     294           0 :         if(pJava_environment)
     295           0 :             pJava_environment->release(pJava_environment);
     296             : 
     297           0 :         if(pUno_environment)
     298           0 :             pUno_environment->release(pUno_environment);
     299             : 
     300           0 :         if(pType_XImplementationLoader)
     301             :             typelib_typedescription_release(
     302           0 :                 reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
     303           0 :         throw;
     304             :     }
     305             :     OSL_TRACE("javaloader.cxx: mapped javaloader - 0x%x", m_javaLoader.get());
     306           0 :     return m_javaLoader;
     307             : }
     308             : 
     309           0 : JavaComponentLoader::JavaComponentLoader(const css::uno::Reference<XComponentContext> & xCtx) throw(RuntimeException) :
     310           0 :     m_xComponentContext(xCtx)
     311             : 
     312             : {
     313             : 
     314           0 : }
     315             : 
     316           0 : JavaComponentLoader::~JavaComponentLoader() throw()
     317             : {
     318           0 : }
     319             : 
     320             : // XServiceInfo
     321           0 : OUString SAL_CALL JavaComponentLoader::getImplementationName()
     322             :     throw(::com::sun::star::uno::RuntimeException)
     323             : {
     324           0 :     return loader_getImplementationName();
     325             : }
     326             : 
     327           0 : sal_Bool SAL_CALL JavaComponentLoader::supportsService(const OUString & ServiceName)
     328             :     throw(::com::sun::star::uno::RuntimeException)
     329             : {
     330           0 :     sal_Bool bSupport = sal_False;
     331             : 
     332           0 :     Sequence<OUString> aSNL = getSupportedServiceNames();
     333           0 :     const OUString * pArray = aSNL.getArray();
     334           0 :     for(sal_Int32 i = 0; i < aSNL.getLength() && !bSupport; ++ i)
     335           0 :         bSupport = pArray[i] == ServiceName;
     336             : 
     337           0 :     return bSupport;
     338             : }
     339             : 
     340           0 : Sequence<OUString> SAL_CALL JavaComponentLoader::getSupportedServiceNames()
     341             :     throw(::com::sun::star::uno::RuntimeException)
     342             : {
     343           0 :     return loader_getSupportedServiceNames();
     344             : }
     345             : 
     346             : 
     347             : 
     348             : // XImplementationLoader
     349           0 : sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(
     350             :     const css::uno::Reference<XRegistryKey> & xKey, const OUString & blabla,
     351             :     const OUString & rLibName)
     352             :     throw(CannotRegisterImplementationException, RuntimeException)
     353             : {
     354           0 :     const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader();
     355           0 :     if (loader.is())
     356           0 :         return loader->writeRegistryInfo(xKey, blabla, rLibName);
     357             :     else
     358             :         throw CannotRegisterImplementationException(
     359           0 :             OUString(RTL_CONSTASCII_USTRINGPARAM("Could not create Java implementation loader")), NULL);
     360             : }
     361             : 
     362             : 
     363           0 : css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader::activate(
     364             :     const OUString & rImplName, const OUString & blabla, const OUString & rLibName,
     365             :     const css::uno::Reference<XRegistryKey> & xKey)
     366             :     throw(CannotActivateFactoryException, RuntimeException)
     367             : {
     368           0 :     const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader();
     369           0 :     if (loader.is())
     370           0 :         return loader->activate(rImplName, blabla, rLibName, xKey);
     371             :     else
     372             :         throw CannotActivateFactoryException(
     373           0 :             OUString(RTL_CONSTASCII_USTRINGPARAM("Could not create Java implementation loader")), NULL);
     374             : }
     375             : 
     376           0 : static Mutex & getInitMutex()
     377             : {
     378             :     static Mutex * pMutex = 0;
     379           0 :     if( ! pMutex )
     380             :     {
     381           0 :         MutexGuard guard( Mutex::getGlobalMutex() );
     382           0 :         if( ! pMutex )
     383             :         {
     384           0 :             static Mutex mutex;
     385           0 :             pMutex = &mutex;
     386           0 :         }
     387             :     }
     388           0 :     return *pMutex;
     389             : }
     390             : 
     391           0 : css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader_CreateInstance(const css::uno::Reference<XComponentContext> & xCtx) throw(Exception)
     392             : {
     393           0 :     css::uno::Reference<XInterface> xRet;
     394             : 
     395             :     try {
     396           0 :         MutexGuard guard( getInitMutex() );
     397             :         // The javaloader is never destroyed and there can be only one!
     398             :         // Note that the first context wins ....
     399             :         static css::uno::Reference< XInterface > *pStaticRef = 0;
     400           0 :         if( pStaticRef )
     401             :         {
     402           0 :             xRet = *pStaticRef;
     403             :         }
     404             :         else
     405             :         {
     406           0 :             xRet = *new JavaComponentLoader(xCtx);
     407           0 :             pStaticRef = new css::uno::Reference< XInterface > ( xRet );
     408           0 :         }
     409             :     }
     410           0 :     catch(const RuntimeException & runtimeException) {
     411           0 :         OString message = OUStringToOString(runtimeException.Message, RTL_TEXTENCODING_ASCII_US);
     412           0 :         osl_trace("javaloader - could not init javaloader cause of %s", message.getStr());
     413           0 :         throw;
     414             :     }
     415             : 
     416           0 :     return xRet;
     417             : }
     418             : 
     419             : } //end namespace
     420             : 
     421             : 
     422             : using namespace stoc_javaloader;
     423             : 
     424             : static struct ImplementationEntry g_entries[] =
     425             : {
     426             :     {
     427             :         JavaComponentLoader_CreateInstance, loader_getImplementationName,
     428             :         loader_getSupportedServiceNames, createSingleComponentFactory,
     429             :         0 , 0
     430             :     },
     431             :     { 0, 0, 0, 0, 0, 0 }
     432             : };
     433             : 
     434             : extern "C"
     435             : {
     436           0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL javaloader_component_getFactory(
     437             :     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
     438             : {
     439           0 :     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
     440             : }
     441             : }
     442             : 
     443             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10