Set java.version to 1.4
[cacao.git] / src / native / vm / VMSystemProperties.c
1 /* native/vm/VMSystemProperties.c - gnu/classpath/VMSystemProperties
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: VMSystemProperties.c 1906 2005-02-09 19:43:24Z twisti $
32
33 */
34
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/utsname.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include "config.h"
43 #include "cacao/cacao.h"
44 #include "mm/memory.h"
45 #include "native/jni.h"
46 #include "native/native.h"
47 #include "native/include/java_util_Properties.h"
48 #include "toolbox/logging.h"
49 #include "vm/exceptions.h"
50 #include "vm/builtin.h"
51 #include "vm/loader.h"
52 #include "vm/tables.h"
53 #include "vm/jit/asmpart.h"
54
55
56 /* temporary property structure */
57
58 typedef struct property property;
59
60 struct property {
61         char     *key;
62         char     *value;
63         property *next;
64 };
65
66 static property *properties = NULL;
67
68
69 /* create_property *************************************************************
70
71    Create a property entry for a command line property definition.
72
73 *******************************************************************************/
74
75 void create_property(char *key, char *value)
76 {
77         property *p;
78
79         p = NEW(property);
80         p->key = key;
81         p->value = value;
82         p->next = properties;
83         properties = p;
84 }
85
86
87 /* insert_property *************************************************************
88
89    Used for inserting a property into the system's properties table. Method m
90    (usually put) and the properties table must be given.
91
92 *******************************************************************************/
93
94 static void insert_property(methodinfo *m, java_util_Properties *p, char *key,
95                                                         char *value)
96 {
97         asm_calljavafunction(m,
98                                                  p,
99                                                  javastring_new_char(key),
100                                                  javastring_new_char(value),
101                                                  NULL);
102 }
103
104
105 /*
106  * Class:     gnu/classpath/VMSystemProperties
107  * Method:    preInit
108  * Signature: (Ljava/util/Properties;)V
109  */
110 JNIEXPORT void JNICALL Java_gnu_classpath_VMSystemProperties_preInit(JNIEnv *env, jclass clazz, java_util_Properties *p)
111 {
112
113 #define BUFFERSIZE 200
114         methodinfo *m;
115         char cwd[BUFFERSIZE];
116         char *java_home;
117         char *user;
118         char *home;
119         char *locale;
120         char *lang;
121         char *region;
122         struct utsname utsnamebuf;
123
124         union {
125                 u4 i;
126                 u1 c[4];
127         } u;
128
129 #if !defined(STATIC_CLASSPATH)
130         char *libpath;
131         s4    libpathlen;
132 #endif
133
134         if (!p) {
135                 *exceptionptr = new_nullpointerexception();
136                 return;
137         }
138
139         /* get properties from system */
140
141         (void) getcwd(cwd, BUFFERSIZE);
142         java_home = getenv("JAVA_HOME");
143         user = getenv("USER");
144         home = getenv("HOME");
145         uname(&utsnamebuf);
146
147         /* search for method to add properties */
148
149         m = class_resolveclassmethod(p->header.vftbl->class,
150                                                                  utf_new_char("put"),
151                                                                  utf_new_char("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"),
152                                                                  clazz,
153                                                                  true);
154
155         if (!m)
156                 return;
157
158         insert_property(m, p, "java.version", "1.4");
159         insert_property(m, p, "java.vendor", "CACAO Team");
160         insert_property(m, p, "java.vendor.url", "http://www.cacaojvm.org/");
161         insert_property(m, p, "java.home", java_home ? java_home : CACAO_INSTALL_PREFIX);
162         insert_property(m, p, "java.vm.specification.version", "1.0");
163         insert_property(m, p, "java.vm.specification.vendor", "Sun Microsystems Inc.");
164         insert_property(m, p, "java.vm.specification.name", "Java Virtual Machine Specification");
165         insert_property(m, p, "java.vm.version", VERSION);
166         insert_property(m, p, "java.vm.vendor", "CACAO Team");
167         insert_property(m, p, "java.vm.name", "CACAO");
168         insert_property(m, p, "java.specification.version", "1.4");
169         insert_property(m, p, "java.specification.vendor", "Sun Microsystems Inc.");
170         insert_property(m, p, "java.specification.name", "Java Platform API Specification");
171         insert_property(m, p, "java.class.version", "48.0");
172         insert_property(m, p, "java.class.path", classpath);
173
174         /* Set bootclasspath properties. One for GNU classpath and the other for  */
175         /* compatibility with Sun (required by most applications).                */
176         insert_property(m, p, "java.boot.class.path", bootclasspath);
177         insert_property(m, p, "sun.boot.class.path", bootclasspath);
178
179 #if defined(STATIC_CLASSPATH)
180         insert_property(m, p, "java.library.path" , ".");
181 #else
182         libpathlen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_LIBRARY_PATH) + 1;
183
184         if (getenv("CACAO_LIB_OVERRIDE"))
185                 libpathlen += strlen(getenv("CACAO_LIB_OVERRIDE")) + 1;
186
187         if (getenv("LD_LIBRARY_PATH"))
188                 libpathlen += strlen(getenv("LD_LIBRARY_PATH")) + 1;
189
190         libpath = MNEW(char, libpathlen);
191
192         if (getenv("CACAO_LIB_OVERRIDE")) {
193                 strcat(libpath, getenv("CACAO_LIB_OVERRIDE"));
194                 strcat(libpath, ":");
195         }
196
197         strcat(libpath, CACAO_INSTALL_PREFIX);
198         strcat(libpath, CACAO_LIBRARY_PATH);
199
200         if (getenv("LD_LIBRARY_PATH")) {
201                 strcat(libpath, ":");
202                 strcat(libpath, getenv("LD_LIBRARY_PATH"));
203         }
204         insert_property(m, p, "java.library.path", libpath);
205
206         MFREE(libpath, char, libpathlen);
207 #endif
208
209         insert_property(m, p, "java.io.tmpdir", "/tmp");
210
211         /* XXX We don't support java.lang.Compiler */
212 /*      insert_property(m, p, "java.compiler", "cacao.jit"); */
213
214         insert_property(m, p, "java.ext.dirs", CACAO_INSTALL_PREFIX""CACAO_EXT_DIR);
215         insert_property(m, p, "os.name", utsnamebuf.sysname);
216         insert_property(m, p, "os.arch", utsnamebuf.machine);
217         insert_property(m, p, "os.version", utsnamebuf.release);
218         insert_property(m, p, "file.separator", "/");
219         /* insert_property(m, p, "file.encoding", "null"); -- this must be set properly */
220         insert_property(m, p, "path.separator", ":");
221         insert_property(m, p, "line.separator", "\n");
222         insert_property(m, p, "user.name", user ? user : "null");
223         insert_property(m, p, "user.home", home ? home : "null");
224         insert_property(m, p, "user.dir", cwd ? cwd : "null");
225
226         /* Are we little or big endian? */
227         u.i = 1;
228         insert_property(m, p, "gnu.cpu.endian", u.c[0] ? "little" : "big");
229
230 #ifdef USE_GTK
231         /* disable gthread-jni's portable native sync due to yet unresolved 
232            threading issues */
233         insert_property(m, p, "gnu.classpath.awt.gtk.portable.native.sync", "false");
234 #endif
235
236
237         /* get locales */
238         locale = getenv("LANG");
239         if (locale != NULL) { /* gnu classpath is going to set en as language */
240                 if (strlen(locale) <= 2) {
241                         insert_property(m, p, "user.language", locale);
242                 } else {
243                         if ((locale[2]=='_')&&(strlen(locale)>=5)) {
244                                 lang = MNEW(char, 3);
245                                 strncpy(lang, (char*)&locale[0], 2);
246                                 lang[2]='\0';
247                                 region = MNEW(char, 3);
248                                 strncpy(region, (char*)&locale[3], 2);
249                                 region[2]='\0';
250                                 insert_property(m, p, "user.language", lang);
251                                 insert_property(m, p, "user.region", region);
252                         }
253                 }
254         }
255         
256         
257 #if 0
258         /* how do we get them? */
259         { "user.country", "US" },
260         { "user.timezone", "Europe/Vienna" },
261
262         /* XXX do we need this one? */
263         { "java.protocol.handler.pkgs", "gnu.java.net.protocol"}
264 #endif
265         insert_property(m, p, "java.protocol.handler.pkgs", "gnu.java.net.protocol");
266
267         /* insert properties defined on commandline */
268
269         while (properties) {
270                 property *tp;
271
272                 insert_property(m, p, properties->key, properties->value);
273
274                 tp = properties;
275                 properties = properties->next;
276                 FREE(tp, property);
277         }
278
279         return;
280 }
281
282
283 /*
284  * These are local overrides for various environment variables in Emacs.
285  * Please do not remove this and leave it at the end of the file, where
286  * Emacs will automagically detect them.
287  * ---------------------------------------------------------------------
288  * Local variables:
289  * mode: c
290  * indent-tabs-mode: t
291  * c-basic-offset: 4
292  * tab-width: 4
293  * End:
294  */