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