Bump for 3.6-28
[LibreOffice.git] / jvmaccess / source / virtualmachine.cxx
blob3e1208808866db5560bd5c652b8f058e4e8a99ce
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "jvmaccess/virtualmachine.hxx"
31 #include "osl/diagnose.h"
33 using jvmaccess::VirtualMachine;
35 VirtualMachine::AttachGuard::CreationException::CreationException()
38 VirtualMachine::AttachGuard::CreationException::CreationException(
39 CreationException const &)
42 VirtualMachine::AttachGuard::CreationException::~CreationException()
45 VirtualMachine::AttachGuard::CreationException &
46 VirtualMachine::AttachGuard::CreationException::operator =(
47 CreationException const &)
49 return *this;
52 VirtualMachine::AttachGuard::AttachGuard(
53 rtl::Reference< VirtualMachine > const & rMachine):
54 m_xMachine(rMachine)
56 OSL_ENSURE(m_xMachine.is(), "bad parameter");
57 m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
58 if (m_pEnvironment == 0)
59 throw CreationException();
62 VirtualMachine::AttachGuard::~AttachGuard()
64 if (m_bDetach)
65 m_xMachine->detachThread();
68 VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
69 JNIEnv * pMainThreadEnv):
70 m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
72 (void) pMainThreadEnv; // avoid warnings
73 #ifdef SOLAR_JAVA
74 OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
75 "bad parameter");
76 #endif
79 VirtualMachine::~VirtualMachine()
81 if (m_bDestroy)
83 // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
84 // not a daemon thread and is never terminated, so that calling
85 // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
86 // forever.
88 jint n = m_pVm->DestroyJavaVM();
89 OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
94 JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
96 #ifndef SOLAR_JAVA
97 (void) pAttached;
98 return 0;
99 #else
100 OSL_ENSURE(pAttached != 0, "bad parameter");
101 JNIEnv * pEnv;
102 jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
103 if (n != JNI_OK && n != JNI_EDETACHED) {
104 OSL_FAIL("JNI: GetEnv failed");
106 if (pEnv == 0)
108 if (m_pVm->AttachCurrentThread
110 #ifndef ANDROID
111 reinterpret_cast< void ** >(&pEnv),
112 #else
113 // The Android <jni.h> has AttachCurrentThread() taking a
114 // JNIEnv** and not void **
115 &pEnv,
116 #endif
118 != JNI_OK)
119 return 0;
120 *pAttached = true;
122 else
123 *pAttached = false;
124 return pEnv;
125 #endif
128 void VirtualMachine::detachThread() const
130 #ifdef SOLAR_JAVA
131 if (m_pVm->DetachCurrentThread() != JNI_OK) {
132 OSL_FAIL("JNI: DetachCurrentThread failed");
134 #endif
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */