update dev300-m58
[ooovba.git] / jvmaccess / source / virtualmachine.cxx
blob536b6815cd4809d8c64e4fedc3d984b7171dad0d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: virtualmachine.cxx,v $
10 * $Revision: 1.8 $
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 &)
51 return *this;
54 VirtualMachine::AttachGuard::AttachGuard(
55 rtl::Reference< VirtualMachine > const & rMachine):
56 m_xMachine(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()
66 if (m_bDetach)
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
75 #ifdef SOLAR_JAVA
76 OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
77 "bad parameter");
78 #endif
81 VirtualMachine::~VirtualMachine()
83 if (m_bDestroy)
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
88 // forever.
90 jint n = m_pVm->DestroyJavaVM();
91 OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
96 JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
98 #ifndef SOLAR_JAVA
99 return 0;
100 #else
101 OSL_ENSURE(pAttached != 0, "bad parameter");
102 JNIEnv * pEnv;
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");
107 if (pEnv == 0)
109 if (m_pVm->AttachCurrentThread(reinterpret_cast< void ** >(&pEnv), 0)
110 != JNI_OK)
111 return 0;
112 *pAttached = true;
114 else
115 *pAttached = false;
116 return pEnv;
117 #endif
120 void VirtualMachine::detachThread() const
122 #ifdef SOLAR_JAVA
123 if (m_pVm->DetachCurrentThread() != JNI_OK) {
124 OSL_ENSURE(false, "JNI: DetachCurrentThread failed");
126 #endif