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