xref: /trunk/main/javaunohelper/source/vm.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
67         css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext );
68 };
69 
disposing()70 void SingletonFactory::disposing()
71 {
72     m_vm_access.clear();
73 }
74 
createInstanceWithContext(css::uno::Reference<css::uno::XComponentContext> const & xContext)75 css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithContext(
76     css::uno::Reference< css::uno::XComponentContext > const & xContext )
77 {
78     sal_Int64 handle = reinterpret_cast< sal_Int64 >( m_vm_access.get() );
79     css::uno::Any arg(
80         css::uno::makeAny(
81             css::beans::NamedValue(
82                 rtl::OUString(
83                     RTL_CONSTASCII_USTRINGPARAM( "UnoVirtualMachine" ) ),
84                 css::uno::makeAny( handle ) ) ) );
85     return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
86         ::rtl::OUString(
87             RTL_CONSTASCII_USTRINGPARAM(
88                 "com.sun.star.java.JavaVirtualMachine")),
89         css::uno::Sequence< css::uno::Any >( &arg, 1 ), xContext );
90 }
91 
createInstanceWithArgumentsAndContext(css::uno::Sequence<css::uno::Any> const & args,css::uno::Reference<css::uno::XComponentContext> const & xContext)92 css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithArgumentsAndContext(
93     css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
94 {
95     return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
96         ::rtl::OUString(
97             RTL_CONSTASCII_USTRINGPARAM(
98                 "com.sun.star.java.JavaVirtualMachine")),
99         args, xContext );
100 }
101 
102 }
103 
104 namespace javaunohelper {
105 
create_vm_access(JNIEnv * jni_env,jobject loader)106 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > create_vm_access(
107     JNIEnv * jni_env, jobject loader )
108 {
109     JavaVM * vm;
110     jni_env->GetJavaVM( &vm );
111     try {
112         return new ::jvmaccess::UnoVirtualMachine(
113             new ::jvmaccess::VirtualMachine(
114                 vm, JNI_VERSION_1_2, false, jni_env ),
115             loader );
116     } catch ( ::jvmaccess::UnoVirtualMachine::CreationException & ) {
117         throw css::uno::RuntimeException(
118             ::rtl::OUString(
119                 RTL_CONSTASCII_USTRINGPARAM(
120                     "jmvaccess::UnoVirtualMachine::CreationException"
121                     " occurred" ) ),
122             css::uno::Reference< css::uno::XInterface >() );
123     }
124 }
125 
install_vm_singleton(css::uno::Reference<::css::uno::XComponentContext> const & xContext,::rtl::Reference<::jvmaccess::UnoVirtualMachine> const & vm_access)126 css::uno::Reference< css::uno::XComponentContext > install_vm_singleton(
127     css::uno::Reference< ::css::uno::XComponentContext > const & xContext,
128     ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
129 {
130     css::uno::Reference< css::lang::XSingleComponentFactory > xFac( new SingletonFactory( vm_access ) );
131     ::cppu::ContextEntry_Init entry(
132         ::rtl::OUString(
133             RTL_CONSTASCII_USTRINGPARAM(
134                 "/singletons/com.sun.star.java.theJavaVirtualMachine")),
135         css::uno::makeAny( xFac ), true );
136     return ::cppu::createComponentContext( &entry, 1, xContext );
137 }
138 
139 }
140