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

Generated by: LCOV version 1.10