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 shared_ptr<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 bundleInfo->setActivatorName(activatorName);
91 bundleInfo->setLibraryPath(bundles[i][BUNDLE_LIBRARY_PATH]);
94 OIC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)",
95 std::string(bundles[i][BUNDLE_ID] + ";" +
96 bundles[i][BUNDLE_PATH]).c_str());
98 registerBundle(bundleInfo);
99 activateBundle(bundleInfo);
104 OIC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path.");
109 OIC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided.");
112 map<std::string, boost::thread >::iterator activatorIterator;
114 for (activatorIterator = m_activators.begin(); activatorIterator != m_activators.end();
117 activatorIterator->second.timed_join(
118 boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC));
119 // wait for bundles to be activated
121 activationLock.unlock();
122 OIC_LOG(INFO, CONTAINER_TAG, "Resource container started.");
125 void ResourceContainerImpl::stopContainer()
127 OIC_LOG(INFO, CONTAINER_TAG, "Stopping resource container.");
129 for (std::map< std::string, shared_ptr<BundleInfoInternal> >::iterator it = m_bundles.begin();
130 it != m_bundles.end(); ++it)
132 deactivateBundle(it->second);
133 unregisterBundle(it->second);
136 if (!m_mapServers.empty())
138 map< std::string, RCSResourceObject::Ptr >::iterator itor = m_mapServers.begin();
140 while (itor != m_mapServers.end())
142 (itor++)->second.reset();
145 m_mapResources.clear();
146 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());
181 activationLock.lock();
182 auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this,
184 boost::thread activator(f);
185 activator.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
186 activationLock.unlock();
187 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
188 std::string(m_bundles[id]->getID()).c_str());
191 void ResourceContainerImpl::deactivateBundle(const std::string &id)
193 if (m_bundles[id]->getJavaBundle())
196 deactivateJavaBundle(id);
201 deactivateSoBundle(id);
206 void ResourceContainerImpl::registerBundle(shared_ptr<RCSBundleInfo> bundleInfo)
208 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)",
209 std::string(bundleInfo->getPath()).c_str());
210 shared_ptr<BundleInfoInternal> bundleInfoInternal =
211 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
213 if (has_suffix(bundleInfo->getPath(), ".jar"))
216 bundleInfoInternal->setJavaBundle(true);
217 bundleInfoInternal->setSoBundle(false);
218 registerJavaBundle(bundleInfo);
220 // android .jar library
221 bundleInfoInternal->setSoBundle(false);
222 bundleInfoInternal->setJavaBundle(false);
223 registerExtBundle(bundleInfo);
226 else if(has_suffix(bundleInfo->getPath(), ".so"))
228 bundleInfoInternal->setSoBundle(true);
229 bundleInfoInternal->setJavaBundle(false);
230 registerSoBundle(bundleInfo);
232 // other cases might be for example .apk for android, which are loaded in the wrapper
234 bundleInfoInternal->setSoBundle(false);
235 bundleInfoInternal->setJavaBundle(false);
236 registerExtBundle(bundleInfo);
240 void ResourceContainerImpl::unregisterBundle(shared_ptr<RCSBundleInfo> bundleInfo)
242 shared_ptr<BundleInfoInternal> bundleInfoInternal =
243 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
244 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
246 if (bundleInfoInternal->getSoBundle())
248 unregisterBundleSo(bundleInfo->getID());
250 else if(bundleInfoInternal->getJavaBundle())
253 unregisterBundleJava(bundleInfo->getID());
259 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
261 void *bundleHandle = m_bundles[id]->getBundleHandle();
263 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
264 std::string(m_bundles[id]->getID()).c_str());
267 dlclose(bundleHandle);
269 if ((error = dlerror()) != NULL)
271 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error);
279 int ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
281 string strUri = resource->m_uri;
282 string strResourceType = resource->m_resourceType;
283 string strInterface = resource->m_interface;
284 RCSResourceObject::Ptr server = nullptr;
287 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" ,
288 std::string(strUri + ", " + strResourceType + "," +
289 resource->m_bundleId).c_str());
291 registrationLock.lock();
292 if (m_mapResources.find(strUri) == m_mapResources.end())
294 if (strInterface.empty()) {
295 strInterface = "oic.if.baseline";
298 server = buildResourceObject(strUri, strResourceType, strInterface);
300 if (server != nullptr)
302 m_mapServers[strUri] = server;
303 m_mapResources[strUri] = resource;
304 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
306 server->setGetRequestHandler(
307 std::bind(&ResourceContainerImpl::getRequestHandler, this,
308 std::placeholders::_1, std::placeholders::_2));
310 server->setSetRequestHandler(
311 std::bind(&ResourceContainerImpl::setRequestHandler, this,
312 std::placeholders::_1, std::placeholders::_2));
314 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
315 std::string(strUri + ", " +
316 strResourceType).c_str());
318 if (m_config && m_config->isHasInput(resource->m_bundleId))
320 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has input (%s)",
321 std::string(strUri + ", " +
322 strResourceType).c_str());
323 discoverInputResource(strUri);
326 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has no input (%s)",
327 std::string(strUri + ", " +
328 strResourceType).c_str());
330 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
331 std::string(strUri + ", " +
332 strResourceType).c_str());
334 // to get notified if bundle resource attributes are updated
335 resource->registerObserver(this);
341 OIC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
342 std::string(strUri + " already exists.").c_str());
345 registrationLock.unlock();
350 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
352 string strUri = resource->m_uri;
353 string strResourceType = resource->m_resourceType;
355 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
356 std::string(resource->m_uri + ", " +
357 resource->m_resourceType).c_str());
359 if (m_config && m_config->isHasInput(resource->m_bundleId))
361 OIC_LOG_V(INFO, CONTAINER_TAG, "Calling undiscover (%s)",
362 std::string(resource->m_uri + ", " +
363 resource->m_resourceType).c_str());
364 undiscoverInputResource(strUri);
367 if (m_mapServers.find(strUri) != m_mapServers.end())
369 OIC_LOG_V(INFO, CONTAINER_TAG, "Resetting server (%s)",
370 std::string(resource->m_uri + ", " +
371 resource->m_resourceType).c_str());
372 m_mapServers[strUri].reset();
374 m_mapResources.erase(m_mapResources.find(strUri));
376 OIC_LOG_V(INFO, CONTAINER_TAG, "Remove bundle resource (%s)",
377 std::string(resource->m_uri + ", " +
378 resource->m_resourceType).c_str());
379 m_mapBundleResources[resource->m_bundleId].remove(strUri);
383 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
384 configInfo *configOutput)
388 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
392 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
393 std::vector< resourceInfo > *configOutput)
397 m_config->getResourceConfiguration(bundleId, configOutput);
400 OIC_LOG_V(DEBUG, CONTAINER_TAG, "no config present ");
404 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
405 const RCSResourceAttributes &)
407 RCSResourceAttributes attr;
408 std::string strResourceUri = request.getResourceUri();
409 OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s",strResourceUri.c_str());
411 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
412 && m_mapResources.find(strResourceUri) != m_mapResources.end())
414 if (m_mapResources[strResourceUri])
416 auto getFunction = [this, &attr, &strResourceUri]()
418 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest();
420 boost::thread getThread(getFunction);
421 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
425 OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %zu attributes",strResourceUri.c_str(), attr.size());
427 return RCSGetResponse::create(std::move(attr), 200);
430 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
431 const RCSResourceAttributes &attributes)
433 RCSResourceAttributes attr;
434 std::list<std::string> lstAttributes;
435 std::string strResourceUri = request.getResourceUri();
437 OIC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %zu attributes",strResourceUri.c_str(), attributes.size());
439 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
440 && m_mapResources.find(strResourceUri) != m_mapResources.end())
442 if (m_mapResources[strResourceUri])
444 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
446 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
448 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
449 itor != attributes.end(); itor++)
451 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
452 != lstAttributes.end())
454 attr[itor->key()] = itor->value();
458 OIC_LOG_V(INFO, CONTAINER_TAG, "Calling handleSetAttributeRequest");
459 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
461 boost::thread setThread(setFunction);
462 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
466 return RCSSetResponse::create(std::move(attr), 200);
469 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
471 OIC_LOG_V(INFO, CONTAINER_TAG,
472 "notification from (%s)", std::string(strResourceUri + ".").c_str());
474 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
476 m_mapServers[strResourceUri]->notify();
480 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
482 static ResourceContainerImpl m_instance;
486 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
487 const std::string &strResourceType, const std::string &strInterface)
489 return RCSResourceObject::Builder(strUri, strResourceType,
490 strInterface).setObservable(
491 true).setDiscoverable(true).build();
494 void ResourceContainerImpl::startBundle(const std::string &bundleId)
496 OIC_LOG_V(INFO, CONTAINER_TAG, "startBundle %s",bundleId.c_str());
497 if (m_bundles.find(bundleId) != m_bundles.end())
499 if (!m_bundles[bundleId]->isActivated())
500 activateBundle(m_bundles[bundleId]);
503 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
508 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
509 std::string(bundleId + "\' is not registered.").c_str());
513 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
515 OIC_LOG_V(INFO, CONTAINER_TAG, "stopBundle %s",bundleId.c_str());
516 if (m_bundles.find(bundleId) != m_bundles.end())
518 if (m_bundles[bundleId]->isActivated())
519 deactivateBundle(m_bundles[bundleId]);
522 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
527 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
528 std::string(bundleId + "\' is not registered.").c_str());
532 void ResourceContainerImpl::addBundle(const std::string &bundleId,
533 const std::string &bundleUri,
534 const std::string &bundlePath,
535 const std::string &activator,
536 std::map< string, string > params)
540 if (m_bundles.find(bundleId) != m_bundles.end())
541 OIC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
545 shared_ptr<BundleInfoInternal> bundleInfo = std::make_shared<BundleInfoInternal>();
546 bundleInfo->setID(bundleId);
547 bundleInfo->setPath(bundlePath);
548 bundleInfo->setActivatorName(activator);
549 if (params.find("libraryPath") != params.end())
551 string activatorName = activator; // modify activator for Java bundle
552 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
553 bundleInfo->setActivatorName(activatorName);
554 bundleInfo->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
557 OIC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
558 std::string(bundleInfo->getID() + "; " +
559 bundleInfo->getPath()).c_str());
561 registerBundle(bundleInfo);
565 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
567 OIC_LOG_V(INFO, CONTAINER_TAG, "removeBundle %s",bundleId.c_str());
568 if (m_bundles.find(bundleId) != m_bundles.end())
570 shared_ptr<BundleInfoInternal> bundleInfo = m_bundles[bundleId];
571 if (bundleInfo->isActivated())
572 deactivateBundle(bundleInfo);
574 if (bundleInfo->isLoaded())
575 unregisterBundle(bundleInfo);
579 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
580 std::string(bundleId + "\' is not ced.").c_str());
584 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
586 OIC_LOG_V(INFO, CONTAINER_TAG,
587 "list bundles (%d)", m_bundles.size());
588 std::list<std::unique_ptr<RCSBundleInfo> > ret;
589 for (std::map< std::string, shared_ptr<BundleInfoInternal> >::iterator it = m_bundles.begin();
590 it != m_bundles.end(); ++it)
593 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
594 bundleInfo->setBundleInfo(it->second);
595 ret.push_back(std::move(bundleInfo));
601 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
602 const std::string &resourceUri, std::map< string, string > params)
604 if (m_bundles.find(bundleId) != m_bundles.end())
606 if (!m_bundles[bundleId]->getJavaBundle())
608 resourceInfo newResourceInfo;
609 newResourceInfo.uri = resourceUri;
611 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
612 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
613 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
614 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
615 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
616 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
618 addSoBundleResource(bundleId, newResourceInfo);
623 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
624 std::string(bundleId + "\' is not registered.").c_str());
628 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
629 const std::string &resourceUri)
631 OIC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(),
632 resourceUri.c_str());
633 if (m_bundles.find(bundleId) != m_bundles.end())
635 if (m_bundles[bundleId]->getSoBundle())
637 removeSoBundleResource(bundleId, resourceUri);
642 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
643 std::string(bundleId + "\' is not registered.").c_str());
647 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
649 OIC_LOG_V(INFO, CONTAINER_TAG, "listBundleResources %s",bundleId.c_str());
650 std::list < string > ret;
652 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
654 ret = m_mapBundleResources[bundleId];
661 void ResourceContainerImpl::registerSoBundle(shared_ptr<RCSBundleInfo> bundleInfo)
663 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle");
666 activator_t *bundleActivator = NULL;
667 deactivator_t *bundleDeactivator = NULL;
668 resourceCreator_t *resourceCreator = NULL;
669 resourceDestroyer_t *resourceDestroyer = NULL;
670 shared_ptr<BundleInfoInternal> bundleInfoInternal =
671 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
673 void *bundleHandle = NULL;
674 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
675 if ((error = dlerror()) != NULL)
677 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
680 if (bundleHandle != NULL)
682 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s",
683 bundleInfoInternal->getActivatorName().c_str());
685 (activator_t *) dlsym(bundleHandle,
686 ("" + bundleInfoInternal->getActivatorName()
687 + "_externalActivateBundle").c_str());
688 if ((error = dlerror()) != NULL)
690 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
693 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
694 bundleInfoInternal->getActivatorName()
695 + "_externalActivateBundle").c_str());
698 (deactivator_t *) dlsym(bundleHandle,
699 ("" + bundleInfoInternal->getActivatorName()
700 + "_externalDeactivateBundle").c_str());
701 if ((error = dlerror()) != NULL)
703 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
706 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
707 bundleInfoInternal->getActivatorName()
708 + "_externalDeactivateBundle").c_str());
711 (resourceCreator_t *) dlsym(bundleHandle,
712 ("" + bundleInfoInternal->getActivatorName()
713 + "_externalCreateResource").c_str());
714 if ((error = dlerror()) != NULL)
716 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
719 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
720 bundleInfoInternal->getActivatorName()
721 + "_externalCreateResource").c_str());
724 (resourceDestroyer_t *) dlsym(bundleHandle,
725 ("" + bundleInfoInternal->getActivatorName()
726 + "_externalDestroyResource").c_str());
727 if ((error = dlerror()) != NULL)
729 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
732 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" +
733 bundleInfoInternal->getActivatorName()
734 + "_externalDestroyResource").c_str());
738 if ((error = dlerror()) != NULL)
740 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
744 bundleInfoInternal->setBundleActivator(bundleActivator);
745 bundleInfoInternal->setBundleDeactivator(bundleDeactivator);
746 bundleInfoInternal->setResourceCreator(resourceCreator);
747 bundleInfoInternal->setResourceDestroyer(resourceDestroyer);
748 bundleInfoInternal->setLoaded(true);
749 bundleInfoInternal->setBundleHandle(bundleHandle);
751 m_bundles[bundleInfo->getID()] = bundleInfoInternal;
756 if ((error = dlerror()) != NULL)
758 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
761 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished");
764 void ResourceContainerImpl::registerExtBundle(shared_ptr<RCSBundleInfo> bundleInfo){
765 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)",
766 std::string(bundleInfo->getID()).c_str());
767 OIC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)",
768 std::string(bundleInfo->getActivatorName()).c_str());
770 shared_ptr<BundleInfoInternal> bundleInfoInternal =
771 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
773 m_bundles[bundleInfo->getID()] = bundleInfoInternal;
774 // in this case at least the resource configuration needs to be loaded
775 // in order to mark potential input resources for soft sensors
776 std::vector< resourceInfo > temp;
777 OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource config(%s)",
778 std::string(bundleInfo->getID()).c_str());
779 getResourceConfiguration(bundleInfo->getID(),
782 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
785 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
787 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
789 if (bundleActivator != NULL)
791 bundleActivator(this, m_bundles[bundleId]->getID());
792 m_bundles[bundleId]->setActivated(true);
796 //Unload module and return error
797 OIC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
800 shared_ptr<BundleInfoInternal> bundleInfoInternal = m_bundles[bundleId];
801 bundleInfoInternal->setActivated(true);
805 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
807 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
808 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
810 OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource.");
811 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
812 OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource done.");
816 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
818 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource %s", outputResourceUri.c_str());
819 auto foundOutputResource = m_mapResources.find(outputResourceUri);
820 // auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
823 m_config->getResourceConfiguration(foundOutputResource->second->m_bundleId,
824 foundOutputResource->second->m_name, &info);
825 map< string, vector< map< string, string > > > resourceProperty = info.resourceProperty;
829 resourceProperty.at(INPUT_RESOURCE);
831 catch (std::out_of_range &e)
833 OIC_LOG_V(DEBUG, CONTAINER_TAG, "No input resource %s", outputResourceUri.c_str());
837 for (auto iter : resourceProperty)
839 if (iter.first.compare(INPUT_RESOURCE) == 0)
841 for (auto it : iter.second)
843 auto makeValue = [&](const std::string & reference) mutable -> std::string
845 std::string retStr = "";
848 retStr = it.at(reference);
850 catch (std::out_of_range &e)
856 std::string uri = makeValue(INPUT_RESOURCE_URI);
857 std::string type = makeValue(INPUT_RESOURCE_TYPE);
858 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
861 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Start discovery: %s, %s, %s", uri.c_str(),
862 type.c_str(), attributeName.c_str());
863 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
864 < DiscoverResourceUnit > (outputResourceUri);
865 newDiscoverUnit->startDiscover(
866 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
868 std::bind(&SoftSensorResource::onUpdatedInputResource,
869 std::static_pointer_cast< SoftSensorResource >
870 (foundOutputResource->second),
871 std::placeholders::_1, std::placeholders::_2));
873 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
875 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
877 foundDiscoverResource->second.push_back(newDiscoverUnit);
881 m_mapDiscoverResourceUnits.insert(
882 std::make_pair(outputResourceUri,
883 std::list< DiscoverResourceUnit::Ptr >
884 { newDiscoverUnit }));
891 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
893 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
895 OIC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
896 m_bundles[id]->getID()).c_str());
898 if (bundleDeactivator != NULL)
901 m_bundles[id]->setActivated(false);
905 //Unload module and return error
906 OIC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
910 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
911 resourceInfo newResourceInfo)
913 resourceCreator_t *resourceCreator;
915 resourceCreator = m_bundles[bundleId]->getResourceCreator();
917 if (resourceCreator != NULL)
919 resourceCreator(newResourceInfo);
923 OIC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
927 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
928 const std::string &resourceUri)
930 if (m_mapResources.find(resourceUri) != m_mapResources.end())
932 resourceDestroyer_t *resourceDestroyer =
933 m_bundles[bundleId]->getResourceDestroyer();
935 if (resourceDestroyer != NULL)
937 resourceDestroyer(m_mapResources[resourceUri]);
941 OIC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
946 void ResourceContainerImpl::activateBundleThread(const std::string &id)
948 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
949 std::string(m_bundles[id]->getID()).c_str());
951 if (m_bundles[id]->getJavaBundle())
954 activateJavaBundle(id);
957 else if(m_bundles[id]->getSoBundle())
959 activateSoBundle (id);
962 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
963 std::string(m_bundles[id]->getID()).c_str());
967 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
969 return m_bundleVM[bundleId];
972 void ResourceContainerImpl::registerJavaBundle(shared_ptr<RCSBundleInfo> bundleInfo)
974 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
975 std::string(bundleInfo->getID()).c_str());
978 JavaVMInitArgs vm_args;
979 JavaVMOption options[3];
981 shared_ptr<BundleInfoInternal> bundleInfoInternal =
982 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
985 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
989 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
990 std::string(bundleInfo->getPath() +
991 " available.").c_str());
995 OIC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
996 std::string(bundleInfo->getPath() + " not available.").c_str());
1001 char optionString[] = "-Djava.compiler=NONE";
1002 options[0].optionString = optionString;
1003 char classpath[1000];
1004 strcpy(classpath, "-Djava.class.path=");
1005 strncat(classpath, bundleInfo->getPath().c_str(), BUNDLE_PATH_MAXLEN);
1007 OIC_LOG(INFO, CONTAINER_TAG,
1008 std::string("Configured classpath: ").append(classpath).c_str());
1010 options[1].optionString = classpath;
1012 char libraryPath[1000];
1013 strcpy(libraryPath, "-Djava.library.path=");
1014 strncat(libraryPath, bundleInfo->getLibraryPath().c_str(), BUNDLE_PATH_MAXLEN);
1015 options[2].optionString = libraryPath;
1017 OIC_LOG(INFO, CONTAINER_TAG,
1018 std::string("Configured library path: ").append(libraryPath).c_str());
1020 vm_args.version = JNI_VERSION_1_4;
1021 vm_args.options = options;
1022 vm_args.nOptions = 3;
1023 vm_args.ignoreUnrecognized = 1;
1026 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
1030 OIC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
1035 OIC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
1038 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
1040 const char *className = bundleInfoInternal->getActivatorName().c_str();
1042 OIC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
1043 bundleInfoInternal->getActivatorName() + "|").c_str());
1045 jclass bundleActivatorClass = env->FindClass(className);
1047 if (bundleActivatorClass == NULL)
1049 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1050 std::string( bundleInfoInternal->getID()
1051 + " bundle activator(" +
1052 bundleInfoInternal->getActivatorName()
1053 + ") not found ").c_str());
1057 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
1060 if (activateMethod == NULL)
1062 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1063 std::string( bundleInfoInternal->getID()
1064 + " activate bundle method not found ").c_str());
1067 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
1069 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
1072 if (deactivateMethod == NULL)
1074 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1075 std::string( bundleInfoInternal->getID()
1076 + " deactivate bundle method not found ").c_str());
1080 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
1082 jmethodID constructor;
1084 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
1086 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
1088 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
1090 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
1092 bundleInfoInternal->setLoaded(true);
1094 m_bundles[bundleInfo->getID()] = bundleInfoInternal;
1097 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
1102 void ResourceContainerImpl::activateJavaBundle(string bundleId)
1104 OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
1106 JavaVM *vm = getJavaVM(bundleId);
1107 bundleInfoInternal = m_bundles[bundleId];
1109 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1111 if (envStat == JNI_EDETACHED)
1113 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1115 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1118 else if (envStat == JNI_EVERSION)
1120 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1123 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1124 bundleInfoInternal->getJavaBundleActivatorMethod());
1126 m_bundles[bundleId]->setActivated(true);
1129 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
1131 OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
1133 JavaVM *vm = getJavaVM(bundleId);
1134 shared_ptr<BundleInfoInternal>bundleInfoInternal = m_bundles[bundleId];
1136 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1138 if (envStat == JNI_EDETACHED)
1140 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1142 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1145 else if (envStat == JNI_EVERSION)
1147 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1150 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1151 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1153 m_bundles[bundleId]->setActivated(false);
1156 void ResourceContainerImpl::unregisterBundleJava(string id)
1158 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1159 m_bundles[id]->getID()).c_str());
1161 OIC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1163 m_bundleVM[id]->DestroyJavaVM();
1165 m_bundles.erase(id);