Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include <cstdarg>
21 : : #include <osl/diagnose.h>
22 : : #include <osl/process.h>
23 : :
24 : : #include <rtl/process.h>
25 : : #include <rtl/ustrbuf.hxx>
26 : :
27 : : #include <uno/environment.h>
28 : : #include <uno/mapping.hxx>
29 : : #include "com/sun/star/uno/RuntimeException.hpp"
30 : :
31 : : #include <cppuhelper/servicefactory.hxx>
32 : :
33 : : #ifdef LINUX
34 : : #undef minor
35 : : #undef major
36 : : #endif
37 : :
38 : : #include <com/sun/star/java/XJavaVM.hpp>
39 : :
40 : : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
41 : :
42 : : #include "jni.h"
43 : :
44 : : #include <cppuhelper/factory.hxx>
45 : : #include <cppuhelper/implementationentry.hxx>
46 : :
47 : : #include <cppuhelper/implbase2.hxx>
48 : :
49 : : #include <com/sun/star/loader/XImplementationLoader.hpp>
50 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
51 : : #include <com/sun/star/lang/XServiceInfo.hpp>
52 : : #include <com/sun/star/lang/XInitialization.hpp>
53 : : #include <com/sun/star/registry/XRegistryKey.hpp>
54 : :
55 : : #include "jvmaccess/unovirtualmachine.hxx"
56 : : #include "jvmaccess/virtualmachine.hxx"
57 : :
58 : : namespace css = com::sun::star;
59 : :
60 : : using namespace ::com::sun::star::java;
61 : : using namespace ::com::sun::star::lang;
62 : : using namespace ::com::sun::star::loader;
63 : : using namespace ::com::sun::star::uno;
64 : : using namespace ::com::sun::star::registry;
65 : :
66 : : using namespace ::cppu;
67 : : using namespace ::rtl;
68 : : using namespace ::osl;
69 : :
70 : : namespace stoc_javaloader {
71 : :
72 : : static Mutex & getInitMutex();
73 : :
74 : 4 : static Sequence< OUString > loader_getSupportedServiceNames()
75 : : {
76 : 4 : Sequence< OUString > seqNames(2);
77 : : seqNames.getArray()[0] = OUString(
78 [ + - ][ + - ]: 4 : RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Java") );
79 [ + - ]: 4 : seqNames.getArray()[1] = OUString(
80 [ + - ]: 8 : RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.loader.Java2") );
81 : 4 : return seqNames;
82 : : }
83 : :
84 : 4 : static OUString loader_getImplementationName()
85 : : {
86 : : return OUString(
87 : 4 : RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.stoc.JavaComponentLoader" ) );
88 : : }
89 : :
90 : : class JavaComponentLoader : public WeakImplHelper2<XImplementationLoader, XServiceInfo>
91 : : {
92 : : css::uno::Reference<XComponentContext> m_xComponentContext;
93 : : /** Do not use m_javaLoader directly. Instead use getJavaLoader.
94 : : */
95 : : css::uno::Reference<XImplementationLoader> m_javaLoader;
96 : : /** The retured Reference contains a null pointer if the office is not configured
97 : : to run java.
98 : :
99 : : @exception com::sun::star::uno::RuntimeException
100 : : If the Java implementation of the loader could not be obtained, for reasons other
101 : : then that java was not configured the RuntimeException is thrown.
102 : : */
103 : : const css::uno::Reference<XImplementationLoader> & getJavaLoader();
104 : :
105 : :
106 : : public:
107 : : JavaComponentLoader(const css::uno::Reference<XComponentContext> & xCtx)
108 : : throw(RuntimeException);
109 : : virtual ~JavaComponentLoader() throw();
110 : :
111 : : public:
112 : : // XServiceInfo
113 : : virtual OUString SAL_CALL getImplementationName() throw(RuntimeException);
114 : : virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
115 : : throw(RuntimeException);
116 : : virtual Sequence<OUString> SAL_CALL getSupportedServiceNames()
117 : : throw(RuntimeException);
118 : :
119 : : // XImplementationLoader
120 : : virtual css::uno::Reference<XInterface> SAL_CALL activate(
121 : : const OUString& implementationName, const OUString& implementationLoaderUrl,
122 : : const OUString& locationUrl, const css::uno::Reference<XRegistryKey>& xKey)
123 : : throw(CannotActivateFactoryException, RuntimeException);
124 : : virtual sal_Bool SAL_CALL writeRegistryInfo(
125 : : const css::uno::Reference<XRegistryKey>& xKey,
126 : : const OUString& implementationLoaderUrl, const OUString& locationUrl)
127 : : throw(CannotRegisterImplementationException, RuntimeException);
128 : : };
129 : :
130 : 8 : const css::uno::Reference<XImplementationLoader> & JavaComponentLoader::getJavaLoader()
131 : : {
132 [ + - ][ + - ]: 8 : MutexGuard aGuard(getInitMutex());
133 : :
134 [ + + ]: 8 : if (m_javaLoader.is())
135 : 4 : return m_javaLoader;
136 : :
137 : 4 : uno_Environment * pJava_environment = NULL;
138 : 4 : uno_Environment * pUno_environment = NULL;
139 : 4 : typelib_InterfaceTypeDescription * pType_XImplementationLoader = 0;
140 : :
141 : : try {
142 : : // get a java vm, where we can create a loader
143 : : css::uno::Reference<XJavaVM> javaVM_xJavaVM(
144 [ + - ]: 4 : m_xComponentContext->getValueByName(
145 : : OUString(RTL_CONSTASCII_USTRINGPARAM(
146 : : "/singletons/"
147 : 4 : "com.sun.star.java.theJavaVirtualMachine"))),
148 [ + - ][ + - ]: 4 : UNO_QUERY_THROW);
[ + - ]
149 : :
150 : : // Use the special protocol of XJavaVM.getJavaVM: If the passed in
151 : : // process ID has an extra 17th byte of value one, the returned any
152 : : // contains a pointer to a jvmaccess::UnoVirtualMachine, instead of the
153 : : // underlying JavaVM pointer:
154 [ + - ]: 4 : Sequence<sal_Int8> processID(17);
155 [ + - ][ + - ]: 4 : rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8 *>(processID.getArray()));
156 [ + - ]: 4 : processID[16] = 1;
157 : :
158 : : // We get a non-refcounted pointer to a jvmaccess::UnoVirtualMachine
159 : : // from the XJavaVM service (the pointer is guaranteed to be valid
160 : : // as long as our reference to the XJavaVM service lasts), and
161 : : // convert the non-refcounted pointer into a refcounted one
162 : : // immediately:
163 : : OSL_ENSURE(sizeof (sal_Int64)
164 : : >= sizeof (jvmaccess::UnoVirtualMachine *),
165 : : "Pointer cannot be represented as sal_Int64");
166 : : sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
167 : 4 : static_cast< jvmaccess::UnoVirtualMachine * >(0));
168 [ + - ][ + - ]: 4 : javaVM_xJavaVM->getJavaVM(processID) >>= nPointer;
169 : : rtl::Reference< jvmaccess::UnoVirtualMachine > xVirtualMachine(
170 [ + - ]: 4 : reinterpret_cast< jvmaccess::UnoVirtualMachine * >(nPointer));
171 [ - + ]: 4 : if (!xVirtualMachine.is())
172 : : {
173 : : //throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
174 : : // "javaloader error - JavaVirtualMachine service could not provide a VM")),
175 : : // css::uno::Reference<XInterface>());
176 : : // We must not throw a RuntimeException, because this might end the applications.
177 : : // It is ok if java components
178 : : // are not working because the office can be installed without Java support.
179 : : SAL_WARN("stoc", "getJavaVM returned null");
180 : 0 : return m_javaLoader; // null-ref
181 : : }
182 : :
183 : : try
184 : : {
185 : : jvmaccess::VirtualMachine::AttachGuard aGuard2(
186 [ + - ][ + - ]: 4 : xVirtualMachine->getVirtualMachine());
[ + - ]
187 : 4 : JNIEnv * pJNIEnv = aGuard2.getEnvironment();
188 : :
189 : : // instantiate the java JavaLoader
190 [ + - ]: 4 : jclass jcClassLoader = pJNIEnv->FindClass("java/lang/ClassLoader");
191 [ + - ][ - + ]: 4 : if(pJNIEnv->ExceptionOccurred())
192 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
193 : : "javaloader error - could not find class java/lang/ClassLoader")),
194 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
195 : : jmethodID jmLoadClass = pJNIEnv->GetMethodID(
196 : : jcClassLoader, "loadClass",
197 [ + - ]: 4 : "(Ljava/lang/String;)Ljava/lang/Class;");
198 [ + - ][ - + ]: 4 : if(pJNIEnv->ExceptionOccurred())
199 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
200 : : "javaloader error - could not find method java/lang/ClassLoader.loadClass")),
201 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
202 : : jvalue arg;
203 : : arg.l = pJNIEnv->NewStringUTF(
204 [ + - ]: 4 : "com.sun.star.comp.loader.JavaLoader");
205 [ + - ][ - + ]: 4 : if(pJNIEnv->ExceptionOccurred())
206 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
207 : : "javaloader error - could not create string")),
208 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
209 : : jclass jcJavaLoader = static_cast< jclass >(
210 : : pJNIEnv->CallObjectMethodA(
211 [ + - ]: 4 : static_cast< jobject >(xVirtualMachine->getClassLoader()),
212 [ + - ]: 4 : jmLoadClass, &arg));
213 [ + - ][ - + ]: 4 : if(pJNIEnv->ExceptionOccurred())
214 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
215 : : "javaloader error - could not find class com/sun/star/comp/loader/JavaLoader")),
216 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
217 [ + - ]: 4 : jmethodID jmJavaLoader_init = pJNIEnv->GetMethodID(jcJavaLoader, "<init>", "()V");
218 [ + - ][ - + ]: 4 : if(pJNIEnv->ExceptionOccurred())
219 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
220 : : "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")),
221 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
222 [ + - ]: 4 : jobject joJavaLoader = pJNIEnv->NewObject(jcJavaLoader, jmJavaLoader_init);
223 [ + - ][ - + ]: 4 : if(pJNIEnv->ExceptionOccurred())
224 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
225 : : "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed")),
226 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
227 : :
228 : : // map the java JavaLoader to this environment
229 [ + - ]: 4 : OUString sJava(RTL_CONSTASCII_USTRINGPARAM("java"));
230 : : uno_getEnvironment(&pJava_environment, sJava.pData,
231 : 4 : xVirtualMachine.get());
232 [ - + ]: 4 : if(!pJava_environment)
233 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
234 [ # # ][ # # ]: 0 : "javaloader error - no Java environment available")), css::uno::Reference<XInterface>());
235 : :
236 : : // why is there no convinient contructor?
237 [ + - ]: 4 : OUString sCppu_current_lb_name(RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
238 : 4 : uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, NULL);
239 [ - + ]: 4 : if(!pUno_environment)
240 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
241 [ # # ][ # # ]: 0 : "javaloader error - no C++ environment available")), css::uno::Reference<XInterface>());
242 : :
243 : 4 : Mapping java_curr(pJava_environment, pUno_environment);
244 [ - + ]: 4 : if(!java_curr.is())
245 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
246 [ # # ][ # # ]: 0 : "javaloader error - no mapping from java to C++ ")), css::uno::Reference<XInterface>());
247 : :
248 : : // release java environment
249 [ + - ]: 4 : pJava_environment->release(pJava_environment);
250 : 4 : pJava_environment = NULL;
251 : :
252 : : // release uno environment
253 [ + - ]: 4 : pUno_environment->release(pUno_environment);
254 : 4 : pUno_environment = NULL;
255 : :
256 [ + - ]: 4 : getCppuType((css::uno::Reference<XImplementationLoader> *) 0).
257 : 4 : getDescription((typelib_TypeDescription **) & pType_XImplementationLoader);
258 [ - + ]: 4 : if(!pType_XImplementationLoader)
259 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
260 : : "javaloader error - no type information for XImplementationLoader")),
261 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
262 : :
263 : : m_javaLoader = css::uno::Reference<XImplementationLoader>(reinterpret_cast<XImplementationLoader *>(
264 [ + - ][ + - ]: 4 : java_curr.mapInterface(joJavaLoader, pType_XImplementationLoader)));
[ + - ]
265 [ + - ]: 4 : pJNIEnv->DeleteLocalRef( joJavaLoader );
266 [ - + ]: 4 : if(!m_javaLoader.is())
267 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
268 : : "javaloader error - mapping of java XImplementationLoader to c++ failed")),
269 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
270 : :
271 : 4 : typelib_typedescription_release(reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
272 [ + - ][ + - ]: 4 : pType_XImplementationLoader = NULL;
273 : : }
274 [ # # ]: 0 : catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
275 : : {
276 : : throw RuntimeException(
277 : : OUString(RTL_CONSTASCII_USTRINGPARAM(
278 : : "jvmaccess::VirtualMachine::AttachGuard"
279 [ # # # # : 0 : "::CreationException")),0);
# # ]
280 : : }
281 : :
282 : : // set the service manager at the javaloader
283 [ + - ]: 4 : css::uno::Reference<XInitialization> javaLoader_XInitialization(m_javaLoader, UNO_QUERY);
284 [ - + ]: 4 : if(!javaLoader_XInitialization.is())
285 : : throw RuntimeException(OUString(RTL_CONSTASCII_USTRINGPARAM(
286 : : "javaloader error - initialization of java javaloader failed, no XInitialization")),
287 [ # # ][ # # ]: 0 : css::uno::Reference<XInterface>());
288 : :
289 : 4 : Any any;
290 : : any <<= css::uno::Reference<XMultiComponentFactory>(
291 [ + - ][ + - ]: 4 : m_xComponentContext->getServiceManager());
[ + - ]
292 : :
293 [ + - ][ + - ]: 4 : javaLoader_XInitialization->initialize(Sequence<Any>(&any, 1));
[ + - ][ + - ]
[ - + ][ + - ]
[ - + ][ + - ]
[ + - ]
294 : : }
295 [ # # ]: 0 : catch(RuntimeException &) {
296 [ # # ]: 0 : if(pJava_environment)
297 [ # # ]: 0 : pJava_environment->release(pJava_environment);
298 : :
299 [ # # ]: 0 : if(pUno_environment)
300 [ # # ]: 0 : pUno_environment->release(pUno_environment);
301 : :
302 [ # # ]: 0 : if(pType_XImplementationLoader)
303 : : typelib_typedescription_release(
304 : 0 : reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
305 : 0 : throw;
306 : : }
307 : : OSL_TRACE("javaloader.cxx: mapped javaloader - 0x%x", m_javaLoader.get());
308 [ + - ]: 8 : return m_javaLoader;
309 : : }
310 : :
311 : 4 : JavaComponentLoader::JavaComponentLoader(const css::uno::Reference<XComponentContext> & xCtx) throw(RuntimeException) :
312 : 4 : m_xComponentContext(xCtx)
313 : :
314 : : {
315 : :
316 : 4 : }
317 : :
318 : 0 : JavaComponentLoader::~JavaComponentLoader() throw()
319 : : {
320 [ # # ]: 0 : }
321 : :
322 : : // XServiceInfo
323 : 0 : OUString SAL_CALL JavaComponentLoader::getImplementationName()
324 : : throw(::com::sun::star::uno::RuntimeException)
325 : : {
326 : 0 : return loader_getImplementationName();
327 : : }
328 : :
329 : 0 : sal_Bool SAL_CALL JavaComponentLoader::supportsService(const OUString & ServiceName)
330 : : throw(::com::sun::star::uno::RuntimeException)
331 : : {
332 : 0 : sal_Bool bSupport = sal_False;
333 : :
334 [ # # ]: 0 : Sequence<OUString> aSNL = getSupportedServiceNames();
335 [ # # ]: 0 : const OUString * pArray = aSNL.getArray();
336 [ # # ][ # # ]: 0 : for(sal_Int32 i = 0; i < aSNL.getLength() && !bSupport; ++ i)
[ # # ]
337 : 0 : bSupport = pArray[i] == ServiceName;
338 : :
339 [ # # ]: 0 : return bSupport;
340 : : }
341 : :
342 : 0 : Sequence<OUString> SAL_CALL JavaComponentLoader::getSupportedServiceNames()
343 : : throw(::com::sun::star::uno::RuntimeException)
344 : : {
345 : 0 : return loader_getSupportedServiceNames();
346 : : }
347 : :
348 : :
349 : :
350 : : // XImplementationLoader
351 : 4 : sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(
352 : : const css::uno::Reference<XRegistryKey> & xKey, const OUString & blabla,
353 : : const OUString & rLibName)
354 : : throw(CannotRegisterImplementationException, RuntimeException)
355 : : {
356 : 4 : const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader();
357 [ + - ]: 4 : if (loader.is())
358 : 4 : return loader->writeRegistryInfo(xKey, blabla, rLibName);
359 : : else
360 : : throw CannotRegisterImplementationException(
361 [ # # ][ # # ]: 0 : OUString(RTL_CONSTASCII_USTRINGPARAM("Could not create Java implementation loader")), NULL);
[ # # ]
362 : : }
363 : :
364 : :
365 : 4 : css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader::activate(
366 : : const OUString & rImplName, const OUString & blabla, const OUString & rLibName,
367 : : const css::uno::Reference<XRegistryKey> & xKey)
368 : : throw(CannotActivateFactoryException, RuntimeException)
369 : : {
370 : 4 : const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader();
371 [ + - ]: 4 : if (loader.is())
372 : 4 : return loader->activate(rImplName, blabla, rLibName, xKey);
373 : : else
374 : : throw CannotActivateFactoryException(
375 [ # # ][ # # ]: 0 : OUString(RTL_CONSTASCII_USTRINGPARAM("Could not create Java implementation loader")), NULL);
[ # # ]
376 : : }
377 : :
378 : 16 : static Mutex & getInitMutex()
379 : : {
380 : : static Mutex * pMutex = 0;
381 [ + + ]: 16 : if( ! pMutex )
382 : : {
383 [ + - ][ + - ]: 4 : MutexGuard guard( Mutex::getGlobalMutex() );
384 [ + - ]: 4 : if( ! pMutex )
385 : : {
386 [ + - ][ + - ]: 4 : static Mutex mutex;
[ + - ][ # # ]
387 : 4 : pMutex = &mutex;
388 [ + - ]: 4 : }
389 : : }
390 : 16 : return *pMutex;
391 : : }
392 : :
393 : 8 : css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader_CreateInstance(const css::uno::Reference<XComponentContext> & xCtx) throw(Exception)
394 : : {
395 : 8 : css::uno::Reference<XInterface> xRet;
396 : :
397 : : try {
398 [ + - ][ + - ]: 8 : MutexGuard guard( getInitMutex() );
399 : : // The javaloader is never destroyed and there can be only one!
400 : : // Note that the first context wins ....
401 : : static css::uno::Reference< XInterface > *pStaticRef = 0;
402 [ + + ]: 8 : if( pStaticRef )
403 : : {
404 [ + - ]: 4 : xRet = *pStaticRef;
405 : : }
406 : : else
407 : : {
408 [ + - ][ + - ]: 4 : xRet = *new JavaComponentLoader(xCtx);
[ + - ]
409 : 4 : pStaticRef = new css::uno::Reference< XInterface > ( xRet );
410 [ + - ]: 8 : }
411 : : }
412 [ # # ]: 0 : catch(const RuntimeException & runtimeException) {
413 [ # # ]: 0 : OString message = OUStringToOString(runtimeException.Message, RTL_TEXTENCODING_ASCII_US);
414 [ # # ]: 0 : osl_trace("javaloader - could not init javaloader cause of %s", message.getStr());
415 : 0 : throw;
416 : : }
417 : :
418 : 8 : return xRet;
419 : : }
420 : :
421 : : } //end namespace
422 : :
423 : :
424 : : using namespace stoc_javaloader;
425 : :
426 : : static struct ImplementationEntry g_entries[] =
427 : : {
428 : : {
429 : : JavaComponentLoader_CreateInstance, loader_getImplementationName,
430 : : loader_getSupportedServiceNames, createSingleComponentFactory,
431 : : 0 , 0
432 : : },
433 : : { 0, 0, 0, 0, 0, 0 }
434 : : };
435 : :
436 : : extern "C"
437 : : {
438 : 4 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
439 : : const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
440 : : {
441 : 4 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
442 : : }
443 : : }
444 : :
445 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|