Authors: Christian Thalinger
- Changes:
-
- $Id: properties.c 4435 2006-02-04 23:59:54Z twisti $
+ $Id: properties.c 6249 2006-12-27 23:00:59Z twisti $
*/
#include "config.h"
#include <stdlib.h>
+#include <string.h>
+#include <sys/utsname.h>
+#include <time.h>
#include "vm/types.h"
#include "vm/global.h"
#include "native/include/java_lang_String.h"
-#include "native/include/java_util_Properties.h"
+
+#if defined(ENABLE_JAVASE)
+# include "native/include/java_util_Properties.h"
+#endif
+
#include "toolbox/list.h"
+#include "toolbox/util.h"
#include "vm/method.h"
#include "vm/options.h"
+#include "vm/properties.h"
#include "vm/stringlocal.h"
+#include "vm/vm.h"
#include "vm/jit/asmpart.h"
-/* temporary property structure ***********************************************/
+/* internal property structure ************************************************/
typedef struct list_properties_entry list_properties_entry;
static list *list_properties = NULL;
-static java_util_Properties *psystem;
-static methodinfo *mput;
-
/* properties_init *************************************************************
- Initialize the properties list.
+ Initialize the properties list and fill the list with default
+ values.
*******************************************************************************/
bool properties_init(void)
{
- list_properties = NEW(list);
+#if defined(ENABLE_JAVASE)
+ char *cwd;
+ char *env_java_home;
+ char *env_user;
+ char *env_home;
+ char *env_lang;
+ char *java_home;
+ char *extdirs;
+ char *lang;
+ char *country;
+ struct utsname *utsnamebuf;
+ s4 len;
+#endif
- list_init(list_properties, OFFSET(list_properties_entry, linkage));
+ /* create the properties list */
- /* everything's ok */
+ list_properties = list_create(OFFSET(list_properties_entry, linkage));
- return true;
-}
+#if defined(ENABLE_JAVASE)
+ /* get properties from system */
+ cwd = _Jv_getcwd();
+ env_java_home = getenv("JAVA_HOME");
+ env_user = getenv("USER");
+ env_home = getenv("HOME");
+ env_lang = getenv("LANG");
-/* properties_postinit *********************************************************
+ utsnamebuf = NEW(struct utsname);
- Post-initialize the properties. The passed properties table is the
- Java system properties table.
+ uname(utsnamebuf);
-*******************************************************************************/
+ /* set JAVA_HOME to default prefix if not defined */
-bool properties_postinit(java_util_Properties *p)
-{
- /* set global Java system properties pointer */
+ if (env_java_home == NULL)
+ env_java_home = cacao_prefix;
- psystem = p;
+ /* fill in system properties */
- /* search for method to add properties */
+ properties_add("java.version", JAVA_VERSION);
+ properties_add("java.vendor", "GNU Classpath");
+ properties_add("java.vendor.url", "http://www.gnu.org/software/classpath/");
+
+ /* add /jre to java.home property */
+
+ len = strlen(env_java_home) + strlen("/jre") + strlen("0");
+
+ java_home = MNEW(char, len);
- mput = class_resolveclassmethod(p->header.vftbl->class,
- utf_new_char("put"),
- utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
- NULL,
- true);
+ strcpy(java_home, env_java_home);
+ strcat(java_home, "/jre");
- if (mput == NULL)
- return false;
+ properties_add("java.home", java_home);
+
+ properties_add("java.vm.specification.version", "1.0");
+ properties_add("java.vm.specification.vendor", "Sun Microsystems Inc.");
+ properties_add("java.vm.specification.name", "Java Virtual Machine Specification");
+ properties_add("java.vm.version", VERSION);
+ properties_add("java.vm.vendor", "CACAO Team");
+ properties_add("java.vm.name", "CACAO");
+ properties_add("java.specification.version", "1.5");
+ properties_add("java.specification.vendor", "Sun Microsystems Inc.");
+ properties_add("java.specification.name", "Java Platform API Specification");
+ properties_add("java.class.version", CLASS_VERSION);
+ properties_add("java.class.path", _Jv_classpath);
+
+ properties_add("java.runtime.version", VERSION);
+ properties_add("java.runtime.name", "CACAO");
+
+ /* Set bootclasspath properties. One for GNU classpath and the
+ other for compatibility with Sun (required by most
+ applications). */
+
+ properties_add("java.boot.class.path", _Jv_bootclasspath);
+ properties_add("sun.boot.class.path", _Jv_bootclasspath);
+
+#if defined(WITH_STATIC_CLASSPATH)
+ properties_add("gnu.classpath.boot.library.path", ".");
+ properties_add("java.library.path" , ".");
+#else
+ /* fill gnu.classpath.boot.library.path with GNU Classpath library
+ path */
+
+ properties_add("gnu.classpath.boot.library.path", classpath_libdir);
+ properties_add("java.library.path", _Jv_java_library_path);
+#endif
+
+ properties_add("java.io.tmpdir", "/tmp");
+
+#if defined(ENABLE_INTRP)
+ if (opt_intrp) {
+ /* XXX We don't support java.lang.Compiler */
+/* properties_add("java.compiler", "cacao.intrp"); */
+ properties_add("java.vm.info", "interpreted mode");
+ properties_add("gnu.java.compiler.name", "cacao.intrp");
+ }
+ else
+#endif
+ {
+ /* XXX We don't support java.lang.Compiler */
+/* properties_add("java.compiler", "cacao.jit"); */
+ properties_add("java.vm.info", "JIT mode");
+ properties_add("gnu.java.compiler.name", "cacao.jit");
+ }
+
+ /* set the java.ext.dirs property */
+
+ len = strlen(env_java_home) + strlen("/jre/lib/ext") + strlen("0");
+
+ extdirs = MNEW(char, len);
+
+ strcpy(extdirs, env_java_home);
+ strcat(extdirs, "/jre/lib/ext");
+
+ properties_add("java.ext.dirs", extdirs);
+
+ properties_add("java.endorsed.dirs", ""CACAO_PREFIX"/jre/lib/endorsed");
+
+#if defined(DISABLE_GC)
+ /* When we disable the GC, we mmap the whole heap to a specific
+ address, so we can compare call traces. For this reason we have
+ to add the same properties on different machines, otherwise
+ more memory may be allocated (e.g. strlen("i386")
+ vs. strlen("alpha"). */
+
+ properties_add("os.arch", "unknown");
+ properties_add("os.name", "unknown");
+ properties_add("os.version", "unknown");
+#else
+ /* We need to set the os.arch hardcoded to be compatible with SUN. */
+
+# if defined(__I386__)
+ /* map all x86 architectures (i386, i486, i686) to i386 */
+
+ properties_add("os.arch", "i386");
+# elif defined(__POWERPC__)
+ properties_add("os.arch", "ppc");
+# elif defined(__X86_64__)
+ properties_add("os.arch", "amd64");
+# else
+ /* default to what uname returns */
+
+ properties_add("os.arch", utsnamebuf->machine);
+# endif
+
+ properties_add("os.name", utsnamebuf->sysname);
+ properties_add("os.version", utsnamebuf->release);
+#endif
+
+ properties_add("file.separator", "/");
+ properties_add("path.separator", ":");
+ properties_add("line.separator", "\n");
+ properties_add("user.name", env_user ? env_user : "null");
+ properties_add("user.home", env_home ? env_home : "null");
+ properties_add("user.dir", cwd ? cwd : "null");
+
+#if defined(WITH_STATIC_CLASSPATH)
+ /* This is just for debugging purposes and can cause troubles in
+ GNU Classpath. */
+
+ properties_add("gnu.cpu.endian", "unknown");
+#else
+# if WORDS_BIGENDIAN == 1
+ properties_add("gnu.cpu.endian", "big");
+# else
+ properties_add("gnu.cpu.endian", "little");
+# endif
+#endif
+
+ /* get locale */
+
+ if (env_lang != NULL) {
+ /* get the local stuff from the environment */
+
+ if (strlen(env_lang) <= 2) {
+ properties_add("user.language", env_lang);
+ }
+ else {
+ if ((env_lang[2] == '_') && (strlen(env_lang) >= 5)) {
+ lang = MNEW(char, 3);
+ strncpy(lang, (char *) &env_lang[0], 2);
+ lang[2] = '\0';
+
+ country = MNEW(char, 3);
+ strncpy(country, (char *) &env_lang[3], 2);
+ country[2] = '\0';
+
+ properties_add("user.language", lang);
+ properties_add("user.country", country);
+ }
+ }
+ }
+ else {
+ /* if no default locale was specified, use `en_US' */
+
+ properties_add("user.language", "en");
+ properties_add("user.country", "US");
+ }
+#elif defined(ENABLE_JAVAME_CLDC1_1)
+ properties_add("microedition.configuration", "CLDC-1.1");
+ properties_add("microedition.platform", "generic");
+ properties_add("microedition.encoding", "ISO8859_1");
+ properties_add("microedition.profiles", "");
+#else
+#error unknown Java configuration
+#endif
+
+ /* everything's ok */
return true;
}
-/* properties_add **************************************************************
+/* properties_postinit *********************************************************
- Adds a property entry for a command line property definition.
+ Re-set some properties that may have changed during command-line
+ parsing.
*******************************************************************************/
-void properties_add(char *key, char *value)
+bool properties_postinit(void)
{
- list_properties_entry *p;
+#if defined(ENABLE_JAVASE)
+ properties_add("java.class.path", _Jv_classpath);
+ properties_add("java.boot.class.path", _Jv_bootclasspath);
+ properties_add("sun.boot.class.path", _Jv_bootclasspath);
+#endif
- p = NEW(list_properties_entry);
- p->key = key;
- p->value = value;
+ /* everything's ok */
- list_addlast(list_properties, p);
+ return true;
}
-/* get_property ****************************************************************
+/* properties_add **************************************************************
- Get a property entry from a command line property definition.
+ Adds a property entry to the internal property list. If there's
+ already an entry with the same key, replace it.
*******************************************************************************/
-char *properties_get(char *key)
+void properties_add(char *key, char *value)
{
- list_properties_entry *p;
+ list_properties_entry *pe;
+
+ /* search for the entry */
+
+ for (pe = list_first_unsynced(list_properties); pe != NULL;
+ pe = list_next_unsynced(list_properties, pe)) {
+ if (strcmp(pe->key, key) == 0) {
+ /* entry was found, replace the value */
- for (p = list_first(list_properties); p != NULL;
- p = list_next(list_properties, p)) {
- if (strcmp(p->key, key) == 0)
- return p->value;
+ pe->value = value;
+
+ return;
+ }
}
- return NULL;
+ /* entry was not found, insert a new one */
+
+ pe = NEW(list_properties_entry);
+
+ pe->key = key;
+ pe->value = value;
+
+ list_add_last_unsynced(list_properties, pe);
}
-/* properties_system_add *******************************************************
+/* properties_get **************************************************************
- Adds a property to the Java system properties.
+ Get a property entry from the internal property list.
*******************************************************************************/
-void properties_system_add(char *key, char *value)
+char *properties_get(char *key)
{
- java_lang_String *k;
- java_lang_String *v;
+ list_properties_entry *pe;
- k = javastring_new_char(key);
- v = javastring_new_char(value);
+ /* We search backwards, so we get the newest entry for a key, as
+ the list may contain more than one entry for a specific key. */
- ASM_CALLJAVAFUNCTION(mput, psystem, k, v, NULL);
+ for (pe = list_last_unsynced(list_properties); pe != NULL;
+ pe = list_prev_unsynced(list_properties, pe)) {
+ if (strcmp(pe->key, key) == 0)
+ return pe->value;
+ }
+
+ return NULL;
}
/* properties_system_add_all ***************************************************
- Adds a all properties from the properties list to the Java system
+ Adds all properties from the properties list to the Java system
properties.
*******************************************************************************/
-void properties_system_add_all(void)
+#if defined(ENABLE_JAVASE)
+void properties_system_add_all(java_util_Properties *p)
{
- list_properties_entry *p;
+ list_properties_entry *pe;
+ methodinfo *m;
+ java_lang_String *key;
+ java_lang_String *value;
- for (p = list_first(list_properties); p != NULL;
- p = list_first(list_properties)) {
- /* add to the Java system properties */
+ /* search for method to add properties */
+
+ m = class_resolveclassmethod(p->header.vftbl->class,
+ utf_new_char("put"),
+ utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
+ NULL,
+ true);
- properties_system_add(p->key, p->value);
+ if (m == NULL)
+ return;
- /* remove the entry from the list */
+ /* process all properties stored in the internal table */
- list_remove(list_properties, p);
+ for (pe = list_first(list_properties); pe != NULL;
+ pe = list_next(list_properties, pe)) {
+ /* add to the Java system properties */
- /* and free the memory */
+ key = javastring_new_from_utf_string(pe->key);
+ value = javastring_new_from_utf_string(pe->value);
- FREE(p, list_properties_entry);
+ (void) vm_call_method(m, (java_objectheader *) p, key, value);
}
}
+#endif /* defined(ENABLE_JAVASE) */
/*