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