1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "JavaBundleResource.h"
28 #include "InternalTypes.h"
30 using namespace OIC::Service;
34 JavaBundleResource::JavaBundleResource()
39 void JavaBundleResource::initAttributes()
44 JavaBundleResource::JavaBundleResource(JNIEnv *env, jobject obj, jobject bundleResource,
45 string bundleId, jobjectArray attributes)
48 int stringCount = env->GetArrayLength(attributes);
50 for (int i = 0; i < stringCount; i++)
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, "");
58 m_bundleId = bundleId;
60 this->m_bundleResource = env->NewGlobalRef(bundleResource);
62 m_bundleResourceClass = env->GetObjectClass(bundleResource);
64 m_attributeSetRequestHandler = env->GetMethodID(m_bundleResourceClass,
65 "handleSetAttributeRequest", "(Ljava/lang/String;Ljava/lang/String;)V");
67 m_attributeGetRequestHandler = env->GetMethodID(m_bundleResourceClass,
68 "handleGetAttributeRequest", "(Ljava/lang/String;)Ljava/lang/String;");
72 JavaBundleResource::~JavaBundleResource()
77 RCSResourceAttributes::Value JavaBundleResource::handleGetAttributeRequest(
78 const std::string &attributeName)
80 JavaVM *vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
83 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
85 if (envStat == JNI_EDETACHED)
87 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
89 OC_LOG_V(ERROR, CONTAINER_TAG,
90 "[JavaBundleResource::handleGetAttributeRequest] Failed to attach ");
93 else if (envStat == JNI_EVERSION)
95 OC_LOG_V(ERROR, CONTAINER_TAG,
96 "[JavaBundleResource::handleGetAttributeRequest] Env: version not supported");
99 jstring attrName = env->NewStringUTF(attributeName.c_str());
101 jstring returnString = (jstring) env->CallObjectMethod(m_bundleResource,
102 m_attributeGetRequestHandler, attrName);
104 const char *js = env->GetStringUTFChars(returnString, NULL);
106 RCSResourceAttributes::Value newVal = val;
107 env->ReleaseStringUTFChars(returnString, js);
108 BundleResource::setAttribute(attributeName, newVal.toString());
109 return BundleResource::getAttribute(attributeName);
112 void JavaBundleResource::handleSetAttributeRequest(const std::string &attributeName,
113 RCSResourceAttributes::Value &&value)
115 JavaVM *vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
118 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
120 if (envStat == JNI_EDETACHED)
122 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
124 OC_LOG_V(ERROR, CONTAINER_TAG,
125 "[JavaBundleResource::handleSetAttributeRequest] Failed to attach ");
128 else if (envStat == JNI_EVERSION)
130 OC_LOG_V(ERROR, CONTAINER_TAG,
131 "[JavaBundleResource::handleSetAttributeRequest] Env: version not supported ");
134 jstring attrName = env->NewStringUTF(attributeName.c_str());
135 jstring val = env->NewStringUTF(value.toString().c_str());
138 env->CallObjectMethod(m_bundleResource, m_attributeSetRequestHandler, attrName, val);
139 BundleResource::setAttribute(attributeName, std::move(value));
143 void JavaBundleResource::handleSetAttributesRequest(RCSResourceAttributes &attrs){
144 for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
146 handleSetAttributeRequest(it->key(),std::move(it->value()));
150 RCSResourceAttributes & JavaBundleResource::handleGetAttributesRequest(){
151 RCSResourceAttributes ret;
152 std::list<string> attrsNames = getAttributeNames();
153 for(std::list<string>::iterator iterator = attrsNames.begin();
154 iterator != attrsNames.end(); ++iterator ){
155 ret[*iterator] = handleGetAttributeRequest(*iterator);