1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) INRIA - Allan CORNET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 /*--------------------------------------------------------------------------*/
17 #include <jni.h>
18 #include <string.h>
19 #include "system_getproperty.h"
20 #include "getScilabJNIEnv.h"
21 #include "sci_malloc.h"
22 #include "os_string.h"
23 /*--------------------------------------------------------------------------*/
system_getproperty(const char * property,const char * defaultproperty)24 char * system_getproperty(const char * property, const char * defaultproperty)
25 {
26     char *retValue = NULL;
27     JNIEnv * currentENV = getScilabJNIEnv();
28 
29     if (currentENV)
30     {
31         jclass cls = NULL;
32         cls = (*currentENV)->FindClass(currentENV, "java/lang/System");
33         if (cls)
34         {
35             jmethodID mid = NULL;
36             mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "getProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
37             if (mid)
38             {
39                 const char *strValue = NULL;
40                 jstring jstrProperty;
41                 jstring jstrDefaultValue;
42                 jstring jstrValue;
43                 jstrProperty = (*currentENV)->NewStringUTF(currentENV, property);
44                 jstrDefaultValue = (*currentENV)->NewStringUTF(currentENV, defaultproperty);
45 
46                 jstrValue = (*currentENV)->CallStaticObjectMethod(currentENV, cls, mid, jstrProperty, jstrDefaultValue);
47 
48                 strValue = (*currentENV)->GetStringUTFChars(currentENV, jstrValue, 0);
49                 if (strValue)
50                 {
51                     retValue = os_strdup(strValue);
52                 }
53                 (*currentENV)->ReleaseStringUTFChars(currentENV, jstrValue , strValue);
54             }
55         }
56     }
57     return retValue;
58 }
59 /*--------------------------------------------------------------------------*/
60