J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of Sun Microsystems nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <memory.h> |
| 33 | #include <stdlib.h> |
| 34 | #include "jinvokerExp.h" |
| 35 | |
| 36 | static int g_nExitCode = 0; |
| 37 | |
| 38 | void system_exit(jint nCode){ |
| 39 | g_nExitCode = nCode; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | Allocating and providing the JVM init argumets. |
| 44 | By MakeJavaVMInitArgs() it is provided two options: providing CLASSPATH |
| 45 | environment variable value and function java.lang.System.exit() |
| 46 | redefinition in order to get the exit code. |
| 47 | See the description of the JNI API in |
| 48 | http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/invocation.html#wp9502 |
| 49 | */ |
| 50 | |
| 51 | int MakeJavaVMInitArgs( void** ppArgs ){ |
| 52 | |
| 53 | int nOptSize = 2; |
| 54 | JavaVMInitArgs* pArgs = new JavaVMInitArgs(); |
| 55 | JavaVMOption* pOptions = new JavaVMOption[nOptSize]; |
| 56 | |
| 57 | //provide CLASSPATH value to java.class.path |
| 58 | |
| 59 | char* szClassPath = getenv("CLASSPATH"); |
| 60 | if( szClassPath == NULL ) |
| 61 | szClassPath = "."; |
| 62 | |
| 63 | pOptions[0].optionString = new char[strlen("-Djava.class.path=")+ |
| 64 | strlen(szClassPath)+1]; |
| 65 | sprintf( pOptions[0].optionString, "-Djava.class.path=%s", szClassPath ); |
| 66 | |
| 67 | //redefine java.lang.System.exit() |
| 68 | |
| 69 | pOptions[1].optionString = "exit"; |
| 70 | pOptions[1].extraInfo = system_exit; |
| 71 | |
| 72 | //Fill the arguments |
| 73 | |
| 74 | memset(pArgs, 0, sizeof(JavaVMInitArgs)); |
| 75 | pArgs->version = 0x00010002; |
| 76 | pArgs->options = pOptions; |
| 77 | pArgs->nOptions = nOptSize; |
| 78 | pArgs->ignoreUnrecognized = JNI_TRUE; |
| 79 | |
| 80 | *ppArgs = pArgs; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | Free the allocated JVM init argumets |
| 87 | */ |
| 88 | |
| 89 | void FreeJavaVMInitArgs( void* pArgs ){ |
| 90 | delete ((JavaVMInitArgs*)pArgs)->options[0].optionString; |
| 91 | delete ((JavaVMInitArgs*)pArgs)->options; |
| 92 | delete pArgs; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | Static wrapper on FindClass() JNI function. |
| 97 | See the description in |
| 98 | http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp16027 |
| 99 | */ |
| 100 | |
| 101 | int FindClass( JNIEnv* pEnv, |
| 102 | const char* szClass, |
| 103 | jclass* pClass ){ |
| 104 | |
| 105 | *pClass = pEnv->FindClass( szClass ); |
| 106 | |
| 107 | if(pEnv->ExceptionCheck() == JNI_TRUE){ |
| 108 | pEnv->ExceptionDescribe(); |
| 109 | return -1; |
| 110 | } |
| 111 | if(*pClass != NULL) |
| 112 | return 0; |
| 113 | else |
| 114 | return -2; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | Static wrapper on GetStaticMethodID() JNI function. |
| 120 | See the description in |
| 121 | http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp20949 |
| 122 | */ |
| 123 | |
| 124 | int GetStaticMethodID(JNIEnv* pEnv, |
| 125 | jclass pClass, |
| 126 | const char* szName, |
| 127 | const char* szArgs, |
| 128 | jmethodID* pMid){ |
| 129 | |
| 130 | *pMid = pEnv->GetStaticMethodID( pClass, szName, szArgs); |
| 131 | |
| 132 | if(pEnv->ExceptionCheck() == JNI_TRUE){ |
| 133 | pEnv->ExceptionDescribe(); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | if( *pMid != NULL ) |
| 138 | return 0; |
| 139 | else |
| 140 | return -2; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | Static wrapper on NewObjectArray() JNI function. |
| 145 | See the description in |
| 146 | http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp21619 |
| 147 | */ |
| 148 | |
| 149 | int NewObjectArray( JNIEnv* pEnv, |
| 150 | int nDimension, |
| 151 | const char* szType, |
| 152 | jobjectArray* pArray ){ |
| 153 | |
| 154 | *pArray = pEnv->NewObjectArray( nDimension, pEnv->FindClass( szType ), NULL); |
| 155 | |
| 156 | if(pEnv->ExceptionCheck() == JNI_TRUE){ |
| 157 | pEnv->ExceptionDescribe(); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | if( pArray != NULL ) |
| 162 | return 0; |
| 163 | else |
| 164 | return -2; |
| 165 | |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | Static wrapper on CallStaticVoidMethod() JNI function. |
| 170 | See the description in |
| 171 | http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp4796 |
| 172 | */ |
| 173 | |
| 174 | int CallStaticVoidMethod( JNIEnv* pEnv, |
| 175 | jclass pClass, |
| 176 | jmethodID pMid, |
| 177 | void* pArgs){ |
| 178 | |
| 179 | g_nExitCode = 0; |
| 180 | pEnv->CallStaticVoidMethod( pClass, pMid, pArgs); |
| 181 | if( pEnv->ExceptionCheck() == JNI_TRUE ){ |
| 182 | pEnv->ExceptionDescribe(); |
| 183 | return -1; |
| 184 | }else |
| 185 | return g_nExitCode; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | Static wrapper on DestroyJavaVM() JNI function. |
| 190 | See the description in |
| 191 | http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/invocation.html#destroy_java_vm |
| 192 | */ |
| 193 | |
| 194 | int DestroyJavaVM( JavaVM* pJVM ){ |
| 195 | pJVM->DestroyJavaVM(); |
| 196 | return 0; |
| 197 | } |