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();
73 if (!configFile.empty())
75 m_config = new Configuration(configFile);
77 if (m_config->isLoaded())
80 m_config->getConfiguredBundles(&bundles);
82 for (unsigned int i = 0; i < bundles.size(); i++)
84 shared_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
85 bundleInfo->setPath(bundles[i][BUNDLE_PATH]);
86 bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]);
87 bundleInfo->setID(bundles[i][BUNDLE_ID]);
88 if (!bundles[i][BUNDLE_ACTIVATOR].empty())
90 string activatorName = bundles[i][BUNDLE_ACTIVATOR];
91 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
92 bundleInfo->setActivatorName(activatorName);
93 bundleInfo->setLibraryPath(bundles[i][BUNDLE_LIBRARY_PATH]);
96 OIC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)",
97 std::string(bundles[i][BUNDLE_ID] + ";" +
98 bundles[i][BUNDLE_PATH]).c_str());
100 registerBundle(bundleInfo);
101 activateBundle(bundleInfo);
106 OIC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path.");
111 OIC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided.");
114 OIC_LOG(INFO, CONTAINER_TAG, "Resource container started.");
118 OIC_LOG(INFO, CONTAINER_TAG, "Resource container failed starting.");
120 activationLock.unlock();
123 void ResourceContainerImpl::stopContainer()
125 OIC_LOG(INFO, CONTAINER_TAG, "Stopping resource container.");
127 for (std::map< std::string, shared_ptr<BundleInfoInternal> >::iterator it = m_bundles.begin();
128 it != m_bundles.end(); ++it)
130 deactivateBundle(it->second);
131 unregisterBundle(it->second);
134 if (!m_mapServers.empty())
136 map< std::string, RCSResourceObject::Ptr >::iterator itor = m_mapServers.begin();
138 while (itor != m_mapServers.end())
140 (itor++)->second.reset();
143 m_mapResources.clear();
144 m_mapBundleResources.clear();
153 void ResourceContainerImpl::activateBundle(shared_ptr<RCSBundleInfo> bundleInfo)
155 activationLock.lock();
157 shared_ptr<BundleInfoInternal> bundleInfoInternal =
158 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
160 if (bundleInfoInternal->isLoaded())
162 activateBundle(bundleInfo->getID());
164 activationLock.unlock();
167 void ResourceContainerImpl::deactivateBundle(shared_ptr<RCSBundleInfo> bundleInfo)
169 shared_ptr<BundleInfoInternal> bundleInfoInternal =
170 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
171 if (bundleInfoInternal->isActivated())
173 deactivateBundle(bundleInfoInternal->getID());
177 void ResourceContainerImpl::activateBundle(const std::string &id)
179 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
180 std::string(m_bundles[id]->getID()).c_str());
182 activationLock.lock();
185 activateBundleThread(id);
189 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s) failed",
190 std::string(m_bundles[id]->getID()).c_str());
192 activationLock.unlock();
193 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
194 std::string(m_bundles[id]->getID()).c_str());
197 void ResourceContainerImpl::deactivateBundle(const std::string &id)
199 if (m_bundles[id]->getJavaBundle())
202 deactivateJavaBundle(id);
207 deactivateSoBundle(id);
212 void ResourceContainerImpl::registerBundle(shared_ptr<RCSBundleInfo> bundleInfo)
214 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)",
215 std::string(bundleInfo->getPath()).c_str());
216 shared_ptr<BundleInfoInternal> bundleInfoInternal =
217 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
219 if (has_suffix(bundleInfo->getPath(), ".jar"))
222 bundleInfoInternal->setJavaBundle(true);
223 bundleInfoInternal->setSoBundle(false);
224 registerJavaBundle(bundleInfo);
226 // android .jar library
227 bundleInfoInternal->setSoBundle(false);
228 bundleInfoInternal->setJavaBundle(false);
229 registerExtBundle(bundleInfo);
232 else if (has_suffix(bundleInfo->getPath(), ".so"))
234 bundleInfoInternal->setSoBundle(true);
235 bundleInfoInternal->setJavaBundle(false);
236 registerSoBundle(bundleInfo);
238 // other cases might be for example .apk for android, which are loaded in the wrapper
241 bundleInfoInternal->setSoBundle(false);
242 bundleInfoInternal->setJavaBundle(false);
243 registerExtBundle(bundleInfo);
247 void ResourceContainerImpl::unregisterBundle(shared_ptr<RCSBundleInfo> bundleInfo)
249 shared_ptr<BundleInfoInternal> bundleInfoInternal =
250 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
251 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
253 if (bundleInfoInternal->getSoBundle())
255 unregisterBundleSo(bundleInfo->getID());
257 else if (bundleInfoInternal->getJavaBundle())
260 unregisterBundleJava(bundleInfo->getID());
266 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
268 void *bundleHandle = m_bundles[id]->getBundleHandle();
270 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
271 std::string(m_bundles[id]->getID()).c_str());
274 dlclose(bundleHandle);
276 if ((error = dlerror()) != NULL)
278 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error);
286 int ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
288 string strUri = resource->m_uri;
289 string strResourceType = resource->m_resourceType;
290 string strInterface = resource->m_interface;
291 RCSResourceObject::Ptr server = nullptr;
294 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" ,
295 std::string(strUri + ", " + strResourceType + "," +
296 resource->m_bundleId).c_str());
298 registrationLock.lock();
299 if (m_mapResources.find(strUri) == m_mapResources.end())
301 if (strInterface.empty())
303 strInterface = "oic.if.baseline";
306 server = buildResourceObject(strUri, strResourceType, strInterface);
308 if (server != nullptr)
310 m_mapServers[strUri] = server;
311 m_mapResources[strUri] = resource;
312 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
314 server->setGetRequestHandler(
315 std::bind(&ResourceContainerImpl::getRequestHandler, this,
316 std::placeholders::_1, std::placeholders::_2));
318 server->setSetRequestHandler(
319 std::bind(&ResourceContainerImpl::setRequestHandler, this,
320 std::placeholders::_1, std::placeholders::_2));
322 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
323 std::string(strUri + ", " +
324 strResourceType).c_str());
326 if (m_config && m_config->isHasInput(resource->m_bundleId))
328 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has input (%s)",
329 std::string(strUri + ", " +
330 strResourceType).c_str());
331 discoverInputResource(strUri);
335 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has no input (%s)",
336 std::string(strUri + ", " +
337 strResourceType).c_str());
339 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
340 std::string(strUri + ", " +
341 strResourceType).c_str());
343 // to get notified if bundle resource attributes are updated
344 resource->registerObserver(this);
350 OIC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
351 std::string(strUri + " already exists.").c_str());
354 registrationLock.unlock();
359 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
361 string strUri = resource->m_uri;
362 string strResourceType = resource->m_resourceType;
364 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
365 std::string(resource->m_uri + ", " +
366 resource->m_resourceType).c_str());
368 if (m_config && m_config->isHasInput(resource->m_bundleId))
370 OIC_LOG_V(INFO, CONTAINER_TAG, "Calling undiscover (%s)",
371 std::string(resource->m_uri + ", " +
372 resource->m_resourceType).c_str());
373 undiscoverInputResource(strUri);
376 if (m_mapServers.find(strUri) != m_mapServers.end())
378 OIC_LOG_V(INFO, CONTAINER_TAG, "Resetting server (%s)",
379 std::string(resource->m_uri + ", " +
380 resource->m_resourceType).c_str());
381 m_mapServers[strUri].reset();
383 m_mapResources.erase(m_mapResources.find(strUri));
385 OIC_LOG_V(INFO, CONTAINER_TAG, "Remove bundle resource (%s)",
386 std::string(resource->m_uri + ", " +
387 resource->m_resourceType).c_str());
388 m_mapBundleResources[resource->m_bundleId].remove(strUri);
392 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
393 configInfo *configOutput)
397 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
401 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
402 std::vector< resourceInfo > *configOutput)
406 m_config->getResourceConfiguration(bundleId, configOutput);
410 OIC_LOG_V(DEBUG, CONTAINER_TAG, "no config present ");
414 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
415 const RCSResourceAttributes &)
417 RCSResourceAttributes attr;
418 std::string strResourceUri = request.getResourceUri();
419 const std::map< std::string, std::string > &queryParams = request.getQueryParams();
421 OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s",strResourceUri.c_str());
423 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
424 && m_mapResources.find(strResourceUri) != m_mapResources.end())
426 if (m_mapResources[strResourceUri])
428 auto getFunction = [this, &attr, &strResourceUri, queryParams]()
430 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest(queryParams);
432 boost::thread getThread(getFunction);
433 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
437 OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %zu attributes",strResourceUri.c_str(), attr.size());
439 return RCSGetResponse::create(std::move(attr), 200);
442 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
443 const RCSResourceAttributes &attributes)
445 RCSResourceAttributes attr;
446 std::list<std::string> lstAttributes;
447 std::string strResourceUri = request.getResourceUri();
448 const std::map< std::string, std::string > &queryParams = request.getQueryParams();
450 OIC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %zu attributes",strResourceUri.c_str(), attributes.size());
452 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
453 && m_mapResources.find(strResourceUri) != m_mapResources.end())
455 if (m_mapResources[strResourceUri])
457 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr, queryParams]()
459 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
461 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
462 itor != attributes.end(); itor++)
464 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
465 != lstAttributes.end())
467 attr[itor->key()] = itor->value();
471 OIC_LOG_V(INFO, CONTAINER_TAG, "Calling handleSetAttributeRequest");
472 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr, queryParams);
474 boost::thread setThread(setFunction);
475 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
479 return RCSSetResponse::create(std::move(attr), 200);
482 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
484 OIC_LOG_V(INFO, CONTAINER_TAG,
485 "notification from (%s)", std::string(strResourceUri + ".").c_str());
487 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
489 m_mapServers[strResourceUri]->notify();
493 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
495 static ResourceContainerImpl m_instance;
499 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
500 const std::string &strResourceType, const std::string &strInterface)
502 return RCSResourceObject::Builder(strUri, strResourceType,
503 strInterface).setObservable(
504 true).setDiscoverable(true).build();
507 void ResourceContainerImpl::startBundle(const std::string &bundleId)
509 OIC_LOG_V(INFO, CONTAINER_TAG, "startBundle %s",bundleId.c_str());
510 if (m_bundles.find(bundleId) != m_bundles.end())
512 if (!m_bundles[bundleId]->isActivated())
514 activateBundle(m_bundles[bundleId]);
518 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
523 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
524 std::string(bundleId + "\' is not registered.").c_str());
528 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
530 OIC_LOG_V(INFO, CONTAINER_TAG, "stopBundle %s",bundleId.c_str());
531 if (m_bundles.find(bundleId) != m_bundles.end())
533 if (m_bundles[bundleId]->isActivated())
535 deactivateBundle(m_bundles[bundleId]);
539 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
544 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
545 std::string(bundleId + "\' is not registered.").c_str());
549 void ResourceContainerImpl::addBundle(const std::string &bundleId,
550 const std::string &bundleUri,
551 const std::string &bundlePath,
552 const std::string &activator,
553 std::map< string, string > params)
557 if (m_bundles.find(bundleId) != m_bundles.end())
559 OIC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
564 shared_ptr<BundleInfoInternal> bundleInfo = std::make_shared<BundleInfoInternal>();
565 bundleInfo->setID(bundleId);
566 bundleInfo->setPath(bundlePath);
567 bundleInfo->setActivatorName(activator);
568 if (params.find("libraryPath") != params.end())
570 string activatorName = activator; // modify activator for Java bundle
571 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
572 bundleInfo->setActivatorName(activatorName);
573 bundleInfo->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
576 OIC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
577 std::string(bundleInfo->getID() + "; " +
578 bundleInfo->getPath()).c_str());
580 registerBundle(bundleInfo);
584 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
586 OIC_LOG_V(INFO, CONTAINER_TAG, "removeBundle %s",bundleId.c_str());
587 if (m_bundles.find(bundleId) != m_bundles.end())
589 shared_ptr<BundleInfoInternal> bundleInfo = m_bundles[bundleId];
590 if (bundleInfo->isActivated())
592 deactivateBundle(bundleInfo);
595 if (bundleInfo->isLoaded())
597 unregisterBundle(bundleInfo);
602 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
603 std::string(bundleId + "\' is not ced.").c_str());
607 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
609 OIC_LOG_V(INFO, CONTAINER_TAG,
610 "list bundles (%d)", m_bundles.size());
611 std::list<std::unique_ptr<RCSBundleInfo> > ret;
612 for (std::map< std::string, shared_ptr<BundleInfoInternal> >::iterator it = m_bundles.begin();
613 it != m_bundles.end(); ++it)
616 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
617 bundleInfo->setBundleInfo(it->second);
618 ret.push_back(std::move(bundleInfo));
624 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
625 const std::string &resourceUri, std::map< string, string > params)
627 if (m_bundles.find(bundleId) != m_bundles.end())
629 if (!m_bundles[bundleId]->getJavaBundle())
631 resourceInfo newResourceInfo;
632 newResourceInfo.uri = resourceUri;
634 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
636 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
638 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
640 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
642 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
644 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
647 addSoBundleResource(bundleId, newResourceInfo);
652 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
653 std::string(bundleId + "\' is not registered.").c_str());
657 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
658 const std::string &resourceUri)
660 OIC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(),
661 resourceUri.c_str());
662 if (m_bundles.find(bundleId) != m_bundles.end())
664 if (m_bundles[bundleId]->getSoBundle())
666 removeSoBundleResource(bundleId, resourceUri);
671 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
672 std::string(bundleId + "\' is not registered.").c_str());
676 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
678 OIC_LOG_V(INFO, CONTAINER_TAG, "listBundleResources %s",bundleId.c_str());
679 std::list < string > ret;
681 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
683 ret = m_mapBundleResources[bundleId];
690 void ResourceContainerImpl::registerSoBundle(shared_ptr<RCSBundleInfo> bundleInfo)
692 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle");
695 activator_t *bundleActivator = NULL;
696 deactivator_t *bundleDeactivator = NULL;
697 resourceCreator_t *resourceCreator = NULL;
698 resourceDestroyer_t *resourceDestroyer = NULL;
699 shared_ptr<BundleInfoInternal> bundleInfoInternal =
700 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
702 void *bundleHandle = NULL;
703 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
704 if ((error = dlerror()) != NULL)
706 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
709 if (bundleHandle != NULL)
711 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s",
712 bundleInfoInternal->getActivatorName().c_str());
714 (activator_t *) dlsym(bundleHandle,
715 ("" + bundleInfoInternal->getActivatorName()
716 + "_externalActivateBundle").c_str());
717 if ((error = dlerror()) != NULL)
719 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
723 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
724 bundleInfoInternal->getActivatorName()
725 + "_externalActivateBundle").c_str());
728 (deactivator_t *) dlsym(bundleHandle,
729 ("" + bundleInfoInternal->getActivatorName()
730 + "_externalDeactivateBundle").c_str());
731 if ((error = dlerror()) != NULL)
733 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
737 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
738 bundleInfoInternal->getActivatorName()
739 + "_externalDeactivateBundle").c_str());
742 (resourceCreator_t *) dlsym(bundleHandle,
743 ("" + bundleInfoInternal->getActivatorName()
744 + "_externalCreateResource").c_str());
745 if ((error = dlerror()) != NULL)
747 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
751 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
752 bundleInfoInternal->getActivatorName()
753 + "_externalCreateResource").c_str());
756 (resourceDestroyer_t *) dlsym(bundleHandle,
757 ("" + bundleInfoInternal->getActivatorName()
758 + "_externalDestroyResource").c_str());
759 if ((error = dlerror()) != NULL)
761 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
765 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
766 bundleInfoInternal->getActivatorName()
767 + "_externalDestroyResource").c_str());
771 if ((error = dlerror()) != NULL)
773 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
777 bundleInfoInternal->setBundleActivator(bundleActivator);
778 bundleInfoInternal->setBundleDeactivator(bundleDeactivator);
779 bundleInfoInternal->setResourceCreator(resourceCreator);
780 bundleInfoInternal->setResourceDestroyer(resourceDestroyer);
781 bundleInfoInternal->setLoaded(true);
782 bundleInfoInternal->setBundleHandle(bundleHandle);
784 m_bundles[bundleInfo->getID()] = bundleInfoInternal;
789 if ((error = dlerror()) != NULL)
791 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
794 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished");
797 void ResourceContainerImpl::registerExtBundle(shared_ptr<RCSBundleInfo> bundleInfo)
799 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)",
800 std::string(bundleInfo->getID()).c_str());
801 OIC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)",
802 std::string(bundleInfo->getActivatorName()).c_str());
804 shared_ptr<BundleInfoInternal> bundleInfoInternal =
805 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
807 m_bundles[bundleInfo->getID()] = bundleInfoInternal;
808 // in this case at least the resource configuration needs to be loaded
809 // in order to mark potential input resources for soft sensors
810 std::vector< resourceInfo > temp;
811 OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource config(%s)",
812 std::string(bundleInfo->getID()).c_str());
813 getResourceConfiguration(bundleInfo->getID(),
816 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
819 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
821 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
823 if (bundleActivator != NULL)
825 bundleActivator(this, m_bundles[bundleId]->getID());
826 m_bundles[bundleId]->setActivated(true);
830 //Unload module and return error
831 OIC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
834 shared_ptr<BundleInfoInternal> bundleInfoInternal = m_bundles[bundleId];
835 bundleInfoInternal->setActivated(true);
839 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
841 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
842 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
844 OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource.");
845 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
846 OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource done.");
850 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
852 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource %s", outputResourceUri.c_str());
853 auto foundOutputResource = m_mapResources.find(outputResourceUri);
856 m_config->getResourceConfiguration(foundOutputResource->second->m_bundleId,
857 outputResourceUri, &info);
858 map< string, vector< map< string, string > > > resourceProperty = info.resourceProperty;
862 resourceProperty.at(INPUT_RESOURCE);
864 catch (std::out_of_range &e)
866 OIC_LOG_V(DEBUG, CONTAINER_TAG, "No input resource %s", outputResourceUri.c_str());
870 for (auto iter : resourceProperty)
872 if (iter.first.compare(INPUT_RESOURCE) == 0)
874 for (auto it : iter.second)
876 auto makeValue = [&](const std::string & reference) mutable -> std::string
878 std::string retStr = "";
881 retStr = it.at(reference);
883 catch (std::out_of_range &e)
889 std::string uri = makeValue(INPUT_RESOURCE_URI);
890 std::string type = makeValue(INPUT_RESOURCE_TYPE);
891 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
894 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Start discovery: %s, %s, %s", uri.c_str(),
895 type.c_str(), attributeName.c_str());
896 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
897 < DiscoverResourceUnit > (outputResourceUri);
898 newDiscoverUnit->startDiscover(
899 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
901 std::bind(&SoftSensorResource::onUpdatedInputResource,
902 std::static_pointer_cast< SoftSensorResource >
903 (foundOutputResource->second),
904 std::placeholders::_1, std::placeholders::_2));
906 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
908 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
910 foundDiscoverResource->second.push_back(newDiscoverUnit);
914 m_mapDiscoverResourceUnits.insert(
915 std::make_pair(outputResourceUri,
916 std::list< DiscoverResourceUnit::Ptr >
917 { newDiscoverUnit }));
924 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
926 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
928 OIC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
929 m_bundles[id]->getID()).c_str());
931 if (bundleDeactivator != NULL)
934 m_bundles[id]->setActivated(false);
938 //Unload module and return error
939 OIC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
943 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
944 resourceInfo newResourceInfo)
946 resourceCreator_t *resourceCreator;
948 resourceCreator = m_bundles[bundleId]->getResourceCreator();
950 if (resourceCreator != NULL)
952 resourceCreator(newResourceInfo);
956 OIC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
960 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
961 const std::string &resourceUri)
963 if (m_mapResources.find(resourceUri) != m_mapResources.end())
965 resourceDestroyer_t *resourceDestroyer =
966 m_bundles[bundleId]->getResourceDestroyer();
968 if (resourceDestroyer != NULL)
970 resourceDestroyer(m_mapResources[resourceUri]);
974 OIC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
979 void ResourceContainerImpl::activateBundleThread(const std::string &id)
981 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
982 std::string(m_bundles[id]->getID()).c_str());
984 if (m_bundles[id]->getJavaBundle())
987 activateJavaBundle(id);
990 else if (m_bundles[id]->getSoBundle())
992 activateSoBundle (id);
995 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
996 std::string(m_bundles[id]->getID()).c_str());
1000 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
1002 return m_bundleVM[bundleId];
1005 void ResourceContainerImpl::registerJavaBundle(shared_ptr<RCSBundleInfo> bundleInfo)
1007 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
1008 std::string(bundleInfo->getID()).c_str());
1011 JavaVMInitArgs vm_args;
1012 JavaVMOption options[3];
1014 shared_ptr<BundleInfoInternal> bundleInfoInternal =
1015 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
1018 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
1022 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
1023 std::string(bundleInfo->getPath() +
1024 " available.").c_str());
1028 OIC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
1029 std::string(bundleInfo->getPath() + " not available.").c_str());
1034 char optionString[] = "-Djava.compiler=NONE";
1035 options[0].optionString = optionString;
1036 char classpath[1000];
1037 strcpy(classpath, "-Djava.class.path=");
1038 strncat(classpath, bundleInfo->getPath().c_str(), BUNDLE_PATH_MAXLEN);
1040 OIC_LOG(INFO, CONTAINER_TAG,
1041 std::string("Configured classpath: ").append(classpath).c_str());
1043 options[1].optionString = classpath;
1045 char libraryPath[1000];
1046 strcpy(libraryPath, "-Djava.library.path=");
1047 strncat(libraryPath, bundleInfo->getLibraryPath().c_str(), BUNDLE_PATH_MAXLEN);
1048 options[2].optionString = libraryPath;
1050 OIC_LOG(INFO, CONTAINER_TAG,
1051 std::string("Configured library path: ").append(libraryPath).c_str());
1053 vm_args.version = JNI_VERSION_1_4;
1054 vm_args.options = options;
1055 vm_args.nOptions = 3;
1056 vm_args.ignoreUnrecognized = 1;
1059 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
1063 OIC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
1068 OIC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
1071 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
1073 const char *className = bundleInfoInternal->getActivatorName().c_str();
1075 OIC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
1076 bundleInfoInternal->getActivatorName() + "|").c_str());
1078 jclass bundleActivatorClass = env->FindClass(className);
1080 if (bundleActivatorClass == NULL)
1082 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1083 std::string( bundleInfoInternal->getID()
1084 + " bundle activator(" +
1085 bundleInfoInternal->getActivatorName()
1086 + ") not found ").c_str());
1090 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
1093 if (activateMethod == NULL)
1095 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1096 std::string( bundleInfoInternal->getID()
1097 + " activate bundle method not found ").c_str());
1100 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
1102 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
1105 if (deactivateMethod == NULL)
1107 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1108 std::string( bundleInfoInternal->getID()
1109 + " deactivate bundle method not found ").c_str());
1113 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
1115 jmethodID constructor;
1117 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
1119 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
1121 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
1123 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
1125 bundleInfoInternal->setLoaded(true);
1127 m_bundles[bundleInfo->getID()] = bundleInfoInternal;
1130 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
1135 void ResourceContainerImpl::activateJavaBundle(string bundleId)
1137 OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
1139 JavaVM *vm = getJavaVM(bundleId);
1140 bundleInfoInternal = m_bundles[bundleId];
1142 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1144 if (envStat == JNI_EDETACHED)
1146 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1148 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1151 else if (envStat == JNI_EVERSION)
1153 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1156 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1157 bundleInfoInternal->getJavaBundleActivatorMethod());
1159 m_bundles[bundleId]->setActivated(true);
1162 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
1164 OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
1166 JavaVM *vm = getJavaVM(bundleId);
1167 shared_ptr<BundleInfoInternal>bundleInfoInternal = m_bundles[bundleId];
1169 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1171 if (envStat == JNI_EDETACHED)
1173 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1175 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1178 else if (envStat == JNI_EVERSION)
1180 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1183 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1184 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1186 m_bundles[bundleId]->setActivated(false);
1189 void ResourceContainerImpl::unregisterBundleJava(string id)
1191 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1192 m_bundles[id]->getID()).c_str());
1194 OIC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1196 m_bundleVM[id]->DestroyJavaVM();
1198 m_bundles.erase(id);