Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "sal/config.h"
22 :
23 : #include "vm.hxx"
24 :
25 : #include "com/sun/star/beans/NamedValue.hpp"
26 : #include "com/sun/star/lang/XSingleComponentFactory.hpp"
27 : #include "cppuhelper/compbase1.hxx"
28 : #include "cppuhelper/component_context.hxx"
29 : #include "jvmaccess/virtualmachine.hxx"
30 : #include "jvmaccess/unovirtualmachine.hxx"
31 : #include "osl/mutex.hxx"
32 :
33 : namespace {
34 :
35 0 : struct MutexHolder
36 : {
37 : ::osl::Mutex m_mutex;
38 : };
39 : typedef ::cppu::WeakComponentImplHelper1<
40 : css::lang::XSingleComponentFactory > t_impl;
41 :
42 0 : class SingletonFactory : public MutexHolder, public t_impl
43 : {
44 : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > m_vm_access;
45 :
46 : protected:
47 : virtual void SAL_CALL disposing() SAL_OVERRIDE;
48 :
49 : public:
50 0 : inline SingletonFactory( ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
51 : : t_impl( m_mutex ),
52 0 : m_vm_access( vm_access )
53 0 : {}
54 :
55 : // XSingleComponentFactory impl
56 : virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithContext(
57 : css::uno::Reference< css::uno::XComponentContext > const & xContext )
58 : throw (css::uno::Exception, std::exception) SAL_OVERRIDE;
59 : virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
60 : css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
61 : throw (css::uno::Exception, std::exception) SAL_OVERRIDE;
62 : };
63 :
64 0 : void SingletonFactory::disposing()
65 : {
66 0 : m_vm_access.clear();
67 0 : }
68 :
69 0 : css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithContext(
70 : css::uno::Reference< css::uno::XComponentContext > const & xContext )
71 : throw (css::uno::Exception, std::exception)
72 : {
73 0 : sal_Int64 handle = reinterpret_cast< sal_Int64 >( m_vm_access.get() );
74 : css::uno::Any arg(
75 : css::uno::makeAny(
76 : css::beans::NamedValue(
77 : OUString( "UnoVirtualMachine" ),
78 0 : css::uno::makeAny( handle ) ) ) );
79 0 : return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
80 : OUString("com.sun.star.java.JavaVirtualMachine"),
81 0 : css::uno::Sequence< css::uno::Any >( &arg, 1 ), xContext );
82 : }
83 :
84 0 : css::uno::Reference< css::uno::XInterface > SingletonFactory::createInstanceWithArgumentsAndContext(
85 : css::uno::Sequence< css::uno::Any > const & args, css::uno::Reference< css::uno::XComponentContext > const & xContext )
86 : throw (css::uno::Exception, std::exception)
87 : {
88 0 : return xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
89 : OUString("com.sun.star.java.JavaVirtualMachine"),
90 0 : args, xContext );
91 : }
92 :
93 : }
94 :
95 : namespace javaunohelper {
96 :
97 0 : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > create_vm_access(
98 : JNIEnv * jni_env, jobject loader )
99 : {
100 : JavaVM * vm;
101 0 : jni_env->GetJavaVM( &vm );
102 : try {
103 : return new ::jvmaccess::UnoVirtualMachine(
104 : new ::jvmaccess::VirtualMachine(
105 0 : vm, JNI_VERSION_1_2, false, jni_env ),
106 0 : loader );
107 0 : } catch ( ::jvmaccess::UnoVirtualMachine::CreationException & ) {
108 0 : throw css::uno::RuntimeException( "jvmaccess::UnoVirtualMachine::CreationException occurred" );
109 : }
110 : }
111 :
112 0 : css::uno::Reference< css::uno::XComponentContext > install_vm_singleton(
113 : css::uno::Reference< css::uno::XComponentContext > const & xContext,
114 : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > const & vm_access )
115 : {
116 0 : css::uno::Reference< css::lang::XSingleComponentFactory > xFac( new SingletonFactory( vm_access ) );
117 : ::cppu::ContextEntry_Init entry(
118 : OUString("/singletons/com.sun.star.java.theJavaVirtualMachine"),
119 0 : css::uno::makeAny( xFac ), true );
120 0 : return ::cppu::createComponentContext( &entry, 1, xContext );
121 : }
122 :
123 : }
124 :
125 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|