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