LCOV - code coverage report
Current view: top level - jvmaccess/source - virtualmachine.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 26 37 70.3 %
Date: 2014-04-11 Functions: 7 12 58.3 %
Legend: Lines: hit not hit

          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         151 : VirtualMachine::AttachGuard::AttachGuard(
      44             :     rtl::Reference< VirtualMachine > const & rMachine):
      45         151 :     m_xMachine(rMachine)
      46             : {
      47             :     OSL_ENSURE(m_xMachine.is(), "bad parameter");
      48         151 :     m_pEnvironment = m_xMachine->attachThread(&m_bDetach);
      49         151 :     if (m_pEnvironment == 0)
      50           0 :         throw CreationException();
      51         151 : }
      52             : 
      53         302 : VirtualMachine::AttachGuard::~AttachGuard()
      54             : {
      55         151 :     if (m_bDetach)
      56         114 :         m_xMachine->detachThread();
      57         151 : }
      58             : 
      59           6 : VirtualMachine::VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
      60             :                                JNIEnv * pMainThreadEnv):
      61           6 :     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           6 : }
      67             : 
      68           9 : VirtualMachine::~VirtualMachine()
      69             : {
      70           3 :     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           6 : }
      82             : 
      83         151 : JNIEnv * VirtualMachine::attachThread(bool * pAttached) const
      84             : {
      85             :     OSL_ENSURE(pAttached != 0, "bad parameter");
      86             :     JNIEnv * pEnv;
      87         151 :     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         151 :     if (pEnv == 0)
      92             :     {
      93         114 :         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         114 :              0)
     103             :             != JNI_OK)
     104           0 :             return 0;
     105         114 :         *pAttached = true;
     106             :     }
     107             :     else
     108          37 :         *pAttached = false;
     109         151 :     return pEnv;
     110             : }
     111             : 
     112         114 : void VirtualMachine::detachThread() const
     113             : {
     114         114 :     if (m_pVm->DetachCurrentThread() != JNI_OK) {
     115             :         OSL_FAIL("JNI: DetachCurrentThread failed");
     116             :     }
     117         114 : }
     118             : 
     119             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10