LibreOffice Module stoc (master) 1
javaloader.cxx
Go to the documentation of this file.
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 <rtl/process.h>
21#include <sal/log.hxx>
22
23#include <uno/environment.h>
24#include <uno/lbnames.h>
25#include <uno/mapping.hxx>
26#include <com/sun/star/uno/RuntimeException.hpp>
27#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
29
30#ifdef LINUX
31#undef minor
32#undef major
33#endif
34
35#include <com/sun/star/java/XJavaVM.hpp>
36
37#if defined __clang__
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wunknown-attributes"
40#endif
41#include <jni.h>
42#if defined __clang__
43#pragma clang diagnostic pop
44#endif
45
46#include <rtl/random.h>
47#include <rtl/ustrbuf.hxx>
48#include <osl/security.hxx>
49
51
55
56#include <com/sun/star/bridge/UnoUrlResolver.hpp>
57#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
58#include <com/sun/star/loader/XImplementationLoader.hpp>
59#include <com/sun/star/lang/XServiceInfo.hpp>
60#include <com/sun/star/lang/XInitialization.hpp>
61#include <com/sun/star/uno/XComponentContext.hpp>
62#include <com/sun/star/util/theMacroExpander.hpp>
63
66
67// this one is header-only
69
70#include <mutex>
71#include <thread>
72#include <utility>
73
74namespace com::sun::star::registry { class XRegistryKey; }
75
76using namespace css::java;
77using namespace css::lang;
78using namespace css::loader;
79using namespace css::uno;
80using namespace css::registry;
81
82using namespace ::cppu;
83using namespace ::osl;
84
85namespace stoc_javaloader {
86
87namespace {
88
89// from desktop/source/deployment/misc/dp_misc.cxx
90OUString generateRandomPipeId()
91{
92 // compute some good pipe id:
93 static rtlRandomPool s_hPool = rtl_random_createPool();
94 if (s_hPool == nullptr)
95 throw RuntimeException( "cannot create random pool!?", nullptr );
96 sal_uInt8 bytes[ 32 ];
97 if (rtl_random_getBytes(
98 s_hPool, bytes, SAL_N_ELEMENTS(bytes) ) != rtl_Random_E_None) {
99 throw RuntimeException( "random pool error!?", nullptr );
100 }
101 OUStringBuffer buf;
102 for (unsigned char byte : bytes) {
103 buf.append( static_cast<sal_Int32>(byte), 0x10 );
104 }
105 return buf.makeStringAndClear();
106}
107
108// from desktop/source/deployment/registry/component/dp_component.cxx
112std::vector<OUString> getCmdBootstrapVariables()
113{
114 std::vector<OUString> ret;
115 sal_uInt32 count = osl_getCommandArgCount();
116 for (sal_uInt32 i = 0; i < count; i++)
117 {
118 OUString arg;
119 osl_getCommandArg(i, &arg.pData);
120 if (arg.startsWith("-env:"))
121 ret.push_back(arg);
122 }
123 return ret;
124}
125
126// from desktop/source/deployment/misc/dp_misc.cxx
127oslProcess raiseProcess(
128 OUString const & appURL, Sequence<OUString> const & args )
129{
130 ::osl::Security sec;
131 oslProcess hProcess = nullptr;
132 oslProcessError rc = osl_executeProcess(
133 appURL.pData,
134 reinterpret_cast<rtl_uString **>(
135 const_cast<OUString *>(args.getConstArray()) ),
136 args.getLength(),
137 osl_Process_DETACHED,
138 sec.getHandle(),
139 nullptr, // => current working dir
140 nullptr, 0, // => no env vars
141 &hProcess );
142
143 switch (rc) {
144 case osl_Process_E_None:
145 break;
146 case osl_Process_E_NotFound:
147 throw RuntimeException( "image not found!", nullptr );
148 case osl_Process_E_TimedOut:
149 throw RuntimeException( "timeout occurred!", nullptr );
150 case osl_Process_E_NoPermission:
151 throw RuntimeException( "permission denied!", nullptr );
152 case osl_Process_E_Unknown:
153 throw RuntimeException( "unknown error!", nullptr );
154 case osl_Process_E_InvalidError:
155 default:
156 throw RuntimeException( "unmapped error!", nullptr );
157 }
158
159 return hProcess;
160}
161
162// from desktop/source/deployment/registry/component/dp_component.cxx
163Reference<XComponentContext> raise_uno_process(
164 Reference<XComponentContext> const & xContext)
165{
166 OSL_ASSERT( xContext.is() );
167
168 OUString const url(css::util::theMacroExpander::get(xContext)->expandMacros("$URE_BIN_DIR/uno"));
169
170 const OUString connectStr = "uno:pipe,name=" + generateRandomPipeId() + ";urp;uno.ComponentContext";
171
172 // raise core UNO process to register/run a component,
173 // javavm service uses unorc next to executable to retrieve deployed
174 // jar typelibs
175
176 std::vector<OUString> args{
177#if OSL_DEBUG_LEVEL == 0
178 "--quiet",
179#endif
180 "--singleaccept",
181 "-u",
182 connectStr,
183 // don't inherit from unorc:
184 "-env:INIFILENAME=" };
185
186 //now add the bootstrap variables which were supplied on the command line
187 std::vector<OUString> bootvars = getCmdBootstrapVariables();
188 args.insert(args.end(), bootvars.begin(), bootvars.end());
189
190 oslProcess hProcess;
191 try {
193 }
194 catch (...) {
195 OUStringBuffer sMsg = "error starting process: " + url;
196 for (const auto& arg : args) {
197 sMsg.append(" " + arg);
198 }
199 throw css::uno::RuntimeException(sMsg.makeStringAndClear());
200 }
201 try {
202 // from desktop/source/deployment/misc/dp_misc.cxx
203 Reference<css::bridge::XUnoUrlResolver> const xUnoUrlResolver(
204 css::bridge::UnoUrlResolver::create(xContext) );
205
206 for (int i = 0; i <= 40; ++i) // 20 seconds
207 {
208 try {
209 return Reference<XComponentContext>(
210 xUnoUrlResolver->resolve(connectStr),
211 UNO_QUERY_THROW );
212 }
213 catch (const css::connection::NoConnectException &) {
214 if (i < 40) {
215 std::this_thread::sleep_for( std::chrono::milliseconds(500) );
216 }
217 else throw;
218 }
219 }
220 return nullptr; // warning C4715
221 }
222 catch (...) {
223 // try to terminate process:
224 if ( osl_terminateProcess( hProcess ) != osl_Process_E_None )
225 {
226 OSL_ASSERT( false );
227 }
228 throw;
229 }
230}
231
232class JavaComponentLoader
233 : protected ::cppu::BaseMutex
234 , public WeakComponentImplHelper<XImplementationLoader, XServiceInfo>
235{
237 css::uno::Reference<XComponentContext> m_xComponentContext;
238
244 css::uno::Reference<XComponentContext> m_xRemoteComponentContext;
245
251 css::uno::Reference<XImplementationLoader> m_javaLoader;
259 const css::uno::Reference<XImplementationLoader> & getJavaLoader(OUString &);
260
261
262public:
264 explicit JavaComponentLoader(css::uno::Reference<XComponentContext> xCtx);
265
266public:
267 // XServiceInfo
268 virtual OUString SAL_CALL getImplementationName() override;
269 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
270 virtual Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
271
272 virtual void SAL_CALL disposing() override;
273
274 // XImplementationLoader
275 virtual css::uno::Reference<XInterface> SAL_CALL activate(
276 const OUString& implementationName, const OUString& implementationLoaderUrl,
277 const OUString& locationUrl, const css::uno::Reference<XRegistryKey>& xKey) override;
278 virtual sal_Bool SAL_CALL writeRegistryInfo(
279 const css::uno::Reference<XRegistryKey>& xKey,
280 const OUString& implementationLoaderUrl, const OUString& locationUrl) override;
281};
282
283}
284
285void JavaComponentLoader::disposing()
286{
287 // Explicitly drop all remote refs to shut down the uno.bin process
288 // and particularly the connection to it, so that it can't do more calls
289 // during late shutdown.
290 m_javaLoader.clear();
291 if (m_xRemoteComponentContext.is()) {
292 Reference<XComponent> const xComp(m_xRemoteComponentContext, UNO_QUERY);
293 assert(xComp.is());
294 xComp->dispose();
296 }
297}
298
299const css::uno::Reference<XImplementationLoader> & JavaComponentLoader::getJavaLoader(OUString & rRemoteArg)
300{
301 static std::mutex ourMutex;
302 std::unique_lock aGuard(ourMutex);
303
304 if (m_javaLoader.is())
305 return m_javaLoader;
306
307 // check if the JVM should be instantiated out-of-process
308 if (rRemoteArg.isEmpty()) {
309 if (!m_xRemoteComponentContext.is()) {
310 Reference<css::container::XHierarchicalNameAccess> const xConf(
311 m_xComponentContext->getServiceManager()->createInstanceWithArgumentsAndContext(
312 "com.sun.star.configuration.ReadOnlyAccess",
313 { Any(OUString("*")) }, // locale isn't relevant here
315 UNO_QUERY);
316
317 // configmgr is not part of URE, so may not exist!
318 if (xConf.is()) {
319 Any const value(xConf->getByHierarchicalName(
320 "org.openoffice.Office.Java/VirtualMachine/RunUnoComponentsOutOfProcess"));
321 bool b;
322 if ((value >>= b) && b) {
323 SAL_INFO("stoc.java", "JavaComponentLoader: starting uno process");
325 }
326 }
327 }
328 if (m_xRemoteComponentContext.is()) {
329 SAL_INFO("stoc.java", "JavaComponentLoader: creating remote instance to start JVM in uno process");
330 // create JVM service in remote uno.bin process
331 Reference<XImplementationLoader> const xLoader(
332 m_xRemoteComponentContext->getServiceManager()->createInstanceWithContext(
333 "com.sun.star.loader.Java2", m_xRemoteComponentContext),
334 UNO_QUERY_THROW);
335 assert(xLoader.is());
336 m_javaLoader = xLoader;
337 rRemoteArg = "remote";
338 SAL_INFO("stoc.java", "JavaComponentLoader: remote proxy instance created: " << m_javaLoader.get());
339 return m_javaLoader;
340 }
341 }
342
343 uno_Environment * pJava_environment = nullptr;
344 uno_Environment * pUno_environment = nullptr;
345 typelib_InterfaceTypeDescription * pType_XImplementationLoader = nullptr;
346
347 try {
348 // get a java vm, where we can create a loader
349 css::uno::Reference<XJavaVM> javaVM_xJavaVM(
350 m_xComponentContext->getValueByName(
351 ("/singletons/"
352 "com.sun.star.java.theJavaVirtualMachine")),
353 UNO_QUERY_THROW);
354
355 // Use the special protocol of XJavaVM.getJavaVM: If the passed in
356 // process ID has an extra 17th byte of value one, the returned any
357 // contains a pointer to a jvmaccess::UnoVirtualMachine, instead of the
358 // underlying JavaVM pointer:
359 Sequence<sal_Int8> processID(17);
360 rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8 *>(processID.getArray()));
361 processID.getArray()[16] = 1;
362
363 // We get a non-refcounted pointer to a jvmaccess::UnoVirtualMachine
364 // from the XJavaVM service (the pointer is guaranteed to be valid
365 // as long as our reference to the XJavaVM service lasts), and
366 // convert the non-refcounted pointer into a refcounted one
367 // immediately:
368 static_assert(sizeof (sal_Int64)
369 >= sizeof (jvmaccess::UnoVirtualMachine *), "must be at least the same size");
370 sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
371 static_cast< jvmaccess::UnoVirtualMachine * >(nullptr));
372 javaVM_xJavaVM->getJavaVM(processID) >>= nPointer;
374 reinterpret_cast< jvmaccess::UnoVirtualMachine * >(nPointer));
375 if (!xVirtualMachine.is())
376 {
377 //throw RuntimeException(
378 // "javaloader error - JavaVirtualMachine service could not provide a VM",
379 // css::uno::Reference<XInterface>());
380 // We must not throw a RuntimeException, because this might end the applications.
381 // It is ok if java components
382 // are not working because the office can be installed without Java support.
383 SAL_WARN("stoc", "getJavaVM returned null");
384 return m_javaLoader; // null-ref
385 }
386
387 try
388 {
390 xVirtualMachine->getVirtualMachine());
391 JNIEnv * pJNIEnv = aGuard2.getEnvironment();
392
393 // instantiate the java JavaLoader
394 jclass jcClassLoader = pJNIEnv->FindClass("java/lang/ClassLoader");
395 if(pJNIEnv->ExceptionOccurred())
396 throw RuntimeException(
397 "javaloader error - could not find class java/lang/ClassLoader");
398 jmethodID jmLoadClass = pJNIEnv->GetMethodID(
399 jcClassLoader, "loadClass",
400 "(Ljava/lang/String;)Ljava/lang/Class;");
401 if(pJNIEnv->ExceptionOccurred())
402 throw RuntimeException(
403 "javaloader error - could not find method java/lang/ClassLoader.loadClass");
404 jvalue arg;
405 arg.l = pJNIEnv->NewStringUTF(
406 "com.sun.star.comp.loader.JavaLoader");
407 if(pJNIEnv->ExceptionOccurred())
408 throw RuntimeException(
409 "javaloader error - could not create string");
410 jclass jcJavaLoader = static_cast< jclass >(
411 pJNIEnv->CallObjectMethodA(
412 static_cast< jobject >(xVirtualMachine->getClassLoader()),
413 jmLoadClass, &arg));
414 if(pJNIEnv->ExceptionOccurred())
415 throw RuntimeException(
416 "javaloader error - could not find class com/sun/star/comp/loader/JavaLoader");
417 jmethodID jmJavaLoader_init = pJNIEnv->GetMethodID(jcJavaLoader, "<init>", "()V");
418 if(pJNIEnv->ExceptionOccurred())
419 throw RuntimeException(
420 "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed");
421 jobject joJavaLoader = pJNIEnv->NewObject(jcJavaLoader, jmJavaLoader_init);
422 if(pJNIEnv->ExceptionOccurred())
423 throw RuntimeException(
424 "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed");
425
426 // map the java JavaLoader to this environment
427 OUString sJava("java");
428 uno_getEnvironment(&pJava_environment, sJava.pData,
429 xVirtualMachine.get());
430 if(!pJava_environment)
431 throw RuntimeException(
432 "javaloader error - no Java environment available");
433
434 // why is there no convenient constructor?
435 OUString sCppu_current_lb_name(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
436 uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, nullptr);
437 if(!pUno_environment)
438 throw RuntimeException(
439 "javaloader error - no C++ environment available");
440
441 Mapping java_curr(pJava_environment, pUno_environment);
442 if(!java_curr.is())
443 throw RuntimeException(
444 "javaloader error - no mapping from java to C++ ");
445
446 // release java environment
447 pJava_environment->release(pJava_environment);
448 pJava_environment = nullptr;
449
450 // release uno environment
451 pUno_environment->release(pUno_environment);
452 pUno_environment = nullptr;
453
455 getDescription(reinterpret_cast<typelib_TypeDescription **>(&pType_XImplementationLoader));
456 if(!pType_XImplementationLoader)
457 throw RuntimeException(
458 "javaloader error - no type information for XImplementationLoader");
459
460 m_javaLoader.set(static_cast<XImplementationLoader *>(java_curr.mapInterface(joJavaLoader, pType_XImplementationLoader)));
461 pJNIEnv->DeleteLocalRef( joJavaLoader );
462 if(!m_javaLoader.is())
463 throw RuntimeException(
464 "javaloader error - mapping of java XImplementationLoader to c++ failed");
465
466 typelib_typedescription_release(reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
467 pType_XImplementationLoader = nullptr;
468 }
470 {
471 css::uno::Any anyEx = cppu::getCaughtException();
472 throw css::lang::WrappedTargetRuntimeException(
473 "jvmaccess::VirtualMachine::AttachGuard::CreationException",
474 getXWeak(), anyEx );
475 }
476
477 // set the service manager at the javaloader
478 css::uno::Reference<XInitialization> javaLoader_XInitialization(m_javaLoader, UNO_QUERY_THROW);
479
480 Any any;
481 any <<= m_xComponentContext->getServiceManager();
482
483 javaLoader_XInitialization->initialize(Sequence<Any>(&any, 1));
484 }
485 catch(RuntimeException &) {
486 if(pJava_environment)
487 pJava_environment->release(pJava_environment);
488
489 if(pUno_environment)
490 pUno_environment->release(pUno_environment);
491
492 if(pType_XImplementationLoader)
494 reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
495 throw;
496 }
497 SAL_INFO("stoc", "javaloader.cxx: mapped javaloader - 0x" << m_javaLoader.get());
498 return m_javaLoader;
499}
500
501JavaComponentLoader::JavaComponentLoader(css::uno::Reference<XComponentContext> xCtx)
502 : WeakComponentImplHelper(m_aMutex)
503 , m_xComponentContext(std::move(xCtx))
504{
505
506}
507
508// XServiceInfo
509OUString SAL_CALL JavaComponentLoader::getImplementationName()
510{
511 return "com.sun.star.comp.stoc.JavaComponentLoader";
512}
513
514sal_Bool SAL_CALL JavaComponentLoader::supportsService(const OUString & ServiceName)
515{
516 return cppu::supportsService(this, ServiceName);
517}
518
519Sequence<OUString> SAL_CALL JavaComponentLoader::getSupportedServiceNames()
520{
521 return { "com.sun.star.loader.Java", "com.sun.star.loader.Java2" };
522}
523
524
525// XImplementationLoader
526sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(
527 const css::uno::Reference<XRegistryKey> & xKey, const OUString & blabla,
528 const OUString & rLibName)
529{
530 OUString remoteArg(blabla);
531 const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader(remoteArg);
532 if (!loader.is())
533 throw CannotRegisterImplementationException("Could not create Java implementation loader");
534 return loader->writeRegistryInfo(xKey, remoteArg, rLibName);
535}
536
537css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader::activate(
538 const OUString & rImplName, const OUString & blabla, const OUString & rLibName,
539 const css::uno::Reference<XRegistryKey> & xKey)
540{
541 OUString remoteArg(blabla);
542 if (rImplName.isEmpty() && blabla.isEmpty() && rLibName.isEmpty())
543 {
544 // preload JVM was requested
545 (void)getJavaLoader(remoteArg);
546 return css::uno::Reference<XInterface>();
547 }
548
549 const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader(remoteArg);
550 if (!loader.is())
551 throw CannotActivateFactoryException("Could not create Java implementation loader");
552 return loader->activate(rImplName, remoteArg, rLibName, xKey);
553}
554
555extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
557 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
558{
559 try {
560 return cppu::acquire(new JavaComponentLoader(context));
561 }
562 catch(const RuntimeException & runtimeException) {
563 SAL_INFO(
564 "stoc",
565 "could not init javaloader due to " << runtimeException);
566 throw;
567 }
568}
569
570} //end namespace
571
572
573
574/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void * rtlRandomPool
const Any & any
css::uno::Type const & get()
Any value
struct _uno_Environment uno_Environment
std::mutex m_aMutex
css::uno::Reference< XComponentContext > m_xComponentContext
local context
Definition: javaloader.cxx:237
css::uno::Reference< XImplementationLoader > m_javaLoader
Do not use m_javaLoader directly.
Definition: javaloader.cxx:251
css::uno::Reference< XComponentContext > m_xRemoteComponentContext
possible remote process' context (use depends on configuration).
Definition: javaloader.cxx:244
void SAL_CALL uno_getEnvironment(uno_Environment **ppEnv, rtl_uString *pEnvDcp, void *pContext) SAL_THROW_EXTERN_C()
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
#define SAL_N_ELEMENTS(arr)
struct _typelib_TypeDescription typelib_TypeDescription
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
OUString expandMacros(OUString const &text)
OUString generateRandomPipeId()
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC oslProcess raiseProcess(OUString const &appURL, css::uno::Sequence< OUString > const &args)
int i
args
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stoc_JavaComponentLoader_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: javaloader.cxx:556
loader
std::vector< sal_uInt8 > bytes
void SAL_CALL typelib_typedescription_release(typelib_TypeDescription *pTD) SAL_THROW_EXTERN_C()
unsigned char sal_uInt8
unsigned char sal_Bool