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 : : #ifndef INCLUDED_JVMACCESS_VIRTUALMACHINE_HXX
21 : : #define INCLUDED_JVMACCESS_VIRTUALMACHINE_HXX
22 : :
23 : : #include "jvmaccessdllapi.h"
24 : : #include "rtl/ref.hxx"
25 : : #include "salhelper/simplereferenceobject.hxx"
26 : :
27 : : #ifdef SOLAR_JAVA
28 : : #include "jni.h"
29 : : #else
30 : : struct JNIEnv_;
31 : : typedef JNIEnv_ JNIEnv;
32 : : struct JavaVM_;
33 : : typedef JavaVM_ JavaVM;
34 : : typedef int jint;
35 : : typedef void * jobject;
36 : : #endif
37 : :
38 : : namespace jvmaccess {
39 : :
40 : : /** An encapsulating wrapper around a Java virtual machine.
41 : : */
42 : : class JVMACCESS_DLLPUBLIC VirtualMachine: public salhelper::SimpleReferenceObject
43 : : {
44 : : public:
45 : : /** A helper to attach a thread to a Java virtual machine.
46 : :
47 : : @descr
48 : : Upon construction of a guard the current thread is attached to the
49 : : virtual machine, and upon destruction of the guard the thread is
50 : : detached again. For any one thread, multiple instances of this class
51 : : may be used in a stack-like fashion (care is taken to only really
52 : : detach the thread from the virtual machine upon destruction of the guard
53 : : at the bottom of the stack).
54 : : */
55 : : class JVMACCESS_DLLPUBLIC AttachGuard
56 : : {
57 : : public:
58 : : /** An exception indicating failure to create an AttachGuard.
59 : : */
60 : : class JVMACCESS_DLLPUBLIC CreationException
61 : : {
62 : : public:
63 : : CreationException();
64 : :
65 : : CreationException(CreationException const &);
66 : :
67 : : virtual ~CreationException();
68 : :
69 : : CreationException & operator =(CreationException const &);
70 : : };
71 : :
72 : : /** Attach the current thread to a virtual machine.
73 : :
74 : : @param rMachine
75 : : The virtual machine to attach to. Must not be a null reference.
76 : :
77 : : @exception CreationException
78 : : Thrown in case attaching fails (due to a JNI problem).
79 : : */
80 : : explicit AttachGuard(rtl::Reference< VirtualMachine > const & rMachine);
81 : :
82 : : /** Detach the current thread from the virtual machine again.
83 : : */
84 : : ~AttachGuard();
85 : :
86 : : /** Get a JNI environment pointer for the current thread.
87 : :
88 : : @return
89 : : A valid JNI environment pointer. Will never be null.
90 : : */
91 : 8 : inline JNIEnv * getEnvironment() const { return m_pEnvironment; }
92 : :
93 : : private:
94 : : AttachGuard(AttachGuard &); // not implemented
95 : : void operator =(AttachGuard); // not implemented
96 : :
97 : : rtl::Reference< VirtualMachine > m_xMachine;
98 : : JNIEnv * m_pEnvironment;
99 : : bool m_bDetach;
100 : : };
101 : :
102 : : /** Create a wrapper around a Java virtual machine.
103 : :
104 : : @param pVm
105 : : A JNI pointer to virtual machine. Must not be null.
106 : :
107 : : @param nVersion
108 : : The JNI version of the virtual machine pointed to by pVm. Must be at
109 : : least JNI_VERSION_1_2. This parameter should be of type jint, not int,
110 : : but at least on some platforms the definition of jint changed from
111 : : JDK 1.3 (long) to JDK 1.4 (int), so that the mangled C++ name of the
112 : : constructor would depend on the JDK version used at compile time.
113 : :
114 : : @param bDestroy
115 : : Whether to destroy the virtual machine when destructing the wrapper
116 : : (i.e., whether the wrapper owns the virtual machine pointed to by pVm).
117 : :
118 : : @param pMainThreadEnv
119 : : A valid JNI environment pointer for the current thread; must not be
120 : : null. The current thread must be "initially attached" to the virtual
121 : : machine while this constructor is being called (i.e., it must be the
122 : : thread that has called JNI_CreateJavaVM in case the virtual machine has
123 : : been started via the JNI Invocation API, and it must not already have
124 : : called DetachCurrentThread; or it must be executing native code called
125 : : from a "primordial" virtual machine). This environment pointer was
126 : : formerly used to obtain a reference to the thread's current context
127 : : class loader (java.lang.Thread.getCurrentClassLoader; if later a native
128 : : thread was attached to the virtual machine, that thread's context class
129 : : loader would be null, so the AttachGuard first of all set it to the
130 : : saved value; this feature has been removed again for performance reasons
131 : : and because the default context class loader is often not useful, so
132 : : that code relying on a context class loader has to set one explicitly,
133 : : anyway). This parameter is currently unused (but may be used again in
134 : : the future).
135 : : */
136 : : VirtualMachine(JavaVM * pVm, int nVersion, bool bDestroy,
137 : : JNIEnv * pMainThreadEnv);
138 : :
139 : : private:
140 : : VirtualMachine(VirtualMachine &); // not implemented
141 : : void operator =(VirtualMachine); // not implemented
142 : :
143 : : virtual ~VirtualMachine();
144 : :
145 : : JNIEnv * attachThread(bool * pAttached) const;
146 : :
147 : : void detachThread() const;
148 : :
149 : : JavaVM * m_pVm;
150 : : jint m_nVersion;
151 : : bool m_bDestroy;
152 : :
153 : : friend class AttachGuard; // to access attachThread, detachThread
154 : : };
155 : :
156 : : }
157 : :
158 : : #endif // INCLUDED_JVMACCESS_VIRTUALMACHINE_HXX
159 : :
160 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|