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