update container and sample bundles for so bundle dynamic configuraion
[platform/upstream/iotivity.git] / service / resource-manipulation / src / resourceContainer / src / ResourceContainerImpl.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 #include <dlfcn.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <iostream>
25 #include <fstream>
26 #include <jni.h>
27 #include <iostream>
28 #include <fstream>
29 #include <stdio.h>
30
31 #include "ResourceContainerImpl.h"
32 #include "BundleActivator.h"
33 #include "ResourceContainer.h"
34 #include "BundleInfoInternal.h"
35 #include "logger.h"
36 #include "oc_logger.hpp"
37
38 using OC::oc_log_stream;
39 using namespace OIC::Service;
40
41 auto error_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
42 {
43     static OC::oc_log_stream ols(oc_make_ostream_logger);
44     static boost::iostreams::stream<OC::oc_log_stream> os(ols);
45     os->set_level(OC_LOG_ERROR);
46     os->set_module("ResourceContainerImpl");
47     return os;
48 };
49
50 auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
51 {
52     static OC::oc_log_stream ols(oc_make_ostream_logger);
53     static boost::iostreams::stream<OC::oc_log_stream> os(ols);
54     os->set_level(OC_LOG_INFO);
55     os->set_module("ResourceContainerImpl");
56     return os;
57 };
58
59 using namespace std;
60 using namespace OIC::Service;
61
62 namespace OIC
63 {
64     namespace Service
65     {
66
67         ResourceContainerImpl::ResourceContainerImpl()
68         {
69             // TODO Auto-generated constructor stub
70
71         }
72
73         ResourceContainerImpl::~ResourceContainerImpl()
74         {
75             // TODO Auto-generated destructor stub
76         }
77
78         bool has_suffix(const std::string &str, const std::string &suffix)
79         {
80             return str.size() >= suffix.size()
81                    && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
82         }
83
84         void ResourceContainerImpl::startContainer(string configFile)
85         {
86             info_logger() << "Starting resource container" << endl;
87
88             m_config = new Configuration(configFile);
89
90             if (m_config->isLoaded())
91             {
92                 configInfo bundles;
93                 m_config->getConfiguredBundles(&bundles);
94
95                 for (unsigned int i = 0; i < bundles.size(); i++)
96                 {
97                     BundleInfo *bundleInfo = BundleInfo::build();
98                     bundleInfo->setPath(bundles[i]["path"]);
99                     bundleInfo->setVersion(bundles[i]["version"]);
100                     bundleInfo->setID(bundles[i]["id"]);
101                     if (!bundles[i]["activator"].empty())
102                     {
103                         string activatorName = bundles[i]["activator"];
104                         std::replace(activatorName.begin(), activatorName.end(), '.', '/');
105                         ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
106                         ((BundleInfoInternal *) bundleInfo)->setLibraryPath(bundles[i]["libraryPath"]);
107
108                     }
109                     info_logger() << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"]
110                                   << endl;
111                     registerBundle(bundleInfo);
112                     activateBundle(bundleInfo);
113                 }
114             }
115             else
116             {
117                 error_logger() << "Container started with invalid configfile path" << endl;
118             }
119         }
120
121         void ResourceContainerImpl::stopContainer()
122         {
123             info_logger() << "Stopping resource container.";
124             for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
125                  it != m_bundles.end(); ++it)
126             {
127                 BundleInfoInternal *bundleInfo = it->second;
128                 deactivateBundle(bundleInfo);
129                 unregisterBundle(bundleInfo);
130             }
131
132             delete m_config;
133         }
134
135         void ResourceContainerImpl::activateBundle(BundleInfo *bundleInfo)
136         {
137             BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
138
139             if (bundleInfoInternal->isLoaded())
140             {
141                 activateBundle(bundleInfo->getID());
142             }
143         }
144
145         void ResourceContainerImpl::deactivateBundle(BundleInfo *bundleInfo)
146         {
147             if (((BundleInfoInternal *) bundleInfo)->isActivated())
148             {
149                 deactivateBundle(bundleInfo->getID());
150             }
151         }
152
153         void ResourceContainerImpl::activateBundle(string id)
154         {
155
156             info_logger() << "Activating bundle: " << m_bundles[id]->getID() << endl;
157
158             if (m_bundles[id]->getJavaBundle())
159             {
160                 activateJavaBundle(id);
161             }
162             else
163             {
164                 activateSoBundle(id);
165             }
166
167             info_logger() << "Bundle activated: " << m_bundles[id]->getID() << endl;
168
169         }
170
171         void ResourceContainerImpl::deactivateBundle(string id)
172         {
173
174             if (m_bundles[id]->getJavaBundle())
175             {
176                 deactivateJavaBundle(id);
177             }
178             else
179             {
180                 deactivateSoBundle(id);
181             }
182         }
183
184         // loads the bundle
185         void ResourceContainerImpl::registerBundle(BundleInfo *bundleInfo)
186         {
187             info_logger() << "Registering bundle: " << bundleInfo->getPath() << endl;
188
189             if (has_suffix(bundleInfo->getPath(), ".jar"))
190             {
191                 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true);
192                 registerJavaBundle(bundleInfo);
193             }
194             else
195             {
196                 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
197                 registerSoBundle(bundleInfo);
198             }
199         }
200
201         void ResourceContainerImpl::unregisterBundle(BundleInfo *bundleInfo)
202         {
203             BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
204             if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
205             {
206                 if (!bundleInfoInternal->getJavaBundle())
207                 {
208                     unregisterBundleSo(bundleInfo->getID());
209                 }
210                 else
211                 {
212                     unregisterBundleJava(bundleInfo->getID());
213                 }
214             }
215         }
216
217         void ResourceContainerImpl::unregisterBundleSo(string id)
218         {
219             void *bundleHandle = m_bundles[id]->getBundleHandle();
220             info_logger() << "Unregister bundle: " << m_bundles[id]->getID() << ", "
221                           << m_bundles[id]->getID() << endl;
222             char *error;
223             dlclose(bundleHandle);
224             if ((error = dlerror()) != NULL)
225             {
226                 error_logger() << error << endl;
227             }
228             else
229             {
230                 delete m_bundles[id];
231                 m_bundles.erase(id);
232             }
233         }
234
235         void ResourceContainerImpl::unregisterBundleJava(string id)
236         {
237             info_logger() << "Unregister Java bundle: " << m_bundles[id]->getID() << ", "
238                           << m_bundles[id]->getID() << endl;
239
240             info_logger() << "Destroying JVM" << endl;
241             m_bundleVM[id]->DestroyJavaVM();
242
243             delete m_bundles[id];
244             m_bundles.erase(id);
245         }
246
247         void ResourceContainerImpl::registerResource(BundleResource *resource)
248         {
249             string strUri = resource->m_uri;
250             string strResourceType = resource->m_resourceType;
251             ResourceObject::Ptr server = nullptr;
252
253             info_logger() << "Registration of resource " << strUri << "," << strResourceType
254                           << endl;
255
256             server = buildResourceObject(strUri, strResourceType);
257
258             if (server != nullptr)
259             {
260                 m_mapServers[strUri] = server;
261                 m_mapResources[strUri] = resource;
262                 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
263
264                 resource->registerObserver(this);
265
266                 server->setGetRequestHandler(
267                     std::bind(&ResourceContainerImpl::getRequestHandler, this,
268                               std::placeholders::_1, std::placeholders::_2));
269
270                 server->setSetRequestHandler(
271                     std::bind(&ResourceContainerImpl::setRequestHandler, this,
272                               std::placeholders::_1, std::placeholders::_2));
273
274                 info_logger() << "Registration finished " << strUri << "," << strResourceType
275                               << endl;
276             }
277         }
278
279         void ResourceContainerImpl::unregisterResource(BundleResource *resource)
280         {
281             string strUri = resource->m_uri;
282             string strResourceType = resource->m_resourceType;
283
284             info_logger() << "Unregistration of resource " << resource->m_uri << "," << resource->m_resourceType
285                           << endl;
286
287             if (m_mapServers.find(strUri) != m_mapServers.end())
288             {
289                 m_mapServers[strUri].reset();
290
291                 m_mapResources.erase(m_mapResources.find(strUri));
292                 m_mapBundleResources[resource->m_bundleId].remove(strUri);
293             }
294         }
295
296         void ResourceContainerImpl::getBundleConfiguration(std::string bundleId,
297                 configInfo *configOutput)
298         {
299             if (m_config)
300             {
301                 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
302             }
303         }
304
305         void ResourceContainerImpl::getResourceConfiguration(std::string bundleId,
306                 std::vector< resourceInfo > *configOutput)
307         {
308             if (m_config)
309             {
310                 m_config->getResourceConfiguration(bundleId, configOutput);
311             }
312         }
313
314         RCSGetResponse ResourceContainerImpl::getRequestHandler(
315             const RCSRequest &request, const ResourceAttributes &attributes)
316         {
317             ResourceAttributes attr;
318
319             if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
320                 && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
321             {
322                 for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
323                 {
324                     attr[attrName] = m_mapResources[request.getResourceUri()]->getAttribute(
325                                          attrName);
326                 }
327             }
328
329             return RCSGetResponse::create(attr);
330         }
331
332         RCSSetResponse ResourceContainerImpl::setRequestHandler(
333             const RCSRequest &request, const ResourceAttributes &attributes)
334         {
335             ResourceAttributes attr = attributes;
336
337             if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
338                 && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
339             {
340                 for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
341                 {
342                     if (!attr[attrName].toString().empty())
343                     {
344                         m_mapResources[request.getResourceUri()]->setAttribute(attrName,
345                                 attr[attrName].toString());
346                     }
347                 }
348             }
349
350             return RCSSetResponse::create(attr);
351         }
352
353         void ResourceContainerImpl::onNotificationReceived(std::string strResourceUri)
354         {
355             info_logger() << "ResourceContainerImpl::onNotificationReceived\n\tnotification from "
356                           << strResourceUri << ".\n";
357
358             if (m_mapServers.find(strResourceUri) != m_mapServers.end())
359             {
360                 m_mapServers[strResourceUri]->notify();
361             }
362         }
363
364         ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
365         {
366             static ResourceContainerImpl m_instance;
367             return &m_instance;
368         }
369
370         ResourceObject::Ptr ResourceContainerImpl::buildResourceObject(string strUri,
371                 string strResourceType)
372         {
373             return ResourceObject::Builder(strUri, strResourceType, "DEFAULT_INTERFACE").setObservable(
374                        true).setDiscoverable(true).build();
375         }
376
377         void ResourceContainerImpl::startBundle(string bundleId)
378         {
379             if (m_bundles.find(bundleId) != m_bundles.end())
380                 activateBundle(m_bundles[bundleId]);
381         }
382
383         void ResourceContainerImpl::stopBundle(string bundleId)
384         {
385             if (m_bundles.find(bundleId) != m_bundles.end())
386                 deactivateBundle(m_bundles[bundleId]);
387         }
388
389         void ResourceContainerImpl::addBundle(string bundleId, string bundleUri, string bundlePath,
390                                               std::map< string, string > params)
391         {
392             if (m_bundles.find(bundleId) != m_bundles.end())
393                 error_logger() << "BundleId already exist" << endl;
394
395             else
396             {
397                 BundleInfo *bundleInfo = BundleInfo::build();
398                 bundleInfo->setID(bundleId);
399                 bundleInfo->setPath(bundlePath);
400                 if (params.find("activator") != params.end())
401                 {
402                     string activatorName = params["activator"];
403                     std::replace(activatorName.begin(), activatorName.end(), '.', '/');
404                     ((BundleInfoInternal *)bundleInfo)->setActivatorName(activatorName);
405                     ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params["libraryPath"]);
406                 }
407                 if (params.find("activator") != params.end())
408                 {
409                     string activatorName = params["activator"];
410                     std::replace(activatorName.begin(), activatorName.end(), '.', '/');
411                     ((BundleInfoInternal *)bundleInfo)->setActivatorName(activatorName);
412                     ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params["libraryPath"]);
413                 }
414
415                 info_logger() << "Add Bundle:" << bundleInfo->getID().c_str() << ";" <<
416                               bundleInfo->getPath().c_str() <<
417                               endl;
418
419                 registerBundle(bundleInfo);
420             }
421         }
422
423         void ResourceContainerImpl::removeBundle(string bundleId)
424         {
425             if (m_bundles.find(bundleId) != m_bundles.end())
426             {
427                 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
428                 if (bundleInfo->isActivated())
429                     deactivateBundle(bundleInfo);
430
431                 if (bundleInfo->isLoaded())
432                     unregisterBundle(bundleInfo);
433             }
434             else
435             {
436                 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl;
437             }
438         }
439
440         std::list< BundleInfo * > ResourceContainerImpl::listBundles()
441         {
442             std::list< BundleInfo * > ret;
443             for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
444                  it != m_bundles.end(); ++it)
445             {
446                 {
447                     BundleInfo *bundleInfo = BundleInfo::build();
448                     ((BundleInfoInternal *) bundleInfo)->setBundleInfo((BundleInfo *) it->second);
449                     ret.push_back(it->second);
450                 }
451             }
452             return ret;
453         }
454
455         void ResourceContainerImpl::addResourceConfig(string bundleId, string resourceUri,
456                 std::map< string, string > params)
457         {
458             if (m_bundles.find(bundleId) != m_bundles.end())
459             {
460                 if (!m_bundles[bundleId]->getJavaBundle())
461                 {
462                     resourceInfo newResourceInfo;
463                     newResourceInfo.uri = resourceUri;
464
465                     if (params.find("name") != params.end())
466                         newResourceInfo.name = params["name"];
467                     if (params.find("resourceType") != params.end())
468                         newResourceInfo.resourceType = params["resourceType"];
469                     if (params.find("address") != params.end())
470                         newResourceInfo.address = params["address"];
471
472                     addSoBundleResource(bundleId, newResourceInfo);
473                 }
474             }
475             else
476             {
477                 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl;
478             }
479         }
480
481         void ResourceContainerImpl::removeResourceConfig(string bundleId, string resourceUri)
482         {
483             if (m_bundles.find(bundleId) != m_bundles.end())
484             {
485                 if (!m_bundles[bundleId]->getJavaBundle())
486                 {
487                     removeSoBundleResource(bundleId, resourceUri);
488                 }
489             }
490             else
491             {
492                 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl;
493             }
494         }
495
496         std::list< string > ResourceContainerImpl::listBundleResources(string bundleId)
497         {
498             std::list< string > ret;
499
500             if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
501             {
502                 ret = m_mapBundleResources[bundleId];
503             }
504
505             return ret;
506
507         }
508
509         JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
510         {
511             return m_bundleVM[bundleId];
512         }
513
514         void ResourceContainerImpl::registerJavaBundle(BundleInfo *bundleInfo)
515         {
516             info_logger() << "Registering Java bundle " << bundleInfo->getID() << endl;
517             JavaVM *jvm;
518             JNIEnv *env;
519             JavaVMInitArgs vm_args;
520             JavaVMOption options[3];
521
522             BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
523
524             if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
525             {
526                 fclose(file);
527                 info_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " available." << endl;
528                 //return true;
529             }
530             else
531             {
532                 error_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " not available" << endl;
533                 return;
534             }
535
536             char optionString[] = "-Djava.compiler=NONE";
537             options[0].optionString = optionString;
538             char classpath[1000];
539             strcpy(classpath, "-Djava.class.path=");
540             strcat(classpath, bundleInfo->getPath().c_str());
541
542
543             info_logger() << "Configured classpath: " << classpath << "|" << endl;
544
545             options[1].optionString = classpath;
546
547             char libraryPath[1000];
548             strcpy(libraryPath, "-Djava.library.path=");
549             strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
550             options[2].optionString = libraryPath;
551
552             info_logger() << "Configured library path: " << libraryPath << "|" << endl;
553
554             vm_args.version = JNI_VERSION_1_4;
555             vm_args.options = options;
556             vm_args.nOptions = 3;
557             vm_args.ignoreUnrecognized = 1;
558
559             int res;
560             res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
561
562             if (res < 0)
563             {
564                 error_logger() << " cannot create JavaVM." << endl;
565                 return;
566             }
567             else
568             {
569                 info_logger() << "JVM successfully created " << endl;
570             }
571
572             m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
573
574             const char *className = bundleInfoInternal->getActivatorName().c_str();
575
576             info_logger() << "Looking up class: " << bundleInfoInternal->getActivatorName() << "|" << endl;
577
578             jclass bundleActivatorClass = env->FindClass(className);
579
580             if (bundleActivatorClass == NULL)
581             {
582                 error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
583                                << " bundle activator(" << bundleInfoInternal->getActivatorName()
584                                << ") not found " << endl;
585                 return;
586             }
587
588             jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
589                                        "()V");
590
591             if (activateMethod == NULL)
592             {
593                 error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
594                                << " activate bundle method not found " << endl;
595                 return;
596             }
597             bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
598
599             jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
600                                          "()V");
601
602             if (deactivateMethod == NULL)
603             {
604                 error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
605                                << " deactivate bundle method not found " << endl;
606                 return;
607             }
608
609             bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
610
611             jmethodID constructor;
612
613             constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
614
615             jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
616
617             jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
618
619             bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
620
621             bundleInfoInternal->setLoaded(true);
622
623             m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
624
625             info_logger() << "Bundle registered" << endl;
626         }
627
628         void ResourceContainerImpl::registerSoBundle(BundleInfo *bundleInfo)
629         {
630             char *error;
631
632             activator_t *bundleActivator = NULL;
633             deactivator_t *bundleDeactivator = NULL;
634             resourceCreator_t *resourceCreator = NULL;
635             resourceDestroyer_t *resourceDestroyer = NULL;
636
637             //sstream << bundleInfo.path << std::ends;
638
639             void *bundleHandle = NULL;
640             bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
641
642             if (bundleHandle != NULL)
643             {
644                 bundleActivator = (activator_t *) dlsym(bundleHandle, "externalActivateBundle");
645                 bundleDeactivator = (deactivator_t *) dlsym(bundleHandle,
646                                     "externalDeactivateBundle");
647                 resourceCreator = (resourceCreator_t *)dlsym(bundleHandle, "externalCreateResource");
648                 resourceDestroyer = (resourceDestroyer_t *)dlsym(bundleHandle, "externalDestroyResource");
649
650                 if ((error = dlerror()) != NULL)
651                 {
652                     error_logger() << error << endl;
653                 }
654                 else
655                 {
656                     ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
657                     ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
658                     ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
659                     ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
660                     ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
661                     ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
662
663                     m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
664                 }
665             }
666             else
667             {
668                 if ((error = dlerror()) != NULL)
669                 {
670                     error_logger() << error << endl;
671                 }
672             }
673         }
674
675         void ResourceContainerImpl::activateJavaBundle(string bundleId)
676         {
677             info_logger() << "Activating java bundle" << endl;
678             JavaVM *vm = getJavaVM(bundleId);
679             BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
680             JNIEnv *env;
681             int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
682
683             if (envStat == JNI_EDETACHED)
684             {
685                 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
686                 {
687                     error_logger() << "Failed to attach " << endl;
688                 }
689             }
690             else if (envStat == JNI_EVERSION)
691             {
692                 error_logger() << "Env: version not supported " << endl;
693             }
694
695             env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
696                                 bundleInfoInternal->getJavaBundleActivatorMethod());
697
698             m_bundles[bundleId]->setActivated(true);
699         }
700
701         void ResourceContainerImpl::activateSoBundle(string bundleId)
702         {
703             activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
704
705             if (bundleActivator != NULL)
706             {
707                 bundleActivator(this, m_bundles[bundleId]->getID());
708                 m_bundles[bundleId]->setActivated(true);
709             }
710             else
711             {
712                 //Unload module and return error
713                 error_logger() << "Activation unsuccessful." << endl;
714             }
715
716             BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
717             bundleInfoInternal->setActivated(true);
718         }
719
720         void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
721         {
722             info_logger() << "Deactivating java bundle" << endl;
723             JavaVM *vm = getJavaVM(bundleId);
724             BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
725             JNIEnv *env;
726             int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
727
728             if (envStat == JNI_EDETACHED)
729             {
730                 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
731                 {
732                     error_logger() << "Failed to attach " << endl;
733                 }
734             }
735             else if (envStat == JNI_EVERSION)
736             {
737                 error_logger() << "Env: version not supported " << endl;
738             }
739
740             env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
741                                 bundleInfoInternal->getJavaBundleDeactivatorMethod());
742
743             m_bundles[bundleId]->setActivated(false);
744         }
745
746         void ResourceContainerImpl::deactivateSoBundle(string id)
747         {
748             deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
749             info_logger() << "De-activating bundle: " << m_bundles[id]->getID() << endl;
750
751             if (bundleDeactivator != NULL)
752             {
753                 bundleDeactivator();
754                 m_bundles[id]->setActivated(false);
755             }
756             else
757             {
758                 //Unload module and return error
759                 error_logger() << "De-activation unsuccessful." << endl;
760             }
761         }
762
763         void ResourceContainerImpl::addSoBundleResource(string bundleId, resourceInfo newResourceInfo)
764         {
765             resourceCreator_t *resourceCreator;
766
767             resourceCreator = m_bundles[bundleId]->getResourceCreator();
768
769             if (resourceCreator != NULL)
770             {
771                 resourceCreator(newResourceInfo);
772             }
773             else
774             {
775                 error_logger() << "addResource unsuccessful." << endl;
776             }
777         }
778
779         void ResourceContainerImpl::removeSoBundleResource(string bundleId, string resourceUri)
780         {
781             if (m_mapResources.find(resourceUri) != m_mapResources.end())
782             {
783                 resourceDestroyer_t *resourceDestroyer = m_bundles[bundleId]->getResourceDestroyer();
784
785                 if (resourceDestroyer != NULL)
786                 {
787                     resourceDestroyer(m_mapResources[resourceUri]);
788                 }
789                 else
790                 {
791                     error_logger() << "removeResource unsuccessful." << endl;
792                 }
793             }
794         }
795     }
796 }
797