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