1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Sun Microsystems, Inc.
* Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
/*
* Code to start a Java VM (*some* code from the JRE)
*/
#include "jsdj.h"
/***************************************************************************/
#ifdef JSD_STANDALONE_JAVA_VM
#include "jre.h"
static char* more_classpath[] =
{
{"..\\..\\jsdj\\dist\\classes"},
{"..\\..\\jsdj\\dist\\classes\\ifc11.jar"},
/*
* {"..\\..\\..\\jsdj\\dist\\classes"},
* {"..\\..\\..\\jsdj\\dist\\classes\\ifc12.jar"},
*/
/*
* {"..\\..\\samples\\jslogger"},
* {"classes"},
* {"ifc12.jar"},
* {"jsd10.jar"},
* {"jsdeb15.jar"}
*/
};
#define MORE_CLASSPATH_COUNT (sizeof(more_classpath)/sizeof(more_classpath[0]))
/*
* static char main_class[] = "callnative";
* static char main_class[] = "simpleIFC";
* static char* params[] = {"16 Dec 1997"};
* #define PARAM_COUNT (sizeof(params)/sizeof(params[0]))
*/
/*
* static char main_class[] = "netscape/jslogger/JSLogger";
* static char main_class[] = "LaunchJSDebugger";
*/
static char main_class[] = "com/netscape/jsdebugging/ifcui/launcher/local/LaunchJSDebugger";
static char* params[] = {NULL};
#define PARAM_COUNT 0
/* Globals */
static char **props; /* User-defined properties */
static int numProps, maxProps; /* Current, max number of properties */
static void *handle;
static JavaVM *jvm;
static JNIEnv *env;
/* Check for null value and return */
#define NULL_CHECK(e) if ((e) == 0) return 0
/*
* Adds a user-defined system property definition.
*/
void AddProperty(char *def)
{
if (numProps >= maxProps) {
if (props == 0) {
maxProps = 4;
props = JRE_Malloc(maxProps * sizeof(char **));
} else {
char **tmp;
maxProps *= 2;
tmp = JRE_Malloc(maxProps * sizeof(char **));
memcpy(tmp, props, numProps * sizeof(char **));
free(props);
props = tmp;
}
}
props[numProps++] = def;
}
/*
* Deletes a property definition by name.
*/
void DeleteProperty(const char *name)
{
int i;
for (i = 0; i < numProps; ) {
char *def = props[i];
char *c = strchr(def, '=');
int n;
if (c != 0) {
n = c - def;
} else {
n = strlen(def);
}
if (strncmp(name, def, n) == 0) {
if (i < --numProps) {
memmove(&props[i], &props[i+1], (numProps-i) * sizeof(char **));
}
} else {
i++;
}
}
}
/*
* Creates an array of Java string objects from the specified array of C
* strings. Returns 0 if the array could not be created.
*/
jarray NewStringArray(JNIEnv *env, char **cpp, int count)
{
jclass cls;
jarray ary;
int i;
NULL_CHECK(cls = (*env)->FindClass(env, "java/lang/String"));
NULL_CHECK(ary = (*env)->NewObjectArray(env, count, cls, 0));
for (i = 0; i < count; i++) {
jstring str = (*env)->NewStringUTF(env, *cpp++);
NULL_CHECK(str);
(*env)->SetObjectArrayElement(env, ary, i, str);
(*env)->DeleteLocalRef(env, str);
}
return ary;
}
/***************************************************************************/
static JNIEnv*
_CreateJavaVM(void)
{
JNIEnv* env = NULL;
JDK1_1InitArgs vmargs;
JRESettings set;
printf("Starting Java...\n");
if(JRE_GetCurrentSettings(&set) != 0)
{
if(JRE_GetDefaultSettings(&set) != 0)
{
fprintf(stderr, "Could not locate Java runtime\n");
return NULL;
}
}
/* Load runtime library */
handle = JRE_LoadLibrary(set.runtimeLib);
if (handle == 0) {
fprintf(stderr, "Could not load runtime library: %s\n",
set.runtimeLib);
return NULL;
}
/* Add pre-defined system properties */
if (set.javaHome != 0) {
char *def = JRE_Malloc(strlen(set.javaHome) + 16);
sprintf(def, "java.home=%s", set.javaHome);
AddProperty(def);
}
if (set.compiler != 0) {
char *def = JRE_Malloc(strlen(set.compiler) + 16);
sprintf(def, "java.compiler=%s", set.compiler);
AddProperty(def);
}
/*
* The following is used to specify that we require at least
* JNI version 1.1. Currently, this field is not checked but
* will be starting with JDK/JRE 1.2. The value returned after
* calling JNI_GetDefaultJavaVMInitArgs() is the actual JNI version
* supported, and is always higher that the requested version.
*/
vmargs.version = 0x00010001;
if (JRE_GetDefaultJavaVMInitArgs(handle, &vmargs) != 0) {
fprintf(stderr, "Could not initialize Java VM\n");
return NULL;
}
/* Tack on our classpath */
if(MORE_CLASSPATH_COUNT)
{
int i;
int size = strlen(set.classPath) + 1;
char sep[2];
sep[0] = PATH_SEPARATOR;
sep[1] = 0;
for(i = 0; i < MORE_CLASSPATH_COUNT; i++)
size += strlen(more_classpath[i]) + 1;
vmargs.classpath = malloc(size);
if(vmargs.classpath == 0)
{
fprintf(stderr, "malloc error\n");
return NULL;
}
strcpy(vmargs.classpath, set.classPath);
for(i = 0; i < MORE_CLASSPATH_COUNT; i++)
{
strcat(vmargs.classpath, sep);
strcat(vmargs.classpath, more_classpath[i]);
}
}
else
{
vmargs.classpath = set.classPath;
}
/*
* fprintf(stderr, "classpath: %s\n", vmargs.classpath);
*/
/* Set user-defined system properties for Java VM */
if (props != 0) {
if (numProps == maxProps) {
char **tmp = JRE_Malloc((numProps + 1) * sizeof(char **));
memcpy(tmp, props, numProps * sizeof(char **));
free(props);
props = tmp;
}
props[numProps] = 0;
vmargs.properties = props;
}
/* verbose? */
/*
* vmargs.verbose = JNI_TRUE;
*/
/* Load and initialize Java VM */
if (JRE_CreateJavaVM(handle, &jvm, &env, &vmargs) != 0) {
fprintf(stderr, "Could not create Java VM\n");
return NULL;
}
/* Free properties */
if (props != 0) {
free(props);
}
return env;
}
static JSBool
_StartDebuggerFE(JNIEnv* env)
{
jclass clazz;
jmethodID mid;
jarray args;
/* Find class */
clazz = (*env)->FindClass(env, main_class);
if (clazz == 0) {
fprintf(stderr, "Class not found: %s\n", main_class);
return JS_FALSE;
}
/* Find main method of class */
mid = (*env)->GetStaticMethodID(env, clazz, "main",
"([Ljava/lang/String;)V");
if (mid == 0) {
fprintf(stderr, "In class %s: public static void main(String args[])"
" is not defined\n");
return JS_FALSE;
}
/* Invoke main method */
args = NewStringArray(env, params, PARAM_COUNT);
if (args == 0) {
JRE_FatalError(env, "Couldn't build argument list for main\n");
}
(*env)->CallStaticVoidMethod(env, clazz, mid, args);
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
return JS_TRUE;
}
JNIEnv*
jsdj_CreateJavaVMAndStartDebugger(JSDJContext* jsdjc)
{
JNIEnv* env = NULL;
env = _CreateJavaVM();
if( ! env )
return NULL;
jsdj_SetJNIEnvForCurrentThread(jsdjc, env);
if( ! jsdj_RegisterNatives(jsdjc) )
return NULL;
if( ! _StartDebuggerFE(env) )
return NULL;
return env;
}
#endif /* JSD_STANDALONE_JAVA_VM */
/***************************************************************************/
|