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

Generated by: LCOV version 1.11