Branch data 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 : 150 : VirtualMachine::AttachGuard::AttachGuard(
44 : : rtl::Reference< VirtualMachine > const & rMachine):
45 : 150 : m_xMachine(rMachine)
46 : : {
47 : : OSL_ENSURE(m_xMachine.is(), "bad parameter");
48 [ + - ]: 150 : m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
49 [ - + ]: 150 : if (m_pEnvironment == 0)
50 [ # # ]: 0 : throw CreationException();
51 : 150 : }
52 : :
53 : 150 : VirtualMachine::AttachGuard::~AttachGuard()
54 : : {
55 [ + + ]: 150 : if (m_bDetach)
56 [ + - ]: 138 : m_xMachine->detachThread();
57 : 150 : }
58 : :
59 : 4 : VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
60 : : JNIEnv * pMainThreadEnv):
61 : 4 : m_pVm(pVm), m_nVersion(nVersion), m_bDestroy(bDestroy)
62 : : {
63 : : (void) pMainThreadEnv; // avoid warnings
64 : : #ifdef SOLAR_JAVA
65 : : OSL_ENSURE(pVm != 0 && nVersion >= JNI_VERSION_1_2 && pMainThreadEnv != 0,
66 : : "bad parameter");
67 : : #endif
68 : 4 : }
69 : :
70 : 0 : VirtualMachine::~VirtualMachine()
71 : : {
72 : 0 : if (m_bDestroy)
73 : : {
74 : : // Do not destroy the VM. Under Java 1.3, the AWT event loop thread is
75 : : // not a daemon thread and is never terminated, so that calling
76 : : // DestroyJavaVM (waiting for all non-daemon threads to terminate) hangs
77 : : // forever.
78 : : /*
79 : : jint n = m_pVm->DestroyJavaVM();
80 : : OSL_ENSURE(n == JNI_OK, "JNI: DestroyJavaVM failed");
81 : : */
82 : : }
83 [ # # ]: 0 : }
84 : :
85 : 150 : JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
86 : : {
87 : : #ifndef SOLAR_JAVA
88 : : (void) pAttached;
89 : : return 0;
90 : : #else
91 : : OSL_ENSURE(pAttached != 0, "bad parameter");
92 : : JNIEnv * pEnv;
93 [ + - ]: 150 : jint n = m_pVm->GetEnv(reinterpret_cast< void ** >(&pEnv), m_nVersion);
94 : : if (n != JNI_OK && n != JNI_EDETACHED) {
95 : : OSL_FAIL("JNI: GetEnv failed");
96 : : }
97 [ + + ]: 150 : if (pEnv == 0)
98 : : {
99 [ - + ]: 138 : if (m_pVm->AttachCurrentThread
100 : : (
101 : : #ifndef ANDROID
102 : : reinterpret_cast< void ** >(&pEnv),
103 : : #else
104 : : // The Android <jni.h> has AttachCurrentThread() taking a
105 : : // JNIEnv** and not void **
106 : : &pEnv,
107 : : #endif
108 [ + - ]: 138 : 0)
109 : : != JNI_OK)
110 : 0 : return 0;
111 : 138 : *pAttached = true;
112 : : }
113 : : else
114 : 12 : *pAttached = false;
115 : 150 : return pEnv;
116 : : #endif
117 : : }
118 : :
119 : 138 : void VirtualMachine::detachThread() const
120 : : {
121 : : #ifdef SOLAR_JAVA
122 : 138 : if (m_pVm->DetachCurrentThread() != JNI_OK) {
123 : : OSL_FAIL("JNI: DetachCurrentThread failed");
124 : : }
125 : : #endif
126 : 138 : }
127 : :
128 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|