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_setproperty.h"
20 #include "getScilabJNIEnv.h"
21 #include "sci_malloc.h"
22 #include "catchIfJavaException.h"
23 #include "os_string.h"
24 /*--------------------------------------------------------------------------*/
system_setproperty(char * property,char * value)25 char * system_setproperty(char *property, char *value)
26 {
27     char *retValue = NULL;
28 
29     JNIEnv * currentENV = getScilabJNIEnv();
30 
31     if (currentENV)
32     {
33         jclass cls = NULL;
34         cls = (*currentENV)->FindClass(currentENV, "java/lang/System");
35         if (cls)
36         {
37             jmethodID mid = NULL;
38             mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
39             if (mid)
40             {
41                 BOOL bOK = FALSE;
42                 const char *strPreviousValue = NULL;
43                 jstring jstrProperty;
44                 jstring jstrValue;
45                 jstring jstrPreviousValue;
46 
47                 jstrProperty = (*currentENV)->NewStringUTF(currentENV, property);
48                 jstrValue = (*currentENV)->NewStringUTF(currentENV, value);
49 
50                 jstrPreviousValue = (*currentENV)->CallStaticObjectMethod(currentENV, cls, mid, jstrProperty, jstrValue);
51                 bOK = catchIfJavaException("");
52 
53                 if (bOK)
54                 {
55                     if (jstrPreviousValue)
56                     {
57                         strPreviousValue = (*currentENV)->GetStringUTFChars(currentENV, jstrPreviousValue, 0);
58                         if (strPreviousValue)
59                         {
60                             retValue = os_strdup(strPreviousValue);
61                         }
62                         (*currentENV)->ReleaseStringUTFChars(currentENV, jstrPreviousValue , strPreviousValue);
63                     }
64                 }
65             }
66         }
67     }
68     return retValue;
69 }
70 /*--------------------------------------------------------------------------*/
71