1*dff3f235SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*dff3f235SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*dff3f235SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*dff3f235SAndrew Rist * distributed with this work for additional information
6*dff3f235SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*dff3f235SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*dff3f235SAndrew Rist * "License"); you may not use this file except in compliance
9*dff3f235SAndrew Rist * with the License. You may obtain a copy of the License at
10*dff3f235SAndrew Rist *
11*dff3f235SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*dff3f235SAndrew Rist *
13*dff3f235SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*dff3f235SAndrew Rist * software distributed under the License is distributed on an
15*dff3f235SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dff3f235SAndrew Rist * KIND, either express or implied. See the License for the
17*dff3f235SAndrew Rist * specific language governing permissions and limitations
18*dff3f235SAndrew Rist * under the License.
19*dff3f235SAndrew Rist *
20*dff3f235SAndrew Rist *************************************************************/
21*dff3f235SAndrew Rist
22*dff3f235SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include "osl/diagnose.h"
27cdf0e10cSrcweir
28cdf0e10cSrcweir using jvmaccess::VirtualMachine;
29cdf0e10cSrcweir
CreationException()30cdf0e10cSrcweir VirtualMachine::AttachGuard::CreationException::CreationException()
31cdf0e10cSrcweir {}
32cdf0e10cSrcweir
CreationException(CreationException const &)33cdf0e10cSrcweir VirtualMachine::AttachGuard::CreationException::CreationException(
34cdf0e10cSrcweir CreationException const &)
35cdf0e10cSrcweir {}
36cdf0e10cSrcweir
~CreationException()37cdf0e10cSrcweir VirtualMachine::AttachGuard::CreationException::~CreationException()
38cdf0e10cSrcweir {}
39cdf0e10cSrcweir
40cdf0e10cSrcweir VirtualMachine::AttachGuard::CreationException &
operator =(CreationException const &)41cdf0e10cSrcweir VirtualMachine::AttachGuard::CreationException::operator =(
42cdf0e10cSrcweir CreationException const &)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir return *this;
45cdf0e10cSrcweir }
46cdf0e10cSrcweir
AttachGuard(rtl::Reference<VirtualMachine> const & rMachine)47cdf0e10cSrcweir VirtualMachine::AttachGuard::AttachGuard(
48cdf0e10cSrcweir rtl::Reference< VirtualMachine > const & rMachine):
49cdf0e10cSrcweir m_xMachine(rMachine)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir OSL_ENSURE(m_xMachine.is(), "bad parameter");
52cdf0e10cSrcweir m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
53cdf0e10cSrcweir if (m_pEnvironment == 0)
54cdf0e10cSrcweir throw CreationException();
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
~AttachGuard()57cdf0e10cSrcweir VirtualMachine::AttachGuard::~AttachGuard()
58cdf0e10cSrcweir {
59cdf0e10cSrcweir if (m_bDetach)
60cdf0e10cSrcweir m_xMachine->detachThread();
61cdf0e10cSrcweir }
62cdf0e10cSrcweir
VirtualMachine(JavaVM * pVm,int nVersion,bool bDestroy,JNIEnv * pMainThreadEnv)63cdf0e10cSrcweir VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
64cdf0e10cSrcweir JNIEnv * pMainThreadEnv):
65cdf0e10cSrcweir m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir (void) pMainThreadEnv; // avoid warnings
68cdf0e10cSrcweir #ifdef SOLAR_JAVA
69cdf0e10cSrcweir OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
70cdf0e10cSrcweir "bad parameter");
71cdf0e10cSrcweir #endif
72cdf0e10cSrcweir }
73cdf0e10cSrcweir
~VirtualMachine()74cdf0e10cSrcweir VirtualMachine::~VirtualMachine()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir if (m_bDestroy)
77cdf0e10cSrcweir {
78cdf0e10cSrcweir // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
79cdf0e10cSrcweir // not a daemon thread and is never terminated, so that calling
80cdf0e10cSrcweir // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
81cdf0e10cSrcweir // forever.
82cdf0e10cSrcweir /*
83cdf0e10cSrcweir jint n = m_pVm->DestroyJavaVM();
84cdf0e10cSrcweir OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
85cdf0e10cSrcweir */
86cdf0e10cSrcweir }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir
attachThread(bool * pAttached) const89cdf0e10cSrcweir JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
90cdf0e10cSrcweir {
91cdf0e10cSrcweir #ifndef SOLAR_JAVA
92cdf0e10cSrcweir return 0;
93cdf0e10cSrcweir #else
94cdf0e10cSrcweir OSL_ENSURE(pAttached != 0, "bad parameter");
95cdf0e10cSrcweir JNIEnv * pEnv;
96cdf0e10cSrcweir jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
97cdf0e10cSrcweir if (n != JNI_OK && n != JNI_EDETACHED) {
98cdf0e10cSrcweir OSL_ENSURE(false, "JNI: GetEnv failed");
99cdf0e10cSrcweir }
100cdf0e10cSrcweir if (pEnv == 0)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir if (m_pVm->AttachCurrentThread(reinterpret_cast< void ** >(&pEnv), 0)
103cdf0e10cSrcweir != JNI_OK)
104cdf0e10cSrcweir return 0;
105cdf0e10cSrcweir *pAttached = true;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir else
108cdf0e10cSrcweir *pAttached = false;
109cdf0e10cSrcweir return pEnv;
110cdf0e10cSrcweir #endif
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
detachThread() const113cdf0e10cSrcweir void VirtualMachine::detachThread() const
114cdf0e10cSrcweir {
115cdf0e10cSrcweir #ifdef SOLAR_JAVA
116cdf0e10cSrcweir if (m_pVm->DetachCurrentThread() != JNI_OK) {
117cdf0e10cSrcweir OSL_ENSURE(false, "JNI: DetachCurrentThread failed");
118cdf0e10cSrcweir }
119cdf0e10cSrcweir #endif
120cdf0e10cSrcweir }
121