Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "jvmaccess/virtualmachine.hxx"
21 :
22 : #include "osl/diagnose.h"
23 :
24 : using jvmaccess::VirtualMachine;
25 :
26 0 : VirtualMachine::AttachGuard::CreationException::CreationException()
27 0 : {}
28 :
29 0 : VirtualMachine::AttachGuard::CreationException::CreationException(
30 0 : CreationException const &)
31 0 : {}
32 :
33 0 : VirtualMachine::AttachGuard::CreationException::~CreationException()
34 0 : {}
35 :
36 : VirtualMachine::AttachGuard::CreationException &
37 0 : VirtualMachine::AttachGuard::CreationException::operator =(
38 : CreationException const &)
39 : {
40 0 : return *this;
41 : }
42 :
43 12906 : VirtualMachine::AttachGuard::AttachGuard(
44 : rtl::Reference< VirtualMachine > const & rMachine):
45 12906 : m_xMachine(rMachine)
46 : {
47 : OSL_ENSURE(m_xMachine.is(), "bad parameter");
48 12906 : m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
49 12906 : if (m_pEnvironment == 0)
50 0 : throw CreationException();
51 12906 : }
52 :
53 25812 : VirtualMachine::AttachGuard::~AttachGuard()
54 : {
55 12906 : if (m_bDetach)
56 12746 : m_xMachine->detachThread();
57 12906 : }
58 :
59 18 : VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
60 : JNIEnv * pMainThreadEnv):
61 18 : m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
62 : {
63 : (void) pMainThreadEnv; // avoid warnings
64 : OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
65 : "bad parameter");
66 18 : }
67 :
68 30 : VirtualMachine::~VirtualMachine()
69 : {
70 10 : if (m_bDestroy)
71 : {
72 : // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
73 : // not a daemon thread and is never terminated, so that calling
74 : // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
75 : // forever.
76 : /*
77 : jint n = m_pVm->DestroyJavaVM();
78 : OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
79 : */
80 : }
81 20 : }
82 :
83 12906 : JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
84 : {
85 : assert(pAttached != 0 && "bad parameter");
86 : JNIEnv * pEnv;
87 12906 : jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
88 : if (n != JNI_OK && n != JNI_EDETACHED) {
89 : OSL_FAIL("JNI: GetEnv failed");
90 : }
91 12906 : if (pEnv == 0)
92 : {
93 12746 : if (m_pVm->AttachCurrentThread
94 : (
95 : #ifndef ANDROID
96 : reinterpret_cast< void ** >(&pEnv),
97 : #else
98 : // The Android <jni.h> has AttachCurrentThread() taking a
99 : // JNIEnv** and not void **
100 : &pEnv,
101 : #endif
102 12746 : 0)
103 : != JNI_OK)
104 0 : return 0;
105 12746 : *pAttached = true;
106 : }
107 : else
108 160 : *pAttached = false;
109 12906 : return pEnv;
110 : }
111 :
112 12746 : void VirtualMachine::detachThread() const
113 : {
114 12746 : if (m_pVm->DetachCurrentThread() != JNI_OK) {
115 : OSL_FAIL("JNI: DetachCurrentThread failed");
116 : }
117 12746 : }
118 :
119 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|