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