LibreOffice Module jvmaccess (master) 1
virtualmachine.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
22#include <cassert>
23
25#include <sal/log.hxx>
26#include <utility>
27
29
30VirtualMachine::AttachGuard::CreationException::CreationException()
31{}
32
33VirtualMachine::AttachGuard::CreationException::CreationException(
34 CreationException const &)
35{}
36
38VirtualMachine::AttachGuard::CreationException::operator =(
39 CreationException const &)
40{
41 return *this;
42}
43
44VirtualMachine::AttachGuard::AttachGuard(
46 m_xMachine(std::move(xMachine))
47{
48 assert(m_xMachine.is());
49 m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
50 if (m_pEnvironment == nullptr)
51 throw CreationException();
52}
53
55{
56 if (m_bDetach)
57 m_xMachine->detachThread();
58}
59
60VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
61 JNIEnv const * pMainThreadEnv):
62 m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
63{
64 (void) pMainThreadEnv; // avoid warnings
65 assert(pVm != nullptr);
66 assert(nVersion >= JNI_VERSION_1_2);
67 assert(pMainThreadEnv);
68}
69
71{
72 if (m_bDestroy)
73 {
74 // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
75 // not a daemon thread and is never terminated, so that calling
76 // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
77 // forever.
78/*
79 jint n = m_pVm->DestroyJavaVM();
80 SAL_WARN_IF(n != JNI_OK, "jvmaccess", "JNI: DestroyJavaVM failed");
81*/
82 }
83}
84
85JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
86{
87 assert(pAttached != nullptr && "bad parameter");
88 JNIEnv * pEnv;
89 jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
91 n != JNI_OK && n != JNI_EDETACHED, "jvmaccess", "JNI: GetEnv failed");
92 if (pEnv == nullptr)
93 {
94 if (m_pVm->AttachCurrentThread
95 (
96#ifndef ANDROID
97 reinterpret_cast< void ** >(&pEnv),
98#else
99 // The Android <jni.h> has AttachCurrentThread() taking a
100 // JNIEnv** and not void **
101 &pEnv,
102#endif
103 nullptr)
104 != JNI_OK)
105 return nullptr;
106 *pAttached = true;
107 }
108 else
109 *pAttached = false;
110 return pEnv;
111}
112
114{
115 jint n = m_pVm->DetachCurrentThread();
116 SAL_WARN_IF(n != JNI_OK, "jvmaccess", "JNI: DetachCurrentThread failed");
117}
118
119/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
An exception indicating failure to create an AttachGuard.
~AttachGuard()
Detach the current thread from the virtual machine again.
rtl::Reference< VirtualMachine > m_xMachine
An encapsulating wrapper around a Java virtual machine.
VirtualMachine(JavaVM *pVm, int nVersion, bool bDestroy, JNIEnv const *pMainThreadEnv)
Create a wrapper around a Java virtual machine.
JNIEnv * attachThread(bool *pAttached) const
virtual ~VirtualMachine() override
sal_Int16 nVersion
sal_Int64 n
#define SAL_WARN_IF(condition, area, stream)