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

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <cstdarg>
      21             : #include <osl/diagnose.h>
      22             : #include <osl/process.h>
      23             : 
      24             : #include <rtl/process.h>
      25             : #include <rtl/ustrbuf.hxx>
      26             : 
      27             : #include <uno/environment.h>
      28             : #include <uno/mapping.hxx>
      29             : #include "com/sun/star/uno/RuntimeException.hpp"
      30             : 
      31             : #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           0 : static Sequence< OUString > loader_getSupportedServiceNames()
      71             : {
      72           0 :     Sequence< OUString > seqNames(2);
      73             :     seqNames.getArray()[0] = OUString(
      74           0 :         RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Java") );
      75           0 :     seqNames.getArray()[1] = OUString(
      76           0 :         RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Java2") );
      77           0 :     return seqNames;
      78             : }
      79             : 
      80           0 : static OUString loader_getImplementationName()
      81             : {
      82             :     return OUString(
      83           0 :         RTL_CONSTASCII_USTRINGPARAM( "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 com::sun::star::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);
     110             :     virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
     111             :         throw(RuntimeException);
     112             :     virtual Sequence<OUString> SAL_CALL getSupportedServiceNames()
     113             :         throw(RuntimeException);
     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);
     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);
     124             : };
     125             : 
     126           0 : const css::uno::Reference<XImplementationLoader> & JavaComponentLoader::getJavaLoader()
     127             : {
     128           0 :     MutexGuard aGuard(getInitMutex());
     129             : 
     130           0 :     if (m_javaLoader.is())
     131           0 :         return m_javaLoader;
     132             : 
     133           0 :     uno_Environment * pJava_environment = NULL;
     134           0 :     uno_Environment * pUno_environment = NULL;
     135           0 :     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           0 :             m_xComponentContext->getValueByName(
     141             :                 OUString(RTL_CONSTASCII_USTRINGPARAM(
     142             :                              "/singletons/"
     143           0 :                              "com.sun.star.java.theJavaVirtualMachine"))),
     144           0 :             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           0 :         Sequence<sal_Int8> processID(17);
     151           0 :         rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8 *>(processID.getArray()));
     152           0 :         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             :         OSL_ENSURE(sizeof (sal_Int64)
     160             :                         >= sizeof (jvmaccess::UnoVirtualMachine *),
     161             :                     "Pointer cannot be represented as sal_Int64");
     162             :         sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
     163           0 :             static_cast< jvmaccess::UnoVirtualMachine * >(0));
     164           0 :         javaVM_xJavaVM->getJavaVM(processID) >>= nPointer;
     165             :         rtl::Reference< jvmaccess::UnoVirtualMachine > xVirtualMachine(
     166           0 :             reinterpret_cast< jvmaccess::UnoVirtualMachine * >(nPointer));
     167           0 :         if (!xVirtualMachine.is())
     168             :         {
     169             :             //throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     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           0 :                 xVirtualMachine->getVirtualMachine());
     183           0 :             JNIEnv * pJNIEnv = aGuard2.getEnvironment();
     184             : 
     185             :             // instantiate the java JavaLoader
     186           0 :             jclass jcClassLoader = pJNIEnv->FindClass("java/lang/ClassLoader");
     187           0 :             if(pJNIEnv->ExceptionOccurred())
     188             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     189             :                     "javaloader error - could not find class java/lang/ClassLoader")),
     190           0 :                     css::uno::Reference<XInterface>());
     191             :             jmethodID jmLoadClass = pJNIEnv->GetMethodID(
     192             :                 jcClassLoader, "loadClass",
     193           0 :                 "(Ljava/lang/String;)Ljava/lang/Class;");
     194           0 :             if(pJNIEnv->ExceptionOccurred())
     195             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     196             :                     "javaloader error - could not find method java/lang/ClassLoader.loadClass")),
     197           0 :                     css::uno::Reference<XInterface>());
     198             :             jvalue arg;
     199             :             arg.l = pJNIEnv->NewStringUTF(
     200           0 :                 "com.sun.star.comp.loader.JavaLoader");
     201           0 :             if(pJNIEnv->ExceptionOccurred())
     202             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     203             :                     "javaloader error - could not create string")),
     204           0 :                     css::uno::Reference<XInterface>());
     205             :             jclass jcJavaLoader = static_cast< jclass >(
     206             :                 pJNIEnv->CallObjectMethodA(
     207           0 :                     static_cast< jobject >(xVirtualMachine->getClassLoader()),
     208           0 :                     jmLoadClass, &arg));
     209           0 :             if(pJNIEnv->ExceptionOccurred())
     210             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     211             :                     "javaloader error - could not find class com/sun/star/comp/loader/JavaLoader")),
     212           0 :                     css::uno::Reference<XInterface>());
     213           0 :             jmethodID jmJavaLoader_init = pJNIEnv->GetMethodID(jcJavaLoader, "<init>", "()V");
     214           0 :             if(pJNIEnv->ExceptionOccurred())
     215             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     216             :                     "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")),
     217           0 :                     css::uno::Reference<XInterface>());
     218           0 :             jobject joJavaLoader = pJNIEnv->NewObject(jcJavaLoader, jmJavaLoader_init);
     219           0 :             if(pJNIEnv->ExceptionOccurred())
     220             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     221             :                     "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")),
     222           0 :                     css::uno::Reference<XInterface>());
     223             : 
     224             :             // map the java JavaLoader to this environment
     225           0 :             OUString sJava(RTL_CONSTASCII_USTRINGPARAM("java"));
     226             :             uno_getEnvironment(&pJava_environment, sJava.pData,
     227           0 :                                 xVirtualMachine.get());
     228           0 :             if(!pJava_environment)
     229             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     230           0 :                     "javaloader error - no Java environment available")), css::uno::Reference<XInterface>());
     231             : 
     232             :             // why is there no convinient contructor?
     233           0 :             OUString sCppu_current_lb_name(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
     234           0 :             uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, NULL);
     235           0 :             if(!pUno_environment)
     236             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     237           0 :                     "javaloader error - no C++ environment available")), css::uno::Reference<XInterface>());
     238             : 
     239           0 :             Mapping java_curr(pJava_environment, pUno_environment);
     240           0 :             if(!java_curr.is())
     241             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     242           0 :                     "javaloader error - no mapping from java to C++ ")), css::uno::Reference<XInterface>());
     243             : 
     244             :             // release java environment
     245           0 :             pJava_environment->release(pJava_environment);
     246           0 :             pJava_environment = NULL;
     247             : 
     248             :             // release uno environment
     249           0 :             pUno_environment->release(pUno_environment);
     250           0 :             pUno_environment = NULL;
     251             : 
     252           0 :             getCppuType((css::uno::Reference<XImplementationLoader> *) 0).
     253           0 :                 getDescription((typelib_TypeDescription **) & pType_XImplementationLoader);
     254           0 :             if(!pType_XImplementationLoader)
     255             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     256             :                     "javaloader error - no type information for XImplementationLoader")),
     257           0 :                     css::uno::Reference<XInterface>());
     258             : 
     259             :             m_javaLoader = css::uno::Reference<XImplementationLoader>(reinterpret_cast<XImplementationLoader *>(
     260           0 :                             java_curr.mapInterface(joJavaLoader, pType_XImplementationLoader)));
     261           0 :             pJNIEnv->DeleteLocalRef( joJavaLoader );
     262           0 :             if(!m_javaLoader.is())
     263             :                 throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     264             :                     "javaloader error - mapping of java XImplementationLoader to c++ failed")),
     265           0 :                     css::uno::Reference<XInterface>());
     266             : 
     267           0 :             typelib_typedescription_release(reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
     268           0 :             pType_XImplementationLoader = NULL;
     269             :         }
     270           0 :         catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
     271             :         {
     272             :             throw RuntimeException(
     273             :                 OUString(RTL_CONSTASCII_USTRINGPARAM(
     274             :                                 "jvmaccess::VirtualMachine::AttachGuard"
     275           0 :                                 "::CreationException")),0);
     276             :         }
     277             : 
     278             :         // set the service manager at the javaloader
     279           0 :         css::uno::Reference<XInitialization> javaLoader_XInitialization(m_javaLoader, UNO_QUERY);
     280           0 :         if(!javaLoader_XInitialization.is())
     281             :             throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
     282             :                 "javaloader error - initialization of java javaloader failed, no XInitialization")),
     283           0 :                 css::uno::Reference<XInterface>());
     284             : 
     285           0 :         Any any;
     286             :         any <<= css::uno::Reference<XMultiComponentFactory>(
     287           0 :             m_xComponentContext->getServiceManager());
     288             : 
     289           0 :         javaLoader_XInitialization->initialize(Sequence<Any>(&any, 1));
     290             :     }
     291           0 :     catch(RuntimeException &) {
     292           0 :         if(pJava_environment)
     293           0 :             pJava_environment->release(pJava_environment);
     294             : 
     295           0 :         if(pUno_environment)
     296           0 :             pUno_environment->release(pUno_environment);
     297             : 
     298           0 :         if(pType_XImplementationLoader)
     299             :             typelib_typedescription_release(
     300           0 :                 reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
     301           0 :         throw;
     302             :     }
     303             :     OSL_TRACE("javaloader.cxx: mapped javaloader - 0x%x", m_javaLoader.get());
     304           0 :     return m_javaLoader;
     305             : }
     306             : 
     307           0 : JavaComponentLoader::JavaComponentLoader(const css::uno::Reference<XComponentContext> & xCtx) throw(RuntimeException) :
     308           0 :     m_xComponentContext(xCtx)
     309             : 
     310             : {
     311             : 
     312           0 : }
     313             : 
     314           0 : JavaComponentLoader::~JavaComponentLoader() throw()
     315             : {
     316           0 : }
     317             : 
     318             : // XServiceInfo
     319           0 : OUString SAL_CALL JavaComponentLoader::getImplementationName()
     320             :     throw(::com::sun::star::uno::RuntimeException)
     321             : {
     322           0 :     return loader_getImplementationName();
     323             : }
     324             : 
     325           0 : sal_Bool SAL_CALL JavaComponentLoader::supportsService(const OUString & ServiceName)
     326             :     throw(::com::sun::star::uno::RuntimeException)
     327             : {
     328           0 :     sal_Bool bSupport = sal_False;
     329             : 
     330           0 :     Sequence<OUString> aSNL = getSupportedServiceNames();
     331           0 :     const OUString * pArray = aSNL.getArray();
     332           0 :     for(sal_Int32 i = 0; i < aSNL.getLength() && !bSupport; ++ i)
     333           0 :         bSupport = pArray[i] == ServiceName;
     334             : 
     335           0 :     return bSupport;
     336             : }
     337             : 
     338           0 : Sequence<OUString> SAL_CALL JavaComponentLoader::getSupportedServiceNames()
     339             :     throw(::com::sun::star::uno::RuntimeException)
     340             : {
     341           0 :     return loader_getSupportedServiceNames();
     342             : }
     343             : 
     344             : 
     345             : 
     346             : // XImplementationLoader
     347           0 : sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(
     348             :     const css::uno::Reference<XRegistryKey> & xKey, const OUString & blabla,
     349             :     const OUString & rLibName)
     350             :     throw(CannotRegisterImplementationException, RuntimeException)
     351             : {
     352           0 :     const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader();
     353           0 :     if (loader.is())
     354           0 :         return loader->writeRegistryInfo(xKey, blabla, rLibName);
     355             :     else
     356             :         throw CannotRegisterImplementationException(
     357           0 :             OUString(RTL_CONSTASCII_USTRINGPARAM("Could not create Java implementation loader")), NULL);
     358             : }
     359             : 
     360             : 
     361           0 : css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader::activate(
     362             :     const OUString & rImplName, const OUString & blabla, const OUString & rLibName,
     363             :     const css::uno::Reference<XRegistryKey> & xKey)
     364             :     throw(CannotActivateFactoryException, RuntimeException)
     365             : {
     366           0 :     const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader();
     367           0 :     if (loader.is())
     368           0 :         return loader->activate(rImplName, blabla, rLibName, xKey);
     369             :     else
     370             :         throw CannotActivateFactoryException(
     371           0 :             OUString(RTL_CONSTASCII_USTRINGPARAM("Could not create Java implementation loader")), NULL);
     372             : }
     373             : 
     374           0 : static Mutex & getInitMutex()
     375             : {
     376             :     static Mutex * pMutex = 0;
     377           0 :     if( ! pMutex )
     378             :     {
     379           0 :         MutexGuard guard( Mutex::getGlobalMutex() );
     380           0 :         if( ! pMutex )
     381             :         {
     382           0 :             static Mutex mutex;
     383           0 :             pMutex = &mutex;
     384           0 :         }
     385             :     }
     386           0 :     return *pMutex;
     387             : }
     388             : 
     389           0 : css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader_CreateInstance(const css::uno::Reference<XComponentContext> & xCtx) throw(Exception)
     390             : {
     391           0 :     css::uno::Reference<XInterface> xRet;
     392             : 
     393             :     try {
     394           0 :         MutexGuard guard( getInitMutex() );
     395             :         // The javaloader is never destroyed and there can be only one!
     396             :         // Note that the first context wins ....
     397             :         static css::uno::Reference< XInterface > *pStaticRef = 0;
     398           0 :         if( pStaticRef )
     399             :         {
     400           0 :             xRet = *pStaticRef;
     401             :         }
     402             :         else
     403             :         {
     404           0 :             xRet = *new JavaComponentLoader(xCtx);
     405           0 :             pStaticRef = new css::uno::Reference< XInterface > ( xRet );
     406           0 :         }
     407             :     }
     408           0 :     catch(const RuntimeException & runtimeException) {
     409           0 :         OString message = OUStringToOString(runtimeException.Message, RTL_TEXTENCODING_ASCII_US);
     410           0 :         osl_trace("javaloader - could not init javaloader cause of %s", message.getStr());
     411           0 :         throw;
     412             :     }
     413             : 
     414           0 :     return xRet;
     415             : }
     416             : 
     417             : } //end namespace
     418             : 
     419             : 
     420             : using namespace stoc_javaloader;
     421             : 
     422             : static struct ImplementationEntry g_entries[] =
     423             : {
     424             :     {
     425             :         JavaComponentLoader_CreateInstance, loader_getImplementationName,
     426             :         loader_getSupportedServiceNames, createSingleComponentFactory,
     427             :         0 , 0
     428             :     },
     429             :     { 0, 0, 0, 0, 0, 0 }
     430             : };
     431             : 
     432             : extern "C"
     433             : {
     434           0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL javaloader_component_getFactory(
     435             :     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
     436             : {
     437           0 :     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
     438             : }
     439             : }
     440             : 
     441             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10