java_lang_System.cpp revision 679cf68b607e9b4a3beb8bcdee06868ae583386f
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 "ScopedUtfChars.h"
22#include "cutils/log.h"
23#include "openssl/opensslv.h"
24#include "toStringArray.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 <unistd.h>
34
35static void System_log(JNIEnv* env, jclass, jchar type, jstring javaMessage, jthrowable exception) {
36    ScopedUtfChars message(env, javaMessage);
37    if (message.c_str() == NULL) {
38        // Since this function is used for last-gasp debugging output, be noisy on failure.
39        ALOGE("message.c_str() == NULL");
40        return;
41    }
42    int priority;
43    switch (type) {
44    case 'D': case 'd': priority = ANDROID_LOG_DEBUG;   break;
45    case 'E': case 'e': priority = ANDROID_LOG_ERROR;   break;
46    case 'F': case 'f': priority = ANDROID_LOG_FATAL;   break;
47    case 'I': case 'i': priority = ANDROID_LOG_INFO;    break;
48    case 'S': case 's': priority = ANDROID_LOG_SILENT;  break;
49    case 'V': case 'v': priority = ANDROID_LOG_VERBOSE; break;
50    case 'W': case 'w': priority = ANDROID_LOG_WARN;    break;
51    default:            priority = ANDROID_LOG_DEFAULT; break;
52    }
53    LOG_PRI(priority, LOG_TAG, "%s", message.c_str());
54    if (exception != NULL) {
55        jniLogException(env, priority, LOG_TAG, exception);
56    }
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    char path[PATH_MAX];
78    properties.push_back(std::string("user.dir=") + getcwd(path, sizeof(path)));
79
80    properties.push_back("android.zlib.version=" ZLIB_VERSION);
81    properties.push_back("android.openssl.version=" OPENSSL_VERSION_TEXT);
82
83    return toStringArray(env, properties);
84}
85
86static JNINativeMethod gMethods[] = {
87    NATIVE_METHOD(System, log, "(CLjava/lang/String;Ljava/lang/Throwable;)V"),
88    NATIVE_METHOD(System, setFieldImpl, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"),
89    NATIVE_METHOD(System, specialProperties, "()[Ljava/lang/String;"),
90};
91int register_java_lang_System(JNIEnv* env) {
92    return jniRegisterNativeMethods(env, "java/lang/System", gMethods, NELEM(gMethods));
93}
94