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