Basic compiler undetected typo
[LibreOffice.git] / javaunohelper / source / vm.cxx
blobbdb2f58e7f1bd1e7661690ecab97af09b2298d7c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <sal/config.h>
23 #include "vm.hxx"
25 #include <com/sun/star/beans/NamedValue.hpp>
26 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
27 #include <cppuhelper/compbase.hxx>
28 #include <cppuhelper/component_context.hxx>
29 #include <cppuhelper/basemutex.hxx>
30 #include <jvmaccess/virtualmachine.hxx>
31 #include <jvmaccess/unovirtualmachine.hxx>
33 namespace {
35 typedef ::cppu::WeakComponentImplHelper<
36 css::lang::XSingleComponentFactory > t_impl;
38 class SingletonFactory : public cppu::BaseMutex, public t_impl
40 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > m_vm_access;
42 protected:
43 virtual void SAL_CALL disposing() override;
45 public:
46 explicit SingletonFactory( ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
47 : t_impl( m_aMutex ),
48 m_vm_access( vm_access )
51 // XSingleComponentFactory impl
52 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext(
53 css::uno::Reference< css::uno::XComponentContext > const & xContext ) override;
54 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
55 css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext ) override;
58 void SingletonFactory::disposing()
60 m_vm_access.clear();
63 css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithContext(
64 css::uno::Reference< css::uno::XComponentContext > const & xContext )
66 sal_Int64 handle = reinterpret_cast< sal_Int64 >( m_vm_access.get() );
67 css::uno::Any arg( css::beans::NamedValue( "UnoVirtualMachine", css::uno::makeAny( handle ) ) );
68 return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
69 "com.sun.star.java.JavaVirtualMachine",
70 css::uno::Sequence< css::uno::Any >( &arg, 1 ), xContext );
73 css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithArgumentsAndContext(
74 css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
76 return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
77 "com.sun.star.java.JavaVirtualMachine",
78 args, xContext );
83 namespace javaunohelper {
85 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > create_vm_access(
86 JNIEnv * jni_env, jobject loader )
88 JavaVM * vm;
89 jni_env->GetJavaVM( &vm );
90 try {
91 return new ::jvmaccess::UnoVirtualMachine(
92 new ::jvmaccess::VirtualMachine(
93 vm, JNI_VERSION_1_2, false, jni_env ),
94 loader );
95 } catch ( ::jvmaccess::UnoVirtualMachine::CreationException & ) {
96 throw css::uno::RuntimeException( "jvmaccess::UnoVirtualMachine::CreationException occurred" );
100 css::uno::Reference< css::uno::XComponentContext > install_vm_singleton(
101 css::uno::Reference< css::uno::XComponentContext > const & xContext,
102 ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
104 css::uno::Reference< css::lang::XSingleComponentFactory > xFac( new SingletonFactory( vm_access ) );
105 ::cppu::ContextEntry_Init entry(
106 "/singletons/com.sun.star.java.theJavaVirtualMachine",
107 css::uno::makeAny( xFac ), true );
108 return ::cppu::createComponentContext( &entry, 1, xContext );
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */