home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / launcher / java_md.c next >
Encoding:
C/C++ Source or Header  |  1999-05-28  |  6.4 KB  |  254 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)java_md.c    1.9 98/06/29
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. #include <windows.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20.  
  21. #include <jni.h>
  22. #include "java.h"
  23.  
  24. #ifdef DEBUG
  25. #define JVM_DLL "jvm_g.dll"
  26. #define JAVA_DLL "java_g.dll"
  27. #else
  28. #define JVM_DLL "jvm.dll"
  29. #define JAVA_DLL "java.dll"
  30. #endif
  31.  
  32. /*
  33.  * Prototypes.
  34.  */
  35. static jboolean
  36. GetPublicJREHome(char *path, jint pathsize);
  37.  
  38. /*
  39.  * Load JVM of "jvmtype", and intialize the invocation functions.  Notice that
  40.  * if jvmtype is NULL, we try to load hotspot VM as the default.  Maybe we
  41.  * need an environment variable that dictates the choice of default VM.
  42.  */
  43. jboolean
  44. LoadJavaVM(char *jvmtype, InvocationFunctions *ifn)
  45. {
  46.     char home[MAXPATHLEN], javadll[MAXPATHLEN], jvmdll[MAXPATHLEN];
  47.     HINSTANCE handle;
  48.     struct stat s;
  49.  
  50.     /* Is JRE co-located with the application? */
  51.     if (GetApplicationHome(home, sizeof(home))) {
  52.     sprintf(javadll, "%s\\bin\\" JAVA_DLL, home);
  53.     if (stat(javadll, &s) == 0)
  54.         goto jrefound;
  55.     }
  56.     /* Does this app ship a private JRE in <apphome>\jre directory? */
  57.     sprintf(javadll, "%s\\jre\\bin\\" JAVA_DLL, home);
  58.     if (stat(javadll, &s) == 0) {
  59.     strcat(home, "\\jre");
  60.     goto jrefound;
  61.     }
  62.     /* Look for a public JRE on this machine. */
  63.     if (!GetPublicJREHome(home, sizeof(home))) {
  64.     return JNI_FALSE;
  65.     }
  66.  
  67.     /* Now we know where JRE is -- the value in the "home" variable. */
  68. jrefound:
  69.     /* Determine if Hotspot VM is installed. */
  70.     if (jvmtype == NULL) {
  71.     sprintf(jvmdll, "%s\\bin\\hotspot\\" JVM_DLL, home);
  72.     if (stat(jvmdll, &s) < 0) {
  73.         jvmtype = "classic";
  74.     } else {
  75.         jvmtype = "hotspot";
  76.     }
  77.     }
  78.  
  79.     /* We now know what jvmtype should be */
  80.     sprintf(jvmdll, "%s\\bin\\%s\\" JVM_DLL, home, jvmtype);
  81.     if (debug) {
  82.     printf("Path to JVM is %s\n", jvmdll);
  83.     }
  84.  
  85.     /* Load the Java VM DLL */
  86.     if ((handle = LoadLibrary(jvmdll)) == 0) {
  87.     fprintf(stderr, "Error loading: %s\n", jvmdll);
  88.     return JNI_FALSE;
  89.     }
  90.  
  91.     /* Now get the function addresses */
  92.     ifn->CreateJavaVM =
  93.     (void *)GetProcAddress(handle, "JNI_CreateJavaVM");
  94.     ifn->GetDefaultJavaVMInitArgs =
  95.     (void *)GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs");
  96.     if (ifn->CreateJavaVM == 0 ||
  97.     ifn->GetDefaultJavaVMInitArgs == 0) {
  98.     fprintf(stderr, "Can't find JNI interfaces in: %s\n", jvmdll);
  99.     return JNI_FALSE;
  100.     }
  101.  
  102.     return JNI_TRUE;
  103. }
  104.  
  105. /*
  106.  * Get the path to the file that has the usage message for -X options.
  107.  */
  108. void
  109. GetXUsagePath(char *buf, jint bufsize)
  110. {
  111.     GetModuleFileName(GetModuleHandle(JVM_DLL), buf, bufsize);
  112.     *(strrchr(buf, '\\')) = '\0';
  113.     strcat(buf, "\\Xusage.txt");
  114. }
  115.  
  116. /*
  117.  * If app is "c:\foo\bin\javac", then put "c:\foo" into buf.
  118.  */
  119. jboolean
  120. GetApplicationHome(char *buf, jint bufsize)
  121. {
  122.     char *cp;
  123.     GetModuleFileName(0, buf, bufsize);
  124.     *strrchr(buf, '\\') = '\0'; /* remove .exe file name */
  125.     if ((cp = strrchr(buf, '\\')) == 0) {
  126.     /* This happens if the application is in a drive root, and
  127.      * there is no bin directory. */
  128.     buf[0] = '\0';
  129.     return JNI_FALSE;
  130.     }
  131.     *cp = '\0';  /* remove the bin\ part */
  132.     return JNI_TRUE;
  133. }
  134.  
  135. #ifdef JAVAW
  136. __declspec(dllimport) char **__initenv;
  137.  
  138. int WINAPI
  139. WinMain(HINSTANCE inst, HINSTANCE previnst, LPSTR cmdline, int cmdshow)
  140. {
  141.     __initenv = _environ;
  142.     return main(__argc, __argv);
  143. }
  144. #endif
  145.  
  146. /*
  147.  * Helpers to look in the registry for a public JRE.
  148.  */
  149. #define DOTRELEASE  "1.2" /* Same for 1.2.1, 1.2.2 etc. */
  150. #define JRE_KEY        "Software\\JavaSoft\\Java Runtime Environment"
  151.  
  152. static jboolean
  153. GetStringFromRegistry(HKEY key, const char *name, char *buf, jint bufsize)
  154. {
  155.     DWORD type, size;
  156.  
  157.     if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0
  158.     && type == REG_SZ
  159.     && (size < (unsigned int)bufsize)) {
  160.     if (RegQueryValueEx(key, name, 0, 0, buf, &size) == 0) {
  161.         return JNI_TRUE;
  162.     }
  163.     }
  164.     return JNI_FALSE;
  165. }
  166.  
  167. static jboolean
  168. GetPublicJREHome(char *buf, jint bufsize)
  169. {
  170.     HKEY key, subkey;
  171.     char version[MAXPATHLEN];
  172.  
  173.     /* Find the current version of the JRE */
  174.     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) != 0) {
  175.     fprintf(stderr, "Error opening registry key '" JRE_KEY "'\n");
  176.     return JNI_FALSE;
  177.     }
  178.  
  179.     if (!GetStringFromRegistry(key, "CurrentVersion",
  180.                    version, sizeof(version))) {
  181.     fprintf(stderr, "Failed reading value of registry key:\n\t"
  182.         JRE_KEY "\\CurrentVersion\n");
  183.     RegCloseKey(key);
  184.     return JNI_FALSE;
  185.     }
  186.  
  187.     if (strcmp(version, DOTRELEASE) != 0) {
  188.     fprintf(stderr, "Registry key '" JRE_KEY "\\CurrentVersion'\nhas "
  189.         "value '%s', but '" DOTRELEASE "' is required.\n", version);
  190.     RegCloseKey(key);
  191.     return JNI_FALSE;
  192.     }
  193.  
  194.     /* Find directory where the current version is installed. */
  195.     if (RegOpenKeyEx(key, version, 0, KEY_READ, &subkey) != 0) {
  196.     fprintf(stderr, "Error opening registry key '"
  197.         JRE_KEY "\\%s'\n", version);
  198.     RegCloseKey(key);
  199.     return JNI_FALSE;
  200.     }
  201.  
  202.     if (!GetStringFromRegistry(subkey, "JavaHome", buf, bufsize)) {
  203.     fprintf(stderr, "Failed reading value of registry key:\n\t"
  204.         JRE_KEY "\\%s\\JavaHome\n", version);
  205.     RegCloseKey(key);
  206.     RegCloseKey(subkey);
  207.     return JNI_FALSE;
  208.     }
  209.  
  210.     if (debug) {
  211.     char micro[MAXPATHLEN];
  212.     if (!GetStringFromRegistry(subkey, "MicroVersion", micro,
  213.                    sizeof(micro))) {
  214.         printf("Warning: Can't read MicroVersion\n");
  215.         micro[0] = '\0';
  216.     }
  217.     printf("Version major.minor.micro = %s.%s\n", version, micro);
  218.     }
  219.  
  220.     RegCloseKey(key);
  221.     RegCloseKey(subkey);
  222.     return JNI_TRUE;
  223. }
  224.  
  225. /*
  226.  * Support for doing cheap, accurate interval timing.
  227.  */
  228. static jboolean counterAvailable = JNI_FALSE;
  229. static jboolean counterInitialized = JNI_FALSE;
  230. static LARGE_INTEGER counterFrequency;
  231.  
  232. jlong CounterGet()
  233. {
  234.     LARGE_INTEGER count;
  235.  
  236.     if (!counterInitialized) {
  237.     counterAvailable = QueryPerformanceFrequency(&counterFrequency);
  238.     counterInitialized = JNI_TRUE;
  239.     }
  240.     if (!counterAvailable) {
  241.     return 0;
  242.     }
  243.     QueryPerformanceCounter(&count);
  244.     return (jlong)(count.QuadPart);
  245. }
  246.  
  247. jlong Counter2Micros(jlong counts)
  248. {
  249.     if (!counterAvailable || !counterInitialized) {
  250.     return 0;
  251.     }
  252.     return (counts * 1000 * 1000)/counterFrequency.QuadPart;
  253. }
  254.