Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / jvmaccess / virtualmachine.hxx
blobdf18b5791e40dd9f8e86acdf965d06e0ab11999d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_JVMACCESS_VIRTUALMACHINE_HXX
21 #define INCLUDED_JVMACCESS_VIRTUALMACHINE_HXX
23 #include <jvmaccess/jvmaccessdllapi.h>
24 #include <rtl/ref.hxx>
25 #include <salhelper/simplereferenceobject.hxx>
27 #include "jni.h"
29 namespace jvmaccess {
31 /** An encapsulating wrapper around a Java virtual machine.
33 class JVMACCESS_DLLPUBLIC VirtualMachine: public salhelper::SimpleReferenceObject
35 public:
36 /** A helper to attach a thread to a Java virtual machine.
38 @descr
39 Upon construction of a guard the current thread is attached to the
40 virtual machine, and upon destruction of the guard the thread is
41 detached again. For any one thread, multiple instances of this class
42 may be used in a stack-like fashion (care is taken to only really
43 detach the thread from the virtual machine upon destruction of the guard
44 at the bottom of the stack).
46 class JVMACCESS_DLLPUBLIC AttachGuard
48 public:
49 /** An exception indicating failure to create an AttachGuard.
51 class JVMACCESS_DLLPUBLIC CreationException final
53 public:
54 CreationException();
56 CreationException(CreationException const &);
58 ~CreationException();
60 CreationException & operator =(CreationException const &);
63 /** Attach the current thread to a virtual machine.
65 @param rMachine
66 The virtual machine to attach to. Must not be a null reference.
68 @exception CreationException
69 Thrown in case attaching fails (due to a JNI problem).
71 explicit AttachGuard(rtl::Reference< VirtualMachine > const & rMachine);
73 /** Detach the current thread from the virtual machine again.
75 ~AttachGuard();
77 /** Get a JNI environment pointer for the current thread.
79 @return
80 A valid JNI environment pointer. Will never be null.
82 JNIEnv * getEnvironment() const { return m_pEnvironment; }
84 private:
85 AttachGuard(AttachGuard &) = delete;
86 void operator =(AttachGuard) = delete;
88 rtl::Reference< VirtualMachine > m_xMachine;
89 JNIEnv * m_pEnvironment;
90 bool m_bDetach;
93 /** Create a wrapper around a Java virtual machine.
95 @param pVm
96 A JNI pointer to virtual machine. Must not be null.
98 @param nVersion
99 The JNI version of the virtual machine pointed to by pVm. Must be at
100 least JNI_VERSION_1_2. This parameter should be of type jint, not int,
101 but at least on some platforms the definition of jint changed from
102 JDK 1.3 (long) to JDK 1.4 (int), so that the mangled C++ name of the
103 constructor would depend on the JDK version used at compile time.
105 @param bDestroy
106 Whether to destroy the virtual machine when destructing the wrapper
107 (i.e., whether the wrapper owns the virtual machine pointed to by pVm).
109 @param pMainThreadEnv
110 A valid JNI environment pointer for the current thread; must not be
111 null. The current thread must be "initially attached" to the virtual
112 machine while this constructor is being called (i.e., it must be the
113 thread that has called JNI_CreateJavaVM in case the virtual machine has
114 been started via the JNI Invocation API, and it must not already have
115 called DetachCurrentThread; or it must be executing native code called
116 from a "primordial" virtual machine). This environment pointer was
117 formerly used to obtain a reference to the thread's current context
118 class loader (java.lang.Thread.getCurrentClassLoader; if later a native
119 thread was attached to the virtual machine, that thread's context class
120 loader would be null, so the AttachGuard first of all set it to the
121 saved value; this feature has been removed again for performance reasons
122 and because the default context class loader is often not useful, so
123 that code relying on a context class loader has to set one explicitly,
124 anyway). This parameter is currently unused (but may be used again in
125 the future).
127 VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
128 JNIEnv * pMainThreadEnv);
130 private:
131 VirtualMachine(VirtualMachine &) = delete;
132 void operator =(VirtualMachine) = delete;
134 virtual ~VirtualMachine() override;
136 JNIEnv * attachThread(bool * pAttached) const;
138 void detachThread() const;
140 JavaVM * m_pVm;
141 jint m_nVersion;
142 bool m_bDestroy;
144 friend class AttachGuard; // to access attachThread, detachThread
149 #endif // INCLUDED_JVMACCESS_VIRTUALMACHINE_HXX
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */