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