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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 #include "ResourceContainerImpl.h"
33 #include "BundleActivator.h"
34 #include "RCSResourceContainer.h"
35 #include "BundleInfoInternal.h"
37 #include "oc_logger.hpp"
38 #include "SoftSensorResource.h"
41 using OC::oc_log_stream;
42 using namespace OIC::Service;
46 const std::string INPUT_RESOURCE = std::string("input");
47 const std::string INPUT_RESOURCE_URI = std::string("resourceUri");
48 const std::string INPUT_RESOURCE_TYPE = std::string("resourceType");
49 const std::string INPUT_RESOURCE_ATTRIBUTENAME = std::string("name");
51 const std::string OUTPUT_RESOURCE_URI = std::string("resourceUri");
54 auto error_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
56 static OC::oc_log_stream ols(oc_make_ostream_logger);
57 static boost::iostreams::stream<OC::oc_log_stream> os(ols);
58 os->set_level(OC_LOG_ERROR);
59 os->set_module("ResourceContainerImpl");
63 auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
65 static OC::oc_log_stream ols(oc_make_ostream_logger);
66 static boost::iostreams::stream<OC::oc_log_stream> os(ols);
67 os->set_level(OC_LOG_INFO);
68 os->set_module("ResourceContainerImpl");
73 using namespace OIC::Service;
80 ResourceContainerImpl::ResourceContainerImpl()
85 ResourceContainerImpl::~ResourceContainerImpl()
90 bool has_suffix(const std::string &str, const std::string &suffix)
92 return str.size() >= suffix.size()
93 && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
96 void ResourceContainerImpl::startContainer(const std::string &configFile)
98 info_logger() << "Starting resource container " << endl;
100 info_logger() << "Resource container has Java support" << endl;
102 info_logger() << "Resource container without Java support" << endl;
105 if (!configFile.empty())
107 m_config = new Configuration(configFile);
109 if (m_config->isLoaded())
112 m_config->getConfiguredBundles(&bundles);
114 for (unsigned int i = 0; i < bundles.size(); i++)
116 RCSBundleInfo *bundleInfo = RCSBundleInfo::build();
117 bundleInfo->setPath(bundles[i]["path"]);
118 bundleInfo->setVersion(bundles[i]["version"]);
119 bundleInfo->setID(bundles[i]["id"]);
120 if (!bundles[i]["activator"].empty())
122 string activatorName = bundles[i]["activator"];
123 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
124 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
125 ((BundleInfoInternal *) bundleInfo)->setLibraryPath(
126 bundles[i]["libraryPath"]);
129 info_logger() << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"]
131 registerBundle(bundleInfo);
133 auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this, bundleInfo);
135 boost::thread activator(f);
136 m_activators.push_back(std::move(activator));
141 error_logger() << "Container started with invalid configfile path" << endl;
146 info_logger() << "No configuration file for the container provided" << endl;
148 vector<boost::thread>::iterator activatorIterator;
150 for(activatorIterator = m_activators.begin(); activatorIterator != m_activators.end(); activatorIterator++){
151 activatorIterator->timed_join(boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC)); // wait for bundles to be activated
155 void ResourceContainerImpl::stopContainer()
157 info_logger() << "Stopping resource container.";
158 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
159 it != m_bundles.end(); ++it)
161 BundleInfoInternal *bundleInfo = it->second;
162 deactivateBundle(bundleInfo);
163 unregisterBundle(bundleInfo);
166 if (!m_mapServers.empty())
168 map< std::string, RCSResourceObject::Ptr >::iterator itor = m_mapServers.begin();
170 while (itor != m_mapServers.end())
172 (itor++)->second.reset();
175 m_mapResources.clear();
176 m_mapBundleResources.clear();
183 void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo)
185 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
187 if (bundleInfoInternal->isLoaded())
189 activateBundle(bundleInfo->getID());
193 void ResourceContainerImpl::deactivateBundle(RCSBundleInfo *bundleInfo)
195 if (((BundleInfoInternal *) bundleInfo)->isActivated())
197 deactivateBundle(bundleInfo->getID());
201 void ResourceContainerImpl::activateBundle(const std::string &id)
204 info_logger() << "Activating bundle: " << m_bundles[id]->getID() << endl;
206 if (m_bundles[id]->getJavaBundle())
209 activateJavaBundle(id);
214 activateSoBundle(id);
217 info_logger() << "Bundle activated: " << m_bundles[id]->getID() << endl;
221 void ResourceContainerImpl::deactivateBundle(const std::string &id)
224 if (m_bundles[id]->getJavaBundle())
227 deactivateJavaBundle(id);
232 deactivateSoBundle(id);
237 void ResourceContainerImpl::registerBundle(RCSBundleInfo *bundleInfo)
239 info_logger() << "Registering bundle: " << bundleInfo->getPath() << endl;
241 if (has_suffix(bundleInfo->getPath(), ".jar"))
244 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true);
245 registerJavaBundle(bundleInfo);
250 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
251 registerSoBundle(bundleInfo);
255 void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo)
257 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
258 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
260 if (!bundleInfoInternal->getJavaBundle())
262 unregisterBundleSo(bundleInfo->getID());
267 unregisterBundleJava(bundleInfo->getID());
273 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
275 void *bundleHandle = m_bundles[id]->getBundleHandle();
276 info_logger() << "Unregister bundle: " << m_bundles[id]->getID() << ", "
277 << m_bundles[id]->getID() << endl;
279 dlclose(bundleHandle);
280 if ((error = dlerror()) != NULL)
282 error_logger() << error << endl;
286 delete m_bundles[id];
291 void ResourceContainerImpl::registerResource(BundleResource *resource)
294 string strUri = resource->m_uri;
295 string strResourceType = resource->m_resourceType;
296 RCSResourceObject::Ptr server = nullptr;
298 info_logger() << "Registration of resource " << strUri << "," << strResourceType << endl;
300 registrationLock.lock();
301 if (m_mapResources.find(strUri) == m_mapResources.end())
303 server = buildResourceObject(strUri, strResourceType);
305 if (server != nullptr)
307 m_mapServers[strUri] = server;
308 m_mapResources[strUri] = resource;
309 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
311 resource->registerObserver(this);
313 server->setGetRequestHandler(
314 std::bind(&ResourceContainerImpl::getRequestHandler, this,
315 std::placeholders::_1, std::placeholders::_2));
317 server->setSetRequestHandler(
318 std::bind(&ResourceContainerImpl::setRequestHandler, this,
319 std::placeholders::_1, std::placeholders::_2));
321 info_logger() << "Registration finished " << strUri << "," << strResourceType
324 if (m_config->isHasInput(resource->m_bundleId))
326 discoverInputResource(strUri);
332 error_logger() << "resource with " << strUri << " already exists." << endl;
334 registrationLock.unlock();
337 void ResourceContainerImpl::unregisterResource(BundleResource *resource)
339 string strUri = resource->m_uri;
340 string strResourceType = resource->m_resourceType;
342 info_logger() << "Unregistration of resource " << resource->m_uri << "," << resource->m_resourceType
345 if (m_config->isHasInput(resource->m_bundleId))
347 undiscoverInputResource(strUri);
350 if (m_mapServers.find(strUri) != m_mapServers.end())
352 m_mapServers[strUri].reset();
354 m_mapResources.erase(m_mapResources.find(strUri));
355 m_mapBundleResources[resource->m_bundleId].remove(strUri);
359 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
360 configInfo *configOutput)
364 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
368 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
369 std::vector< resourceInfo > *configOutput)
373 m_config->getResourceConfiguration(bundleId, configOutput);
377 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
378 const RCSResourceAttributes &attributes)
381 RCSResourceAttributes attr;
383 if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
384 && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
386 for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
388 attr[attrName] = m_mapResources[request.getResourceUri()]->getAttribute(
393 return RCSGetResponse::create(attr);
396 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
397 const RCSResourceAttributes &attributes)
399 RCSResourceAttributes attr = attributes;
401 if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
402 && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
404 for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
406 if (!attr[attrName].toString().empty())
408 m_mapResources[request.getResourceUri()]->setAttribute(attrName,
409 attr[attrName].toString());
414 return RCSSetResponse::create(attr);
417 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
419 info_logger() << "ResourceContainerImpl::onNotificationReceived\n\tnotification from "
420 << strResourceUri << ".\n";
422 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
424 m_mapServers[strResourceUri]->notify();
428 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
430 static ResourceContainerImpl m_instance;
434 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
435 const std::string &strResourceType)
437 return RCSResourceObject::Builder(strUri, strResourceType, "DEFAULT_INTERFACE").setObservable(
438 true).setDiscoverable(true).build();
441 void ResourceContainerImpl::startBundle(const std::string &bundleId)
443 if (m_bundles.find(bundleId) != m_bundles.end())
445 if (!m_bundles[bundleId]->isActivated())
446 activateBundle(m_bundles[bundleId]);
448 error_logger() << "Bundle already started" << endl;
452 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl;
456 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
458 if (m_bundles.find(bundleId) != m_bundles.end())
460 if (m_bundles[bundleId]->isActivated())
461 deactivateBundle(m_bundles[bundleId]);
463 error_logger() << "Bundle not activated" << endl;
467 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl;
471 void ResourceContainerImpl::addBundle(const std::string &bundleId, const std::string &bundleUri,
472 const std::string &bundlePath,
473 std::map< string, string > params)
476 if (m_bundles.find(bundleId) != m_bundles.end())
477 error_logger() << "BundleId already exist" << endl;
481 RCSBundleInfo *bundleInfo = RCSBundleInfo::build();
482 bundleInfo->setID(bundleId);
483 bundleInfo->setPath(bundlePath);
484 if (params.find("activator") != params.end())
486 string activatorName = params["activator"];
487 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
488 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
489 ((BundleInfoInternal *) bundleInfo)->setLibraryPath(params["libraryPath"]);
492 info_logger() << "Add Bundle:" << bundleInfo->getID().c_str() << ";"
493 << bundleInfo->getPath().c_str() << endl;
495 registerBundle(bundleInfo);
499 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
501 if (m_bundles.find(bundleId) != m_bundles.end())
503 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
504 if (bundleInfo->isActivated())
505 deactivateBundle(bundleInfo);
507 if (bundleInfo->isLoaded())
508 unregisterBundle(bundleInfo);
512 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered."
517 std::list< RCSBundleInfo * > ResourceContainerImpl::listBundles()
519 std::list< RCSBundleInfo * > ret;
520 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
521 it != m_bundles.end(); ++it)
524 RCSBundleInfo *bundleInfo = RCSBundleInfo::build();
525 ((BundleInfoInternal *) bundleInfo)->setBundleInfo((RCSBundleInfo *) it->second);
526 ret.push_back(bundleInfo);
532 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
533 const std::string &resourceUri,
534 std::map< string, string > params)
536 if (m_bundles.find(bundleId) != m_bundles.end())
538 if (!m_bundles[bundleId]->getJavaBundle())
540 resourceInfo newResourceInfo;
541 newResourceInfo.uri = resourceUri;
543 if (params.find("name") != params.end())
544 newResourceInfo.name = params["name"];
545 if (params.find("resourceType") != params.end())
546 newResourceInfo.resourceType = params["resourceType"];
547 if (params.find("address") != params.end())
548 newResourceInfo.address = params["address"];
550 addSoBundleResource(bundleId, newResourceInfo);
555 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered."
560 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
561 const std::string &resourceUri)
563 if (m_bundles.find(bundleId) != m_bundles.end())
565 if (!m_bundles[bundleId]->getJavaBundle())
567 removeSoBundleResource(bundleId, resourceUri);
572 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered."
577 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
579 std::list< string > ret;
581 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
583 ret = m_mapBundleResources[bundleId];
590 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
594 activator_t *bundleActivator = NULL;
595 deactivator_t *bundleDeactivator = NULL;
596 resourceCreator_t *resourceCreator = NULL;
597 resourceDestroyer_t *resourceDestroyer = NULL;
599 //sstream << bundleInfo.path << std::ends;
601 void *bundleHandle = NULL;
602 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
604 if (bundleHandle != NULL)
606 bundleActivator = (activator_t *) dlsym(bundleHandle, "externalActivateBundle");
607 bundleDeactivator = (deactivator_t *) dlsym(bundleHandle,
608 "externalDeactivateBundle");
609 resourceCreator = (resourceCreator_t *) dlsym(bundleHandle,
610 "externalCreateResource");
611 resourceDestroyer = (resourceDestroyer_t *) dlsym(bundleHandle,
612 "externalDestroyResource");
614 if ((error = dlerror()) != NULL)
616 error_logger() << error << endl;
620 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
621 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
622 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
623 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
624 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
625 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
627 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
632 if ((error = dlerror()) != NULL)
634 error_logger() << error << endl;
639 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
641 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
643 if (bundleActivator != NULL)
645 bundleActivator(this, m_bundles[bundleId]->getID());
646 m_bundles[bundleId]->setActivated(true);
650 //Unload module and return error
651 error_logger() << "Activation unsuccessful." << endl;
654 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
655 bundleInfoInternal->setActivated(true);
659 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
661 auto foundDiscoverResource
662 = m_mapDiscoverResourceUnits.find(outputResourceUri);
663 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
665 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
669 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
671 auto foundOutputResource = m_mapResources.find(outputResourceUri);
672 auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
676 resourceProperty.at(INPUT_RESOURCE);
678 catch (std::out_of_range &e)
683 for (auto iter : resourceProperty)
685 if (iter.first.compare(INPUT_RESOURCE) == 0)
687 for (auto it : iter.second)
689 auto makeValue = [&](const std::string & reference) mutable -> std::string
691 std::string retStr = "";
694 retStr = it.at(reference);
696 catch (std::out_of_range &e)
702 std::string uri = makeValue(INPUT_RESOURCE_URI);
703 std::string type = makeValue(INPUT_RESOURCE_TYPE);
704 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
706 DiscoverResourceUnit::Ptr newDiscoverUnit
707 = std::make_shared<DiscoverResourceUnit>(outputResourceUri);
708 newDiscoverUnit->startDiscover(
709 DiscoverResourceUnit::DiscoverResourceInfo(uri, type, attributeName),
710 std::bind(&SoftSensorResource::onUpdatedInputResource,
711 (SoftSensorResource*)foundOutputResource->second,
712 std::placeholders::_1, std::placeholders::_2));
714 auto foundDiscoverResource
715 = m_mapDiscoverResourceUnits.find(outputResourceUri);
716 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
718 foundDiscoverResource->second.push_back(newDiscoverUnit);
722 m_mapDiscoverResourceUnits.insert(
723 std::make_pair(outputResourceUri,
724 std::list<DiscoverResourceUnit::Ptr>{newDiscoverUnit}));
731 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
733 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
734 info_logger() << "De-activating bundle: " << m_bundles[id]->getID() << endl;
736 if (bundleDeactivator != NULL)
739 m_bundles[id]->setActivated(false);
743 //Unload module and return error
744 error_logger() << "De-activation unsuccessful." << endl;
748 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
749 resourceInfo newResourceInfo)
751 resourceCreator_t *resourceCreator;
753 resourceCreator = m_bundles[bundleId]->getResourceCreator();
755 if (resourceCreator != NULL)
757 resourceCreator(newResourceInfo);
761 error_logger() << "addResource unsuccessful." << endl;
765 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
766 const std::string &resourceUri)
768 if (m_mapResources.find(resourceUri) != m_mapResources.end())
770 resourceDestroyer_t *resourceDestroyer =
771 m_bundles[bundleId]->getResourceDestroyer();
773 if (resourceDestroyer != NULL)
775 resourceDestroyer(m_mapResources[resourceUri]);
779 error_logger() << "removeResource unsuccessful." << endl;
784 void ResourceContainerImpl::activateBundleThread(RCSBundleInfo* rcsBundleInfo){
785 activateBundle(rcsBundleInfo);
789 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
791 return m_bundleVM[bundleId];
794 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
796 info_logger() << "Registering Java bundle " << bundleInfo->getID() << endl;
799 JavaVMInitArgs vm_args;
800 JavaVMOption options[3];
802 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
804 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
807 info_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " available." << endl;
812 error_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " not available" << endl;
816 char optionString[] = "-Djava.compiler=NONE";
817 options[0].optionString = optionString;
818 char classpath[1000];
819 strcpy(classpath, "-Djava.class.path=");
820 strcat(classpath, bundleInfo->getPath().c_str());
822 info_logger() << "Configured classpath: " << classpath << "|" << endl;
824 options[1].optionString = classpath;
826 char libraryPath[1000];
827 strcpy(libraryPath, "-Djava.library.path=");
828 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
829 options[2].optionString = libraryPath;
831 info_logger() << "Configured library path: " << libraryPath << "|" << endl;
833 vm_args.version = JNI_VERSION_1_4;
834 vm_args.options = options;
835 vm_args.nOptions = 3;
836 vm_args.ignoreUnrecognized = 1;
839 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
843 error_logger() << " cannot create JavaVM." << endl;
848 info_logger() << "JVM successfully created " << endl;
851 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
853 const char *className = bundleInfoInternal->getActivatorName().c_str();
855 info_logger() << "Looking up class: " << bundleInfoInternal->getActivatorName() << "|" << endl;
857 jclass bundleActivatorClass = env->FindClass(className);
859 if (bundleActivatorClass == NULL)
861 error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
862 << " bundle activator(" << bundleInfoInternal->getActivatorName()
863 << ") not found " << endl;
867 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
870 if (activateMethod == NULL)
872 error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
873 << " activate bundle method not found " << endl;
876 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
878 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
881 if (deactivateMethod == NULL)
883 error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
884 << " deactivate bundle method not found " << endl;
888 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
890 jmethodID constructor;
892 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
894 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
896 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
898 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
900 bundleInfoInternal->setLoaded(true);
902 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
904 info_logger() << "Bundle registered" << endl;
907 void ResourceContainerImpl::activateJavaBundle(string bundleId)
909 info_logger() << "Activating java bundle" << endl;
910 JavaVM *vm = getJavaVM(bundleId);
911 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
913 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
915 if (envStat == JNI_EDETACHED)
917 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
919 error_logger() << "Failed to attach " << endl;
922 else if (envStat == JNI_EVERSION)
924 error_logger() << "Env: version not supported " << endl;
927 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
928 bundleInfoInternal->getJavaBundleActivatorMethod());
930 m_bundles[bundleId]->setActivated(true);
933 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
935 info_logger() << "Deactivating java bundle" << endl;
936 JavaVM *vm = getJavaVM(bundleId);
937 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
939 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
941 if (envStat == JNI_EDETACHED)
943 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
945 error_logger() << "Failed to attach " << endl;
948 else if (envStat == JNI_EVERSION)
950 error_logger() << "Env: version not supported " << endl;
953 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
954 bundleInfoInternal->getJavaBundleDeactivatorMethod());
956 m_bundles[bundleId]->setActivated(false);
959 void ResourceContainerImpl::unregisterBundleJava(string id)
961 info_logger() << "Unregister Java bundle: " << m_bundles[id]->getID() << ", "
962 << m_bundles[id]->getID() << endl;
964 info_logger() << "Destroying JVM" << endl;
965 m_bundleVM[id]->DestroyJavaVM();
967 delete m_bundles[id];