Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / protocol-plugin / plugin-manager / src / FelixAdapter.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 /// @file FelixAdapter.cpp
22
23 /// @brief
24
25 #include "FelixAdapter.h"
26 #include <sstream>
27
28 using namespace OIC;
29
30 FelixAdapter *FelixAdapter::s_pinstance;
31
32 FelixAdapter::FelixAdapter(JavaVM *args)
33 {
34     jvm = args;
35
36     config = Config::Getinstance(args);
37     std::string pluginpath = config->getPluginPath();
38     if (pluginpath != "")
39     {
40         printf("Current path is %s\n", pluginpath.c_str());
41     }
42     else
43     {
44         fprintf(stderr, "Pluin path does not exist\n");
45         pluginpath = "";
46     }
47     registerAllPlugin(pluginpath);
48 }
49
50 FelixAdapter::~FelixAdapter(void)
51 {
52
53     s_pinstance->deleteinstance();
54     s_pinstance = NULL;
55 }
56
57 int FelixAdapter::installPlugin(const std::string path)
58 {
59     return 1;
60 }
61
62 int FelixAdapter::findPluginRecursive(const std::string path)
63 {
64     return 1;
65 }
66
67 int FelixAdapter::loadPluginInfoToManager(const std::string path)
68 {
69     return 1;
70 }
71
72 int FelixAdapter::registerPlugin(const std::string path)
73 {
74     if (path == "")
75     {
76         printf("FelixAdapter path == null\n");
77         return 0;
78     }
79
80     JNIEnv *env;
81     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
82
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");
86
87     // call registerPlugin() function
88     return  (int)env->CallStaticIntMethod(cls, mid, jpath);
89 }
90
91 int FelixAdapter::registerAllPlugin(const std::string path)
92 {
93     if (path == "")
94     {
95         printf("FelixAdapter path == null\n");
96         return 0;
97     }
98
99     JNIEnv *env;
100     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
101
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");
105
106     // call registerAllPlugin() function
107     return (int)env->CallStaticIntMethod(cls, mid, jpath);
108 }
109
110 int FelixAdapter::unregisterPlugin(Plugin *const plugin)
111 {
112     JNIEnv *env;
113     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
114
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");
119
120     // call unregisterPlugin() function
121     return (int)env->CallStaticIntMethod(cls, mid, jpath);
122 }
123
124 int FelixAdapter::unregisterAllPlugin(void)
125 {
126     JNIEnv *env;
127     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
128
129     jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
130     jmethodID mid = env->GetStaticMethodID(cls, "unregisterAllPlugin", "()I");
131
132     // call registerAllPlugin() function
133     return (int)env->CallStaticIntMethod(cls, mid);
134 }
135
136 std::vector<Plugin> &FelixAdapter::getAllPlugins(void)
137 {
138     JNIEnv *env;
139     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
140
141
142     jclass cls = env->FindClass("org/iotivity/service/ppm/FelixManager");
143     jmethodID mid = env->GetStaticMethodID(cls, "getAllPlugins", "()[Lorg/osgi/framework/Bundle;");
144
145     // call getAllPlugins() function
146     jobjectArray jresultArray = (jobjectArray)env->CallStaticObjectMethod(cls, mid);
147     jint size = env->GetArrayLength(jresultArray);
148     m_plugins.clear();
149
150     for (int i = 0; i < (int)size; i++)
151     {
152         Plugin *plugin = new Plugin;
153
154         jobject result = env->GetObjectArrayElement(jresultArray, i);
155         cls = env->GetObjectClass(result);
156         // set Language value
157         plugin->setValue("Language", "JAVA");
158         // set Id value
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);
163         // set Version value
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);
171         // set Name value
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);
188     }
189
190     return m_plugins;
191 }
192
193 std::vector<Plugin> *FelixAdapter::findPlugins(const std::string key, const std::string value)
194 {
195     JNIEnv *env;
196     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
197
198     jstring jkey = env->NewStringUTF(key.c_str());
199     jstring jvalue = env->NewStringUTF(value.c_str());
200
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;");
204
205     // call findPlugins() function
206     jobjectArray jresultArray = (jobjectArray)env->CallStaticObjectMethod(cls, mid, (jstring)jkey,
207                                 (jstring)jvalue);
208
209     jint size = env->GetArrayLength(jresultArray);
210
211     std::vector<Plugin> *re_plugins;
212     re_plugins = new std::vector<Plugin>;
213
214     for (int i = 0; i < (int)size; i++)
215     {
216         Plugin *plugin = new Plugin;
217
218         jobject result = env->GetObjectArrayElement(jresultArray, i);
219         cls = env->GetObjectClass(result);
220         // set Language value
221         plugin->setValue("Language", "JAVA");
222         // set Id value
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);
227         // set Version value
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);
235         // set Name value
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);
252     }
253
254     return re_plugins;
255 }
256 /*
257 Plugin *FelixAdapter::getPlugin(const std::string plugID)
258 {
259     return nullptr;
260 }
261 */
262 bool FelixAdapter::getFileList(File_list &list, const std::string strDir)
263 {
264     return true;
265 }
266
267
268 void FelixAdapter::printPluginList(cp_plugin_info_t **plugins)
269 {
270
271 }
272
273 int FelixAdapter::start(Plugin *const plugin, void *const arg)
274 {
275     JNIEnv *env;
276     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
277
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");
282
283     // call start() function
284     return (int)env->CallStaticIntMethod(cls, mid, jid);
285 }
286
287 int FelixAdapter::stop(Plugin *const plugin)
288 {
289     JNIEnv *env;
290     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
291
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");
296
297     // call stop() function
298     return (int)env->CallStaticIntMethod(cls, mid, jid);
299 }
300
301 bool FelixAdapter::isStarted(Plugin *plugin)
302 {
303     JNIEnv *env;
304     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
305
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");
310
311     // call isStarted() function
312     return (bool)env->CallStaticBooleanMethod(cls, mid, jid);
313 }
314
315 /*
316 void FelixAdapter::observePluginPath(void *str)
317 {
318
319 }
320 */
321
322 const std::string FelixAdapter::getState(const std::string plugID)
323 {
324     JNIEnv *env;
325     jvm->GetEnv((void **)&env, JNI_VERSION_1_6);
326
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;");
331
332     // call getState() function
333     jstring jresult = (jstring)env->CallStaticObjectMethod(cls, mid, jplugID);
334     return env->GetStringUTFChars(jresult, 0);
335 }