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, %d 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, %d 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);
822 m_config->getResourceConfiguration(foundOutputResource->second->m_bundleId,
823 outputResourceUri, &info);
824 map< string, vector< map< string, string > > > resourceProperty = info.resourceProperty;
828 resourceProperty.at(INPUT_RESOURCE);
830 catch (std::out_of_range &e)
832 OIC_LOG_V(DEBUG, CONTAINER_TAG, "No input resource %s", outputResourceUri.c_str());
836 for (auto iter : resourceProperty)
838 if (iter.first.compare(INPUT_RESOURCE) == 0)
840 for (auto it : iter.second)
842 auto makeValue = [&](const std::string & reference) mutable -> std::string
844 std::string retStr = "";
847 retStr = it.at(reference);
849 catch (std::out_of_range &e)
855 std::string uri = makeValue(INPUT_RESOURCE_URI);
856 std::string type = makeValue(INPUT_RESOURCE_TYPE);
857 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
860 OIC_LOG_V(DEBUG, CONTAINER_TAG, "Start discovery: %s, %s, %s", uri.c_str(),
861 type.c_str(), attributeName.c_str());
862 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
863 < DiscoverResourceUnit > (outputResourceUri);
864 newDiscoverUnit->startDiscover(
865 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
867 std::bind(&SoftSensorResource::onUpdatedInputResource,
868 std::static_pointer_cast< SoftSensorResource >
869 (foundOutputResource->second),
870 std::placeholders::_1, std::placeholders::_2));
872 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
874 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
876 foundDiscoverResource->second.push_back(newDiscoverUnit);
880 m_mapDiscoverResourceUnits.insert(
881 std::make_pair(outputResourceUri,
882 std::list< DiscoverResourceUnit::Ptr >
883 { newDiscoverUnit }));
890 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
892 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
894 OIC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
895 m_bundles[id]->getID()).c_str());
897 if (bundleDeactivator != NULL)
900 m_bundles[id]->setActivated(false);
904 //Unload module and return error
905 OIC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
909 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
910 resourceInfo newResourceInfo)
912 resourceCreator_t *resourceCreator;
914 resourceCreator = m_bundles[bundleId]->getResourceCreator();
916 if (resourceCreator != NULL)
918 resourceCreator(newResourceInfo);
922 OIC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
926 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
927 const std::string &resourceUri)
929 if (m_mapResources.find(resourceUri) != m_mapResources.end())
931 resourceDestroyer_t *resourceDestroyer =
932 m_bundles[bundleId]->getResourceDestroyer();
934 if (resourceDestroyer != NULL)
936 resourceDestroyer(m_mapResources[resourceUri]);
940 OIC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
945 void ResourceContainerImpl::activateBundleThread(const std::string &id)
947 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
948 std::string(m_bundles[id]->getID()).c_str());
950 if (m_bundles[id]->getJavaBundle())
953 activateJavaBundle(id);
956 else if(m_bundles[id]->getSoBundle())
958 activateSoBundle (id);
961 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
962 std::string(m_bundles[id]->getID()).c_str());
966 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
968 return m_bundleVM[bundleId];
971 void ResourceContainerImpl::registerJavaBundle(shared_ptr<RCSBundleInfo> bundleInfo)
973 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
974 std::string(bundleInfo->getID()).c_str());
977 JavaVMInitArgs vm_args;
978 JavaVMOption options[3];
980 shared_ptr<BundleInfoInternal> bundleInfoInternal =
981 std::static_pointer_cast<BundleInfoInternal>(bundleInfo);
984 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
988 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
989 std::string(bundleInfo->getPath() +
990 " available.").c_str());
994 OIC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
995 std::string(bundleInfo->getPath() + " not available.").c_str());
1000 char optionString[] = "-Djava.compiler=NONE";
1001 options[0].optionString = optionString;
1002 char classpath[1000];
1003 strcpy(classpath, "-Djava.class.path=");
1004 strncat(classpath, bundleInfo->getPath().c_str(), BUNDLE_PATH_MAXLEN);
1006 OIC_LOG(INFO, CONTAINER_TAG,
1007 std::string("Configured classpath: ").append(classpath).c_str());
1009 options[1].optionString = classpath;
1011 char libraryPath[1000];
1012 strcpy(libraryPath, "-Djava.library.path=");
1013 strncat(libraryPath, bundleInfo->getLibraryPath().c_str(), BUNDLE_PATH_MAXLEN);
1014 options[2].optionString = libraryPath;
1016 OIC_LOG(INFO, CONTAINER_TAG,
1017 std::string("Configured library path: ").append(libraryPath).c_str());
1019 vm_args.version = JNI_VERSION_1_4;
1020 vm_args.options = options;
1021 vm_args.nOptions = 3;
1022 vm_args.ignoreUnrecognized = 1;
1025 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
1029 OIC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
1034 OIC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
1037 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
1039 const char *className = bundleInfoInternal->getActivatorName().c_str();
1041 OIC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
1042 bundleInfoInternal->getActivatorName() + "|").c_str());
1044 jclass bundleActivatorClass = env->FindClass(className);
1046 if (bundleActivatorClass == NULL)
1048 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1049 std::string( bundleInfoInternal->getID()
1050 + " bundle activator(" +
1051 bundleInfoInternal->getActivatorName()
1052 + ") not found ").c_str());
1056 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
1059 if (activateMethod == NULL)
1061 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1062 std::string( bundleInfoInternal->getID()
1063 + " activate bundle method not found ").c_str());
1066 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
1068 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
1071 if (deactivateMethod == NULL)
1073 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1074 std::string( bundleInfoInternal->getID()
1075 + " deactivate bundle method not found ").c_str());
1079 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
1081 jmethodID constructor;
1083 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
1085 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
1087 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
1089 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
1091 bundleInfoInternal->setLoaded(true);
1093 m_bundles[bundleInfo->getID()] = bundleInfoInternal;
1096 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
1101 void ResourceContainerImpl::activateJavaBundle(string bundleId)
1103 OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
1105 JavaVM *vm = getJavaVM(bundleId);
1106 bundleInfoInternal = m_bundles[bundleId];
1108 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1110 if (envStat == JNI_EDETACHED)
1112 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1114 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1117 else if (envStat == JNI_EVERSION)
1119 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1122 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1123 bundleInfoInternal->getJavaBundleActivatorMethod());
1125 m_bundles[bundleId]->setActivated(true);
1128 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
1130 OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
1132 JavaVM *vm = getJavaVM(bundleId);
1133 shared_ptr<BundleInfoInternal>bundleInfoInternal = m_bundles[bundleId];
1135 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1137 if (envStat == JNI_EDETACHED)
1139 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1141 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1144 else if (envStat == JNI_EVERSION)
1146 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1149 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1150 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1152 m_bundles[bundleId]->setActivated(false);
1155 void ResourceContainerImpl::unregisterBundleJava(string id)
1157 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1158 m_bundles[id]->getID()).c_str());
1160 OIC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1162 m_bundleVM[id]->DestroyJavaVM();
1164 m_bundles.erase(id);