android: Update app-specific/MIME type icons
[LibreOffice.git] / stoc / source / javaloader / javaloader.cxx
blobb6d67a6bc9230632d57b1b4c2de40077d03c0f67
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 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 );
101 OUStringBuffer buf;
102 for (unsigned char byte : bytes) {
103 buf.append( static_cast<sal_Int32>(byte), 0x10 );
105 return buf.makeStringAndClear();
108 // from desktop/source/deployment/registry/component/dp_component.cxx
109 /** return a vector of bootstrap variables which have been provided
110 as command arguments.
112 std::vector<OUString> getCmdBootstrapVariables()
114 std::vector<OUString> ret;
115 sal_uInt32 count = osl_getCommandArgCount();
116 for (sal_uInt32 i = 0; i < count; i++)
118 OUString arg;
119 osl_getCommandArg(i, &arg.pData);
120 if (arg.startsWith("-env:"))
121 ret.push_back(arg);
123 return ret;
126 // from desktop/source/deployment/misc/dp_misc.cxx
127 oslProcess raiseProcess(
128 OUString const & appURL, Sequence<OUString> const & args )
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 );
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 );
159 return hProcess;
162 // from desktop/source/deployment/registry/component/dp_component.cxx
163 Reference<XComponentContext> raise_uno_process(
164 Reference<XComponentContext> const & xContext)
166 OSL_ASSERT( xContext.is() );
168 OUString const url(css::util::theMacroExpander::get(xContext)->expandMacros("$URE_BIN_DIR/uno"));
170 const OUString connectStr = "uno:pipe,name=" + generateRandomPipeId() + ";urp;uno.ComponentContext";
172 // raise core UNO process to register/run a component,
173 // javavm service uses unorc next to executable to retrieve deployed
174 // jar typelibs
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=" };
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());
190 oslProcess hProcess;
191 try {
192 hProcess = raiseProcess(url, comphelper::containerToSequence(args));
194 catch (...) {
195 OUStringBuffer sMsg = "error starting process: " + url;
196 for (const auto& arg : args) {
197 sMsg.append(" " + arg);
199 throw css::uno::RuntimeException(sMsg.makeStringAndClear());
201 try {
202 // from desktop/source/deployment/misc/dp_misc.cxx
203 Reference<css::bridge::XUnoUrlResolver> const xUnoUrlResolver(
204 css::bridge::UnoUrlResolver::create(xContext) );
206 for (int i = 0; i <= 40; ++i) // 20 seconds
208 try {
209 return Reference<XComponentContext>(
210 xUnoUrlResolver->resolve(connectStr),
211 UNO_QUERY_THROW );
213 catch (const css::connection::NoConnectException &) {
214 if (i < 40) {
215 std::this_thread::sleep_for( std::chrono::milliseconds(500) );
217 else throw;
220 return nullptr; // warning C4715
222 catch (...) {
223 // try to terminate process:
224 if ( osl_terminateProcess( hProcess ) != osl_Process_E_None )
226 OSL_ASSERT( false );
228 throw;
232 class JavaComponentLoader
233 : protected ::cppu::BaseMutex
234 , public WeakComponentImplHelper<XImplementationLoader, XServiceInfo>
236 /** local context */
237 css::uno::Reference<XComponentContext> m_xComponentContext;
239 /** possible remote process' context (use depends on configuration).
240 note: lifetime must be effectively "static" as this JavaComponentLoader
241 has no control over the lifetime of the services created via this
242 context; hence JavaComponentLoader is a single-instance service.
244 css::uno::Reference<XComponentContext> m_xRemoteComponentContext;
246 /** Do not use m_javaLoader directly. Instead use getJavaLoader.
247 This is either an in-process loader implemented in Java,
248 or a remote instance of JavaComponentLoader running in uno process,
249 acting as a proxy.
251 css::uno::Reference<XImplementationLoader> m_javaLoader;
252 /** The returned Reference contains a null pointer if the office is not configured
253 to run java.
255 @exception css::uno::RuntimeException
256 If the Java implementation of the loader could not be obtained, for reasons other
257 then that java was not configured the RuntimeException is thrown.
259 const css::uno::Reference<XImplementationLoader> & getJavaLoader(OUString &);
262 public:
263 /// @throws RuntimeException
264 explicit JavaComponentLoader(css::uno::Reference<XComponentContext> xCtx);
266 public:
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;
272 virtual void SAL_CALL disposing() override;
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;
285 void JavaComponentLoader::disposing()
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();
295 m_xRemoteComponentContext.clear();
299 const css::uno::Reference<XImplementationLoader> & JavaComponentLoader::getJavaLoader(OUString & rRemoteArg)
301 static std::mutex ourMutex;
302 std::unique_lock aGuard(ourMutex);
304 if (m_javaLoader.is())
305 return m_javaLoader;
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
314 m_xComponentContext),
315 UNO_QUERY);
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");
324 m_xRemoteComponentContext = raise_uno_process(m_xComponentContext);
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;
343 uno_Environment * pJava_environment = nullptr;
344 uno_Environment * pUno_environment = nullptr;
345 typelib_InterfaceTypeDescription * pType_XImplementationLoader = nullptr;
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);
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;
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;
373 rtl::Reference< jvmaccess::UnoVirtualMachine > xVirtualMachine(
374 reinterpret_cast< jvmaccess::UnoVirtualMachine * >(nPointer));
375 if (!xVirtualMachine.is())
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
389 jvmaccess::VirtualMachine::AttachGuard aGuard2(
390 xVirtualMachine->getVirtualMachine());
391 JNIEnv * pJNIEnv = aGuard2.getEnvironment();
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");
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");
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");
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++ ");
446 // release java environment
447 pJava_environment->release(pJava_environment);
448 pJava_environment = nullptr;
450 // release uno environment
451 pUno_environment->release(pUno_environment);
452 pUno_environment = nullptr;
454 cppu::UnoType<XImplementationLoader>::get().
455 getDescription(reinterpret_cast<typelib_TypeDescription **>(&pType_XImplementationLoader));
456 if(!pType_XImplementationLoader)
457 throw RuntimeException(
458 "javaloader error - no type information for XImplementationLoader");
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");
466 typelib_typedescription_release(reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
467 pType_XImplementationLoader = nullptr;
469 catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &)
471 css::uno::Any anyEx = cppu::getCaughtException();
472 throw css::lang::WrappedTargetRuntimeException(
473 "jvmaccess::VirtualMachine::AttachGuard::CreationException",
474 static_cast< cppu::OWeakObject * >(this), anyEx );
477 // set the service manager at the javaloader
478 css::uno::Reference<XInitialization> javaLoader_XInitialization(m_javaLoader, UNO_QUERY_THROW);
480 Any any;
481 any <<= m_xComponentContext->getServiceManager();
483 javaLoader_XInitialization->initialize(Sequence<Any>(&any, 1));
485 catch(RuntimeException &) {
486 if(pJava_environment)
487 pJava_environment->release(pJava_environment);
489 if(pUno_environment)
490 pUno_environment->release(pUno_environment);
492 if(pType_XImplementationLoader)
493 typelib_typedescription_release(
494 reinterpret_cast<typelib_TypeDescription *>(pType_XImplementationLoader));
495 throw;
497 SAL_INFO("stoc", "javaloader.cxx: mapped javaloader - 0x" << m_javaLoader.get());
498 return m_javaLoader;
501 JavaComponentLoader::JavaComponentLoader(css::uno::Reference<XComponentContext> xCtx)
502 : WeakComponentImplHelper(m_aMutex)
503 , m_xComponentContext(std::move(xCtx))
508 // XServiceInfo
509 OUString SAL_CALL JavaComponentLoader::getImplementationName()
511 return "com.sun.star.comp.stoc.JavaComponentLoader";
514 sal_Bool SAL_CALL JavaComponentLoader::supportsService(const OUString & ServiceName)
516 return cppu::supportsService(this, ServiceName);
519 Sequence<OUString> SAL_CALL JavaComponentLoader::getSupportedServiceNames()
521 return { "com.sun.star.loader.Java", "com.sun.star.loader.Java2" };
525 // XImplementationLoader
526 sal_Bool SAL_CALL JavaComponentLoader::writeRegistryInfo(
527 const css::uno::Reference<XRegistryKey> & xKey, const OUString & blabla,
528 const OUString & rLibName)
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);
537 css::uno::Reference<XInterface> SAL_CALL JavaComponentLoader::activate(
538 const OUString & rImplName, const OUString & blabla, const OUString & rLibName,
539 const css::uno::Reference<XRegistryKey> & xKey)
541 OUString remoteArg(blabla);
542 if (rImplName.isEmpty() && blabla.isEmpty() && rLibName.isEmpty())
544 // preload JVM was requested
545 (void)getJavaLoader(remoteArg);
546 return css::uno::Reference<XInterface>();
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);
555 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
556 stoc_JavaComponentLoader_get_implementation(
557 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
559 try {
560 return cppu::acquire(new JavaComponentLoader(context));
562 catch(const RuntimeException & runtimeException) {
563 SAL_INFO(
564 "stoc",
565 "could not init javaloader due to " << runtimeException);
566 throw;
570 } //end namespace
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */