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