java_lang_System.cpp revision 2ef68714ff84d43705f57f724fbcb1a28ebd954d
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#define LOG_TAG "System"
18
19#include "JNIHelp.h"
20#include "JniConstants.h"
21#include "ScopedLocalRef.h"
22#include "ScopedUtfChars.h"
23
24#include "openssl/opensslv.h"
25#include "zlib.h"
26
27#include <string>
28#include <vector>
29
30#include <limits.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/utsname.h>
34#include <unistd.h>
35
36static jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
37    jobjectArray result = env->NewObjectArray(strings.size(), JniConstants::stringClass, NULL);
38    for (size_t i = 0; i < strings.size(); ++i) {
39        ScopedLocalRef<jstring> s(env, env->NewStringUTF(strings[i].c_str()));
40        env->SetObjectArrayElement(result, i, s.get());
41    }
42    return result;
43}
44
45static jstring System_getEnvByName(JNIEnv* env, jclass, jstring javaName) {
46    ScopedUtfChars name(env, javaName);
47    if (name.c_str() == NULL) {
48        return NULL;
49    }
50    return env->NewStringUTF(getenv(name.c_str()));
51}
52
53static jstring System_getEnvByIndex(JNIEnv* env, jclass, jint index) {
54    // Pointer to complete environment.
55    extern char** environ;
56    return env->NewStringUTF(environ[index]);
57}
58
59// Sets a field via JNI. Used for the standard streams, which are read-only otherwise.
60static void System_setFieldImpl(JNIEnv* env, jclass clazz,
61        jstring javaName, jstring javaSignature, jobject object) {
62    ScopedUtfChars name(env, javaName);
63    if (name.c_str() == NULL) {
64        return;
65    }
66    ScopedUtfChars signature(env, javaSignature);
67    if (signature.c_str() == NULL) {
68        return;
69    }
70    jfieldID fieldID = env->GetStaticFieldID(clazz, name.c_str(), signature.c_str());
71    env->SetStaticObjectField(clazz, fieldID, object);
72}
73
74static jobjectArray System_specialProperties(JNIEnv* env, jclass) {
75    std::vector<std::string> properties;
76
77    struct utsname info;
78    uname(&info);
79    properties.push_back(std::string("os.arch=") + info.machine);
80    properties.push_back(std::string("os.name=") + info.sysname);
81    properties.push_back(std::string("os.version=") + info.release);
82
83    char path[PATH_MAX];
84    properties.push_back(std::string("user.dir=") + getcwd(path, sizeof(path)));
85
86    properties.push_back("android.zlib.version=" ZLIB_VERSION);
87    properties.push_back("android.openssl.version=" OPENSSL_VERSION_TEXT);
88
89    return toStringArray(env, properties);
90}
91
92static JNINativeMethod gMethods[] = {
93    NATIVE_METHOD(System, getEnvByIndex, "(I)Ljava/lang/String;"),
94    NATIVE_METHOD(System, getEnvByName, "(Ljava/lang/String;)Ljava/lang/String;"),
95    NATIVE_METHOD(System, setFieldImpl, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"),
96    NATIVE_METHOD(System, specialProperties, "()[Ljava/lang/String;"),
97};
98int register_java_lang_System(JNIEnv* env) {
99    return jniRegisterNativeMethods(env, "java/lang/System", gMethods, NELEM(gMethods));
100}
101