xref: /aoo42x/main/javaunohelper/source/vm.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_javaunohelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "sal/config.h"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "vm.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "com/sun/star/beans/NamedValue.hpp"
36*cdf0e10cSrcweir #include "com/sun/star/lang/XSingleComponentFactory.hpp"
37*cdf0e10cSrcweir #include "cppuhelper/compbase1.hxx"
38*cdf0e10cSrcweir #include "cppuhelper/component_context.hxx"
39*cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx"
40*cdf0e10cSrcweir #include "jvmaccess/unovirtualmachine.hxx"
41*cdf0e10cSrcweir #include "osl/mutex.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir namespace {
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace css = ::com::sun::star;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir struct MutexHolder
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     ::osl::Mutex m_mutex;
50*cdf0e10cSrcweir };
51*cdf0e10cSrcweir typedef ::cppu::WeakComponentImplHelper1<
52*cdf0e10cSrcweir     css::lang::XSingleComponentFactory > t_impl;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir class SingletonFactory : public MutexHolder, public t_impl
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > m_vm_access;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir protected:
59*cdf0e10cSrcweir     virtual void SAL_CALL disposing();
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir public:
62*cdf0e10cSrcweir     inline SingletonFactory( ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
63*cdf0e10cSrcweir         : t_impl( m_mutex ),
64*cdf0e10cSrcweir           m_vm_access( vm_access )
65*cdf0e10cSrcweir         {}
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     // XSingleComponentFactory impl
68*cdf0e10cSrcweir     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext(
69*cdf0e10cSrcweir         css::uno::Reference< css::uno::XComponentContext > const & xContext )
70*cdf0e10cSrcweir         throw (css::uno::Exception);
71*cdf0e10cSrcweir     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
72*cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
73*cdf0e10cSrcweir         throw (css::uno::Exception);
74*cdf0e10cSrcweir };
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir void SingletonFactory::disposing()
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir     m_vm_access.clear();
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithContext(
82*cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > const & xContext )
83*cdf0e10cSrcweir     throw (css::uno::Exception)
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     sal_Int64 handle = reinterpret_cast< sal_Int64 >( m_vm_access.get() );
86*cdf0e10cSrcweir     css::uno::Any arg(
87*cdf0e10cSrcweir         css::uno::makeAny(
88*cdf0e10cSrcweir             css::beans::NamedValue(
89*cdf0e10cSrcweir                 rtl::OUString(
90*cdf0e10cSrcweir                     RTL_CONSTASCII_USTRINGPARAM( "UnoVirtualMachine" ) ),
91*cdf0e10cSrcweir                 css::uno::makeAny( handle ) ) ) );
92*cdf0e10cSrcweir     return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
93*cdf0e10cSrcweir         ::rtl::OUString(
94*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
95*cdf0e10cSrcweir                 "com.sun.star.java.JavaVirtualMachine")),
96*cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any >( &arg, 1 ), xContext );
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithArgumentsAndContext(
100*cdf0e10cSrcweir     css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
101*cdf0e10cSrcweir     throw (css::uno::Exception)
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir     return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
104*cdf0e10cSrcweir         ::rtl::OUString(
105*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
106*cdf0e10cSrcweir                 "com.sun.star.java.JavaVirtualMachine")),
107*cdf0e10cSrcweir         args, xContext );
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir namespace javaunohelper {
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > create_vm_access(
115*cdf0e10cSrcweir     JNIEnv * jni_env, jobject loader )
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir     JavaVM * vm;
118*cdf0e10cSrcweir     jni_env->GetJavaVM( &vm );
119*cdf0e10cSrcweir     try {
120*cdf0e10cSrcweir         return new ::jvmaccess::UnoVirtualMachine(
121*cdf0e10cSrcweir             new ::jvmaccess::VirtualMachine(
122*cdf0e10cSrcweir                 vm, JNI_VERSION_1_2, false, jni_env ),
123*cdf0e10cSrcweir             loader );
124*cdf0e10cSrcweir     } catch ( ::jvmaccess::UnoVirtualMachine::CreationException & ) {
125*cdf0e10cSrcweir         throw css::uno::RuntimeException(
126*cdf0e10cSrcweir             ::rtl::OUString(
127*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
128*cdf0e10cSrcweir                     "jmvaccess::UnoVirtualMachine::CreationException"
129*cdf0e10cSrcweir                     " occurred" ) ),
130*cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface >() );
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir css::uno::Reference< css::uno::XComponentContext > install_vm_singleton(
135*cdf0e10cSrcweir     css::uno::Reference< ::css::uno::XComponentContext > const & xContext,
136*cdf0e10cSrcweir     ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir     css::uno::Reference< css::lang::XSingleComponentFactory > xFac( new SingletonFactory( vm_access ) );
139*cdf0e10cSrcweir     ::cppu::ContextEntry_Init entry(
140*cdf0e10cSrcweir         ::rtl::OUString(
141*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
142*cdf0e10cSrcweir                 "/singletons/com.sun.star.java.theJavaVirtualMachine")),
143*cdf0e10cSrcweir         css::uno::makeAny( xFac ), true );
144*cdf0e10cSrcweir     return ::cppu::createComponentContext( &entry, 1, xContext );
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir }
148