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"
29 using namespace OIC::Service;
32 #include "oc_logger.hpp"
34 using OC::oc_log_stream;
35 using namespace OIC::Service;
37 auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
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");
46 auto error_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
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");
55 JavaBundleResource::JavaBundleResource()
60 void JavaBundleResource::initAttributes()
65 JavaBundleResource::JavaBundleResource(JNIEnv *env, jobject obj, jobject bundleResource,
66 string bundleId, jobjectArray attributes)
68 int stringCount = env->GetArrayLength(attributes);
70 for (int i = 0; i < stringCount; i++)
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, "");
78 m_bundleId = bundleId;
80 this->bundleResource = env->NewGlobalRef(bundleResource);
82 bundleResourceClass = env->GetObjectClass(bundleResource);
84 attributeSetter = env->GetMethodID(bundleResourceClass, "setAttribute",
85 "(Ljava/lang/String;Ljava/lang/String;)V");
87 attributeGetter = env->GetMethodID(bundleResourceClass, "getAttribute",
88 "(Ljava/lang/String;)Ljava/lang/String;");
92 JavaBundleResource::~JavaBundleResource()
97 RCSResourceAttributes& JavaBundleResource::getAttributes(){
98 return BundleResource::getAttributes();
101 RCSResourceAttributes::Value JavaBundleResource::getAttribute(const std::string& attributeName)
103 JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
106 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
108 if (envStat == JNI_EDETACHED)
110 if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
112 error_logger() << "Failed to attach " << endl;
115 else if (envStat == JNI_EVERSION)
117 error_logger() << "Env: version not supported " << endl;
120 jstring attrName = env->NewStringUTF(attributeName.c_str());
122 jstring returnString = (jstring) env->CallObjectMethod(bundleResource, attributeGetter,
125 const char *js = env->GetStringUTFChars(returnString, NULL);
127 RCSResourceAttributes::Value newVal = val;
128 env->ReleaseStringUTFChars(returnString, js);
129 BundleResource::setAttribute(attributeName, newVal.toString());
130 return BundleResource::getAttribute(attributeName);
133 void JavaBundleResource::setAttribute(std::string attributeName, RCSResourceAttributes::Value&& value)
135 JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
138 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
140 if (envStat == JNI_EDETACHED)
142 if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
144 error_logger() << "Failed to attach " << endl;
147 else if (envStat == JNI_EVERSION)
149 error_logger() << "Env: version not supported " << endl;
152 jstring attrName = env->NewStringUTF(attributeName.c_str());
153 jstring val = env->NewStringUTF(value.toString().c_str());
156 env->CallObjectMethod(bundleResource, attributeSetter, attrName, val);
157 BundleResource::setAttribute(attributeName, std::move(value));