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

Generated by: LCOV version 1.10