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.net.preferIPv6Addresses", "true");
170 setProperty(propObj, put, "java.vendor", projectName);
171 setProperty(propObj, put, "java.vendor.url", projectUrl);
172 setProperty(propObj, put, "java.version", "0");
173 setProperty(propObj, put, "java.vm.name", "Dalvik");
174 setProperty(propObj, put, "java.vm.specification.name",
175 "Dalvik Virtual Machine Specification");
176 setProperty(propObj, put, "java.vm.specification.vendor", projectName);
177 setProperty(propObj, put, "java.vm.specification.version", "0.9");
178 setProperty(propObj, put, "java.vm.vendor", projectName);
181 sprintf(tmpBuf, "%d.%d.%d",
182 DALVIK_MAJOR_VERSION, DALVIK_MINOR_VERSION, DALVIK_BUG_VERSION);
183 setProperty(propObj, put, "java.vm.version", tmpBuf);
185 setProperty(propObj, put, "java.specification.name",
186 "Dalvik Core Library");
187 setProperty(propObj, put, "java.specification.vendor", projectName);
188 setProperty(propObj, put, "java.specification.version", "0.9");
190 setProperty(propObj, put, "os.arch", info.machine);
191 setProperty(propObj, put, "os.name", info.sysname);
192 setProperty(propObj, put, "os.version", info.release);
193 setProperty(propObj, put, "user.home", getenv("HOME"));
194 setProperty(propObj, put, "user.name", getenv("USER"));
197 setProperty(propObj, put, "user.dir", getcwd(path, sizeof(path)));
199 setProperty(propObj, put, "file.separator", "/");
200 setProperty(propObj, put, "line.separator", "\n");
201 setProperty(propObj, put, "path.separator", ":");
204 * These show up elsewhere, so do them here too.
206 setProperty(propObj, put, "java.runtime.name", "Android Runtime");
207 setProperty(propObj, put, "java.runtime.version", "0.9");
208 setProperty(propObj, put, "java.vm.vendor.url", projectUrl);
210 setProperty(propObj, put, "file.encoding", "UTF-8");
211 setProperty(propObj, put, "user.language", "en");
212 setProperty(propObj, put, "user.region", "US");
215 * These are unique to Android/Dalvik.
217 setProperty(propObj, put, "android.vm.dexfile", "true");
221 * Add anything specified on the command line.
223 void dvmSetCommandLineProperties(Object* propObj)
225 Method* put = getPut(propObj->clazz);
231 for (i = 0; i < gDvm.numProps; i++) {
234 /* value starts after the end of the key string */
235 for (value = gDvm.propList[i]; *value != '\0'; value++)
237 setProperty(propObj, put, gDvm.propList[i], value+1);
242 * Get a property by calling System.getProperty(key).
244 * Returns a newly-allocated string, or NULL on failure or key not found.
245 * (Unexpected failures will also raise an exception.)
247 char* dvmGetProperty(const char* key)
251 StringObject* keyObj = NULL;
252 StringObject* valueObj;
257 system = dvmFindSystemClass("Ljava/lang/System;");
261 getProp = dvmFindDirectMethodByDescriptor(system, "getProperty",
262 "(Ljava/lang/String;)Ljava/lang/String;");
263 if (getProp == NULL) {
264 LOGW("Could not find getProperty(String) in java.lang.System\n");
268 keyObj = dvmCreateStringFromCstr(key, ALLOC_DEFAULT);
273 dvmCallMethod(dvmThreadSelf(), getProp, NULL, &val, keyObj);
274 valueObj = (StringObject*) val.l;
275 if (valueObj == NULL)
278 result = dvmCreateCstrFromString(valueObj);
279 /* fall through with result */
282 dvmReleaseTrackedAlloc((Object*)keyObj, NULL);