tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / stoc / source / javaloader / javaloader.cxx
blobff64ca45a3300be148377208716e3daf6b9e8456
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/process.h>
21 #include <sal/log.hxx>
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>
28 #include <cppuhelper/exc_hlp.hxx>
30 #ifdef LINUX
31 #undef minor
32 #undef major
33 #endif
35 #include <com/sun/star/java/XJavaVM.hpp>
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
46 #include <rtl/random.h>
47 #include <rtl/ustrbuf.hxx>
48 #include <osl/security.hxx>
50 #include <cppuhelper/factory.hxx>
52 #include <cppuhelper/basemutex.hxx>
53 #include <cppuhelper/compbase.hxx>
54 #include <cppuhelper/supportsservice.hxx>
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>
64 #include <jvmaccess/unovirtualmachine.hxx>
65 #include <jvmaccess/virtualmachine.hxx>
67 // this one is header-only
68 #include <comphelper/sequence.hxx>
70 #include <mutex>
71 #include <thread>
72 #include <utility>
74 namespace com::sun::star::registry { class XRegistryKey; }
76 using namespace css::java;
77 using namespace css::lang;
78 using namespace css::loader;
79 using namespace css::uno;
80 using namespace css::registry;
82 using namespace ::cppu;
83 using namespace ::osl;
85 namespace stoc_javaloader {
87 namespace {
89 // from desktop/source/deployment/misc/dp_misc.cxx
90 OUString generateRandomPipeId()
92 // compute some good pipe id:
93 sal_uInt8 bytes[ 32 ];
94 if (rtl_random_getBytes(
95 nullptr, bytes, SAL_N_ELEMENTS(bytes) ) != rtl_Random_E_None) {
96 throw RuntimeException( u"random pool error!?"_ustr, nullptr );
98 OUStringBuffer buf;
99 for (unsigned char byte : bytes) {
100 buf.append( static_cast<sal_Int32>(byte), 0x10 );
102 return buf.makeStringAndClear();
105 // from desktop/source/deployment/registry/component/dp_component.cxx
106 /** return a vector of bootstrap variables which have been provided
107 as command arguments.
109 std::vector<OUString> getCmdBootstrapVariables()
111 std::vector<OUString> ret;
112 sal_uInt32 count = osl_getCommandArgCount();
113 for (sal_uInt32 i = 0; i < count; i++)
115 OUString arg;
116 osl_getCommandArg(i, &arg.pData);
117 if (arg.startsWith("-env:"))
118 ret.push_back(arg);
120 return ret;
123 // from desktop/source/deployment/misc/dp_misc.cxx
124 oslProcess raiseProcess(
125 OUString const & appURL, Sequence<OUString> const & args )
127 ::osl::Security sec;
128 oslProcess hProcess = nullptr;
129 oslProcessError rc = osl_executeProcess(
130 appURL.pData,
131 reinterpret_cast<rtl_uString **>(
132 const_cast<OUString *>(args.getConstArray()) ),
133 args.getLength(),
134 osl_Process_DETACHED,
135 sec.getHandle(),
136 nullptr, // => current working dir
137 nullptr, 0, // => no env vars
138 &hProcess );
140 switch (rc) {
141 case osl_Process_E_None:
142 break;
143 case osl_Process_E_NotFound:
144 throw RuntimeException( u"image not found!"_ustr, nullptr );
145 case osl_Process_E_TimedOut:
146 throw RuntimeException( u"timeout occurred!"_ustr, nullptr );
147 case osl_Process_E_NoPermission:
148 throw RuntimeException( u"permission denied!"_ustr, nullptr );
149 case osl_Process_E_Unknown:
150 throw RuntimeException( u"unknown error!"_ustr, nullptr );
151 case osl_Process_E_InvalidError:
152 default:
153 throw RuntimeException( u"unmapped error!"_ustr, nullptr );
156 return hProcess;
159 // from desktop/source/deployment/registry/component/dp_component.cxx
160 Reference<XComponentContext> raise_uno_process(
161 Reference<XComponentContext> const & xContext)
163 OSL_ASSERT( xContext.is() );
165 OUString const url(css::util::theMacroExpander::get(xContext)->expandMacros(u"$URE_BIN_DIR/uno"_ustr));
167 const OUString connectStr = "uno:pipe,name=" + generateRandomPipeId() + ";urp;uno.ComponentContext";
169 // raise core UNO process to register/run a component,
170 // javavm service uses unorc next to executable to retrieve deployed
171 // jar typelibs
173 std::vector<OUString> args{
174 #if OSL_DEBUG_LEVEL == 0
175 "--quiet",
176 #endif
177 u"--singleaccept"_ustr,
178 u"-u"_ustr,
179 connectStr,
180 // don't inherit from unorc:
181 u"-env:INIFILENAME="_ustr };
183 //now add the bootstrap variables which were supplied on the command line
184 std::vector<OUString> bootvars = getCmdBootstrapVariables();
185 args.insert(args.end(), bootvars.begin(), bootvars.end());
187 oslProcess hProcess;
188 try {
189 hProcess = raiseProcess(url, comphelper::containerToSequence(args));
191 catch (...) {
192 OUStringBuffer sMsg = "error starting process: " + url;
193 for (const auto& arg : args) {
194 sMsg.append(" " + arg);
196 throw css::uno::RuntimeException(sMsg.makeStringAndClear());
198 try {
199 // from desktop/source/deployment/misc/dp_misc.cxx
200 Reference<css::bridge::XUnoUrlResolver> const xUnoUrlResolver(
201 css::bridge::UnoUrlResolver::create(xContext) );
203 for (int i = 0; i <= 40; ++i) // 20 seconds
205 try {
206 return Reference<XComponentContext>(
207 xUnoUrlResolver->resolve(connectStr),
208 UNO_QUERY_THROW );
210 catch (const css::connection::NoConnectException &) {
211 if (i < 40) {
212 std::this_thread::sleep_for( std::chrono::milliseconds(500) );
214 else throw;
217 return nullptr; // warning C4715
219 catch (...) {
220 // try to terminate process:
221 if ( osl_terminateProcess( hProcess ) != osl_Process_E_None )
223 OSL_ASSERT( false );
225 throw;
229 class JavaComponentLoader
230 : protected ::cppu::BaseMutex
231 , public WeakComponentImplHelper<XImplementationLoader, XServiceInfo>
233 /** local context */
234 css::uno::Reference<XComponentContext> m_xComponentContext;
236 /** possible remote process' context (use depends on configuration).
237 note: lifetime must be effectively "static" as this JavaComponentLoader
238 has no control over the lifetime of the services created via this
239 context; hence JavaComponentLoader is a single-instance service.
241 css::uno::Reference<XComponentContext> m_xRemoteComponentContext;
243 /** Do not use m_javaLoader directly. Instead use getJavaLoader.
244 This is either an in-process loader implemented in Java,
245 or a remote instance of JavaComponentLoader running in uno process,
246 acting as a proxy.
248 css::uno::Reference<XImplementationLoader> m_javaLoader;
249 /** The returned Reference contains a null pointer if the office is not configured
250 to run java.
252 @exception css::uno::RuntimeException
253 If the Java implementation of the loader could not be obtained, for reasons other
254 then that java was not configured the RuntimeException is thrown.
256 const css::uno::Reference<XImplementationLoader> & getJavaLoader(OUString &);
259 public:
260 /// @throws RuntimeException
261 explicit JavaComponentLoader(css::uno::Reference<XComponentContext> xCtx);
263 public:
264 // XServiceInfo
265 virtual OUString SAL_CALL getImplementationName() override;
266 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
267 virtual Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
269 virtual void SAL_CALL disposing() override;
271 // XImplementationLoader
272 virtual css::uno::Reference<XInterface> SAL_CALL activate(
273 const OUString& implementationName, const OUString& implementationLoaderUrl,
274 const OUString& locationUrl, const css::uno::Reference<XRegistryKey>& xKey) override;
275 virtual sal_Bool SAL_CALL writeRegistryInfo(
276 const css::uno::Reference<XRegistryKey>& xKey,
277 const OUString& implementationLoaderUrl, const OUString& locationUrl) override;
282 void JavaComponentLoader::disposing()
284 // Explicitly drop all remote refs to shut down the uno.bin process
285 // and particularly the connection to it, so that it can't do more calls
286 // during late shutdown.
287 m_javaLoader.clear();
288 if (m_xRemoteComponentContext.is()) {
289 Reference<XComponent> const xComp(m_xRemoteComponentContext, UNO_QUERY);
290 assert(xComp.is());
291 xComp->dispose();
292 m_xRemoteComponentContext.clear();
296 const css::uno::Reference<XImplementationLoader> & JavaComponentLoader::getJavaLoader(OUString & rRemoteArg)
298 static std::mutex ourMutex;
299 std::unique_lock aGuard(ourMutex);
301 if (m_javaLoader.is())
302 return m_javaLoader;
304 // check if the JVM should be instantiated out-of-process
305 if (rRemoteArg.isEmpty()) {
306 if (!m_xRemoteComponentContext.is()) {
307 Reference<css::container::XHierarchicalNameAccess> const xConf(
308 m_xComponentContext->getServiceManager()->createInstanceWithArgumentsAndContext(
309 u"com.sun.star.configuration.ReadOnlyAccess"_ustr,
310 { Any(u"*"_ustr) }, // locale isn't relevant here
311 m_xComponentContext),
312 UNO_QUERY);
314 // configmgr is not part of URE, so may not exist!
315 if (xConf.is()) {
316 Any const value(xConf->getByHierarchicalName(
317 u"org.openoffice.Office.Java/VirtualMachine/RunUnoComponentsOutOfProcess"_ustr));
318 bool b;
319 if ((value >>= b) && b) {
320 SAL_INFO("stoc.java", "JavaComponentLoader: starting uno process");
321 m_xRemoteComponentContext = raise_uno_process(m_xComponentContext);
325 if (m_xRemoteComponentContext.is()) {
326 SAL_INFO("stoc.java", "JavaComponentLoader: creating remote instance to start JVM in uno process");
327 // create JVM service in remote uno.bin process
328 Reference<XImplementationLoader> const xLoader(
329 m_xRemoteComponentContext->getServiceManager()->createInstanceWithContext(
330 u"com.sun.star.loader.Java2"_ustr, m_xRemoteComponentContext),
331 UNO_QUERY_THROW);
332 assert(xLoader.is());
333 m_javaLoader = xLoader;
334 rRemoteArg = "remote";
335 SAL_INFO("stoc.java", "JavaComponentLoader: remote proxy instance created: " << m_javaLoader.get());
336 return m_javaLoader;
340 uno_Environment * pJava_environment = nullptr;
341 uno_Environment * pUno_environment = nullptr;
342 typelib_InterfaceTypeDescription * pType_XImplementationLoader = nullptr;
344 try {
345 // get a java vm, where we can create a loader
346 css::uno::Reference<XJavaVM> javaVM_xJavaVM(
347 m_xComponentContext->getValueByName(
348 (u"/singletons/"
349 "com.sun.star.java.theJavaVirtualMachine"_ustr)),
350 UNO_QUERY_THROW);
352 // Use the special protocol of XJavaVM.getJavaVM: If the passed in
353 // process ID has an extra 17th byte of value one, the returned any
354 // contains a pointer to a jvmaccess::UnoVirtualMachine, instead of the
355 // underlying JavaVM pointer:
356 Sequence<sal_Int8> processID(17);
357 rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8 *>(processID.getArray()));
358 processID.getArray()[16] = 1;
360 // We get a non-refcounted pointer to a jvmaccess::UnoVirtualMachine
361 // from the XJavaVM service (the pointer is guaranteed to be valid
362 // as long as our reference to the XJavaVM service lasts), and
363 // convert the non-refcounted pointer into a refcounted one
364 // immediately:
365 static_assert(sizeof (sal_Int64)
366 >= sizeof (jvmaccess::UnoVirtualMachine *), "must be at least the same size");
367 sal_Int64 nPointer = reinterpret_cast< sal_Int64 >(
368 static_cast< jvmaccess::UnoVirtualMachine * >(nullptr));
369 javaVM_xJavaVM->getJavaVM(processID) >>= nPointer;
370 rtl::Reference< jvmaccess::UnoVirtualMachine > xVirtualMachine(
371 reinterpret_cast< jvmaccess::UnoVirtualMachine * >(nPointer));
372 if (!xVirtualMachine.is())
374 //throw RuntimeException(
375 // "javaloader error - JavaVirtualMachine service could not provide a VM",
376 // css::uno::Reference<XInterface>());
377 // We must not throw a RuntimeException, because this might end the applications.
378 // It is ok if java components
379 // are not working because the office can be installed without Java support.
380 SAL_WARN("stoc", "getJavaVM returned null");
381 return m_javaLoader; // null-ref
386 jvmaccess::VirtualMachine::AttachGuard aGuard2(
387 xVirtualMachine->getVirtualMachine());
388 JNIEnv * pJNIEnv = aGuard2.getEnvironment();
390 // instantiate the java JavaLoader
391 jclass jcClassLoader = pJNIEnv->FindClass("java/lang/ClassLoader");
392 if(pJNIEnv->ExceptionOccurred())
393 throw RuntimeException(
394 u"javaloader error - could not find class java/lang/ClassLoader"_ustr);
395 jmethodID jmLoadClass = pJNIEnv->GetMethodID(
396 jcClassLoader, "loadClass",
397 "(Ljava/lang/String;)Ljava/lang/Class;");
398 if(pJNIEnv->ExceptionOccurred())
399 throw RuntimeException(
400 u"javaloader error - could not find method java/lang/ClassLoader.loadClass"_ustr);
401 jvalue arg;
402 arg.l = pJNIEnv->NewStringUTF(
403 "com.sun.star.comp.loader.JavaLoader");
404 if(pJNIEnv->ExceptionOccurred())
405 throw RuntimeException(
406 u"javaloader error - could not create string"_ustr);
407 jclass jcJavaLoader = static_cast< jclass >(
408 pJNIEnv->CallObjectMethodA(
409 static_cast< jobject >(xVirtualMachine->getClassLoader()),
410 jmLoadClass, &arg));
411 if(pJNIEnv->ExceptionOccurred())
412 throw RuntimeException(
413 u"javaloader error - could not find class com/sun/star/comp/loader/JavaLoader"_ustr);
414 jmethodID jmJavaLoader_init = pJNIEnv->GetMethodID(jcJavaLoader, "<init>", "()V");
415 if(pJNIEnv->ExceptionOccurred())
416 throw RuntimeException(
417 u"javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed"_ustr);
418 jobject joJavaLoader = pJNIEnv->NewObject(jcJavaLoader, jmJavaLoader_init);
419 if(pJNIEnv->ExceptionOccurred())
420 throw RuntimeException(
421 u"javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed"_ustr);
423 // map the java JavaLoader to this environment
424 OUString sJava(u"java"_ustr);
425 uno_getEnvironment(&pJava_environment, sJava.pData,
426 xVirtualMachine.get());
427 if(!pJava_environment)
428 throw RuntimeException(
429 u"javaloader error - no Java environment available"_ustr);
431 // why is there no convenient constructor?
432 OUString sCppu_current_lb_name(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
433 uno_getEnvironment(&pUno_environment, sCppu_current_lb_name.pData, nullptr);
434 if(!pUno_environment)
435 throw RuntimeException(
436 u"javaloader error - no C++ environment available"_ustr);
438 Mapping java_curr(pJava_environment, pUno_environment);
439 if(!java_curr.is())
440 throw RuntimeException(
441 u"javaloader error - no mapping from java to C++ "_ustr);
443 // release java environment
444 pJava_environment->release(pJava_environment);
445 pJava_environment = nullptr;
447 // release uno environment
448 pUno_environment->release(pUno_environment);
449 pUno_environment = nullptr;
451 cppu::UnoType<XImplementationLoader>::get().
452 getDescription(reinterpret_cast<typelib_TypeDescription **>(&pType_XImplementationLoader));
453 if(!pType_XImplementationLoader)
454 throw RuntimeException(
455 u"javaloader error - no type information for XImplementationLoader"_ustr);
457 m_javaLoader.set(static_cast<XImplementationLoader *>(java_curr.mapInterface(joJavaLoader, pType_XImplementationLoader)));
458 pJNIEnv->DeleteLocalRef( joJavaLoader );
459 if(!m_javaLoader.is())
460 throw RuntimeException(
461 u"javaloader error - mapping of java XImplementationLoader to c++ failed"_ustr);
463 typelib_typedescription_release(reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
464 pType_XImplementationLoader = nullptr;
466 catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
468 css::uno::Any anyEx = cppu::getCaughtException();
469 throw css::lang::WrappedTargetRuntimeException(
470 u"jvmaccess::VirtualMachine::AttachGuard::CreationException"_ustr,
471 getXWeak(), anyEx );
474 // set the service manager at the javaloader
475 css::uno::Reference<XInitialization> javaLoader_XInitialization(m_javaLoader, UNO_QUERY_THROW);
477 Any any;
478 any <<= m_xComponentContext->getServiceManager();
480 javaLoader_XInitialization->initialize(Sequence<Any>(&any, 1));
482 catch(RuntimeException &) {
483 if(pJava_environment)
484 pJava_environment->release(pJava_environment);
486 if(pUno_environment)
487 pUno_environment->release(pUno_environment);
489 if(pType_XImplementationLoader)
490 typelib_typedescription_release(
491 reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
492 throw;
494 SAL_INFO("stoc", "javaloader.cxx: mapped javaloader - 0x" << m_javaLoader.get());
495 return m_javaLoader;
498 JavaComponentLoader::JavaComponentLoader(css::uno::Reference<XComponentContext> xCtx)
499 : WeakComponentImplHelper(m_aMutex)
500 , m_xComponentContext(std::move(xCtx))
505 // XServiceInfo
506 OUString SAL_CALL JavaComponentLoader::getImplementationName()
508 return u"com.sun.star.comp.stoc.JavaComponentLoader"_ustr;
511 sal_Bool SAL_CALL JavaComponentLoader::supportsService(const OUString & ServiceName)
513 return cppu::supportsService(this, ServiceName);
516 Sequence<OUString> SAL_CALL JavaComponentLoader::getSupportedServiceNames()
518 return { u"com.sun.star.loader.Java"_ustr, u"com.sun.star.loader.Java2"_ustr };
522 // XImplementationLoader
523 sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(
524 const css::uno::Reference<XRegistryKey> & xKey, const OUString & blabla,
525 const OUString & rLibName)
527 OUString remoteArg(blabla);
528 const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader(remoteArg);
529 if (!loader.is())
530 throw CannotRegisterImplementationException(u"Could not create Java implementation loader"_ustr);
531 return loader->writeRegistryInfo(xKey, remoteArg, rLibName);
534 css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader::activate(
535 const OUString & rImplName, const OUString & blabla, const OUString & rLibName,
536 const css::uno::Reference<XRegistryKey> & xKey)
538 OUString remoteArg(blabla);
539 if (rImplName.isEmpty() && blabla.isEmpty() && rLibName.isEmpty())
541 // preload JVM was requested
542 (void)getJavaLoader(remoteArg);
543 return css::uno::Reference<XInterface>();
546 const css::uno::Reference<XImplementationLoader> & loader = getJavaLoader(remoteArg);
547 if (!loader.is())
548 throw CannotActivateFactoryException(u"Could not create Java implementation loader"_ustr);
549 return loader->activate(rImplName, remoteArg, rLibName, xKey);
552 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
553 stoc_JavaComponentLoader_get_implementation(
554 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
556 try {
557 return cppu::acquire(new JavaComponentLoader(context));
559 catch(const RuntimeException & runtimeException) {
560 SAL_INFO(
561 "stoc",
562 "could not init javaloader due to " << runtimeException);
563 throw;
567 } //end namespace
571 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */