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