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 <sal/config.h>
21 :
22 : #include <cassert>
23 :
24 : #include <jvmaccess/virtualmachine.hxx>
25 : #include <sal/log.hxx>
26 :
27 : using jvmaccess::VirtualMachine;
28 :
29 0 : VirtualMachine::AttachGuard::CreationException::CreationException()
30 0 : {}
31 :
32 0 : VirtualMachine::AttachGuard::CreationException::CreationException(
33 0 : CreationException const &)
34 0 : {}
35 :
36 0 : VirtualMachine::AttachGuard::CreationException::~CreationException()
37 0 : {}
38 :
39 : VirtualMachine::AttachGuard::CreationException &
40 0 : VirtualMachine::AttachGuard::CreationException::operator =(
41 : CreationException const &)
42 : {
43 0 : return *this;
44 : }
45 :
46 250559 : VirtualMachine::AttachGuard::AttachGuard(
47 : rtl::Reference< VirtualMachine > const & rMachine):
48 250559 : m_xMachine(rMachine)
49 : {
50 : assert(m_xMachine.is());
51 250559 : m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
52 250559 : if (m_pEnvironment == 0)
53 0 : throw CreationException();
54 250559 : }
55 :
56 501118 : VirtualMachine::AttachGuard::~AttachGuard()
57 : {
58 250559 : if (m_bDetach)
59 208025 : m_xMachine->detachThread();
60 250559 : }
61 :
62 7 : VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
63 : JNIEnv * pMainThreadEnv):
64 7 : m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
65 : {
66 : (void) pMainThreadEnv; // avoid warnings
67 : assert(pVm != 0);
68 : assert(nVersion >= JNI_VERSION_1_2);
69 : assert(pMainThreadEnv);
70 7 : }
71 :
72 9 : VirtualMachine::~VirtualMachine()
73 : {
74 3 : if (m_bDestroy)
75 : {
76 : // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
77 : // not a daemon thread and is never terminated, so that calling
78 : // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
79 : // forever.
80 : /*
81 : jint n = m_pVm->DestroyJavaVM();
82 : SAL_WARN_IF(n != JNI_OK, "jvmaccess", "JNI: DestroyJavaVM failed");
83 : */
84 : }
85 6 : }
86 :
87 250559 : JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
88 : {
89 : assert(pAttached != 0 && "bad parameter");
90 : JNIEnv * pEnv;
91 250559 : jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
92 : SAL_WARN_IF(
93 : n != JNI_OK && n != JNI_EDETACHED, "jvmaccess", "JNI: GetEnv failed");
94 250559 : if (pEnv == 0)
95 : {
96 208025 : if (m_pVm->AttachCurrentThread
97 : (
98 : #ifndef ANDROID
99 : reinterpret_cast< void ** >(&pEnv),
100 : #else
101 : // The Android <jni.h> has AttachCurrentThread() taking a
102 : // JNIEnv** and not void **
103 : &pEnv,
104 : #endif
105 208025 : 0)
106 : != JNI_OK)
107 0 : return 0;
108 208025 : *pAttached = true;
109 : }
110 : else
111 42534 : *pAttached = false;
112 250559 : return pEnv;
113 : }
114 :
115 208025 : void VirtualMachine::detachThread() const
116 : {
117 208025 : jint n = m_pVm->DetachCurrentThread();
118 : SAL_WARN_IF(n != JNI_OK, "jvmaccess", "JNI: DetachCurrentThread failed");
119 208025 : }
120 :
121 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|