sw: lokit: make sure to read annotation html with utf-8
[LibreOffice.git] / jvmaccess / source / virtualmachine.cxx
blobeb4b8069cad7b3789aac3c157c95955d3edbcfee
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 .
20 #include <sal/config.h>
22 #include <cassert>
24 #include <jvmaccess/virtualmachine.hxx>
25 #include <sal/log.hxx>
26 #include <utility>
28 using jvmaccess::VirtualMachine;
30 VirtualMachine::AttachGuard::CreationException::CreationException()
33 VirtualMachine::AttachGuard::CreationException::CreationException(
34 CreationException const &)
37 VirtualMachine::AttachGuard::CreationException &
38 VirtualMachine::AttachGuard::CreationException::operator =(
39 CreationException const &)
41 return *this;
44 VirtualMachine::AttachGuard::AttachGuard(
45 rtl::Reference< VirtualMachine > xMachine):
46 m_xMachine(std::move(xMachine))
48 assert(m_xMachine.is());
49 m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
50 if (m_pEnvironment == nullptr)
51 throw CreationException();
54 VirtualMachine::AttachGuard::~AttachGuard()
56 if (m_bDetach)
57 m_xMachine->detachThread();
60 VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
61 JNIEnv const * pMainThreadEnv):
62 m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
64 (void) pMainThreadEnv; // avoid warnings
65 assert(pVm != nullptr);
66 assert(nVersion >= JNI_VERSION_1_2);
67 assert(pMainThreadEnv);
70 VirtualMachine::~VirtualMachine()
72 if (m_bDestroy)
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.
79 jint n = m_pVm->DestroyJavaVM();
80 SAL_WARN_IF(n != JNI_OK, "jvmaccess", "JNI: DestroyJavaVM failed");
85 JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
87 assert(pAttached != nullptr && "bad parameter");
88 JNIEnv * pEnv;
89 jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
90 SAL_WARN_IF(
91 n != JNI_OK && n != JNI_EDETACHED, "jvmaccess", "JNI: GetEnv failed");
92 if (pEnv == nullptr)
94 if (m_pVm->AttachCurrentThread
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;
108 else
109 *pAttached = false;
110 return pEnv;
113 void VirtualMachine::detachThread() const
115 jint n = m_pVm->DetachCurrentThread();
116 SAL_WARN_IF(n != JNI_OK, "jvmaccess", "JNI: DetachCurrentThread failed");
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */