xref: /aoo42x/main/javaunohelper/source/vm.cxx (revision 61f28c9e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_javaunohelper.hxx"
26 
27 #include "sal/config.h"
28 
29 #include "vm.hxx"
30 
31 #include "com/sun/star/beans/NamedValue.hpp"
32 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
33 #include "cppuhelper/compbase1.hxx"
34 #include "cppuhelper/component_context.hxx"
35 #include "jvmaccess/virtualmachine.hxx"
36 #include "jvmaccess/unovirtualmachine.hxx"
37 #include "osl/mutex.hxx"
38 
39 namespace {
40 
41 namespace css = ::com::sun::star;
42 
43 struct MutexHolder
44 {
45     ::osl::Mutex m_mutex;
46 };
47 typedef ::cppu::WeakComponentImplHelper1<
48     css::lang::XSingleComponentFactory > t_impl;
49 
50 class SingletonFactory : public MutexHolder, public t_impl
51 {
52     ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > m_vm_access;
53 
54 protected:
55     virtual void SAL_CALL disposing();
56 
57 public:
SingletonFactory(::rtl::Reference<::jvmaccess::UnoVirtualMachine> const & vm_access)58     inline SingletonFactory( ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
59         : t_impl( m_mutex ),
60           m_vm_access( vm_access )
61         {}
62 
63     // XSingleComponentFactory impl
64     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext(
65         css::uno::Reference< css::uno::XComponentContext > const & xContext )
66         throw (css::uno::Exception);
67     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
68         css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
69         throw (css::uno::Exception);
70 };
71 
disposing()72 void SingletonFactory::disposing()
73 {
74     m_vm_access.clear();
75 }
76 
createInstanceWithContext(css::uno::Reference<css::uno::XComponentContext> const & xContext)77 css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithContext(
78     css::uno::Reference< css::uno::XComponentContext > const & xContext )
79     throw (css::uno::Exception)
80 {
81     sal_Int64 handle = reinterpret_cast< sal_Int64 >( m_vm_access.get() );
82     css::uno::Any arg(
83         css::uno::makeAny(
84             css::beans::NamedValue(
85                 rtl::OUString(
86                     RTL_CONSTASCII_USTRINGPARAM( "UnoVirtualMachine" ) ),
87                 css::uno::makeAny( handle ) ) ) );
88     return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
89         ::rtl::OUString(
90             RTL_CONSTASCII_USTRINGPARAM(
91                 "com.sun.star.java.JavaVirtualMachine")),
92         css::uno::Sequence< css::uno::Any >( &arg, 1 ), xContext );
93 }
94 
createInstanceWithArgumentsAndContext(css::uno::Sequence<css::uno::Any> const & args,css::uno::Reference<css::uno::XComponentContext> const & xContext)95 css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithArgumentsAndContext(
96     css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
97     throw (css::uno::Exception)
98 {
99     return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
100         ::rtl::OUString(
101             RTL_CONSTASCII_USTRINGPARAM(
102                 "com.sun.star.java.JavaVirtualMachine")),
103         args, xContext );
104 }
105 
106 }
107 
108 namespace javaunohelper {
109 
create_vm_access(JNIEnv * jni_env,jobject loader)110 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > create_vm_access(
111     JNIEnv * jni_env, jobject loader )
112 {
113     JavaVM * vm;
114     jni_env->GetJavaVM( &vm );
115     try {
116         return new ::jvmaccess::UnoVirtualMachine(
117             new ::jvmaccess::VirtualMachine(
118                 vm, JNI_VERSION_1_2, false, jni_env ),
119             loader );
120     } catch ( ::jvmaccess::UnoVirtualMachine::CreationException & ) {
121         throw css::uno::RuntimeException(
122             ::rtl::OUString(
123                 RTL_CONSTASCII_USTRINGPARAM(
124                     "jmvaccess::UnoVirtualMachine::CreationException"
125                     " occurred" ) ),
126             css::uno::Reference< css::uno::XInterface >() );
127     }
128 }
129 
install_vm_singleton(css::uno::Reference<::css::uno::XComponentContext> const & xContext,::rtl::Reference<::jvmaccess::UnoVirtualMachine> const & vm_access)130 css::uno::Reference< css::uno::XComponentContext > install_vm_singleton(
131     css::uno::Reference< ::css::uno::XComponentContext > const & xContext,
132     ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
133 {
134     css::uno::Reference< css::lang::XSingleComponentFactory > xFac( new SingletonFactory( vm_access ) );
135     ::cppu::ContextEntry_Init entry(
136         ::rtl::OUString(
137             RTL_CONSTASCII_USTRINGPARAM(
138                 "/singletons/com.sun.star.java.theJavaVirtualMachine")),
139         css::uno::makeAny( xFac ), true );
140     return ::cppu::createComponentContext( &entry, 1, xContext );
141 }
142 
143 }
144