Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-container / 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
25 #include <jni.h>
26 #include <string.h>
27 #include <iostream>
28 #include "InternalTypes.h"
29
30 using namespace OIC::Service;
31 using namespace std;
32
33
34 JavaBundleResource::JavaBundleResource()
35 {
36
37 }
38
39 void JavaBundleResource::initAttributes()
40 {
41
42 }
43
44 JavaBundleResource::JavaBundleResource(JNIEnv *env, jobject obj, jobject bundleResource,
45                                        string bundleId, jobjectArray attributes)
46 {
47     (void) obj;
48     int stringCount = env->GetArrayLength(attributes);
49
50     for (int i = 0; i < stringCount; i++)
51     {
52         jstring str = (jstring) env->GetObjectArrayElement(attributes, i);
53         const char *rawString = env->GetStringUTFChars(str, 0);
54         string s(rawString, strlen(rawString));
55         BundleResource::setAttribute(s, "");
56     }
57
58     m_bundleId = bundleId;
59
60     this->m_bundleResource = env->NewGlobalRef(bundleResource);
61
62     m_bundleResourceClass = env->GetObjectClass(bundleResource);
63
64     m_attributeSetRequestHandler = env->GetMethodID(m_bundleResourceClass,
65             "handleSetAttributeRequest", "(Ljava/lang/String;Ljava/lang/String;)V");
66
67     m_attributeGetRequestHandler = env->GetMethodID(m_bundleResourceClass,
68             "handleGetAttributeRequest", "(Ljava/lang/String;)Ljava/lang/String;");
69
70 }
71
72 JavaBundleResource::~JavaBundleResource()
73 {
74
75 }
76
77 RCSResourceAttributes::Value JavaBundleResource::handleGetAttributeRequest(
78         const std::string &attributeName)
79 {
80     JavaVM *vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
81
82     JNIEnv *env;
83     int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
84
85     if (envStat == JNI_EDETACHED)
86     {
87         if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
88         {
89             OC_LOG_V(ERROR, CONTAINER_TAG,
90                     "[JavaBundleResource::handleGetAttributeRequest] Failed to attach ");
91         }
92     }
93     else if (envStat == JNI_EVERSION)
94     {
95         OC_LOG_V(ERROR, CONTAINER_TAG,
96                 "[JavaBundleResource::handleGetAttributeRequest] Env: version not supported");
97     }
98
99     jstring attrName = env->NewStringUTF(attributeName.c_str());
100
101     jstring returnString = (jstring) env->CallObjectMethod(m_bundleResource,
102             m_attributeGetRequestHandler, attrName);
103
104     const char *js = env->GetStringUTFChars(returnString, NULL);
105     std::string val(js);
106     RCSResourceAttributes::Value newVal = val;
107     env->ReleaseStringUTFChars(returnString, js);
108     BundleResource::setAttribute(attributeName, newVal.toString());
109     return BundleResource::getAttribute(attributeName);
110 }
111
112 void JavaBundleResource::handleSetAttributeRequest(const std::string &attributeName,
113                                       RCSResourceAttributes::Value &&value)
114 {
115     JavaVM *vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
116
117     JNIEnv *env;
118     int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
119
120     if (envStat == JNI_EDETACHED)
121     {
122         if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
123         {
124             OC_LOG_V(ERROR, CONTAINER_TAG,
125                     "[JavaBundleResource::handleSetAttributeRequest] Failed to attach ");
126         }
127     }
128     else if (envStat == JNI_EVERSION)
129     {
130         OC_LOG_V(ERROR, CONTAINER_TAG,
131                 "[JavaBundleResource::handleSetAttributeRequest] Env: version not supported ");
132     }
133
134     jstring attrName = env->NewStringUTF(attributeName.c_str());
135     jstring val = env->NewStringUTF(value.toString().c_str());
136
137
138     env->CallObjectMethod(m_bundleResource, m_attributeSetRequestHandler, attrName, val);
139     BundleResource::setAttribute(attributeName, std::move(value));
140 }
141
142
143 void JavaBundleResource::handleSetAttributesRequest(RCSResourceAttributes &attrs){
144     for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
145     {
146         handleSetAttributeRequest(it->key(),std::move(it->value()));
147     }
148 }
149
150 RCSResourceAttributes & JavaBundleResource::handleGetAttributesRequest()
151 {
152     std::list<string> attrsNames = getAttributeNames();
153     for(std::list<string>::iterator iterator = attrsNames.begin();
154             iterator != attrsNames.end(); ++iterator )
155     {
156         handleGetAttributeRequest(*iterator);
157     }
158     return BundleResource::getAttributes();
159 }
160 #endif