1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: virtualmachine.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "jvmaccess/virtualmachine.hxx"
33 #include "osl/diagnose.h"
35 using jvmaccess::VirtualMachine
;
37 VirtualMachine::AttachGuard::CreationException::CreationException()
40 VirtualMachine::AttachGuard::CreationException::CreationException(
41 CreationException
const &)
44 VirtualMachine::AttachGuard::CreationException::~CreationException()
47 VirtualMachine::AttachGuard::CreationException
&
48 VirtualMachine::AttachGuard::CreationException::operator =(
49 CreationException
const &)
54 VirtualMachine::AttachGuard::AttachGuard(
55 rtl::Reference
< VirtualMachine
> const & rMachine
):
58 OSL_ENSURE(m_xMachine
.is(), "bad parameter");
59 m_pEnvironment
= m_xMachine
->attachThread(&m_bDetach
);
60 if (m_pEnvironment
== 0)
61 throw CreationException();
64 VirtualMachine::AttachGuard::~AttachGuard()
67 m_xMachine
->detachThread();
70 VirtualMachine::VirtualMachine(JavaVM
* pVm
, int nVersion
, bool bDestroy
,
71 JNIEnv
* pMainThreadEnv
):
72 m_pVm(pVm
), m_nVersion(nVersion
), m_bDestroy(bDestroy
)
74 (void) pMainThreadEnv
; // avoid warnings
76 OSL_ENSURE(pVm
!= 0 && nVersion
>= JNI_VERSION_1_2
&& pMainThreadEnv
!= 0,
81 VirtualMachine::~VirtualMachine()
85 // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
86 // not a daemon thread and is never terminated, so that calling
87 // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
90 jint n = m_pVm->DestroyJavaVM();
91 OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
96 JNIEnv
* VirtualMachine::attachThread(bool * pAttached
) const
101 OSL_ENSURE(pAttached
!= 0, "bad parameter");
103 jint n
= m_pVm
->GetEnv(reinterpret_cast< void ** >(&pEnv
), m_nVersion
);
104 if (n
!= JNI_OK
&& n
!= JNI_EDETACHED
) {
105 OSL_ENSURE(false, "JNI: GetEnv failed");
109 if (m_pVm
->AttachCurrentThread(reinterpret_cast< void ** >(&pEnv
), 0)
120 void VirtualMachine::detachThread() const
123 if (m_pVm
->DetachCurrentThread() != JNI_OK
) {
124 OSL_ENSURE(false, "JNI: DetachCurrentThread failed");