LCOV - code coverage report
Current view: top level - stoc/source/javavm - javavm.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 755 0.0 %
Date: 2014-04-14 Functions: 0 46 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             : 
      21             : #include "javavm.hxx"
      22             : 
      23             : #include "interact.hxx"
      24             : #include "jvmargs.hxx"
      25             : 
      26             : #include "com/sun/star/beans/NamedValue.hpp"
      27             : #include "com/sun/star/beans/PropertyState.hpp"
      28             : #include "com/sun/star/beans/PropertyValue.hpp"
      29             : #include "com/sun/star/container/XContainer.hpp"
      30             : #include "com/sun/star/java/JavaNotFoundException.hpp"
      31             : #include "com/sun/star/java/InvalidJavaSettingsException.hpp"
      32             : #include "com/sun/star/java/RestartRequiredException.hpp"
      33             : #include "com/sun/star/java/JavaDisabledException.hpp"
      34             : #include "com/sun/star/java/JavaVMCreationFailureException.hpp"
      35             : #include "com/sun/star/lang/DisposedException.hpp"
      36             : #include "com/sun/star/lang/IllegalArgumentException.hpp"
      37             : #include "com/sun/star/lang/XEventListener.hpp"
      38             : #include "com/sun/star/lang/XMultiComponentFactory.hpp"
      39             : #include "com/sun/star/lang/XSingleComponentFactory.hpp"
      40             : #include "com/sun/star/lang/WrappedTargetRuntimeException.hpp"
      41             : #include "com/sun/star/registry/XRegistryKey.hpp"
      42             : #include "com/sun/star/registry/XSimpleRegistry.hpp"
      43             : #include "com/sun/star/task/XInteractionHandler.hpp"
      44             : #include "com/sun/star/uno/Exception.hpp"
      45             : #include "com/sun/star/uno/Reference.hxx"
      46             : #include "com/sun/star/uno/RuntimeException.hpp"
      47             : #include "com/sun/star/uno/Sequence.hxx"
      48             : #include "com/sun/star/uno/XComponentContext.hpp"
      49             : #include "com/sun/star/uno/XCurrentContext.hpp"
      50             : #include "com/sun/star/uno/XInterface.hpp"
      51             : #include "com/sun/star/util/theMacroExpander.hpp"
      52             : #include "com/sun/star/container/XNameAccess.hpp"
      53             : #include "cppuhelper/exc_hlp.hxx"
      54             : #include "cppuhelper/factory.hxx"
      55             : #include "cppuhelper/implbase1.hxx"
      56             : #include "cppuhelper/implementationentry.hxx"
      57             : #include <cppuhelper/supportsservice.hxx>
      58             : #include "jvmaccess/classpath.hxx"
      59             : #include "jvmaccess/unovirtualmachine.hxx"
      60             : #include "jvmaccess/virtualmachine.hxx"
      61             : #include "osl/file.hxx"
      62             : #include "osl/thread.h"
      63             : #include "rtl/bootstrap.hxx"
      64             : #include "rtl/process.h"
      65             : #include "rtl/string.h"
      66             : #include "rtl/ustrbuf.hxx"
      67             : #include "rtl/ustring.h"
      68             : #include "rtl/ustring.hxx"
      69             : #include "rtl/uri.hxx"
      70             : #include "sal/types.h"
      71             : #include "uno/current_context.hxx"
      72             : #include "uno/environment.h"
      73             : #include "jvmfwk/framework.h"
      74             : #include "jni.h"
      75             : 
      76             : #include <stack>
      77             : #include <string.h>
      78             : #include <time.h>
      79             : #include <memory>
      80             : #include <vector>
      81             : #include "boost/noncopyable.hpp"
      82             : #include "boost/scoped_array.hpp"
      83             : 
      84             : // Properties of the javavm can be put
      85             : // as a komma separated list in this
      86             : // environment variable
      87             : #ifdef UNIX
      88             : #define TIMEZONE "MEZ"
      89             : #else
      90             : #define TIMEZONE "MET"
      91             : #endif
      92             : 
      93             : /* Within this implementation of the com.sun.star.java.JavaVirtualMachine
      94             :  * service and com.sun.star.java.theJavaVirtualMachine singleton, the method
      95             :  * com.sun.star.java.XJavaVM.getJavaVM relies on the following:
      96             :  * 1  The string "$URE_INTERNAL_JAVA_DIR/" is expanded via the
      97             :  * com.sun.star.util.theMacroExpander singleton into an internal (see the
      98             :  * com.sun.star.uri.ExternalUriReferenceTranslator service), hierarchical URI
      99             :  * reference relative to which the URE JAR files can be addressed.
     100             :  * 2  The string "$URE_INTERNAL_JAVA_CLASSPATH" is either not expandable via the
     101             :  * com.sun.star.util.theMacroExpander singleton
     102             :  * (com.sun.star.lang.IllegalArgumentException), or is expanded via the
     103             :  * com.sun.star.util.theMacroExpander singleton into a list of zero or more
     104             :  * internal (see the com.sun.star.uri.ExternalUriReferenceTranslator service)
     105             :  * URIs, where any space characters (U+0020) are ignored (and, in particular,
     106             :  * separate adjacent URIs).
     107             :  * If either of these requirements is not met, getJavaVM raises a
     108             :  * com.sun.star.uno.RuntimeException.
     109             :  */
     110             : 
     111             : using stoc_javavm::JavaVirtualMachine;
     112             : 
     113             : namespace {
     114             : 
     115             : 
     116             : 
     117             : class NoJavaIniException: public css::uno::Exception
     118             : {
     119             : };
     120             : 
     121             : class SingletonFactory:
     122             :     private cppu::WeakImplHelper1< css::lang::XEventListener >,
     123             :     private boost::noncopyable
     124             : {
     125             : public:
     126             :     static css::uno::Reference< css::uno::XInterface > getSingleton(
     127             :         css::uno::Reference< css::uno::XComponentContext > const & rContext);
     128             : 
     129             : private:
     130           0 :     inline SingletonFactory() {}
     131             : 
     132           0 :     virtual inline ~SingletonFactory() {}
     133             : 
     134             :     virtual void SAL_CALL disposing(css::lang::EventObject const &)
     135             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     136             : 
     137             :     static void dispose();
     138             : 
     139             :     // TODO ok to keep these static?
     140             :     static osl::Mutex m_aMutex;
     141             :     static css::uno::Reference< css::uno::XInterface > m_xSingleton;
     142             :     static bool m_bDisposed;
     143             : };
     144             : 
     145           0 : css::uno::Reference< css::uno::XInterface > SingletonFactory::getSingleton(
     146             :     css::uno::Reference< css::uno::XComponentContext > const & rContext)
     147             : {
     148           0 :     css::uno::Reference< css::uno::XInterface > xSingleton;
     149           0 :     css::uno::Reference< css::lang::XComponent > xComponent;
     150             :     {
     151           0 :         osl::MutexGuard aGuard(m_aMutex);
     152           0 :         if (!m_xSingleton.is())
     153             :         {
     154           0 :             if (m_bDisposed)
     155           0 :                 throw css::lang::DisposedException();
     156           0 :             xComponent = css::uno::Reference< css::lang::XComponent >(
     157           0 :                 rContext, css::uno::UNO_QUERY_THROW);
     158           0 :             m_xSingleton = static_cast< cppu::OWeakObject * >(
     159           0 :                 new JavaVirtualMachine(rContext));
     160             :         }
     161           0 :         xSingleton = m_xSingleton;
     162             :     }
     163           0 :     if (xComponent.is())
     164             :         try
     165             :         {
     166           0 :             xComponent->addEventListener(new SingletonFactory);
     167             :         }
     168           0 :         catch (...)
     169             :         {
     170           0 :             dispose();
     171           0 :             throw;
     172             :         }
     173           0 :     return xSingleton;
     174             : }
     175             : 
     176           0 : void SAL_CALL SingletonFactory::disposing(css::lang::EventObject const &)
     177             :     throw (css::uno::RuntimeException, std::exception)
     178             : {
     179           0 :     dispose();
     180           0 : }
     181             : 
     182           0 : void SingletonFactory::dispose()
     183             : {
     184           0 :     css::uno::Reference< css::lang::XComponent > xComponent;
     185             :     {
     186           0 :         osl::MutexGuard aGuard(m_aMutex);
     187           0 :         xComponent = css::uno::Reference< css::lang::XComponent >(
     188           0 :             m_xSingleton, css::uno::UNO_QUERY);
     189           0 :         m_xSingleton.clear();
     190           0 :         m_bDisposed = true;
     191             :     }
     192           0 :     if (xComponent.is())
     193           0 :         xComponent->dispose();
     194           0 : }
     195             : 
     196           0 : osl::Mutex SingletonFactory::m_aMutex;
     197           0 : css::uno::Reference< css::uno::XInterface > SingletonFactory::m_xSingleton;
     198             : bool SingletonFactory::m_bDisposed = false;
     199             : 
     200           0 : OUString serviceGetImplementationName()
     201             : {
     202           0 :     return OUString("com.sun.star.comp.stoc.JavaVirtualMachine");
     203             : }
     204             : 
     205           0 : css::uno::Sequence< OUString > serviceGetSupportedServiceNames()
     206             : {
     207           0 :     OUString aServiceName("com.sun.star.java.JavaVirtualMachine");
     208           0 :     return css::uno::Sequence< OUString >(&aServiceName, 1);
     209             : }
     210             : 
     211           0 : css::uno::Reference< css::uno::XInterface > SAL_CALL serviceCreateInstance(
     212             :     css::uno::Reference< css::uno::XComponentContext > const & rContext)
     213             :     SAL_THROW((css::uno::Exception))
     214             : {
     215             :     // Only one single instance of this service is ever constructed, and is
     216             :     // available until the component context used to create this instance is
     217             :     // disposed.  Afterwards, this function throws a DisposedException (as do
     218             :     // all relevant methods on the single service instance).
     219           0 :     return SingletonFactory::getSingleton(rContext);
     220             : }
     221             : 
     222             : cppu::ImplementationEntry const aServiceImplementation[]
     223             :     = { { serviceCreateInstance,
     224             :           serviceGetImplementationName,
     225             :           serviceGetSupportedServiceNames,
     226             :           cppu::createSingleComponentFactory,
     227             :           0, 0 },
     228             :         { 0, 0, 0, 0, 0, 0 } };
     229             : 
     230             : typedef std::stack< jvmaccess::VirtualMachine::AttachGuard * > GuardStack;
     231             : 
     232             : extern "C" {
     233             : 
     234           0 : static void destroyAttachGuards(void * pData)
     235             : {
     236           0 :     GuardStack * pStack = static_cast< GuardStack * >(pData);
     237           0 :     if (pStack != 0)
     238             :     {
     239           0 :         while (!pStack->empty())
     240             :         {
     241           0 :             delete pStack->top();
     242           0 :             pStack->pop();
     243             :         }
     244           0 :         delete pStack;
     245             :     }
     246           0 : }
     247             : 
     248             : }
     249             : 
     250           0 : bool askForRetry(css::uno::Any const & rException)
     251             : {
     252             :     css::uno::Reference< css::uno::XCurrentContext > xContext(
     253           0 :         css::uno::getCurrentContext());
     254           0 :     if (xContext.is())
     255             :     {
     256           0 :         css::uno::Reference< css::task::XInteractionHandler > xHandler;
     257           0 :         xContext->getValueByName("java-vm.interaction-handler")
     258           0 :             >>= xHandler;
     259           0 :         if (xHandler.is())
     260             :         {
     261             :             rtl::Reference< stoc_javavm::InteractionRequest > xRequest(
     262           0 :                 new stoc_javavm::InteractionRequest(rException));
     263           0 :             xHandler->handle(xRequest.get());
     264           0 :             return xRequest->retry();
     265           0 :         }
     266             :     }
     267           0 :     return false;
     268             : }
     269             : 
     270             : // Only gets the properties if the "Proxy Server" entry in the option dialog is
     271             : // set to manual (i.e. not to none)
     272           0 : void getINetPropsFromConfig(stoc_javavm::JVM * pjvm,
     273             :                             const css::uno::Reference<css::lang::XMultiComponentFactory> & xSMgr,
     274             :                             const css::uno::Reference<css::uno::XComponentContext> &xCtx ) throw (css::uno::Exception)
     275             : {
     276           0 :     css::uno::Reference<css::uno::XInterface> xConfRegistry = xSMgr->createInstanceWithContext(
     277             :             OUString("com.sun.star.configuration.ConfigurationRegistry"),
     278           0 :             xCtx );
     279           0 :     if(!xConfRegistry.is()) throw css::uno::RuntimeException("javavm.cxx: couldn't get ConfigurationRegistry", 0);
     280             : 
     281           0 :     css::uno::Reference<css::registry::XSimpleRegistry> xConfRegistry_simple(xConfRegistry, css::uno::UNO_QUERY);
     282           0 :     if(!xConfRegistry_simple.is()) throw css::uno::RuntimeException("javavm.cxx: couldn't get ConfigurationRegistry", 0);
     283             : 
     284           0 :     xConfRegistry_simple->open(OUString("org.openoffice.Inet"), sal_True, sal_False);
     285           0 :     css::uno::Reference<css::registry::XRegistryKey> xRegistryRootKey = xConfRegistry_simple->getRootKey();
     286             : 
     287             : //  if ooInetProxyType is not 0 then read the settings
     288           0 :     css::uno::Reference<css::registry::XRegistryKey> proxyEnable= xRegistryRootKey->openKey("Settings/ooInetProxyType");
     289           0 :     if( proxyEnable.is() && 0 != proxyEnable->getLongValue())
     290             :     {
     291             :         // read ftp proxy name
     292           0 :         css::uno::Reference<css::registry::XRegistryKey> ftpProxy_name = xRegistryRootKey->openKey("Settings/ooInetFTPProxyName");
     293           0 :         if(ftpProxy_name.is() && !ftpProxy_name->getStringValue().isEmpty()) {
     294           0 :             OUString ftpHost = "ftp.proxyHost=";
     295           0 :             ftpHost += ftpProxy_name->getStringValue();
     296             : 
     297             :             // read ftp proxy port
     298           0 :             css::uno::Reference<css::registry::XRegistryKey> ftpProxy_port = xRegistryRootKey->openKey("Settings/ooInetFTPProxyPort");
     299           0 :             if(ftpProxy_port.is() && ftpProxy_port->getLongValue()) {
     300           0 :                 OUString ftpPort = "ftp.proxyPort=";
     301           0 :                 ftpPort += OUString::number(ftpProxy_port->getLongValue());
     302             : 
     303           0 :                 pjvm->pushProp(ftpHost);
     304           0 :                 pjvm->pushProp(ftpPort);
     305           0 :             }
     306             :         }
     307             : 
     308             :         // read http proxy name
     309           0 :         css::uno::Reference<css::registry::XRegistryKey> httpProxy_name = xRegistryRootKey->openKey("Settings/ooInetHTTPProxyName");
     310           0 :         if(httpProxy_name.is() && !httpProxy_name->getStringValue().isEmpty()) {
     311           0 :             OUString httpHost = "http.proxyHost=";
     312           0 :             httpHost += httpProxy_name->getStringValue();
     313             : 
     314             :             // read http proxy port
     315           0 :             css::uno::Reference<css::registry::XRegistryKey> httpProxy_port = xRegistryRootKey->openKey("Settings/ooInetHTTPProxyPort");
     316           0 :             if(httpProxy_port.is() && httpProxy_port->getLongValue()) {
     317           0 :                 OUString httpPort = "http.proxyPort=";
     318           0 :                 httpPort += OUString::number(httpProxy_port->getLongValue());
     319             : 
     320           0 :                 pjvm->pushProp(httpHost);
     321           0 :                 pjvm->pushProp(httpPort);
     322           0 :             }
     323             :         }
     324             : 
     325             :         // read https proxy name
     326           0 :         css::uno::Reference<css::registry::XRegistryKey> httpsProxy_name = xRegistryRootKey->openKey("Settings/ooInetHTTPSProxyName");
     327           0 :         if(httpsProxy_name.is() && !httpsProxy_name->getStringValue().isEmpty()) {
     328           0 :             OUString httpsHost = "https.proxyHost=";
     329           0 :             httpsHost += httpsProxy_name->getStringValue();
     330             : 
     331             :             // read https proxy port
     332           0 :             css::uno::Reference<css::registry::XRegistryKey> httpsProxy_port = xRegistryRootKey->openKey("Settings/ooInetHTTPSProxyPort");
     333           0 :             if(httpsProxy_port.is() && httpsProxy_port->getLongValue()) {
     334           0 :                 OUString httpsPort = "https.proxyPort=";
     335           0 :                 httpsPort += OUString::number(httpsProxy_port->getLongValue());
     336             : 
     337           0 :                 pjvm->pushProp(httpsHost);
     338           0 :                 pjvm->pushProp(httpsPort);
     339           0 :             }
     340             :         }
     341             : 
     342             :         // read  nonProxyHosts
     343           0 :         css::uno::Reference<css::registry::XRegistryKey> nonProxies_name = xRegistryRootKey->openKey("Settings/ooInetNoProxy");
     344           0 :         if(nonProxies_name.is() && !nonProxies_name->getStringValue().isEmpty()) {
     345           0 :             OUString httpNonProxyHosts = "http.nonProxyHosts=";
     346           0 :             OUString ftpNonProxyHosts = "ftp.nonProxyHosts=";
     347           0 :             OUString value= nonProxies_name->getStringValue();
     348             :             // replace the separator ";" by "|"
     349           0 :             value= value.replace((sal_Unicode)';', (sal_Unicode)'|');
     350             : 
     351           0 :             httpNonProxyHosts += value;
     352           0 :             ftpNonProxyHosts += value;
     353             : 
     354           0 :             pjvm->pushProp(httpNonProxyHosts);
     355           0 :             pjvm->pushProp(ftpNonProxyHosts);
     356           0 :         }
     357             :     }
     358           0 :     xConfRegistry_simple->close();
     359           0 : }
     360             : 
     361           0 : void getDefaultLocaleFromConfig(
     362             :     stoc_javavm::JVM * pjvm,
     363             :     const css::uno::Reference<css::lang::XMultiComponentFactory> & xSMgr,
     364             :     const css::uno::Reference<css::uno::XComponentContext> &xCtx ) throw(css::uno::Exception)
     365             : {
     366             :     css::uno::Reference<css::uno::XInterface> xConfRegistry =
     367           0 :         xSMgr->createInstanceWithContext(
     368           0 :         OUString("com.sun.star.configuration.ConfigurationRegistry"), xCtx );
     369           0 :     if(!xConfRegistry.is())
     370             :         throw css::uno::RuntimeException(
     371           0 :             OUString("javavm.cxx: couldn't get ConfigurationRegistry"), 0);
     372             : 
     373             :     css::uno::Reference<css::registry::XSimpleRegistry> xConfRegistry_simple(
     374           0 :         xConfRegistry, css::uno::UNO_QUERY);
     375           0 :     if(!xConfRegistry_simple.is())
     376             :         throw css::uno::RuntimeException(
     377           0 :             OUString("javavm.cxx: couldn't get ConfigurationRegistry"), 0);
     378             : 
     379           0 :     xConfRegistry_simple->open(OUString("org.openoffice.Setup"), sal_True, sal_False);
     380           0 :     css::uno::Reference<css::registry::XRegistryKey> xRegistryRootKey = xConfRegistry_simple->getRootKey();
     381             : 
     382             :     // read locale
     383           0 :     css::uno::Reference<css::registry::XRegistryKey> locale = xRegistryRootKey->openKey("L10N/ooLocale");
     384           0 :     if(locale.is() && !locale->getStringValue().isEmpty()) {
     385           0 :         OUString language;
     386           0 :         OUString country;
     387             : 
     388           0 :         sal_Int32 index = locale->getStringValue().indexOf((sal_Unicode) '-');
     389             : 
     390           0 :         if(index >= 0) {
     391           0 :             language = locale->getStringValue().copy(0, index);
     392           0 :             country = locale->getStringValue().copy(index + 1);
     393             : 
     394           0 :             if(!language.isEmpty()) {
     395           0 :                 OUString prop("user.language=");
     396           0 :                 prop += language;
     397             : 
     398           0 :                 pjvm->pushProp(prop);
     399             :             }
     400             : 
     401           0 :             if(!country.isEmpty()) {
     402           0 :                 OUString prop("user.country=");
     403           0 :                 prop += country;
     404             : 
     405           0 :                 pjvm->pushProp(prop);
     406             :             }
     407           0 :         }
     408             :     }
     409             : 
     410           0 :     xConfRegistry_simple->close();
     411           0 : }
     412             : 
     413             : 
     414             : 
     415           0 : void getJavaPropsFromSafetySettings(
     416             :     stoc_javavm::JVM * pjvm,
     417             :     const css::uno::Reference<css::lang::XMultiComponentFactory> & xSMgr,
     418             :     const css::uno::Reference<css::uno::XComponentContext> &xCtx) throw(css::uno::Exception)
     419             : {
     420             :     css::uno::Reference<css::uno::XInterface> xConfRegistry =
     421           0 :         xSMgr->createInstanceWithContext(
     422             :             OUString("com.sun.star.configuration.ConfigurationRegistry"),
     423           0 :             xCtx);
     424           0 :     if(!xConfRegistry.is())
     425             :         throw css::uno::RuntimeException(
     426           0 :             OUString("javavm.cxx: couldn't get ConfigurationRegistry"), 0);
     427             : 
     428             :     css::uno::Reference<css::registry::XSimpleRegistry> xConfRegistry_simple(
     429           0 :         xConfRegistry, css::uno::UNO_QUERY);
     430           0 :     if(!xConfRegistry_simple.is())
     431             :         throw css::uno::RuntimeException(
     432           0 :             OUString("javavm.cxx: couldn't get ConfigurationRegistry"), 0);
     433             : 
     434           0 :     xConfRegistry_simple->open(
     435             :         OUString("org.openoffice.Office.Java"),
     436           0 :         sal_True, sal_False);
     437             :     css::uno::Reference<css::registry::XRegistryKey> xRegistryRootKey =
     438           0 :         xConfRegistry_simple->getRootKey();
     439             : 
     440           0 :     if (xRegistryRootKey.is())
     441             :     {
     442           0 :         css::uno::Reference<css::registry::XRegistryKey> key_NetAccess= xRegistryRootKey->openKey("VirtualMachine/NetAccess");
     443           0 :         if (key_NetAccess.is())
     444             :         {
     445           0 :             sal_Int32 val= key_NetAccess->getLongValue();
     446           0 :             OUString sVal;
     447           0 :             switch( val)
     448             :             {
     449           0 :             case 0: sVal = "host";
     450           0 :                 break;
     451           0 :             case 1: sVal = "unrestricted";
     452           0 :                 break;
     453           0 :             case 3: sVal = "none";
     454           0 :                 break;
     455             :             }
     456           0 :             OUString sProperty("appletviewer.security.mode=");
     457           0 :             sProperty= sProperty + sVal;
     458           0 :             pjvm->pushProp(sProperty);
     459             :         }
     460           0 :         css::uno::Reference<css::registry::XRegistryKey> key_CheckSecurity= xRegistryRootKey->openKey(
     461           0 :             OUString("VirtualMachine/Security"));
     462           0 :         if( key_CheckSecurity.is())
     463             :         {
     464           0 :             sal_Bool val= (sal_Bool) key_CheckSecurity->getLongValue();
     465           0 :             OUString sProperty("stardiv.security.disableSecurity=");
     466           0 :             if( val)
     467           0 :                 sProperty= sProperty + "false";
     468             :             else
     469           0 :                 sProperty= sProperty + "true";
     470           0 :             pjvm->pushProp( sProperty);
     471           0 :         }
     472             :     }
     473           0 :     xConfRegistry_simple->close();
     474           0 : }
     475             : 
     476           0 : static void setTimeZone(stoc_javavm::JVM * pjvm) throw() {
     477             :     /* A Bug in the Java function
     478             :     ** struct Hjava_util_Properties * java_lang_System_initProperties(
     479             :     ** struct Hjava_lang_System *this,
     480             :     ** struct Hjava_util_Properties *props);
     481             :     ** This function doesn't detect MEZ, MET or "W. Europe Standard Time"
     482             :     */
     483             :     struct tm *tmData;
     484           0 :     time_t clock = time(NULL);
     485           0 :     tzset();
     486           0 :     tmData = localtime(&clock);
     487             : #ifdef MACOSX
     488             :     char * p = tmData->tm_zone;
     489             : #else
     490           0 :     char * p = tzname[0];
     491             :     (void)tmData;
     492             : #endif
     493             : 
     494           0 :     if (!strcmp(TIMEZONE, p))
     495           0 :         pjvm->pushProp(OUString("user.timezone=ECT"));
     496           0 : }
     497             : 
     498           0 : void initVMConfiguration(
     499             :     stoc_javavm::JVM * pjvm,
     500             :     const css::uno::Reference<css::lang::XMultiComponentFactory> & xSMgr,
     501             :     const css::uno::Reference<css::uno::XComponentContext > &xCtx) throw(css::uno::Exception)
     502             : {
     503           0 :     stoc_javavm::JVM jvm;
     504             :     try {
     505           0 :         getINetPropsFromConfig(&jvm, xSMgr, xCtx);
     506             :     }
     507           0 :     catch(const css::uno::Exception & exception) {
     508             : #if OSL_DEBUG_LEVEL > 1
     509             :         OString message = OUStringToOString(exception.Message, RTL_TEXTENCODING_ASCII_US);
     510             :         OSL_TRACE("javavm.cxx: can not get INetProps cause of >%s<", message.getStr());
     511             : #else
     512             :         (void) exception; // unused
     513             : #endif
     514             :     }
     515             : 
     516             :     try {
     517           0 :         getDefaultLocaleFromConfig(&jvm, xSMgr,xCtx);
     518             :     }
     519           0 :     catch(const css::uno::Exception & exception) {
     520             : #if OSL_DEBUG_LEVEL > 1
     521             :         OString message = OUStringToOString(exception.Message, RTL_TEXTENCODING_ASCII_US);
     522             :         OSL_TRACE("javavm.cxx: can not get locale cause of >%s<", message.getStr());
     523             : #else
     524             :         (void) exception; // unused
     525             : #endif
     526             :     }
     527             : 
     528             :     try
     529             :     {
     530           0 :         getJavaPropsFromSafetySettings(&jvm, xSMgr, xCtx);
     531             :     }
     532           0 :     catch(const css::uno::Exception & exception) {
     533             : #if OSL_DEBUG_LEVEL > 1
     534             :         OString message = OUStringToOString(exception.Message, RTL_TEXTENCODING_ASCII_US);
     535             :         OSL_TRACE("javavm.cxx: couldn't get safety settings because of >%s<", message.getStr());
     536             : #else
     537             :         (void) exception; // unused
     538             : #endif
     539             :     }
     540             : 
     541           0 :     *pjvm= jvm;
     542           0 :     setTimeZone(pjvm);
     543             : 
     544           0 : }
     545             : 
     546             : class DetachCurrentThread: private boost::noncopyable {
     547             : public:
     548           0 :     explicit DetachCurrentThread(JavaVM * jvm): m_jvm(jvm) {}
     549             : 
     550           0 :     ~DetachCurrentThread() {
     551           0 :         if (m_jvm->DetachCurrentThread() != 0) {
     552             :             OSL_ASSERT(false);
     553             :         }
     554           0 :     }
     555             : 
     556             : private:
     557             :     JavaVM * m_jvm;
     558             : };
     559             : 
     560             : }
     561             : 
     562           0 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL javavm_component_getFactory(sal_Char const * pImplName,
     563             :                                                 void * pServiceManager,
     564             :                                                 void * pRegistryKey)
     565             : {
     566             :     return cppu::component_getFactoryHelper(pImplName, pServiceManager,
     567             :                                             pRegistryKey,
     568           0 :                                             aServiceImplementation);
     569             : }
     570             : 
     571           0 : JavaVirtualMachine::JavaVirtualMachine(
     572             :     css::uno::Reference< css::uno::XComponentContext > const & rContext):
     573             :     JavaVirtualMachine_Impl(m_aMutex),
     574             :     m_xContext(rContext),
     575             :     m_bDisposed(false),
     576             :     m_pJavaVm(0),
     577             :     m_bDontCreateJvm(false),
     578           0 :     m_aAttachGuards(destroyAttachGuards) // TODO check for validity
     579           0 : {}
     580             : 
     581             : void SAL_CALL
     582           0 : JavaVirtualMachine::initialize(css::uno::Sequence< css::uno::Any > const &
     583             :                                    rArguments)
     584             :     throw (css::uno::Exception, std::exception)
     585             : {
     586           0 :     osl::MutexGuard aGuard(m_aMutex);
     587           0 :     if (m_bDisposed)
     588             :         throw css::lang::DisposedException(
     589           0 :             OUString(), static_cast< cppu::OWeakObject * >(this));
     590           0 :     if (m_xUnoVirtualMachine.is())
     591             :         throw css::uno::RuntimeException(
     592             :             OUString("bad call to initialize"),
     593           0 :             static_cast< cppu::OWeakObject * >(this));
     594           0 :     css::beans::NamedValue val;
     595           0 :     if (rArguments.getLength() == 1 && (rArguments[0] >>= val) && val.Name == "UnoVirtualMachine" )
     596             :     {
     597             :         OSL_ENSURE(
     598             :             sizeof (sal_Int64) >= sizeof (jvmaccess::UnoVirtualMachine *),
     599             :             "Pointer cannot be represented as sal_Int64");
     600             :         sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
     601           0 :             static_cast< jvmaccess::UnoVirtualMachine * >(0));
     602           0 :         val.Value >>= nPointer;
     603           0 :         m_xUnoVirtualMachine =
     604           0 :             reinterpret_cast< jvmaccess::UnoVirtualMachine * >(nPointer);
     605             :     } else {
     606             :         OSL_ENSURE(
     607             :             sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *),
     608             :             "Pointer cannot be represented as sal_Int64");
     609             :         sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
     610           0 :             static_cast< jvmaccess::VirtualMachine * >(0));
     611           0 :         if (rArguments.getLength() == 1)
     612           0 :             rArguments[0] >>= nPointer;
     613             :         rtl::Reference< jvmaccess::VirtualMachine > vm(
     614           0 :             reinterpret_cast< jvmaccess::VirtualMachine * >(nPointer));
     615           0 :         if (vm.is()) {
     616             :             try {
     617           0 :                 m_xUnoVirtualMachine = new jvmaccess::UnoVirtualMachine(vm, 0);
     618           0 :             } catch (jvmaccess::UnoVirtualMachine::CreationException &) {
     619             :                 throw css::uno::RuntimeException(
     620             :                     OUString("jvmaccess::UnoVirtualMachine::CreationException"),
     621           0 :                     static_cast< cppu::OWeakObject * >(this));
     622             :             }
     623           0 :         }
     624             :     }
     625           0 :     if (!m_xUnoVirtualMachine.is()) {
     626             :         throw css::lang::IllegalArgumentException(
     627             :             OUString("sequence of exactly one any containing either (a) a"
     628             :                     " com.sun.star.beans.NamedValue with Name"
     629             :                     " \"UnoVirtualMachine\" and Value a hyper representing a"
     630             :                     " non-null pointer to a jvmaccess:UnoVirtualMachine, or (b)"
     631             :                     " a hyper representing a non-null pointer to a"
     632             :                     " jvmaccess::VirtualMachine required"),
     633           0 :             static_cast< cppu::OWeakObject * >(this), 0);
     634             :     }
     635           0 :     m_xVirtualMachine = m_xUnoVirtualMachine->getVirtualMachine();
     636           0 : }
     637             : 
     638           0 : OUString SAL_CALL JavaVirtualMachine::getImplementationName()
     639             :     throw (css::uno::RuntimeException, std::exception)
     640             : {
     641           0 :     return serviceGetImplementationName();
     642             : }
     643             : 
     644             : sal_Bool SAL_CALL
     645           0 : JavaVirtualMachine::supportsService(OUString const & rServiceName)
     646             :     throw (css::uno::RuntimeException, std::exception)
     647             : {
     648           0 :     return cppu::supportsService(this, rServiceName);
     649             : }
     650             : 
     651             : css::uno::Sequence< OUString > SAL_CALL
     652           0 : JavaVirtualMachine::getSupportedServiceNames()
     653             :     throw (css::uno::RuntimeException, std::exception)
     654             : {
     655           0 :     return serviceGetSupportedServiceNames();
     656             : }
     657             : 
     658             : namespace {
     659             : 
     660             : struct JavaInfoGuard: private boost::noncopyable {
     661           0 :     JavaInfoGuard(): info(0) {}
     662             : 
     663           0 :     ~JavaInfoGuard() { jfw_freeJavaInfo(info); }
     664             : 
     665           0 :     void clear() {
     666           0 :         jfw_freeJavaInfo(info);
     667           0 :         info = 0;
     668           0 :     }
     669             : 
     670             :     JavaInfo * info;
     671             : };
     672             : 
     673             : }
     674             : 
     675             : css::uno::Any SAL_CALL
     676           0 : JavaVirtualMachine::getJavaVM(css::uno::Sequence< sal_Int8 > const & rProcessId)
     677             :     throw (css::uno::RuntimeException, std::exception)
     678             : {
     679           0 :     osl::MutexGuard aGuard(m_aMutex);
     680           0 :     if (m_bDisposed)
     681             :         throw css::lang::DisposedException(
     682           0 :             OUString(), static_cast< cppu::OWeakObject * >(this));
     683           0 :     css::uno::Sequence< sal_Int8 > aId(16);
     684           0 :     rtl_getGlobalProcessId(reinterpret_cast< sal_uInt8 * >(aId.getArray()));
     685             :     enum ReturnType {
     686             :         RETURN_JAVAVM, RETURN_VIRTUALMACHINE, RETURN_UNOVIRTUALMACHINE };
     687             :     ReturnType returnType =
     688           0 :         rProcessId.getLength() == 17 && rProcessId[16] == 0
     689             :         ? RETURN_VIRTUALMACHINE
     690           0 :         : rProcessId.getLength() == 17 && rProcessId[16] == 1
     691             :         ? RETURN_UNOVIRTUALMACHINE
     692           0 :         : RETURN_JAVAVM;
     693           0 :     css::uno::Sequence< sal_Int8 > aProcessId(rProcessId);
     694           0 :     if (returnType != RETURN_JAVAVM)
     695           0 :         aProcessId.realloc(16);
     696           0 :     if (aId != aProcessId)
     697           0 :         return css::uno::Any();
     698             : 
     699           0 :     JavaInfoGuard info;
     700           0 :     while (!m_xVirtualMachine.is()) // retry until successful
     701             :     {
     702             :         // This is the second attempt to create Java.  m_bDontCreateJvm is
     703             :         // set which means instantiating the JVM might crash.
     704           0 :         if (m_bDontCreateJvm)
     705             :             //throw css::uno::RuntimeException();
     706           0 :             return css::uno::Any();
     707             : 
     708           0 :         stoc_javavm::JVM aJvm;
     709           0 :         initVMConfiguration(&aJvm, m_xContext->getServiceManager(),
     710           0 :                             m_xContext);
     711             :         //Create the JavaVMOption array
     712           0 :         const std::vector<OUString> & props = aJvm.getProperties();
     713             :         boost::scoped_array<JavaVMOption> sarOptions(
     714           0 :             new JavaVMOption[props.size()]);
     715           0 :         JavaVMOption * arOptions = sarOptions.get();
     716             :         //Create an array that contains the strings which are passed
     717             :         //into the options
     718             :         boost::scoped_array<OString> sarPropStrings(
     719           0 :              new OString[props.size()]);
     720           0 :         OString * arPropStrings = sarPropStrings.get();
     721             : 
     722           0 :         OString sJavaOption("-");
     723             :         typedef std::vector<OUString>::const_iterator cit;
     724           0 :         int index = 0;
     725           0 :         for (cit i = props.begin(); i != props.end(); ++i)
     726             :         {
     727             :             OString sOption = OUStringToOString(
     728           0 :                 *i, osl_getThreadTextEncoding());
     729             : 
     730           0 :             if (!sOption.matchIgnoreAsciiCase(sJavaOption, 0))
     731           0 :                 arPropStrings[index]= OString("-D") + sOption;
     732             :             else
     733           0 :                 arPropStrings[index] = sOption;
     734             : 
     735           0 :             arOptions[index].optionString = (sal_Char*)arPropStrings[index].getStr();
     736           0 :             arOptions[index].extraInfo = 0;
     737           0 :             index ++;
     738           0 :         }
     739             : 
     740           0 :         JNIEnv * pMainThreadEnv = 0;
     741           0 :         javaFrameworkError errcode = JFW_E_NONE;
     742             : 
     743           0 :         if (getenv("STOC_FORCE_NO_JRE"))
     744           0 :             errcode = JFW_E_NO_SELECT;
     745             :         else
     746             :             errcode = jfw_startVM(info.info, arOptions, index, & m_pJavaVm,
     747           0 :                                   & pMainThreadEnv);
     748             : 
     749           0 :         bool bStarted = false;
     750           0 :         switch (errcode)
     751             :         {
     752           0 :         case JFW_E_NONE: bStarted = true; break;
     753             :         case JFW_E_NO_SELECT:
     754             :         {
     755             :             // No Java configured. We silenty run the java configuration
     756             :             // Java.
     757           0 :             info.clear();
     758           0 :             javaFrameworkError errFind = jfw_findAndSelectJRE(&info.info);
     759           0 :             if (getenv("STOC_FORCE_NO_JRE"))
     760           0 :                 errFind = JFW_E_NO_JAVA_FOUND;
     761           0 :             if (errFind == JFW_E_NONE)
     762             :             {
     763           0 :                 continue;
     764             :             }
     765           0 :             else if (errFind == JFW_E_NO_JAVA_FOUND)
     766             :             {
     767             : 
     768             :                 //Warning MessageBox:
     769             :                 //%PRODUCTNAME requires a Java runtime environment (JRE) to perform this task.
     770             :                 //Please install a JRE and restart %PRODUCTNAME.
     771             :                 css::java::JavaNotFoundException exc(
     772             :                     OUString("JavaVirtualMachine::getJavaVM failed because"
     773             :                              " No suitable JRE found!"),
     774           0 :                     static_cast< cppu::OWeakObject * >(this));
     775           0 :                 askForRetry(css::uno::makeAny(exc));
     776           0 :                 return css::uno::Any();
     777             :             }
     778             :             else
     779             :             {
     780             :                 //An unexpected error occurred
     781             :                 throw css::uno::RuntimeException(
     782             :                     OUString("[JavaVirtualMachine]:An unexpected error occurred"
     783           0 :                              " while searching for a Java!"), 0);
     784             :             }
     785             :         }
     786             :         case JFW_E_INVALID_SETTINGS:
     787             :         {
     788             :             //Warning MessageBox:
     789             :             // The %PRODUCTNAME configuration has been changed. Under Tools
     790             :             // - Options - %PRODUCTNAME - Java, select the Java runtime environment
     791             :             // you want to have used by %PRODUCTNAME.
     792             :             css::java::InvalidJavaSettingsException exc(
     793             :                 OUString("JavaVirtualMachine::getJavaVM failed because"
     794             :                          " Java settings have changed!"),
     795           0 :                 static_cast< cppu::OWeakObject * >(this));
     796           0 :             askForRetry(css::uno::makeAny(exc));
     797           0 :             return css::uno::Any();
     798             :         }
     799             :         case JFW_E_JAVA_DISABLED:
     800             :         {
     801             :             //QueryBox:
     802             :             //%PRODUCTNAME requires a Java runtime environment (JRE) to perform
     803             :             //this task. However, use of a JRE has been disabled. Do you want to
     804             :             //enable the use of a JRE now?
     805             :             css::java::JavaDisabledException exc(
     806             :                 OUString("JavaVirtualMachine::getJavaVM failed because Java is disabled!"),
     807           0 :                 static_cast< cppu::OWeakObject * >(this));
     808           0 :             if( ! askForRetry(css::uno::makeAny(exc)))
     809           0 :                 return css::uno::Any();
     810           0 :             continue;
     811             :         }
     812             :         case JFW_E_VM_CREATION_FAILED:
     813             :         {
     814             :             //If the creation failed because the JRE has been uninstalled then
     815             :             //we search another one. As long as there is a javaldx, we should
     816             :             //never come into this situation. javaldx checks always if the JRE
     817             :             //still exist.
     818           0 :             JavaInfo * pJavaInfo = NULL;
     819           0 :             if (JFW_E_NONE == jfw_getSelectedJRE(&pJavaInfo))
     820             :             {
     821           0 :                 sal_Bool bExist = sal_False;
     822           0 :                 if (JFW_E_NONE == jfw_existJRE(pJavaInfo, &bExist))
     823             :                 {
     824           0 :                     if (bExist == sal_False
     825           0 :                         && ! (pJavaInfo->nRequirements & JFW_REQUIRE_NEEDRESTART))
     826             :                     {
     827           0 :                         info.clear();
     828             :                         javaFrameworkError errFind = jfw_findAndSelectJRE(
     829           0 :                             &info.info);
     830           0 :                         if (errFind == JFW_E_NONE)
     831             :                         {
     832           0 :                             continue;
     833             :                         }
     834             :                     }
     835             :                 }
     836             :             }
     837             : 
     838           0 :             jfw_freeJavaInfo(pJavaInfo);
     839             : 
     840             :             //Error: %PRODUCTNAME requires a Java
     841             :             //runtime environment (JRE) to perform this task. The selected JRE
     842             :             //is defective. Please select another version or install a new JRE
     843             :             //and select it under Tools - Options - %PRODUCTNAME - Java.
     844             :             css::java::JavaVMCreationFailureException exc(
     845             :                 OUString("JavaVirtualMachine::getJavaVM failed because Java is defective!"),
     846           0 :                 static_cast< cppu::OWeakObject * >(this), 0);
     847           0 :             askForRetry(css::uno::makeAny(exc));
     848           0 :             return css::uno::Any();
     849             :         }
     850             :         case JFW_E_RUNNING_JVM:
     851             :         {
     852             :             //This service should make sure that we do not start java twice.
     853             :             OSL_ASSERT(false);
     854           0 :             break;
     855             :         }
     856             :         case JFW_E_NEED_RESTART:
     857             :         {
     858             :             //Error:
     859             :             //For the selected Java runtime environment to work properly,
     860             :             //%PRODUCTNAME must be restarted. Please restart %PRODUCTNAME now.
     861             :             css::java::RestartRequiredException exc(
     862             :                 OUString("JavaVirtualMachine::getJavaVM failed because "
     863             :                          "Office must be restarted before Java can be used!"),
     864           0 :                 static_cast< cppu::OWeakObject * >(this));
     865           0 :             askForRetry(css::uno::makeAny(exc));
     866           0 :             return css::uno::Any();
     867             :         }
     868             :         default:
     869             :             //RuntimeException: error is somewhere in the java framework.
     870             :             //An unexpected error occurred
     871             :             throw css::uno::RuntimeException(
     872             :                 OUString("[JavaVirtualMachine]:An unexpected error occurred"
     873           0 :                          " while starting Java!"), 0);
     874             :         }
     875             : 
     876           0 :         if (bStarted)
     877             :         {
     878             :             {
     879           0 :                 DetachCurrentThread detach(m_pJavaVm);
     880             :                     // necessary to make debugging work; this thread will be
     881             :                     // suspended when the destructor of detach returns
     882           0 :                 m_xVirtualMachine = new jvmaccess::VirtualMachine(
     883           0 :                     m_pJavaVm, JNI_VERSION_1_2, true, pMainThreadEnv);
     884           0 :                 setUpUnoVirtualMachine(pMainThreadEnv);
     885             :             }
     886             :             // Listen for changes in the configuration (e.g. proxy settings):
     887             :             // TODO this is done too late; changes to the configuration done
     888             :             // after the above call to initVMConfiguration are lost
     889           0 :             registerConfigChangesListener();
     890             : 
     891           0 :             break;
     892             :         }
     893           0 :     }
     894           0 :     if (!m_xUnoVirtualMachine.is()) {
     895             :         try {
     896           0 :             jvmaccess::VirtualMachine::AttachGuard guard(m_xVirtualMachine);
     897           0 :             setUpUnoVirtualMachine(guard.getEnvironment());
     898           0 :         } catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &) {
     899             :             throw css::uno::RuntimeException(
     900             :                 OUString("jvmaccess::VirtualMachine::AttachGuard::CreationException occurred"),
     901           0 :                 static_cast< cppu::OWeakObject * >(this));
     902             :         }
     903             :     }
     904           0 :     switch (returnType) {
     905             :     default: // RETURN_JAVAVM
     906           0 :         if (m_pJavaVm == 0) {
     907             :             throw css::uno::RuntimeException(
     908             :                 OUString("JavaVirtualMachine service was initialized in a way"
     909             :                          " that the requested JavaVM pointer is not available"),
     910           0 :                 static_cast< cppu::OWeakObject * >(this));
     911             :         }
     912           0 :         return css::uno::makeAny(reinterpret_cast< sal_IntPtr >(m_pJavaVm));
     913             :     case RETURN_VIRTUALMACHINE:
     914             :         OSL_ASSERT(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *));
     915             :         return css::uno::makeAny(
     916             :             reinterpret_cast< sal_Int64 >(
     917           0 :                 m_xUnoVirtualMachine->getVirtualMachine().get()));
     918             :     case RETURN_UNOVIRTUALMACHINE:
     919             :         OSL_ASSERT(sizeof (sal_Int64) >= sizeof (jvmaccess::VirtualMachine *));
     920             :         return css::uno::makeAny(
     921           0 :             reinterpret_cast< sal_Int64 >(m_xUnoVirtualMachine.get()));
     922           0 :     }
     923             : }
     924             : 
     925           0 : sal_Bool SAL_CALL JavaVirtualMachine::isVMStarted()
     926             :     throw (css::uno::RuntimeException, std::exception)
     927             : {
     928           0 :     osl::MutexGuard aGuard(m_aMutex);
     929           0 :     if (m_bDisposed)
     930             :         throw css::lang::DisposedException(
     931           0 :             OUString(), static_cast< cppu::OWeakObject * >(this));
     932           0 :     return m_xUnoVirtualMachine.is();
     933             : }
     934             : 
     935           0 : sal_Bool SAL_CALL JavaVirtualMachine::isVMEnabled()
     936             :     throw (css::uno::RuntimeException, std::exception)
     937             : {
     938             :     {
     939           0 :         osl::MutexGuard aGuard(m_aMutex);
     940           0 :         if (m_bDisposed)
     941             :             throw css::lang::DisposedException(
     942           0 :                 OUString(), static_cast< cppu::OWeakObject * >(this));
     943             :     }
     944             : //    stoc_javavm::JVM aJvm;
     945             : //    initVMConfiguration(&aJvm, m_xContext->getServiceManager(), m_xContext);
     946             : //    return aJvm.isEnabled();
     947             :     //ToDo
     948           0 :     sal_Bool bEnabled = sal_False;
     949           0 :     if (jfw_getEnabled( & bEnabled) != JFW_E_NONE)
     950           0 :         throw css::uno::RuntimeException();
     951           0 :     return bEnabled;
     952             : }
     953             : 
     954           0 : sal_Bool SAL_CALL JavaVirtualMachine::isThreadAttached()
     955             :     throw (css::uno::RuntimeException, std::exception)
     956             : {
     957           0 :     osl::MutexGuard aGuard(m_aMutex);
     958           0 :     if (m_bDisposed)
     959             :         throw css::lang::DisposedException(
     960           0 :             OUString(), static_cast< cppu::OWeakObject * >(this));
     961             :     // TODO isThreadAttached only returns true if the thread was attached via
     962             :     // registerThread:
     963             :     GuardStack * pStack
     964           0 :         = static_cast< GuardStack * >(m_aAttachGuards.getData());
     965           0 :     return pStack != 0 && !pStack->empty();
     966             : }
     967             : 
     968           0 : void SAL_CALL JavaVirtualMachine::registerThread()
     969             :     throw (css::uno::RuntimeException, std::exception)
     970             : {
     971           0 :     osl::MutexGuard aGuard(m_aMutex);
     972           0 :     if (m_bDisposed)
     973             :         throw css::lang::DisposedException(
     974           0 :             OUString(), static_cast< cppu::OWeakObject * >(this));
     975           0 :     if (!m_xUnoVirtualMachine.is())
     976             :         throw css::uno::RuntimeException(
     977             :             OUString("JavaVirtualMachine::registerThread: null VirtualMachine"),
     978           0 :             static_cast< cppu::OWeakObject * >(this));
     979             :     GuardStack * pStack
     980           0 :         = static_cast< GuardStack * >(m_aAttachGuards.getData());
     981           0 :     if (pStack == 0)
     982             :     {
     983           0 :         pStack = new GuardStack;
     984           0 :         m_aAttachGuards.setData(pStack);
     985             :     }
     986             :     try
     987             :     {
     988             :         pStack->push(
     989             :             new jvmaccess::VirtualMachine::AttachGuard(
     990           0 :                 m_xUnoVirtualMachine->getVirtualMachine()));
     991             :     }
     992           0 :     catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
     993             :     {
     994             :         throw css::uno::RuntimeException(
     995             :             OUString("JavaVirtualMachine::registerThread: jvmaccess::"
     996             :                      "VirtualMachine::AttachGuard::CreationException"),
     997           0 :             static_cast< cppu::OWeakObject * >(this));
     998           0 :     }
     999           0 : }
    1000             : 
    1001           0 : void SAL_CALL JavaVirtualMachine::revokeThread()
    1002             :     throw (css::uno::RuntimeException, std::exception)
    1003             : {
    1004           0 :     osl::MutexGuard aGuard(m_aMutex);
    1005           0 :     if (m_bDisposed)
    1006             :         throw css::lang::DisposedException(
    1007           0 :             OUString(), static_cast< cppu::OWeakObject * >(this));
    1008           0 :     if (!m_xUnoVirtualMachine.is())
    1009             :         throw css::uno::RuntimeException(
    1010             :             OUString("JavaVirtualMachine::revokeThread: null VirtualMachine"),
    1011           0 :             static_cast< cppu::OWeakObject * >(this));
    1012             :     GuardStack * pStack
    1013           0 :         = static_cast< GuardStack * >(m_aAttachGuards.getData());
    1014           0 :     if (pStack == 0 || pStack->empty())
    1015             :         throw css::uno::RuntimeException(
    1016             :             OUString("JavaVirtualMachine::revokeThread: no matching registerThread"),
    1017           0 :             static_cast< cppu::OWeakObject * >(this));
    1018           0 :     delete pStack->top();
    1019           0 :     pStack->pop();
    1020           0 : }
    1021             : 
    1022             : void SAL_CALL
    1023           0 : JavaVirtualMachine::disposing(css::lang::EventObject const & rSource)
    1024             :     throw (css::uno::RuntimeException, std::exception)
    1025             : {
    1026           0 :     osl::MutexGuard aGuard(m_aMutex);
    1027           0 :     if (rSource.Source == m_xInetConfiguration)
    1028           0 :         m_xInetConfiguration.clear();
    1029           0 :     if (rSource.Source == m_xJavaConfiguration)
    1030           0 :         m_xJavaConfiguration.clear();
    1031           0 : }
    1032             : 
    1033           0 : void SAL_CALL JavaVirtualMachine::elementInserted(
    1034             :     css::container::ContainerEvent const &)
    1035             :     throw (css::uno::RuntimeException, std::exception)
    1036           0 : {}
    1037             : 
    1038           0 : void SAL_CALL JavaVirtualMachine::elementRemoved(
    1039             :     css::container::ContainerEvent const &)
    1040             :     throw (css::uno::RuntimeException, std::exception)
    1041           0 : {}
    1042             : 
    1043             : // If a user changes the setting, for example for proxy settings, then this
    1044             : // function will be called from the configuration manager.  Even if the .xml
    1045             : // file does not contain an entry yet and that entry has to be inserted, this
    1046             : // function will be called.  We call java.lang.System.setProperty for the new
    1047             : // values.
    1048           0 : void SAL_CALL JavaVirtualMachine::elementReplaced(
    1049             :     css::container::ContainerEvent const & rEvent)
    1050             :     throw (css::uno::RuntimeException, std::exception)
    1051             : {
    1052             :     // TODO Using the new value stored in rEvent is wrong here.  If two threads
    1053             :     // receive different elementReplaced calls in quick succession, it is
    1054             :     // unspecified which changes the JVM's system properties last.  A correct
    1055             :     // solution must atomically (i.e., protected by a mutex) read the latest
    1056             :     // value from the configuration and set it as a system property at the JVM.
    1057             : 
    1058           0 :     OUString aAccessor;
    1059           0 :     rEvent.Accessor >>= aAccessor;
    1060           0 :     OUString aPropertyName;
    1061           0 :     OUString aPropertyName2;
    1062           0 :     OUString aPropertyValue;
    1063           0 :     bool bSecurityChanged = false;
    1064           0 :     if ( aAccessor == "ooInetProxyType" )
    1065             :     {
    1066             :         // Proxy none, manually
    1067           0 :         sal_Int32 value = 0;
    1068           0 :         rEvent.Element >>= value;
    1069           0 :         setINetSettingsInVM(value != 0);
    1070           0 :         return;
    1071             :     }
    1072           0 :     else if ( aAccessor == "ooInetHTTPProxyName" )
    1073             :     {
    1074           0 :         aPropertyName = "http.proxyHost";
    1075           0 :         rEvent.Element >>= aPropertyValue;
    1076             :     }
    1077           0 :     else if ( aAccessor == "ooInetHTTPProxyPort" )
    1078             :     {
    1079           0 :         aPropertyName = "http.proxyPort";
    1080           0 :         sal_Int32 n = 0;
    1081           0 :         rEvent.Element >>= n;
    1082           0 :         aPropertyValue = OUString::number(n);
    1083             :     }
    1084           0 :     else if ( aAccessor == "ooInetHTTPSProxyName" )
    1085             :     {
    1086           0 :         aPropertyName = "https.proxyHost";
    1087           0 :         rEvent.Element >>= aPropertyValue;
    1088             :     }
    1089           0 :     else if ( aAccessor == "ooInetHTTPSProxyPort" )
    1090             :     {
    1091           0 :         aPropertyName = "https.proxyPort";
    1092           0 :         sal_Int32 n = 0;
    1093           0 :         rEvent.Element >>= n;
    1094           0 :         aPropertyValue = OUString::number(n);
    1095             :     }
    1096           0 :     else if ( aAccessor == "ooInetFTPProxyName" )
    1097             :     {
    1098           0 :         aPropertyName = "ftp.proxyHost";
    1099           0 :         rEvent.Element >>= aPropertyValue;
    1100             :     }
    1101           0 :     else if ( aAccessor == "ooInetFTPProxyPort" )
    1102             :     {
    1103           0 :         aPropertyName = "ftp.proxyPort";
    1104           0 :         sal_Int32 n = 0;
    1105           0 :         rEvent.Element >>= n;
    1106           0 :         aPropertyValue = OUString::number(n);
    1107             :     }
    1108           0 :     else if ( aAccessor == "ooInetNoProxy" )
    1109             :     {
    1110           0 :         aPropertyName = "http.nonProxyHosts";
    1111           0 :         aPropertyName2 = "ftp.nonProxyHosts";
    1112           0 :         rEvent.Element >>= aPropertyValue;
    1113           0 :         aPropertyValue = aPropertyValue.replace(';', '|');
    1114             :     }
    1115           0 :     else if ( aAccessor == "NetAccess" )
    1116             :     {
    1117           0 :         aPropertyName = "appletviewer.security.mode";
    1118           0 :         sal_Int32 n = 0;
    1119           0 :         if (rEvent.Element >>= n)
    1120           0 :             switch (n)
    1121             :             {
    1122             :             case 0:
    1123           0 :                 aPropertyValue = "host";
    1124           0 :                 break;
    1125             :             case 1:
    1126           0 :                 aPropertyValue = "unrestricted";
    1127           0 :                 break;
    1128             :             case 3:
    1129           0 :                 aPropertyValue = "none";
    1130           0 :                 break;
    1131             :             }
    1132             :         else
    1133           0 :             return;
    1134           0 :         bSecurityChanged = true;
    1135             :     }
    1136           0 :     else if ( aAccessor == "Security" )
    1137             :     {
    1138           0 :         aPropertyName = "stardiv.security.disableSecurity";
    1139           0 :         sal_Bool b = sal_Bool();
    1140           0 :         if (rEvent.Element >>= b)
    1141           0 :             if (b)
    1142           0 :                 aPropertyValue = "false";
    1143             :             else
    1144           0 :                 aPropertyValue = "true";
    1145             :         else
    1146           0 :             return;
    1147           0 :         bSecurityChanged = true;
    1148             :     }
    1149             :     else
    1150           0 :         return;
    1151             : 
    1152           0 :     rtl::Reference< jvmaccess::VirtualMachine > xVirtualMachine;
    1153             :     {
    1154           0 :         osl::MutexGuard aGuard(m_aMutex);
    1155           0 :         if (m_xUnoVirtualMachine.is()) {
    1156           0 :             xVirtualMachine = m_xUnoVirtualMachine->getVirtualMachine();
    1157           0 :         }
    1158             :     }
    1159           0 :     if (xVirtualMachine.is())
    1160             :     {
    1161             :         try
    1162             :         {
    1163             :             jvmaccess::VirtualMachine::AttachGuard aAttachGuard(
    1164           0 :                 xVirtualMachine);
    1165           0 :             JNIEnv * pJNIEnv = aAttachGuard.getEnvironment();
    1166             : 
    1167             :             // call java.lang.System.setProperty
    1168             :             // String setProperty( String key, String value)
    1169           0 :             jclass jcSystem= pJNIEnv->FindClass("java/lang/System");
    1170           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:FindClass java/lang/System", 0);
    1171           0 :             jmethodID jmSetProps= pJNIEnv->GetStaticMethodID( jcSystem, "setProperty","(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    1172           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetStaticMethodID java.lang.System.setProperty", 0);
    1173             : 
    1174           0 :             jstring jsPropName= pJNIEnv->NewString( aPropertyName.getStr(), aPropertyName.getLength());
    1175           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1176             : 
    1177             :             // remove the property if it does not have a value ( user left the dialog field empty)
    1178             :             // or if the port is set to 0
    1179           0 :             aPropertyValue= aPropertyValue.trim();
    1180           0 :             if( aPropertyValue.isEmpty() ||
    1181           0 :                ( ( aPropertyName == "ftp.proxyPort" || aPropertyName == "http.proxyPort" /*|| aPropertyName == "socksProxyPort"*/ ) && aPropertyValue == "0" )
    1182             :               )
    1183             :             {
    1184             :                 // call java.lang.System.getProperties
    1185           0 :                 jmethodID jmGetProps= pJNIEnv->GetStaticMethodID( jcSystem, "getProperties","()Ljava/util/Properties;");
    1186           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetStaticMethodID java.lang.System.getProperties", 0);
    1187           0 :                 jobject joProperties= pJNIEnv->CallStaticObjectMethod( jcSystem, jmGetProps);
    1188           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.getProperties", 0);
    1189             :                 // call java.util.Properties.remove
    1190           0 :                 jclass jcProperties= pJNIEnv->FindClass("java/util/Properties");
    1191           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:FindClass java/util/Properties", 0);
    1192           0 :                 jmethodID jmRemove= pJNIEnv->GetMethodID( jcProperties, "remove", "(Ljava/lang/Object;)Ljava/lang/Object;");
    1193           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetMethodID java.util.Properties.remove", 0);
    1194           0 :                 pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsPropName);
    1195             : 
    1196             :                 // special calse for ftp.nonProxyHosts and http.nonProxyHosts. The office only
    1197             :                 // has a value for two java properties
    1198           0 :                 if (!aPropertyName2.isEmpty())
    1199             :                 {
    1200           0 :                     jstring jsPropName2= pJNIEnv->NewString( aPropertyName2.getStr(), aPropertyName2.getLength());
    1201           0 :                     if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1202           0 :                     pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsPropName2);
    1203             :                 }
    1204             :             }
    1205             :             else
    1206             :             {
    1207             :                 // Change the Value of the property
    1208           0 :                 jstring jsPropValue= pJNIEnv->NewString( aPropertyValue.getStr(), aPropertyValue.getLength());
    1209           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1210           0 :                 pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsPropName, jsPropValue);
    1211           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1212             : 
    1213             :                 // special calse for ftp.nonProxyHosts and http.nonProxyHosts. The office only
    1214             :                 // has a value for two java properties
    1215           0 :                 if (!aPropertyName2.isEmpty())
    1216             :                 {
    1217           0 :                     jstring jsPropName2= pJNIEnv->NewString( aPropertyName2.getStr(), aPropertyName2.getLength());
    1218           0 :                     if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1219           0 :                     jsPropValue= pJNIEnv->NewString( aPropertyValue.getStr(), aPropertyValue.getLength());
    1220           0 :                     if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1221           0 :                     pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsPropName2, jsPropValue);
    1222           0 :                     if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1223             :                 }
    1224             :             }
    1225             : 
    1226             :             // If the settings for Security and NetAccess changed then we have to notify the SandboxSecurity
    1227             :             // SecurityManager
    1228             :             // call System.getSecurityManager()
    1229           0 :             if (bSecurityChanged)
    1230             :             {
    1231           0 :                 jmethodID jmGetSecur= pJNIEnv->GetStaticMethodID( jcSystem,"getSecurityManager","()Ljava/lang/SecurityManager;");
    1232           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetStaticMethodID java.lang.System.getSecurityManager", 0);
    1233           0 :                 jobject joSecur= pJNIEnv->CallStaticObjectMethod( jcSystem, jmGetSecur);
    1234           0 :                 if (joSecur != 0)
    1235             :                 {
    1236             :                     // Make sure the SecurityManager is our SandboxSecurity
    1237             :                     // FindClass("com.sun.star.lib.sandbox.SandboxSecurityManager" only worked at the first time
    1238             :                     // this code was executed. Maybe it is a security feature. However, all attempts to debug the
    1239             :                     // SandboxSecurity class (maybe the VM invokes checkPackageAccess)  failed.
    1240             : //                  jclass jcSandboxSec= pJNIEnv->FindClass("com.sun.star.lib.sandbox.SandboxSecurity");
    1241             : //                  if(pJNIEnv->ExceptionOccurred()) throw RuntimeException("JNI:FindClass com.sun.star.lib.sandbox.SandboxSecurity", Reference<XInterface>());
    1242             : //                  jboolean bIsSand= pJNIEnv->IsInstanceOf( joSecur, jcSandboxSec);
    1243             :                     // The SecurityManagers class Name must be com.sun.star.lib.sandbox.SandboxSecurity
    1244           0 :                     jclass jcSec= pJNIEnv->GetObjectClass( joSecur);
    1245           0 :                     jclass jcClass= pJNIEnv->FindClass("java/lang/Class");
    1246           0 :                     if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:FindClass java.lang.Class", 0);
    1247           0 :                     jmethodID jmName= pJNIEnv->GetMethodID( jcClass,"getName","()Ljava/lang/String;");
    1248           0 :                     if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetMethodID java.lang.Class.getName", 0);
    1249           0 :                     jstring jsClass= (jstring) pJNIEnv->CallObjectMethod( jcSec, jmName);
    1250           0 :                     const jchar* jcharName= pJNIEnv->GetStringChars( jsClass, NULL);
    1251           0 :                     OUString sName( jcharName);
    1252             :                     jboolean bIsSandbox;
    1253           0 :                     if ( sName == "com.sun.star.lib.sandbox.SandboxSecurity" )
    1254           0 :                         bIsSandbox= JNI_TRUE;
    1255             :                     else
    1256           0 :                         bIsSandbox= JNI_FALSE;
    1257           0 :                     pJNIEnv->ReleaseStringChars( jsClass, jcharName);
    1258             : 
    1259           0 :                     if (bIsSandbox == JNI_TRUE)
    1260             :                     {
    1261             :                         // call SandboxSecurity.reset
    1262           0 :                         jmethodID jmReset= pJNIEnv->GetMethodID( jcSec,"reset","()V");
    1263           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetMethodID com.sun.star.lib.sandbox.SandboxSecurity.reset", 0);
    1264           0 :                         pJNIEnv->CallVoidMethod( joSecur, jmReset);
    1265           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallVoidMethod com.sun.star.lib.sandbox.SandboxSecurity.reset", 0);
    1266           0 :                     }
    1267             :                 }
    1268           0 :             }
    1269             :         }
    1270           0 :         catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    1271             :         {
    1272             :             throw css::uno::RuntimeException(
    1273             :                 OUString("jvmaccess::VirtualMachine::AttachGuard::CreationException"),
    1274           0 :                 0);
    1275             :         }
    1276           0 :     }
    1277             : }
    1278             : 
    1279           0 : JavaVirtualMachine::~JavaVirtualMachine()
    1280             : {
    1281           0 :     if (m_xInetConfiguration.is())
    1282             :         // We should never get here, but just in case...
    1283             :         try
    1284             :         {
    1285           0 :             m_xInetConfiguration->removeContainerListener(this);
    1286             :         }
    1287           0 :         catch (css::uno::Exception &)
    1288             :         {
    1289             :             OSL_FAIL("com.sun.star.uno.Exception caught");
    1290             :         }
    1291           0 :     if (m_xJavaConfiguration.is())
    1292             :         // We should never get here, but just in case...
    1293             :         try
    1294             :         {
    1295           0 :             m_xJavaConfiguration->removeContainerListener(this);
    1296             :         }
    1297           0 :         catch (css::uno::Exception &)
    1298             :         {
    1299             :             OSL_FAIL("com.sun.star.uno.Exception caught");
    1300             :         }
    1301           0 : }
    1302             : 
    1303           0 : void SAL_CALL JavaVirtualMachine::disposing()
    1304             : {
    1305           0 :     css::uno::Reference< css::container::XContainer > xContainer1;
    1306           0 :     css::uno::Reference< css::container::XContainer > xContainer2;
    1307             :     {
    1308           0 :         osl::MutexGuard aGuard(m_aMutex);
    1309           0 :         m_bDisposed = true;
    1310           0 :         xContainer1 = m_xInetConfiguration;
    1311           0 :         m_xInetConfiguration.clear();
    1312           0 :         xContainer2 = m_xJavaConfiguration;
    1313           0 :         m_xJavaConfiguration.clear();
    1314             :     }
    1315           0 :     if (xContainer1.is())
    1316           0 :         xContainer1->removeContainerListener(this);
    1317           0 :     if (xContainer2.is())
    1318           0 :         xContainer2->removeContainerListener(this);
    1319           0 : }
    1320             : 
    1321             : /*We listen to changes in the configuration. For example, the user changes the proxy
    1322             :   settings in the options dialog (menu tools). Then we are notified of this change and
    1323             :   if the java vm is already running we change the properties (System.lang.System.setProperties)
    1324             :   through JNI.
    1325             :   To receive notifications this class implements XContainerListener.
    1326             : */
    1327           0 : void JavaVirtualMachine::registerConfigChangesListener()
    1328             : {
    1329             :     try
    1330             :     {
    1331             :         css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
    1332           0 :             m_xContext->getValueByName(
    1333           0 :                 "/singletons/com.sun.star.configuration.theDefaultProvider"),
    1334           0 :             css::uno::UNO_QUERY);
    1335             : 
    1336           0 :         if (xConfigProvider.is())
    1337             :         {
    1338             :             // We register this instance as listener to changes in org.openoffice.Inet/Settings
    1339             :             // arguments for ConfigurationAccess
    1340           0 :             css::uno::Sequence< css::uno::Any > aArguments(2);
    1341           0 :             aArguments[0] <<= css::beans::PropertyValue(
    1342             :                 OUString("nodepath"),
    1343             :                 0,
    1344             :                 css::uno::makeAny(OUString("org.openoffice.Inet/Settings")),
    1345           0 :                 css::beans::PropertyState_DIRECT_VALUE);
    1346             :             // depth: -1 means unlimited
    1347           0 :             aArguments[1] <<= css::beans::PropertyValue(
    1348             :                 OUString("depth"),
    1349             :                 0,
    1350             :                 css::uno::makeAny( (sal_Int32)-1),
    1351           0 :                 css::beans::PropertyState_DIRECT_VALUE);
    1352             : 
    1353             :             m_xInetConfiguration
    1354           0 :                 = css::uno::Reference< css::container::XContainer >(
    1355           0 :                     xConfigProvider->createInstanceWithArguments(
    1356             :                         OUString("com.sun.star.configuration.ConfigurationAccess"),
    1357           0 :                         aArguments),
    1358           0 :                     css::uno::UNO_QUERY);
    1359             : 
    1360           0 :             if (m_xInetConfiguration.is())
    1361           0 :                 m_xInetConfiguration->addContainerListener(this);
    1362             : 
    1363             :             // now register as listener to changes in org.openoffice.Java/VirtualMachine
    1364           0 :             css::uno::Sequence< css::uno::Any > aArguments2(2);
    1365           0 :             aArguments2[0] <<= css::beans::PropertyValue(
    1366             :                 OUString("nodepath"),
    1367             :                 0,
    1368             :                 css::uno::makeAny(OUString("org.openoffice.Office.Java/VirtualMachine")),
    1369           0 :                 css::beans::PropertyState_DIRECT_VALUE);
    1370             :             // depth: -1 means unlimited
    1371           0 :             aArguments2[1] <<= css::beans::PropertyValue(
    1372             :                 OUString("depth"),
    1373             :                 0,
    1374             :                 css::uno::makeAny( (sal_Int32)-1),
    1375           0 :                 css::beans::PropertyState_DIRECT_VALUE);
    1376             : 
    1377             :             m_xJavaConfiguration
    1378           0 :                 = css::uno::Reference< css::container::XContainer >(
    1379           0 :                     xConfigProvider->createInstanceWithArguments(
    1380             :                         OUString("com.sun.star.configuration.ConfigurationAccess"),
    1381           0 :                         aArguments2),
    1382           0 :                     css::uno::UNO_QUERY);
    1383             : 
    1384           0 :             if (m_xJavaConfiguration.is())
    1385           0 :                 m_xJavaConfiguration->addContainerListener(this);
    1386           0 :         }
    1387           0 :     }catch(const css::uno::Exception & e)
    1388             :     {
    1389             : #if OSL_DEBUG_LEVEL > 1
    1390             :         OString message = OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US);
    1391             :         OSL_TRACE("javavm.cxx: could not set up listener for Configuration because of >%s<", message.getStr());
    1392             : #else
    1393             :         (void) e; // unused
    1394             : #endif
    1395             :     }
    1396           0 : }
    1397             : 
    1398             : // param true: all Inet setting are set as Java Properties on a live VM.
    1399             : // false: the Java net properties are set to empty value.
    1400           0 : void JavaVirtualMachine::setINetSettingsInVM(bool set_reset)
    1401             : {
    1402           0 :     osl::MutexGuard aGuard(m_aMutex);
    1403             :     try
    1404             :     {
    1405           0 :         if (m_xUnoVirtualMachine.is())
    1406             :         {
    1407             :             jvmaccess::VirtualMachine::AttachGuard aAttachGuard(
    1408           0 :                 m_xUnoVirtualMachine->getVirtualMachine());
    1409           0 :             JNIEnv * pJNIEnv = aAttachGuard.getEnvironment();
    1410             : 
    1411             :             // The Java Properties
    1412           0 :             OUString sFtpProxyHost("ftp.proxyHost");
    1413           0 :             OUString sFtpProxyPort("ftp.proxyPort");
    1414           0 :             OUString sFtpNonProxyHosts ("ftp.nonProxyHosts");
    1415           0 :             OUString sHttpProxyHost("http.proxyHost");
    1416           0 :             OUString sHttpProxyPort("http.proxyPort");
    1417           0 :             OUString sHttpNonProxyHosts("http.nonProxyHosts");
    1418             : 
    1419             :             // creat Java Properties as JNI strings
    1420           0 :             jstring jsFtpProxyHost= pJNIEnv->NewString( sFtpProxyHost.getStr(), sFtpProxyHost.getLength());
    1421           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1422           0 :             jstring jsFtpProxyPort= pJNIEnv->NewString( sFtpProxyPort.getStr(), sFtpProxyPort.getLength());
    1423           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1424           0 :             jstring jsFtpNonProxyHosts= pJNIEnv->NewString( sFtpNonProxyHosts.getStr(), sFtpNonProxyHosts.getLength());
    1425           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1426           0 :             jstring jsHttpProxyHost= pJNIEnv->NewString( sHttpProxyHost.getStr(), sHttpProxyHost.getLength());
    1427           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1428           0 :             jstring jsHttpProxyPort= pJNIEnv->NewString( sHttpProxyPort.getStr(), sHttpProxyPort.getLength());
    1429           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1430           0 :             jstring jsHttpNonProxyHosts= pJNIEnv->NewString( sHttpNonProxyHosts.getStr(), sHttpNonProxyHosts.getLength());
    1431           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1432             : 
    1433             :             // prepare java.lang.System.setProperty
    1434           0 :             jclass jcSystem= pJNIEnv->FindClass("java/lang/System");
    1435           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:FindClass java/lang/System", 0);
    1436           0 :             jmethodID jmSetProps= pJNIEnv->GetStaticMethodID( jcSystem, "setProperty","(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    1437           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetStaticMethodID java.lang.System.setProperty", 0);
    1438             : 
    1439             :             // call java.lang.System.getProperties
    1440           0 :             jmethodID jmGetProps= pJNIEnv->GetStaticMethodID( jcSystem, "getProperties","()Ljava/util/Properties;");
    1441           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetStaticMethodID java.lang.System.getProperties", 0);
    1442           0 :             jobject joProperties= pJNIEnv->CallStaticObjectMethod( jcSystem, jmGetProps);
    1443           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.getProperties", 0);
    1444             :             // prepare java.util.Properties.remove
    1445           0 :             jclass jcProperties= pJNIEnv->FindClass("java/util/Properties");
    1446           0 :             if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:FindClass java/util/Properties", 0);
    1447             : 
    1448           0 :             if (set_reset)
    1449             :             {
    1450             :                 // Set all network properties with the VM
    1451           0 :                 JVM jvm;
    1452           0 :                 getINetPropsFromConfig( &jvm, m_xContext->getServiceManager(), m_xContext);
    1453           0 :                 const ::std::vector< OUString> & Props = jvm.getProperties();
    1454             :                 typedef ::std::vector< OUString >::const_iterator C_IT;
    1455             : 
    1456           0 :                 for( C_IT i= Props.begin(); i != Props.end(); ++i)
    1457             :                 {
    1458           0 :                     OUString prop= *i;
    1459           0 :                     sal_Int32 index= prop.indexOf( (sal_Unicode)'=');
    1460           0 :                     OUString propName= prop.copy( 0, index);
    1461           0 :                     OUString propValue= prop.copy( index + 1);
    1462             : 
    1463           0 :                     if( propName.equals( sFtpProxyHost))
    1464             :                     {
    1465           0 :                         jstring jsVal= pJNIEnv->NewString( propValue.getStr(), propValue.getLength());
    1466           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1467           0 :                         pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsFtpProxyHost, jsVal);
    1468           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1469             :                     }
    1470           0 :                     else if( propName.equals( sFtpProxyPort))
    1471             :                     {
    1472           0 :                         jstring jsVal= pJNIEnv->NewString( propValue.getStr(), propValue.getLength());
    1473           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1474           0 :                         pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsFtpProxyPort, jsVal);
    1475           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1476             :                     }
    1477           0 :                     else if( propName.equals( sFtpNonProxyHosts))
    1478             :                     {
    1479           0 :                         jstring jsVal= pJNIEnv->NewString( propValue.getStr(), propValue.getLength());
    1480           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1481           0 :                         pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsFtpNonProxyHosts, jsVal);
    1482           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1483             :                     }
    1484           0 :                     else if( propName.equals( sHttpProxyHost))
    1485             :                     {
    1486           0 :                         jstring jsVal= pJNIEnv->NewString( propValue.getStr(), propValue.getLength());
    1487           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1488           0 :                         pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsHttpProxyHost, jsVal);
    1489           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1490             :                     }
    1491           0 :                     else if( propName.equals( sHttpProxyPort))
    1492             :                     {
    1493           0 :                         jstring jsVal= pJNIEnv->NewString( propValue.getStr(), propValue.getLength());
    1494           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1495           0 :                         pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsHttpProxyPort, jsVal);
    1496           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1497             :                     }
    1498           0 :                     else if( propName.equals( sHttpNonProxyHosts))
    1499             :                     {
    1500           0 :                         jstring jsVal= pJNIEnv->NewString( propValue.getStr(), propValue.getLength());
    1501           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:NewString", 0);
    1502           0 :                         pJNIEnv->CallStaticObjectMethod( jcSystem, jmSetProps, jsHttpNonProxyHosts, jsVal);
    1503           0 :                         if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:CallStaticObjectMethod java.lang.System.setProperty", 0);
    1504             :                     }
    1505           0 :                 }
    1506             :             }
    1507             :             else
    1508             :             {
    1509             :                 // call java.util.Properties.remove
    1510           0 :                 jmethodID jmRemove= pJNIEnv->GetMethodID( jcProperties, "remove", "(Ljava/lang/Object;)Ljava/lang/Object;");
    1511           0 :                 if(pJNIEnv->ExceptionOccurred()) throw css::uno::RuntimeException("JNI:GetMethodID java.util.Property.remove", 0);
    1512           0 :                 pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsFtpProxyHost);
    1513           0 :                 pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsFtpProxyPort);
    1514           0 :                 pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsFtpNonProxyHosts);
    1515           0 :                 pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsHttpProxyHost);
    1516           0 :                 pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsHttpProxyPort);
    1517           0 :                 pJNIEnv->CallObjectMethod( joProperties, jmRemove, jsHttpNonProxyHosts);
    1518           0 :             }
    1519             :         }
    1520             :     }
    1521           0 :     catch (css::uno::RuntimeException &)
    1522             :     {
    1523             :         OSL_FAIL("RuntimeException");
    1524             :     }
    1525           0 :     catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
    1526             :     {
    1527             :         OSL_FAIL("jvmaccess::VirtualMachine::AttachGuard::CreationException");
    1528           0 :     }
    1529           0 : }
    1530             : 
    1531           0 : void JavaVirtualMachine::setUpUnoVirtualMachine(JNIEnv * environment) {
    1532           0 :     css::uno::Reference< css::util::XMacroExpander > exp = css::util::theMacroExpander::get(m_xContext);
    1533           0 :     OUString baseUrl;
    1534             :     try {
    1535           0 :         baseUrl = exp->expandMacros(
    1536           0 :             OUString("$URE_INTERNAL_JAVA_DIR/"));
    1537           0 :     } catch (css::lang::IllegalArgumentException &) {
    1538             :         throw css::uno::RuntimeException(
    1539             :             OUString("com::sun::star::lang::IllegalArgumentException"),
    1540           0 :             static_cast< cppu::OWeakObject * >(this));
    1541             :     }
    1542           0 :     OUString classPath;
    1543             :     try {
    1544           0 :         classPath = exp->expandMacros(
    1545           0 :             OUString("$URE_INTERNAL_JAVA_CLASSPATH"));
    1546           0 :     } catch (css::lang::IllegalArgumentException &) {}
    1547             :     jclass class_URLClassLoader = environment->FindClass(
    1548           0 :         "java/net/URLClassLoader");
    1549           0 :     if (class_URLClassLoader == 0) {
    1550           0 :         handleJniException(environment);
    1551             :     }
    1552             :     jmethodID ctor_URLClassLoader = environment->GetMethodID(
    1553           0 :         class_URLClassLoader, "<init>", "([Ljava/net/URL;)V");
    1554           0 :     if (ctor_URLClassLoader == 0) {
    1555           0 :         handleJniException(environment);
    1556             :     }
    1557           0 :     jclass class_URL = environment->FindClass("java/net/URL");
    1558           0 :     if (class_URL == 0) {
    1559           0 :         handleJniException(environment);
    1560             :     }
    1561             :     jmethodID ctor_URL_1 = environment->GetMethodID(
    1562           0 :         class_URL, "<init>", "(Ljava/lang/String;)V");
    1563           0 :     if (ctor_URL_1 == 0) {
    1564           0 :         handleJniException(environment);
    1565             :     }
    1566             :     jvalue args[3];
    1567             :     args[0].l = environment->NewString(
    1568             :         static_cast< jchar const * >(baseUrl.getStr()),
    1569           0 :         static_cast< jsize >(baseUrl.getLength()));
    1570           0 :     if (args[0].l == 0) {
    1571           0 :         handleJniException(environment);
    1572             :     }
    1573           0 :     jobject base = environment->NewObjectA(class_URL, ctor_URL_1, args);
    1574           0 :     if (base == 0) {
    1575           0 :         handleJniException(environment);
    1576             :     }
    1577             :     jmethodID ctor_URL_2 = environment->GetMethodID(
    1578           0 :         class_URL, "<init>", "(Ljava/net/URL;Ljava/lang/String;)V");
    1579           0 :     if (ctor_URL_2 == 0) {
    1580           0 :         handleJniException(environment);
    1581             :     }
    1582             :     jobjectArray classpath = jvmaccess::ClassPath::translateToUrls(
    1583           0 :         m_xContext, environment, classPath);
    1584           0 :     if (classpath == 0) {
    1585           0 :         handleJniException(environment);
    1586             :     }
    1587           0 :     args[0].l = base;
    1588           0 :     args[1].l = environment->NewStringUTF("unoloader.jar");
    1589           0 :     if (args[1].l == 0) {
    1590           0 :         handleJniException(environment);
    1591             :     }
    1592           0 :     args[0].l = environment->NewObjectA(class_URL, ctor_URL_2, args);
    1593           0 :     if (args[0].l == 0) {
    1594           0 :         handleJniException(environment);
    1595             :     }
    1596           0 :     args[0].l = environment->NewObjectArray(1, class_URL, args[0].l);
    1597           0 :     if (args[0].l == 0) {
    1598           0 :         handleJniException(environment);
    1599             :     }
    1600             :     jobject cl1 = environment->NewObjectA(
    1601           0 :         class_URLClassLoader, ctor_URLClassLoader, args);
    1602           0 :     if (cl1 == 0) {
    1603           0 :         handleJniException(environment);
    1604             :     }
    1605             :     jmethodID method_loadClass = environment->GetMethodID(
    1606             :         class_URLClassLoader, "loadClass",
    1607           0 :         "(Ljava/lang/String;)Ljava/lang/Class;");
    1608           0 :     if (method_loadClass == 0) {
    1609           0 :         handleJniException(environment);
    1610             :     }
    1611             :     args[0].l = environment->NewStringUTF(
    1612           0 :         "com.sun.star.lib.unoloader.UnoClassLoader");
    1613           0 :     if (args[0].l == 0) {
    1614           0 :         handleJniException(environment);
    1615             :     }
    1616             :     jclass class_UnoClassLoader = static_cast< jclass >(
    1617           0 :         environment->CallObjectMethodA(cl1, method_loadClass, args));
    1618           0 :     if (class_UnoClassLoader == 0) {
    1619           0 :         handleJniException(environment);
    1620             :     }
    1621             :     jmethodID ctor_UnoClassLoader = environment->GetMethodID(
    1622             :         class_UnoClassLoader, "<init>",
    1623           0 :         "(Ljava/net/URL;[Ljava/net/URL;Ljava/lang/ClassLoader;)V");
    1624           0 :     if (ctor_UnoClassLoader == 0) {
    1625           0 :         handleJniException(environment);
    1626             :     }
    1627           0 :     args[0].l = base;
    1628           0 :     args[1].l = classpath;
    1629           0 :     args[2].l = cl1;
    1630             :     jobject cl2 = environment->NewObjectA(
    1631           0 :         class_UnoClassLoader, ctor_UnoClassLoader, args);
    1632           0 :     if (cl2 == 0) {
    1633           0 :         handleJniException(environment);
    1634             :     }
    1635             :     try {
    1636           0 :         m_xUnoVirtualMachine = new jvmaccess::UnoVirtualMachine(
    1637           0 :             m_xVirtualMachine, cl2);
    1638           0 :     } catch (jvmaccess::UnoVirtualMachine::CreationException &) {
    1639             :         throw css::uno::RuntimeException(
    1640             :             OUString("jvmaccess::UnoVirtualMachine::CreationException"),
    1641           0 :             static_cast< cppu::OWeakObject * >(this));
    1642           0 :     }
    1643           0 : }
    1644             : 
    1645           0 : void JavaVirtualMachine::handleJniException(JNIEnv * environment) {
    1646           0 :     environment->ExceptionClear();
    1647             :     throw css::uno::RuntimeException(
    1648             :         OUString("JNI exception occurred"),
    1649           0 :         static_cast< cppu::OWeakObject * >(this));
    1650           0 : }
    1651             : 
    1652             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10