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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ResourceContainerImpl.h"
33 #include "BundleActivator.h"
34 #include "SoftSensorResource.h"
35 #include "InternalTypes.h"
37 using namespace OIC::Service;
44 ResourceContainerImpl::ResourceContainerImpl()
49 ResourceContainerImpl::~ResourceContainerImpl()
54 bool has_suffix(const std::string &str, const std::string &suffix)
56 return str.size() >= suffix.size()
57 && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
60 void ResourceContainerImpl::startContainer(const std::string &configFile)
62 OIC_LOG(INFO, CONTAINER_TAG, "Starting resource container.");
64 OIC_LOG(INFO, CONTAINER_TAG, "Resource container has Java support.");
66 OIC_LOG(INFO, CONTAINER_TAG, "Resource container without Java support.");
70 activationLock.lock();
71 if (!configFile.empty())
73 m_config = new Configuration(configFile);
75 if (m_config->isLoaded())
78 m_config->getConfiguredBundles(&bundles);
80 for (unsigned int i = 0; i < bundles.size(); i++)
82 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
83 bundleInfo->setPath(bundles[i][BUNDLE_PATH]);
84 bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]);
85 bundleInfo->setID(bundles[i][BUNDLE_ID]);
86 if (!bundles[i][BUNDLE_ACTIVATOR].empty())
88 string activatorName = bundles[i][BUNDLE_ACTIVATOR];
89 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
90 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
91 ((BundleInfoInternal *) bundleInfo)->setLibraryPath(
92 bundles[i][BUNDLE_LIBRARY_PATH]);
95 OIC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)",
96 std::string(bundles[i][BUNDLE_ID] + ";" +
97 bundles[i][BUNDLE_PATH]).c_str());
99 registerBundle(bundleInfo);
100 activateBundle(bundleInfo);
105 OIC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path.");
110 OIC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided.");
113 map<std::string, boost::thread >::iterator activatorIterator;
115 for (activatorIterator = m_activators.begin(); activatorIterator != m_activators.end();
118 activatorIterator->second.timed_join(
119 boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC));
120 // wait for bundles to be activated
122 activationLock.unlock();
123 OIC_LOG(INFO, CONTAINER_TAG, "Resource container started.");
126 void ResourceContainerImpl::stopContainer()
128 OIC_LOG(INFO, CONTAINER_TAG, "Stopping resource container.");
130 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
131 it != m_bundles.end(); ++it)
133 BundleInfoInternal *bundleInfo = it->second;
134 deactivateBundle(bundleInfo);
135 unregisterBundle(bundleInfo);
138 if (!m_mapServers.empty())
140 map< std::string, RCSResourceObject::Ptr >::iterator itor = m_mapServers.begin();
142 while (itor != m_mapServers.end())
144 (itor++)->second.reset();
147 m_mapResources.clear();
148 m_mapBundleResources.clear();
155 void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo)
157 activationLock.lock();
158 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
160 if (bundleInfoInternal->isLoaded())
162 activateBundle(bundleInfo->getID());
164 activationLock.unlock();
167 void ResourceContainerImpl::deactivateBundle(RCSBundleInfo *bundleInfo)
169 if (((BundleInfoInternal *) bundleInfo)->isActivated())
171 deactivateBundle(bundleInfo->getID());
175 void ResourceContainerImpl::activateBundle(const std::string &id)
177 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
178 std::string(m_bundles[id]->getID()).c_str());
179 activationLock.lock();
180 auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this,
182 boost::thread activator(f);
183 activator.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
184 activationLock.unlock();
185 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
186 std::string(m_bundles[id]->getID()).c_str());
189 void ResourceContainerImpl::deactivateBundle(const std::string &id)
191 if (m_bundles[id]->getJavaBundle())
194 deactivateJavaBundle(id);
199 deactivateSoBundle(id);
204 void ResourceContainerImpl::registerBundle(RCSBundleInfo *bundleInfo)
206 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)",
207 std::string(bundleInfo->getPath()).c_str());
209 if (has_suffix(bundleInfo->getPath(), ".jar"))
212 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true);
213 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
214 registerJavaBundle(bundleInfo);
216 // android .jar library
217 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
218 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
219 registerExtBundle(bundleInfo);
222 else if(has_suffix(bundleInfo->getPath(), ".so"))
224 ((BundleInfoInternal *) bundleInfo)->setSoBundle(true);
225 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
226 registerSoBundle(bundleInfo);
228 // other cases might be for example .apk for android, which are loaded in the wrapper
230 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
231 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
232 registerExtBundle(bundleInfo);
236 void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo)
238 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
239 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
241 if (bundleInfoInternal->getSoBundle())
243 unregisterBundleSo(bundleInfo->getID());
245 else if(bundleInfoInternal->getJavaBundle())
248 unregisterBundleJava(bundleInfo->getID());
254 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
256 void *bundleHandle = m_bundles[id]->getBundleHandle();
258 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
259 std::string(m_bundles[id]->getID()).c_str());
262 dlclose(bundleHandle);
264 if ((error = dlerror()) != NULL)
266 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error);
270 delete m_bundles[id];
275 int ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
277 string strUri = resource->m_uri;
278 string strResourceType = resource->m_resourceType;
279 string strInterface = resource->m_interface;
280 RCSResourceObject::Ptr server = nullptr;
283 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" ,
284 std::string(strUri + ", " + strResourceType + "," +
285 resource->m_bundleId).c_str());
287 registrationLock.lock();
288 if (m_mapResources.find(strUri) == m_mapResources.end())
290 if (strInterface.empty()) {
291 strInterface = "oic.if.baseline";
294 server = buildResourceObject(strUri, strResourceType, strInterface);
296 if (server != nullptr)
298 m_mapServers[strUri] = server;
299 m_mapResources[strUri] = resource;
300 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
302 server->setGetRequestHandler(
303 std::bind(&ResourceContainerImpl::getRequestHandler, this,
304 std::placeholders::_1, std::placeholders::_2));
306 server->setSetRequestHandler(
307 std::bind(&ResourceContainerImpl::setRequestHandler, this,
308 std::placeholders::_1, std::placeholders::_2));
310 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
311 std::string(strUri + ", " +
312 strResourceType).c_str());
314 if (m_config && m_config->isHasInput(resource->m_bundleId))
316 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has input (%s)",
317 std::string(strUri + ", " +
318 strResourceType).c_str());
319 discoverInputResource(strUri);
322 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has no input (%s)",
323 std::string(strUri + ", " +
324 strResourceType).c_str());
326 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
327 std::string(strUri + ", " +
328 strResourceType).c_str());
330 // to get notified if bundle resource attributes are updated
331 resource->registerObserver(this);
337 OIC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
338 std::string(strUri + " already exists.").c_str());
341 registrationLock.unlock();
346 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
348 string strUri = resource->m_uri;
349 string strResourceType = resource->m_resourceType;
351 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
352 std::string(resource->m_uri + ", " +
353 resource->m_resourceType).c_str());
355 if (m_config && m_config->isHasInput(resource->m_bundleId))
357 OIC_LOG_V(INFO, CONTAINER_TAG, "Calling undiscover (%s)",
358 std::string(resource->m_uri + ", " +
359 resource->m_resourceType).c_str());
360 undiscoverInputResource(strUri);
363 if (m_mapServers.find(strUri) != m_mapServers.end())
365 OIC_LOG_V(INFO, CONTAINER_TAG, "Resetting server (%s)",
366 std::string(resource->m_uri + ", " +
367 resource->m_resourceType).c_str());
368 m_mapServers[strUri].reset();
370 m_mapResources.erase(m_mapResources.find(strUri));
372 OIC_LOG_V(INFO, CONTAINER_TAG, "Remove bundle resource (%s)",
373 std::string(resource->m_uri + ", " +
374 resource->m_resourceType).c_str());
375 m_mapBundleResources[resource->m_bundleId].remove(strUri);
379 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
380 configInfo *configOutput)
384 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
388 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
389 std::vector< resourceInfo > *configOutput)
393 m_config->getResourceConfiguration(bundleId, configOutput);
396 OIC_LOG_V(DEBUG, CONTAINER_TAG, "no config present ");
400 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
401 const RCSResourceAttributes &)
403 RCSResourceAttributes attr;
404 std::string strResourceUri = request.getResourceUri();
405 OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s",strResourceUri.c_str());
407 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
408 && m_mapResources.find(strResourceUri) != m_mapResources.end())
410 if (m_mapResources[strResourceUri])
412 auto getFunction = [this, &attr, &strResourceUri]()
414 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest();
416 boost::thread getThread(getFunction);
417 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
421 OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %d attributes",strResourceUri.c_str(), attr.size());
423 return RCSGetResponse::create(std::move(attr), 200);
426 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
427 const RCSResourceAttributes &attributes)
429 RCSResourceAttributes attr;
430 std::list<std::string> lstAttributes;
431 std::string strResourceUri = request.getResourceUri();
433 OIC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %d attributes",strResourceUri.c_str(), attributes.size());
435 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
436 && m_mapResources.find(strResourceUri) != m_mapResources.end())
438 if (m_mapResources[strResourceUri])
440 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
442 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
444 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
445 itor != attributes.end(); itor++)
447 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
448 != lstAttributes.end())
450 attr[itor->key()] = itor->value();
454 OIC_LOG_V(INFO, CONTAINER_TAG, "Calling handleSetAttributeRequest");
455 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
457 boost::thread setThread(setFunction);
458 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
462 return RCSSetResponse::create(std::move(attr), 200);
465 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
467 OIC_LOG_V(INFO, CONTAINER_TAG,
468 "notification from (%s)", std::string(strResourceUri + ".").c_str());
470 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
472 m_mapServers[strResourceUri]->notify();
476 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
478 static ResourceContainerImpl m_instance;
482 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
483 const std::string &strResourceType, const std::string &strInterface)
485 return RCSResourceObject::Builder(strUri, strResourceType,
486 strInterface).setObservable(
487 true).setDiscoverable(true).build();
490 void ResourceContainerImpl::startBundle(const std::string &bundleId)
492 OIC_LOG_V(INFO, CONTAINER_TAG, "startBundle %s",bundleId.c_str());
493 if (m_bundles.find(bundleId) != m_bundles.end())
495 if (!m_bundles[bundleId]->isActivated())
496 activateBundle(m_bundles[bundleId]);
499 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
504 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
505 std::string(bundleId + "\' is not registered.").c_str());
509 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
511 OIC_LOG_V(INFO, CONTAINER_TAG, "stopBundle %s",bundleId.c_str());
512 if (m_bundles.find(bundleId) != m_bundles.end())
514 if (m_bundles[bundleId]->isActivated())
515 deactivateBundle(m_bundles[bundleId]);
518 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
523 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
524 std::string(bundleId + "\' is not registered.").c_str());
528 void ResourceContainerImpl::addBundle(const std::string &bundleId,
529 const std::string &bundleUri,
530 const std::string &bundlePath,
531 const std::string &activator,
532 std::map< string, string > params)
536 if (m_bundles.find(bundleId) != m_bundles.end())
537 OIC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
541 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
542 bundleInfo->setID(bundleId);
543 bundleInfo->setPath(bundlePath);
544 bundleInfo->setActivatorName(activator);
545 if (params.find("libraryPath") != params.end())
547 string activatorName = activator; // modify activator for Java bundle
548 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
549 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
550 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
553 OIC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
554 std::string(bundleInfo->getID() + "; " +
555 bundleInfo->getPath()).c_str());
557 registerBundle(bundleInfo);
561 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
563 OIC_LOG_V(INFO, CONTAINER_TAG, "removeBundle %s",bundleId.c_str());
564 if (m_bundles.find(bundleId) != m_bundles.end())
566 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
567 if (bundleInfo->isActivated())
568 deactivateBundle(bundleInfo);
570 if (bundleInfo->isLoaded())
571 unregisterBundle(bundleInfo);
575 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
576 std::string(bundleId + "\' is not ced.").c_str());
580 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
582 OIC_LOG_V(INFO, CONTAINER_TAG,
583 "list bundles (%d)", m_bundles.size());
584 std::list<std::unique_ptr<RCSBundleInfo> > ret;
585 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
586 it != m_bundles.end(); ++it)
589 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
590 (bundleInfo)->setBundleInfo(it->second);
591 ret.push_back(std::move(bundleInfo));
597 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
598 const std::string &resourceUri, std::map< string, string > params)
600 if (m_bundles.find(bundleId) != m_bundles.end())
602 if (!m_bundles[bundleId]->getJavaBundle())
604 resourceInfo newResourceInfo;
605 newResourceInfo.uri = resourceUri;
607 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
608 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
609 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
610 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
611 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
612 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
614 addSoBundleResource(bundleId, newResourceInfo);
619 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
620 std::string(bundleId + "\' is not registered.").c_str());
624 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
625 const std::string &resourceUri)
627 OIC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(), resourceUri.c_str());
628 if (m_bundles.find(bundleId) != m_bundles.end())
630 if (m_bundles[bundleId]->getSoBundle())
632 removeSoBundleResource(bundleId, resourceUri);
637 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
638 std::string(bundleId + "\' is not registered.").c_str());
642 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
644 OIC_LOG_V(INFO, CONTAINER_TAG, "listBundleResources %s",bundleId.c_str());
645 std::list < string > ret;
647 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
649 ret = m_mapBundleResources[bundleId];
656 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
658 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle");
661 activator_t *bundleActivator = NULL;
662 deactivator_t *bundleDeactivator = NULL;
663 resourceCreator_t *resourceCreator = NULL;
664 resourceDestroyer_t *resourceDestroyer = NULL;
665 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
666 void *bundleHandle = NULL;
667 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
668 if ((error = dlerror()) != NULL)
670 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
673 if (bundleHandle != NULL)
675 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s", bundleInfoInternal->getActivatorName().c_str());
677 (activator_t *) dlsym(bundleHandle,
678 ("" + bundleInfoInternal->getActivatorName()
679 + "_externalActivateBundle").c_str());
680 if ((error = dlerror()) != NULL)
682 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
685 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
686 + "_externalActivateBundle").c_str());
689 (deactivator_t *) dlsym(bundleHandle,
690 ("" + bundleInfoInternal->getActivatorName()
691 + "_externalDeactivateBundle").c_str());
692 if ((error = dlerror()) != NULL)
694 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
697 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
698 + "_externalDeactivateBundle").c_str());
701 (resourceCreator_t *) dlsym(bundleHandle,
702 ("" + bundleInfoInternal->getActivatorName()
703 + "_externalCreateResource").c_str());
704 if ((error = dlerror()) != NULL)
706 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
709 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
710 + "_externalCreateResource").c_str());
713 (resourceDestroyer_t *) dlsym(bundleHandle,
714 ("" + bundleInfoInternal->getActivatorName()
715 + "_externalDestroyResource").c_str());
716 if ((error = dlerror()) != NULL)
718 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
721 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
722 + "_externalDestroyResource").c_str());
726 if ((error = dlerror()) != NULL)
728 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
732 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
733 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
734 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
735 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
736 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
737 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
739 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
744 if ((error = dlerror()) != NULL)
746 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
749 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished");
752 void ResourceContainerImpl::registerExtBundle(RCSBundleInfo *bundleInfo){
753 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)",
754 std::string(bundleInfo->getID()).c_str());
755 OIC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)",
756 std::string(bundleInfo->getActivatorName()).c_str());
758 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
759 // in this case at least the resource configuration needs to be loaded
760 // in order to mark potential input resources for soft sensors
761 std::vector< resourceInfo > temp;
762 OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource config(%s)",
763 std::string(bundleInfo->getID()).c_str());
764 getResourceConfiguration(bundleInfo->getID(),
767 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
770 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
772 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
774 if (bundleActivator != NULL)
776 bundleActivator(this, m_bundles[bundleId]->getID());
777 m_bundles[bundleId]->setActivated(true);
781 //Unload module and return error
782 OIC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
785 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
786 bundleInfoInternal->setActivated(true);
790 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
792 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
793 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
795 OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource.");
796 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
797 OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource done.");
801 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
803 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource %s", outputResourceUri.c_str());
804 auto foundOutputResource = m_mapResources.find(outputResourceUri);
805 // auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
808 m_config->getResourceConfiguration(foundOutputResource->second->m_bundleId,
809 foundOutputResource->second->m_name, &info);
810 map< string, vector< map< string, string > > > resourceProperty = info.resourceProperty;
814 resourceProperty.at(INPUT_RESOURCE);
816 catch (std::out_of_range &e)
818 OIC_LOG_V(DEBUG, CONTAINER_TAG, "No input resource %s", outputResourceUri.c_str());
822 for (auto iter : resourceProperty)
824 if (iter.first.compare(INPUT_RESOURCE) == 0)
826 for (auto it : iter.second)
828 auto makeValue = [&](const std::string & reference) mutable -> std::string
830 std::string retStr = "";
833 retStr = it.at(reference);
835 catch (std::out_of_range &e)
841 std::string uri = makeValue(INPUT_RESOURCE_URI);
842 std::string type = makeValue(INPUT_RESOURCE_TYPE);
843 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
846 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Start discovery: %s, %s, %s", uri.c_str(),
847 type.c_str(), attributeName.c_str());
848 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
849 < DiscoverResourceUnit > (outputResourceUri);
850 newDiscoverUnit->startDiscover(
851 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
853 std::bind(&SoftSensorResource::onUpdatedInputResource,
854 std::static_pointer_cast< SoftSensorResource >
855 (foundOutputResource->second),
856 std::placeholders::_1, std::placeholders::_2));
858 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
860 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
862 foundDiscoverResource->second.push_back(newDiscoverUnit);
866 m_mapDiscoverResourceUnits.insert(
867 std::make_pair(outputResourceUri,
868 std::list< DiscoverResourceUnit::Ptr >
869 { newDiscoverUnit }));
876 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
878 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
880 OIC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
881 m_bundles[id]->getID()).c_str());
883 if (bundleDeactivator != NULL)
886 m_bundles[id]->setActivated(false);
890 //Unload module and return error
891 OIC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
895 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
896 resourceInfo newResourceInfo)
898 resourceCreator_t *resourceCreator;
900 resourceCreator = m_bundles[bundleId]->getResourceCreator();
902 if (resourceCreator != NULL)
904 resourceCreator(newResourceInfo);
908 OIC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
912 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
913 const std::string &resourceUri)
915 if (m_mapResources.find(resourceUri) != m_mapResources.end())
917 resourceDestroyer_t *resourceDestroyer =
918 m_bundles[bundleId]->getResourceDestroyer();
920 if (resourceDestroyer != NULL)
922 resourceDestroyer(m_mapResources[resourceUri]);
926 OIC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
931 void ResourceContainerImpl::activateBundleThread(const std::string &id)
933 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
934 std::string(m_bundles[id]->getID()).c_str());
936 if (m_bundles[id]->getJavaBundle())
939 activateJavaBundle(id);
942 else if(m_bundles[id]->getSoBundle())
944 activateSoBundle (id);
947 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
948 std::string(m_bundles[id]->getID()).c_str());
952 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
954 return m_bundleVM[bundleId];
957 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
959 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
960 std::string(bundleInfo->getID()).c_str());
963 JavaVMInitArgs vm_args;
964 JavaVMOption options[3];
966 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
968 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
972 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
973 std::string(bundleInfo->getPath() +
974 " available.").c_str());
978 OIC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
979 std::string(bundleInfo->getPath() + " not available.").c_str());
984 char optionString[] = "-Djava.compiler=NONE";
985 options[0].optionString = optionString;
986 char classpath[1000];
987 strcpy(classpath, "-Djava.class.path=");
988 strcat(classpath, bundleInfo->getPath().c_str());
990 OIC_LOG(INFO, CONTAINER_TAG,
991 std::string("Configured classpath: ").append(classpath).c_str());
993 options[1].optionString = classpath;
995 char libraryPath[1000];
996 strcpy(libraryPath, "-Djava.library.path=");
997 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
998 options[2].optionString = libraryPath;
1000 OIC_LOG(INFO, CONTAINER_TAG,
1001 std::string("Configured library path: ").append(libraryPath).c_str());
1003 vm_args.version = JNI_VERSION_1_4;
1004 vm_args.options = options;
1005 vm_args.nOptions = 3;
1006 vm_args.ignoreUnrecognized = 1;
1009 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
1013 OIC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
1018 OIC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
1021 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
1023 const char *className = bundleInfoInternal->getActivatorName().c_str();
1025 OIC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
1026 bundleInfoInternal->getActivatorName() + "|").c_str());
1028 jclass bundleActivatorClass = env->FindClass(className);
1030 if (bundleActivatorClass == NULL)
1032 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1033 std::string( bundleInfoInternal->getID()
1034 + " bundle activator(" +
1035 bundleInfoInternal->getActivatorName()
1036 + ") not found ").c_str());
1040 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
1043 if (activateMethod == NULL)
1045 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1046 std::string( bundleInfoInternal->getID()
1047 + " activate bundle method not found ").c_str());
1050 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
1052 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
1055 if (deactivateMethod == NULL)
1057 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1058 std::string( bundleInfoInternal->getID()
1059 + " deactivate bundle method not found ").c_str());
1063 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
1065 jmethodID constructor;
1067 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
1069 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
1071 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
1073 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
1075 bundleInfoInternal->setLoaded(true);
1077 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
1080 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
1085 void ResourceContainerImpl::activateJavaBundle(string bundleId)
1087 OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
1089 JavaVM *vm = getJavaVM(bundleId);
1090 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1092 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1094 if (envStat == JNI_EDETACHED)
1096 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1098 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1101 else if (envStat == JNI_EVERSION)
1103 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1106 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1107 bundleInfoInternal->getJavaBundleActivatorMethod());
1109 m_bundles[bundleId]->setActivated(true);
1112 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
1114 OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
1116 JavaVM *vm = getJavaVM(bundleId);
1117 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1119 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1121 if (envStat == JNI_EDETACHED)
1123 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1125 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1128 else if (envStat == JNI_EVERSION)
1130 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1133 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1134 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1136 m_bundles[bundleId]->setActivated(false);
1139 void ResourceContainerImpl::unregisterBundleJava(string id)
1141 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1142 m_bundles[id]->getID()).c_str());
1144 OIC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1146 m_bundleVM[id]->DestroyJavaVM();
1148 delete m_bundles[id];
1149 m_bundles.erase(id);