1 //******************************************************************
3 // Copyright 2014 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file FelixAdapter.cpp
25 #include "FelixAdapter.h"
30 FelixAdapter *FelixAdapter::s_pinstance;
32 FelixAdapter::FelixAdapter(JavaVM *args)
36 config = Config::Getinstance(args);
37 std::string pluginpath = config->getPluginPath();
40 printf("Current path is %s\n", pluginpath.c_str());
44 fprintf(stderr, "Pluin path does not exist\n");
47 registerAllPlugin(pluginpath);
50 FelixAdapter::~FelixAdapter(void)
53 s_pinstance->deleteinstance();
57 int FelixAdapter::installPlugin(const std::string path)
62 int FelixAdapter::findPluginRecursive(const std::string path)
67 int FelixAdapter::loadPluginInfoToManager(const std::string path)
72 int FelixAdapter::registerPlugin(const std::string path)
76 printf("FelixAdapter path == null\n");
81 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
83 jstring jpath = env->NewStringUTF(path.c_str());
84 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
85 jmethodID mid = env->GetStaticMethodID(cls, "registerPlugin", "(Ljava/lang/String;)I");
87 // call registerPlugin() function
88 return (int)env->CallStaticIntMethod(cls, mid, jpath);
91 int FelixAdapter::registerAllPlugin(const std::string path)
95 printf("FelixAdapter path == null\n");
100 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
102 jstring jpath = env->NewStringUTF(path.c_str());
103 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
104 jmethodID mid = env->GetStaticMethodID(cls, "registerAllPlugin", "(Ljava/lang/String;)I");
106 // call registerAllPlugin() function
107 return (int)env->CallStaticIntMethod(cls, mid, jpath);
110 int FelixAdapter::unregisterPlugin(Plugin *const plugin)
113 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
115 const char *cpath = plugin->getID().c_str();
116 jstring jpath = env->NewStringUTF(cpath);
117 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
118 jmethodID mid = env->GetStaticMethodID(cls, "unregisterPlugin", "(Ljava/lang/String;)I");
120 // call unregisterPlugin() function
121 return (int)env->CallStaticIntMethod(cls, mid, jpath);
124 int FelixAdapter::unregisterAllPlugin(void)
127 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
129 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
130 jmethodID mid = env->GetStaticMethodID(cls, "unregisterAllPlugin", "()I");
132 // call registerAllPlugin() function
133 return (int)env->CallStaticIntMethod(cls, mid);
136 std::vector<Plugin> &FelixAdapter::getAllPlugins(void)
139 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
142 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
143 jmethodID mid = env->GetStaticMethodID(cls, "getAllPlugins", "()[Lorg/osgi/framework/Bundle;");
145 // call getAllPlugins() function
146 jobjectArray jresultArray = (jobjectArray)env->CallStaticObjectMethod(cls, mid);
147 jint size = env->GetArrayLength(jresultArray);
150 for (int i = 0; i < (int)size; i++)
152 Plugin *plugin = new Plugin;
154 jobject result = env->GetObjectArrayElement(jresultArray, i);
155 cls = env->GetObjectClass(result);
156 // set Language value
157 plugin->setValue("Language", "JAVA");
159 mid = env->GetMethodID(cls, "getSymbolicName", "()Ljava/lang/String;");
160 jstring jid = (jstring)env->CallObjectMethod(result, mid);
161 std::string id = env->GetStringUTFChars(jid, 0);
162 plugin->setValue("Id", id);
164 mid = env->GetMethodID(cls, "getVersion", "()Lorg/osgi/framework/Version;");
165 jobject jversion_object = env->CallObjectMethod(result, mid);
166 jclass vcls = env->GetObjectClass(jversion_object);
167 mid = env->GetMethodID(vcls, "toString", "()Ljava/lang/String;");
168 jstring jversion = (jstring)env->CallObjectMethod(jversion_object, mid);
169 std::string version = env->GetStringUTFChars(jversion, 0);
170 plugin->setValue("Version", version);
172 cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
173 mid = env->GetStaticMethodID(cls, "getValue",
174 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
175 std::string key = "Name";
176 jstring jname = (jstring)env->CallStaticObjectMethod(cls, mid, jid,
177 env->NewStringUTF(key.c_str()));
178 std::string name = env->GetStringUTFChars(jname, 0);
179 plugin->setValue("Name", name);
180 // set ResourceType value
181 key = "ResourceType";
182 jstring jresourcetype = (jstring)env->CallStaticObjectMethod(cls, mid, jid,
183 env->NewStringUTF(key.c_str()));
184 std::string resourcetype = env->GetStringUTFChars(jresourcetype, 0);
185 plugin->setValue("ResourceType", resourcetype);
186 // push the plugin into the vector
187 m_plugins.push_back(*plugin);
193 std::vector<Plugin> *FelixAdapter::findPlugins(const std::string key, const std::string value)
196 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
198 jstring jkey = env->NewStringUTF(key.c_str());
199 jstring jvalue = env->NewStringUTF(value.c_str());
201 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
202 jmethodID mid = env->GetStaticMethodID(cls, "findPlugins",
203 "(Ljava/lang/String;Ljava/lang/String;)[Lorg/osgi/framework/Bundle;");
205 // call findPlugins() function
206 jobjectArray jresultArray = (jobjectArray)env->CallStaticObjectMethod(cls, mid, (jstring)jkey,
209 jint size = env->GetArrayLength(jresultArray);
211 std::vector<Plugin> *re_plugins;
212 re_plugins = new std::vector<Plugin>;
214 for (int i = 0; i < (int)size; i++)
216 Plugin *plugin = new Plugin;
218 jobject result = env->GetObjectArrayElement(jresultArray, i);
219 cls = env->GetObjectClass(result);
220 // set Language value
221 plugin->setValue("Language", "JAVA");
223 mid = env->GetMethodID(cls, "getSymbolicName", "()Ljava/lang/String;");
224 jstring jid = (jstring)env->CallObjectMethod(result, mid);
225 std::string id = env->GetStringUTFChars(jid, 0);
226 plugin->setValue("Id", id);
228 mid = env->GetMethodID(cls, "getVersion", "()Lorg/osgi/framework/Version;");
229 jobject jversion_object = env->CallObjectMethod(result, mid);
230 jclass vcls = env->GetObjectClass(jversion_object);
231 mid = env->GetMethodID(vcls, "toString", "()Ljava/lang/String;");
232 jstring jversion = (jstring)env->CallObjectMethod(jversion_object, mid);
233 std::string version = env->GetStringUTFChars(jversion, 0);
234 plugin->setValue("Version", version);
236 cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
237 mid = env->GetStaticMethodID(cls, "getValue",
238 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
239 std::string key = "Name";
240 jstring jname = (jstring)env->CallStaticObjectMethod(cls, mid, jid,
241 env->NewStringUTF(key.c_str()));
242 std::string name = env->GetStringUTFChars(jname, 0);
243 plugin->setValue("Name", name);
244 // set ResourceType value
245 key = "ResourceType";
246 jstring jresourcetype = (jstring)env->CallStaticObjectMethod(cls, mid, jid,
247 env->NewStringUTF(key.c_str()));
248 std::string resourcetype = env->GetStringUTFChars(jresourcetype, 0);
249 plugin->setValue("ResourceType", resourcetype);
250 // push the plugin into the vector
251 m_plugins.push_back(*plugin);
257 Plugin *FelixAdapter::getPlugin(const std::string plugID)
262 bool FelixAdapter::getFileList(File_list &list, const std::string strDir)
268 void FelixAdapter::printPluginList(cp_plugin_info_t **plugins)
273 int FelixAdapter::start(Plugin *const plugin, void *const arg)
276 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
278 const char *cid = plugin->getID().c_str();
279 jstring jid = env->NewStringUTF(cid);
280 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
281 jmethodID mid = env->GetStaticMethodID(cls, "start", "(Ljava/lang/String;)I");
283 // call start() function
284 return (int)env->CallStaticIntMethod(cls, mid, jid);
287 int FelixAdapter::stop(Plugin *const plugin)
290 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
292 const char *cid = plugin->getID().c_str();
293 jstring jid = env->NewStringUTF(cid);
294 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
295 jmethodID mid = env->GetStaticMethodID(cls, "stop", "(Ljava/lang/String;)I");
297 // call stop() function
298 return (int)env->CallStaticIntMethod(cls, mid, jid);
301 bool FelixAdapter::isStarted(Plugin *plugin)
304 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
306 const char *cid = plugin->getID().c_str();
307 jstring jid = env->NewStringUTF(cid);
308 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
309 jmethodID mid = env->GetStaticMethodID(cls, "isStarted", "(Ljava/lang/String;)Z");
311 // call isStarted() function
312 return (bool)env->CallStaticBooleanMethod(cls, mid, jid);
316 void FelixAdapter::observePluginPath(void *str)
322 const std::string FelixAdapter::getState(const std::string plugID)
325 jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
327 jstring jplugID = env->NewStringUTF(plugID.c_str());
328 jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
329 jmethodID mid = env->GetStaticMethodID(cls, "getState",
330 "(Ljava/lang/String;)Ljava/lang/String;");
332 // call getState() function
333 jstring jresult = (jstring)env->CallStaticObjectMethod(cls, mid, jplugID);
334 return env->GetStringUTFChars(jresult, 0);