2010-10-14 08:30:07 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2002-12-06 10:35:41 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-11 10:44:29 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2002-12-06 10:35:41 +00:00
|
|
|
*
|
2010-02-12 15:01:35 +01:00
|
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
2002-12-06 10:35:41 +00:00
|
|
|
*
|
2008-04-11 10:44:29 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2002-12-06 10:35:41 +00:00
|
|
|
*
|
2008-04-11 10:44:29 +00:00
|
|
|
* This file is part of OpenOffice.org.
|
2002-12-06 10:35:41 +00:00
|
|
|
*
|
2008-04-11 10:44:29 +00:00
|
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
* only, as published by the Free Software Foundation.
|
2002-12-06 10:35:41 +00:00
|
|
|
*
|
2008-04-11 10:44:29 +00:00
|
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License version 3 for more details
|
|
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
2002-12-06 10:35:41 +00:00
|
|
|
*
|
2008-04-11 10:44:29 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
|
|
* <http://www.openoffice.org/license.html>
|
|
|
|
* for a copy of the LGPLv3 License.
|
2002-12-06 10:35:41 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#include "jvmaccess/virtualmachine.hxx"
|
|
|
|
|
|
|
|
#include "osl/diagnose.h"
|
|
|
|
|
|
|
|
using jvmaccess::VirtualMachine;
|
|
|
|
|
|
|
|
VirtualMachine::AttachGuard::CreationException::CreationException()
|
|
|
|
{}
|
|
|
|
|
|
|
|
VirtualMachine::AttachGuard::CreationException::CreationException(
|
|
|
|
CreationException const &)
|
|
|
|
{}
|
|
|
|
|
|
|
|
VirtualMachine::AttachGuard::CreationException::~CreationException()
|
|
|
|
{}
|
|
|
|
|
|
|
|
VirtualMachine::AttachGuard::CreationException &
|
|
|
|
VirtualMachine::AttachGuard::CreationException::operator =(
|
|
|
|
CreationException const &)
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualMachine::AttachGuard::AttachGuard(
|
|
|
|
rtl::Reference< VirtualMachine > const & rMachine):
|
|
|
|
m_xMachine(rMachine)
|
|
|
|
{
|
|
|
|
OSL_ENSURE(m_xMachine.is(), "bad parameter");
|
|
|
|
m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
|
|
|
|
if (m_pEnvironment == 0)
|
|
|
|
throw CreationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualMachine::AttachGuard::~AttachGuard()
|
|
|
|
{
|
|
|
|
if (m_bDetach)
|
|
|
|
m_xMachine->detachThread();
|
|
|
|
}
|
|
|
|
|
2003-03-26 11:41:32 +00:00
|
|
|
VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
|
2002-12-06 10:35:41 +00:00
|
|
|
JNIEnv * pMainThreadEnv):
|
|
|
|
m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
|
|
|
|
{
|
2009-04-23 10:42:05 +00:00
|
|
|
(void) pMainThreadEnv; // avoid warnings
|
2005-06-15 09:25:58 +00:00
|
|
|
#ifdef SOLAR_JAVA
|
2002-12-06 10:35:41 +00:00
|
|
|
OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
|
|
|
|
"bad parameter");
|
2005-06-15 09:25:58 +00:00
|
|
|
#endif
|
2002-12-06 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualMachine::~VirtualMachine()
|
|
|
|
{
|
|
|
|
if (m_bDestroy)
|
|
|
|
{
|
2003-03-26 11:41:32 +00:00
|
|
|
// Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
|
|
|
|
// not a daemon thread and is never terminated, so that calling
|
|
|
|
// DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
|
|
|
|
// forever.
|
|
|
|
/*
|
2002-12-06 10:35:41 +00:00
|
|
|
jint n = m_pVm->DestroyJavaVM();
|
|
|
|
OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
|
2003-03-26 11:41:32 +00:00
|
|
|
*/
|
2002-12-06 10:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
|
|
|
|
{
|
2004-11-09 10:50:10 +00:00
|
|
|
#ifndef SOLAR_JAVA
|
2011-10-03 16:13:17 +03:00
|
|
|
(void) pAttached;
|
2004-11-09 10:50:10 +00:00
|
|
|
return 0;
|
|
|
|
#else
|
2002-12-06 10:35:41 +00:00
|
|
|
OSL_ENSURE(pAttached != 0, "bad parameter");
|
|
|
|
JNIEnv * pEnv;
|
|
|
|
jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
|
2006-06-19 17:58:39 +00:00
|
|
|
if (n != JNI_OK && n != JNI_EDETACHED) {
|
2011-03-12 11:49:53 +01:00
|
|
|
OSL_FAIL("JNI: GetEnv failed");
|
2006-06-19 17:58:39 +00:00
|
|
|
}
|
2002-12-06 10:35:41 +00:00
|
|
|
if (pEnv == 0)
|
|
|
|
{
|
|
|
|
if (m_pVm->AttachCurrentThread(reinterpret_cast< void ** >(&pEnv), 0)
|
|
|
|
!= JNI_OK)
|
|
|
|
return 0;
|
|
|
|
*pAttached = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*pAttached = false;
|
|
|
|
return pEnv;
|
2004-11-09 10:50:10 +00:00
|
|
|
#endif
|
2002-12-06 10:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualMachine::detachThread() const
|
|
|
|
{
|
2004-11-09 10:50:10 +00:00
|
|
|
#ifdef SOLAR_JAVA
|
2006-06-19 17:58:39 +00:00
|
|
|
if (m_pVm->DetachCurrentThread() != JNI_OK) {
|
2011-03-12 11:49:53 +01:00
|
|
|
OSL_FAIL("JNI: DetachCurrentThread failed");
|
2006-06-19 17:58:39 +00:00
|
|
|
}
|
2004-11-09 10:50:10 +00:00
|
|
|
#endif
|
2002-12-06 10:35:41 +00:00
|
|
|
}
|
2010-10-14 08:30:07 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|