1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "jvmaccess/virtualmachine.hxx"
29 
30 #include "osl/diagnose.h"
31 
32 using jvmaccess::VirtualMachine;
33 
34 VirtualMachine::AttachGuard::CreationException::CreationException()
35 {}
36 
37 VirtualMachine::AttachGuard::CreationException::CreationException(
38     CreationException const &)
39 {}
40 
41 VirtualMachine::AttachGuard::CreationException::~CreationException()
42 {}
43 
44 VirtualMachine::AttachGuard::CreationException &
45 VirtualMachine::AttachGuard::CreationException::operator =(
46     CreationException const &)
47 {
48     return *this;
49 }
50 
51 VirtualMachine::AttachGuard::AttachGuard(
52     rtl::Reference< VirtualMachine > const & rMachine):
53     m_xMachine(rMachine)
54 {
55     OSL_ENSURE(m_xMachine.is(), "bad parameter");
56     m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
57     if (m_pEnvironment == 0)
58         throw CreationException();
59 }
60 
61 VirtualMachine::AttachGuard::~AttachGuard()
62 {
63     if (m_bDetach)
64         m_xMachine->detachThread();
65 }
66 
67 VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
68                                JNIEnv * pMainThreadEnv):
69     m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
70 {
71     (void) pMainThreadEnv; // avoid warnings
72 #ifdef SOLAR_JAVA
73     OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
74                "bad parameter");
75 #endif
76 }
77 
78 VirtualMachine::~VirtualMachine()
79 {
80     if (m_bDestroy)
81     {
82         // Do not destroy the VM.  Under Java 1.3, the AWT event loop thread is
83         // not a daemon thread and is never terminated, so that calling
84         // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
85         // forever.
86 /*
87         jint n = m_pVm->DestroyJavaVM();
88         OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
89 */
90     }
91 }
92 
93 JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
94 {
95 #ifndef SOLAR_JAVA
96     return 0;
97 #else
98     OSL_ENSURE(pAttached != 0, "bad parameter");
99     JNIEnv * pEnv;
100     jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
101     if (n != JNI_OK && n != JNI_EDETACHED) {
102         OSL_ENSURE(false, "JNI: GetEnv failed");
103     }
104     if (pEnv == 0)
105     {
106         if (m_pVm->AttachCurrentThread(reinterpret_cast< void ** >(&pEnv), 0)
107             != JNI_OK)
108             return 0;
109         *pAttached = true;
110     }
111     else
112         *pAttached = false;
113     return pEnv;
114 #endif
115 }
116 
117 void VirtualMachine::detachThread() const
118 {
119 #ifdef SOLAR_JAVA
120     if (m_pVm->DetachCurrentThread() != JNI_OK) {
121         OSL_ENSURE(false, "JNI: DetachCurrentThread failed");
122     }
123 #endif
124 }
125