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 &)
52 VirtualMachine::AttachGuard::AttachGuard(
53 rtl::Reference
< VirtualMachine
> const & 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()
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
74 OSL_ENSURE(pVm
!= 0 && nVersion
>= JNI_VERSION_1_2
&& pMainThreadEnv
!= 0,
79 VirtualMachine::~VirtualMachine()
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
88 jint n = m_pVm->DestroyJavaVM();
89 OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
94 JNIEnv
* VirtualMachine::attachThread(bool * pAttached
) const
100 OSL_ENSURE(pAttached
!= 0, "bad parameter");
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");
108 if (m_pVm
->AttachCurrentThread
111 reinterpret_cast< void ** >(&pEnv
),
113 // The Android <jni.h> has AttachCurrentThread() taking a
114 // JNIEnv** and not void **
128 void VirtualMachine::detachThread() const
131 if (m_pVm
->DetachCurrentThread() != JNI_OK
) {
132 OSL_FAIL("JNI: DetachCurrentThread failed");
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */