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