2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * Set up values for System.getProperties().
22 #include <sys/utsname.h>
27 * Create some storage for properties read from the command line.
29 bool dvmPropertiesStartup(int maxProps)
31 gDvm.maxProps = maxProps;
33 gDvm.propList = (char**) malloc(maxProps * sizeof(char*));
34 if (gDvm.propList == NULL)
45 void dvmPropertiesShutdown(void)
49 for (i = 0; i < gDvm.numProps; i++)
50 free(gDvm.propList[i]);
56 * Add a property specified on the command line. "argStr" has the form
57 * "name=value". "name" must have nonzero length.
59 * Returns "true" if argStr appears valid.
61 bool dvmAddCommandLineProperty(const char* argStr)
66 mangle = strdup(argStr);
67 equals = strchr(mangle, '=');
68 if (equals == NULL || equals == mangle) {
74 assert(gDvm.numProps < gDvm.maxProps);
75 gDvm.propList[gDvm.numProps++] = mangle;
82 * Find the "put" method for this class.
84 * Returns NULL and throws an exception if not found.
86 static Method* getPut(ClassObject* clazz)
90 put = dvmFindVirtualMethodHierByDescriptor(clazz, "setProperty",
91 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
93 dvmThrowException("Ljava/lang/RuntimeException;",
94 "could not find setProperty(String,String) in Properties");
95 /* fall through to return */
101 * Set the value of the property.
103 static void setProperty(Object* propObj, Method* put, const char* key,
106 StringObject* keyStr;
107 StringObject* valueStr;
110 /* unclear what to do; probably want to create prop w/ empty string */
114 keyStr = dvmCreateStringFromCstr(key, ALLOC_DEFAULT);
115 valueStr = dvmCreateStringFromCstr(value, ALLOC_DEFAULT);
116 if (keyStr == NULL || valueStr == NULL) {
117 LOGW("setProperty string creation failed\n");
122 dvmCallMethod(dvmThreadSelf(), put, propObj, &unused, keyStr, valueStr);
125 dvmReleaseTrackedAlloc((Object*) keyStr, NULL);
126 dvmReleaseTrackedAlloc((Object*) valueStr, NULL);
130 * Create the VM-default system properties.
132 * We can do them here, or do them in interpreted code with lots of native
133 * methods to get bits and pieces. This is a bit smaller.
135 void dvmCreateDefaultProperties(Object* propObj)
137 Method* put = getPut(propObj->clazz);
145 /* constant strings that are used multiple times below */
146 const char *projectUrl = "http://www.android.com/";
147 const char *projectName = "The Android Project";
150 * These are listed in the docs.
153 setProperty(propObj, put, "java.boot.class.path", gDvm.bootClassPathStr);
154 setProperty(propObj, put, "java.class.path", gDvm.classPathStr);
155 setProperty(propObj, put, "java.class.version", "46.0");
156 setProperty(propObj, put, "java.compiler", "");
157 setProperty(propObj, put, "java.ext.dirs", "");
159 if (getenv("JAVA_HOME") != NULL) {
160 setProperty(propObj, put, "java.home", getenv("JAVA_HOME"));
162 setProperty(propObj, put, "java.home", "/system");
165 setProperty(propObj, put, "java.io.tmpdir", "/tmp");
166 setProperty(propObj, put, "java.library.path", getenv("LD_LIBRARY_PATH"));
168 setProperty(propObj, put, "java.vendor", projectName);
169 setProperty(propObj, put, "java.vendor.url", projectUrl);
170 setProperty(propObj, put, "java.version", "0");
171 setProperty(propObj, put, "java.vm.name", "Dalvik");
172 setProperty(propObj, put, "java.vm.specification.name",
173 "Dalvik Virtual Machine Specification");
174 setProperty(propObj, put, "java.vm.specification.vendor", projectName);
175 setProperty(propObj, put, "java.vm.specification.version", "0.9");
176 setProperty(propObj, put, "java.vm.vendor", projectName);
179 sprintf(tmpBuf, "%d.%d.%d",
180 DALVIK_MAJOR_VERSION, DALVIK_MINOR_VERSION, DALVIK_BUG_VERSION);
181 setProperty(propObj, put, "java.vm.version", tmpBuf);
183 setProperty(propObj, put, "java.specification.name",
184 "Dalvik Core Library");
185 setProperty(propObj, put, "java.specification.vendor", projectName);
186 setProperty(propObj, put, "java.specification.version", "0.9");
188 #define OS_ARCH generic /* TODO: Use an "arch" header. */
189 #define OS_ARCH_QUOTE(x) #x
190 setProperty(propObj, put, "os.arch", OS_ARCH_QUOTE(OS_ARCH));
194 setProperty(propObj, put, "os.name", info.sysname);
195 setProperty(propObj, put, "os.version", info.release);
196 setProperty(propObj, put, "user.home", getenv("HOME"));
197 setProperty(propObj, put, "user.name", getenv("USER"));
200 setProperty(propObj, put, "user.dir", getcwd(path, sizeof(path)));
202 setProperty(propObj, put, "file.separator", "/");
203 setProperty(propObj, put, "line.separator", "\n");
204 setProperty(propObj, put, "path.separator", ":");
207 * These show up elsewhere, so do them here too.
209 setProperty(propObj, put, "java.runtime.name", "Android Runtime");
210 setProperty(propObj, put, "java.runtime.version", "0.9");
211 setProperty(propObj, put, "java.vm.vendor.url", projectUrl);
213 setProperty(propObj, put, "file.encoding", "UTF-8");
214 setProperty(propObj, put, "user.language", "en");
215 setProperty(propObj, put, "user.region", "US");
218 * These are unique to Android/Dalvik.
220 setProperty(propObj, put, "android.vm.dexfile", "true");
224 * Add anything specified on the command line.
226 void dvmSetCommandLineProperties(Object* propObj)
228 Method* put = getPut(propObj->clazz);
234 for (i = 0; i < gDvm.numProps; i++) {
237 /* value starts after the end of the key string */
238 for (value = gDvm.propList[i]; *value != '\0'; value++)
240 setProperty(propObj, put, gDvm.propList[i], value+1);
245 * Get a property by calling System.getProperty(key).
247 * Returns a newly-allocated string, or NULL on failure or key not found.
248 * (Unexpected failures will also raise an exception.)
250 char* dvmGetProperty(const char* key)
254 StringObject* keyObj = NULL;
255 StringObject* valueObj;
260 system = dvmFindSystemClass("Ljava/lang/System;");
264 getProp = dvmFindDirectMethodByDescriptor(system, "getProperty",
265 "(Ljava/lang/String;)Ljava/lang/String;");
266 if (getProp == NULL) {
267 LOGW("Could not find getProperty(String) in java.lang.System\n");
271 keyObj = dvmCreateStringFromCstr(key, ALLOC_DEFAULT);
276 dvmCallMethod(dvmThreadSelf(), getProp, NULL, &val, keyObj);
277 valueObj = (StringObject*) val.l;
278 if (valueObj == NULL)
281 result = dvmCreateCstrFromString(valueObj);
282 /* fall through with result */
285 dvmReleaseTrackedAlloc((Object*)keyObj, NULL);