Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / src / JavaBundleResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #if(JAVA_SUPPORT)
23     #include "JavaBundleResource.h"
24     #include <jni.h>
25     #include <string.h>
26
27     #include <iostream>
28
29     using namespace OIC::Service;
30     using namespace std;
31
32     #include "oc_logger.hpp"
33
34     using OC::oc_log_stream;
35     using namespace OIC::Service;
36
37     auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
38     {
39         static OC::oc_log_stream ols(oc_make_ostream_logger);
40         static boost::iostreams::stream<OC::oc_log_stream> os(ols);
41         os->set_level(OC_LOG_INFO);
42         os->set_module("JavaBundleResource");
43         return os;
44     };
45
46     auto error_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
47     {
48         static OC::oc_log_stream ols(oc_make_ostream_logger);
49         static boost::iostreams::stream<OC::oc_log_stream> os(ols);
50         os->set_level(OC_LOG_ERROR);
51         os->set_module("JavaBundleResource");
52         return os;
53     };
54
55     JavaBundleResource::JavaBundleResource()
56     {
57
58     }
59
60     void JavaBundleResource::initAttributes()
61     {
62
63     }
64
65     JavaBundleResource::JavaBundleResource(JNIEnv *env, jobject obj, jobject bundleResource,
66             string bundleId, jobjectArray attributes)
67     {
68         int stringCount = env->GetArrayLength(attributes);
69
70         for (int i = 0; i < stringCount; i++)
71         {
72             jstring str = (jstring) env->GetObjectArrayElement(attributes, i);
73             const char *rawString = env->GetStringUTFChars(str, 0);
74             string s(rawString, strlen(rawString));
75             BundleResource::setAttribute(s, "");
76         }
77
78         m_bundleId = bundleId;
79
80         this->bundleResource = env->NewGlobalRef(bundleResource);
81
82         bundleResourceClass = env->GetObjectClass(bundleResource);
83
84         attributeSetter = env->GetMethodID(bundleResourceClass, "setAttribute",
85                 "(Ljava/lang/String;Ljava/lang/String;)V");
86
87         attributeGetter = env->GetMethodID(bundleResourceClass, "getAttribute",
88                 "(Ljava/lang/String;)Ljava/lang/String;");
89
90     }
91
92     JavaBundleResource::~JavaBundleResource()
93     {
94
95     }
96
97     RCSResourceAttributes& JavaBundleResource::getAttributes(){
98         return BundleResource::getAttributes();
99     }
100
101     RCSResourceAttributes::Value JavaBundleResource::getAttribute(const std::string& attributeName)
102     {
103         JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
104
105         JNIEnv * env;
106         int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
107
108         if (envStat == JNI_EDETACHED)
109         {
110             if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
111             {
112                 error_logger() << "Failed to attach " << endl;
113             }
114         }
115         else if (envStat == JNI_EVERSION)
116         {
117             error_logger() << "Env: version not supported " << endl;
118         }
119
120         jstring attrName = env->NewStringUTF(attributeName.c_str());
121
122         jstring returnString = (jstring) env->CallObjectMethod(bundleResource, attributeGetter,
123                 attrName);
124
125         const char *js = env->GetStringUTFChars(returnString, NULL);
126         std::string val(js);
127         RCSResourceAttributes::Value newVal = val;
128         env->ReleaseStringUTFChars(returnString, js);
129         BundleResource::setAttribute(attributeName, newVal.toString());
130         return BundleResource::getAttribute(attributeName);
131     }
132
133     void JavaBundleResource::setAttribute(std::string attributeName, RCSResourceAttributes::Value&& value)
134     {
135         JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
136
137         JNIEnv * env;
138         int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
139
140         if (envStat == JNI_EDETACHED)
141         {
142             if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
143             {
144                 error_logger() << "Failed to attach " << endl;
145             }
146         }
147         else if (envStat == JNI_EVERSION)
148         {
149             error_logger() << "Env: version not supported " << endl;
150         }
151
152         jstring attrName = env->NewStringUTF(attributeName.c_str());
153         jstring val = env->NewStringUTF(value.toString().c_str());
154
155
156         env->CallObjectMethod(bundleResource, attributeSetter, attrName, val);
157         BundleResource::setAttribute(attributeName, std::move(value));
158     }
159 #endif