1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 #include <tools/diagnose_ex.h>
36 #include <com/sun/star/java/XJavaVM.hpp>
40 #include <cppuhelper/factory.hxx>
41 #include <cppuhelper/implementationentry.hxx>
43 #include <cppuhelper/implbase.hxx>
44 #include <cppuhelper/supportsservice.hxx>
46 #include <com/sun/star/loader/XImplementationLoader.hpp>
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 #include <com/sun/star/lang/XInitialization.hpp>
49 #include <com/sun/star/uno/XComponentContext.hpp>
51 #include <jvmaccess/unovirtualmachine.hxx>
52 #include <jvmaccess/virtualmachine.hxx>
54 namespace com::sun::star::registry
{ class XRegistryKey
; }
56 using namespace css::java
;
57 using namespace css::lang
;
58 using namespace css::loader
;
59 using namespace css::uno
;
60 using namespace css::registry
;
62 using namespace ::cppu
;
63 using namespace ::osl
;
65 namespace stoc_javaloader
{
69 class JavaComponentLoader
: public WeakImplHelper
<XImplementationLoader
, XServiceInfo
>
71 css::uno::Reference
<XComponentContext
> m_xComponentContext
;
72 /** Do not use m_javaLoader directly. Instead use getJavaLoader.
74 css::uno::Reference
<XImplementationLoader
> m_javaLoader
;
75 /** The returned Reference contains a null pointer if the office is not configured
78 @exception css::uno::RuntimeException
79 If the Java implementation of the loader could not be obtained, for reasons other
80 then that java was not configured the RuntimeException is thrown.
82 const css::uno::Reference
<XImplementationLoader
> & getJavaLoader();
86 /// @throws RuntimeException
87 explicit JavaComponentLoader(const css::uno::Reference
<XComponentContext
> & xCtx
);
91 virtual OUString SAL_CALL
getImplementationName() override
;
92 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
93 virtual Sequence
<OUString
> SAL_CALL
getSupportedServiceNames() override
;
95 // XImplementationLoader
96 virtual css::uno::Reference
<XInterface
> SAL_CALL
activate(
97 const OUString
& implementationName
, const OUString
& implementationLoaderUrl
,
98 const OUString
& locationUrl
, const css::uno::Reference
<XRegistryKey
>& xKey
) override
;
99 virtual sal_Bool SAL_CALL
writeRegistryInfo(
100 const css::uno::Reference
<XRegistryKey
>& xKey
,
101 const OUString
& implementationLoaderUrl
, const OUString
& locationUrl
) override
;
106 const css::uno::Reference
<XImplementationLoader
> & JavaComponentLoader::getJavaLoader()
108 static Mutex ourMutex
;
109 MutexGuard
aGuard(ourMutex
);
111 if (m_javaLoader
.is())
114 uno_Environment
* pJava_environment
= nullptr;
115 uno_Environment
* pUno_environment
= nullptr;
116 typelib_InterfaceTypeDescription
* pType_XImplementationLoader
= nullptr;
119 // get a java vm, where we can create a loader
120 css::uno::Reference
<XJavaVM
> javaVM_xJavaVM(
121 m_xComponentContext
->getValueByName(
123 "com.sun.star.java.theJavaVirtualMachine")),
126 // Use the special protocol of XJavaVM.getJavaVM: If the passed in
127 // process ID has an extra 17th byte of value one, the returned any
128 // contains a pointer to a jvmaccess::UnoVirtualMachine, instead of the
129 // underlying JavaVM pointer:
130 Sequence
<sal_Int8
> processID(17);
131 rtl_getGlobalProcessId(reinterpret_cast<sal_uInt8
*>(processID
.getArray()));
134 // We get a non-refcounted pointer to a jvmaccess::UnoVirtualMachine
135 // from the XJavaVM service (the pointer is guaranteed to be valid
136 // as long as our reference to the XJavaVM service lasts), and
137 // convert the non-refcounted pointer into a refcounted one
139 static_assert(sizeof (sal_Int64
)
140 >= sizeof (jvmaccess::UnoVirtualMachine
*), "must be at least the same size");
141 sal_Int64 nPointer
= reinterpret_cast< sal_Int64
>(
142 static_cast< jvmaccess::UnoVirtualMachine
* >(nullptr));
143 javaVM_xJavaVM
->getJavaVM(processID
) >>= nPointer
;
144 rtl::Reference
< jvmaccess::UnoVirtualMachine
> xVirtualMachine(
145 reinterpret_cast< jvmaccess::UnoVirtualMachine
* >(nPointer
));
146 if (!xVirtualMachine
.is())
148 //throw RuntimeException(
149 // "javaloader error - JavaVirtualMachine service could not provide a VM",
150 // css::uno::Reference<XInterface>());
151 // We must not throw a RuntimeException, because this might end the applications.
152 // It is ok if java components
153 // are not working because the office can be installed without Java support.
154 SAL_WARN("stoc", "getJavaVM returned null");
155 return m_javaLoader
; // null-ref
160 jvmaccess::VirtualMachine::AttachGuard
aGuard2(
161 xVirtualMachine
->getVirtualMachine());
162 JNIEnv
* pJNIEnv
= aGuard2
.getEnvironment();
164 // instantiate the java JavaLoader
165 jclass jcClassLoader
= pJNIEnv
->FindClass("java/lang/ClassLoader");
166 if(pJNIEnv
->ExceptionOccurred())
167 throw RuntimeException(
168 "javaloader error - could not find class java/lang/ClassLoader");
169 jmethodID jmLoadClass
= pJNIEnv
->GetMethodID(
170 jcClassLoader
, "loadClass",
171 "(Ljava/lang/String;)Ljava/lang/Class;");
172 if(pJNIEnv
->ExceptionOccurred())
173 throw RuntimeException(
174 "javaloader error - could not find method java/lang/ClassLoader.loadClass");
176 arg
.l
= pJNIEnv
->NewStringUTF(
177 "com.sun.star.comp.loader.JavaLoader");
178 if(pJNIEnv
->ExceptionOccurred())
179 throw RuntimeException(
180 "javaloader error - could not create string");
181 jclass jcJavaLoader
= static_cast< jclass
>(
182 pJNIEnv
->CallObjectMethodA(
183 static_cast< jobject
>(xVirtualMachine
->getClassLoader()),
185 if(pJNIEnv
->ExceptionOccurred())
186 throw RuntimeException(
187 "javaloader error - could not find class com/sun/star/comp/loader/JavaLoader");
188 jmethodID jmJavaLoader_init
= pJNIEnv
->GetMethodID(jcJavaLoader
, "<init>", "()V");
189 if(pJNIEnv
->ExceptionOccurred())
190 throw RuntimeException(
191 "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed");
192 jobject joJavaLoader
= pJNIEnv
->NewObject(jcJavaLoader
, jmJavaLoader_init
);
193 if(pJNIEnv
->ExceptionOccurred())
194 throw RuntimeException(
195 "javaloader error - instantiation of com.sun.star.comp.loader.JavaLoader failed");
197 // map the java JavaLoader to this environment
198 OUString
sJava("java");
199 uno_getEnvironment(&pJava_environment
, sJava
.pData
,
200 xVirtualMachine
.get());
201 if(!pJava_environment
)
202 throw RuntimeException(
203 "javaloader error - no Java environment available");
205 // why is there no convenient constructor?
206 OUString
sCppu_current_lb_name(CPPU_CURRENT_LANGUAGE_BINDING_NAME
);
207 uno_getEnvironment(&pUno_environment
, sCppu_current_lb_name
.pData
, nullptr);
208 if(!pUno_environment
)
209 throw RuntimeException(
210 "javaloader error - no C++ environment available");
212 Mapping
java_curr(pJava_environment
, pUno_environment
);
214 throw RuntimeException(
215 "javaloader error - no mapping from java to C++ ");
217 // release java environment
218 pJava_environment
->release(pJava_environment
);
219 pJava_environment
= nullptr;
221 // release uno environment
222 pUno_environment
->release(pUno_environment
);
223 pUno_environment
= nullptr;
225 cppu::UnoType
<XImplementationLoader
>::get().
226 getDescription(reinterpret_cast<typelib_TypeDescription
**>(&pType_XImplementationLoader
));
227 if(!pType_XImplementationLoader
)
228 throw RuntimeException(
229 "javaloader error - no type information for XImplementationLoader");
231 m_javaLoader
.set(static_cast<XImplementationLoader
*>(java_curr
.mapInterface(joJavaLoader
, pType_XImplementationLoader
)));
232 pJNIEnv
->DeleteLocalRef( joJavaLoader
);
233 if(!m_javaLoader
.is())
234 throw RuntimeException(
235 "javaloader error - mapping of java XImplementationLoader to c++ failed");
237 typelib_typedescription_release(reinterpret_cast<typelib_TypeDescription
*>(pType_XImplementationLoader
));
238 pType_XImplementationLoader
= nullptr;
240 catch (jvmaccess::VirtualMachine::AttachGuard::CreationException
&)
242 css::uno::Any anyEx
= cppu::getCaughtException();
243 throw css::lang::WrappedTargetRuntimeException(
244 "jvmaccess::VirtualMachine::AttachGuard::CreationException",
245 static_cast< cppu::OWeakObject
* >(this), anyEx
);
248 // set the service manager at the javaloader
249 css::uno::Reference
<XInitialization
> javaLoader_XInitialization(m_javaLoader
, UNO_QUERY_THROW
);
252 any
<<= m_xComponentContext
->getServiceManager();
254 javaLoader_XInitialization
->initialize(Sequence
<Any
>(&any
, 1));
256 catch(RuntimeException
&) {
257 if(pJava_environment
)
258 pJava_environment
->release(pJava_environment
);
261 pUno_environment
->release(pUno_environment
);
263 if(pType_XImplementationLoader
)
264 typelib_typedescription_release(
265 reinterpret_cast<typelib_TypeDescription
*>(pType_XImplementationLoader
));
268 SAL_INFO("stoc", "javaloader.cxx: mapped javaloader - 0x" << m_javaLoader
.get());
272 JavaComponentLoader::JavaComponentLoader(const css::uno::Reference
<XComponentContext
> & xCtx
) :
273 m_xComponentContext(xCtx
)
280 OUString SAL_CALL
JavaComponentLoader::getImplementationName()
282 return "com.sun.star.comp.stoc.JavaComponentLoader";
285 sal_Bool SAL_CALL
JavaComponentLoader::supportsService(const OUString
& ServiceName
)
287 return cppu::supportsService(this, ServiceName
);
290 Sequence
<OUString
> SAL_CALL
JavaComponentLoader::getSupportedServiceNames()
292 return { "com.sun.star.loader.Java", "com.sun.star.loader.Java2" };
296 // XImplementationLoader
297 sal_Bool SAL_CALL
JavaComponentLoader::writeRegistryInfo(
298 const css::uno::Reference
<XRegistryKey
> & xKey
, const OUString
& blabla
,
299 const OUString
& rLibName
)
301 const css::uno::Reference
<XImplementationLoader
> & loader
= getJavaLoader();
303 throw CannotRegisterImplementationException("Could not create Java implementation loader");
304 return loader
->writeRegistryInfo(xKey
, blabla
, rLibName
);
307 css::uno::Reference
<XInterface
> SAL_CALL
JavaComponentLoader::activate(
308 const OUString
& rImplName
, const OUString
& blabla
, const OUString
& rLibName
,
309 const css::uno::Reference
<XRegistryKey
> & xKey
)
311 if (rImplName
.isEmpty() && blabla
.isEmpty() && rLibName
.isEmpty())
313 // preload JVM was requested
314 (void)getJavaLoader();
315 return css::uno::Reference
<XInterface
>();
318 const css::uno::Reference
<XImplementationLoader
> & loader
= getJavaLoader();
320 throw CannotActivateFactoryException("Could not create Java implementation loader");
321 return loader
->activate(rImplName
, blabla
, rLibName
, xKey
);
324 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
325 stoc_JavaComponentLoader_get_implementation(
326 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
329 return cppu::acquire(new JavaComponentLoader(context
));
331 catch(const RuntimeException
&) {
332 TOOLS_INFO_EXCEPTION("stoc", "could not init javaloader");
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */