Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / jvmaccess / source / virtualmachine.cxx
blob23192ab3e437785412f3c38d30c6d83a74bc1f80
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 "jvmaccess/virtualmachine.hxx"
22 #include "osl/diagnose.h"
24 using jvmaccess::VirtualMachine;
26 VirtualMachine::AttachGuard::CreationException::CreationException()
29 VirtualMachine::AttachGuard::CreationException::CreationException(
30 CreationException const &)
33 VirtualMachine::AttachGuard::CreationException::~CreationException()
36 VirtualMachine::AttachGuard::CreationException &
37 VirtualMachine::AttachGuard::CreationException::operator =(
38 CreationException const &)
40 return *this;
43 VirtualMachine::AttachGuard::AttachGuard(
44 rtl::Reference< VirtualMachine > const & rMachine):
45 m_xMachine(rMachine)
47 OSL_ENSURE(m_xMachine.is(), "bad parameter");
48 m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
49 if (m_pEnvironment == 0)
50 throw CreationException();
53 VirtualMachine::AttachGuard::~AttachGuard()
55 if (m_bDetach)
56 m_xMachine->detachThread();
59 VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
60 JNIEnv * pMainThreadEnv):
61 m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
63 (void) pMainThreadEnv; // avoid warnings
64 OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
65 "bad parameter");
68 VirtualMachine::~VirtualMachine()
70 if (m_bDestroy)
72 // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
73 // not a daemon thread and is never terminated, so that calling
74 // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
75 // forever.
77 jint n = m_pVm->DestroyJavaVM();
78 OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
83 JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
85 OSL_ENSURE(pAttached != 0, "bad parameter");
86 JNIEnv * pEnv;
87 jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
88 if (n != JNI_OK && n != JNI_EDETACHED) {
89 OSL_FAIL("JNI: GetEnv failed");
91 if (pEnv == 0)
93 if (m_pVm->AttachCurrentThread
95 #ifndef ANDROID
96 reinterpret_cast< void ** >(&pEnv),
97 #else
98 // The Android <jni.h> has AttachCurrentThread() taking a
99 // JNIEnv** and not void **
100 &pEnv,
101 #endif
103 != JNI_OK)
104 return 0;
105 *pAttached = true;
107 else
108 *pAttached = false;
109 return pEnv;
112 void VirtualMachine::detachThread() const
114 if (m_pVm->DetachCurrentThread() != JNI_OK) {
115 OSL_FAIL("JNI: DetachCurrentThread failed");
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */