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

Generated by: LCOV version 1.10