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 OC_LOG(INFO, CONTAINER_TAG, "Starting resource container.");
64 OC_LOG(INFO, CONTAINER_TAG, "Resource container has Java support.");
66 OC_LOG(INFO, CONTAINER_TAG, "Resource container without Java support.");
70 activationLock.lock();
71 if (!configFile.empty())
73 m_config = new Configuration(configFile);
75 if (m_config->isLoaded())
78 m_config->getConfiguredBundles(&bundles);
80 for (unsigned int i = 0; i < bundles.size(); i++)
82 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
83 bundleInfo->setPath(bundles[i][BUNDLE_PATH]);
84 bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]);
85 bundleInfo->setID(bundles[i][BUNDLE_ID]);
86 if (!bundles[i][BUNDLE_ACTIVATOR].empty())
88 string activatorName = bundles[i][BUNDLE_ACTIVATOR];
89 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
90 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
91 ((BundleInfoInternal *) bundleInfo)->setLibraryPath(
92 bundles[i][BUNDLE_LIBRARY_PATH]);
95 OC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)",
96 std::string(bundles[i][BUNDLE_ID] + ";" +
97 bundles[i][BUNDLE_PATH]).c_str());
99 registerBundle(bundleInfo);
100 activateBundle(bundleInfo);
105 OC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path.");
110 OC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided.");
113 map<std::string, boost::thread >::iterator activatorIterator;
115 for (activatorIterator = m_activators.begin(); activatorIterator != m_activators.end();
118 activatorIterator->second.timed_join(
119 boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC));
120 // wait for bundles to be activated
122 activationLock.unlock();
123 OC_LOG(INFO, CONTAINER_TAG, "Resource container started.");
126 void ResourceContainerImpl::stopContainer()
128 OC_LOG(INFO, CONTAINER_TAG, "Stopping resource container.");
130 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
131 it != m_bundles.end(); ++it)
133 BundleInfoInternal *bundleInfo = it->second;
134 deactivateBundle(bundleInfo);
135 unregisterBundle(bundleInfo);
138 if (!m_mapServers.empty())
140 map< std::string, RCSResourceObject::Ptr >::iterator itor = m_mapServers.begin();
142 while (itor != m_mapServers.end())
144 (itor++)->second.reset();
147 m_mapResources.clear();
148 m_mapBundleResources.clear();
155 void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo)
157 activationLock.lock();
158 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
160 if (bundleInfoInternal->isLoaded())
162 activateBundle(bundleInfo->getID());
164 activationLock.unlock();
167 void ResourceContainerImpl::deactivateBundle(RCSBundleInfo *bundleInfo)
169 if (((BundleInfoInternal *) bundleInfo)->isActivated())
171 deactivateBundle(bundleInfo->getID());
175 void ResourceContainerImpl::activateBundle(const std::string &id)
177 OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
178 std::string(m_bundles[id]->getID()).c_str());
179 activationLock.lock();
180 auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this,
182 boost::thread activator(f);
183 activator.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
184 activationLock.unlock();
185 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
186 std::string(m_bundles[id]->getID()).c_str());
189 void ResourceContainerImpl::deactivateBundle(const std::string &id)
191 if (m_bundles[id]->getJavaBundle())
194 deactivateJavaBundle(id);
199 deactivateSoBundle(id);
204 void ResourceContainerImpl::registerBundle(RCSBundleInfo *bundleInfo)
206 OC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)",
207 std::string(bundleInfo->getPath()).c_str());
209 if (has_suffix(bundleInfo->getPath(), ".jar"))
212 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true);
213 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
214 registerJavaBundle(bundleInfo);
217 else if(has_suffix(bundleInfo->getPath(), ".so"))
219 ((BundleInfoInternal *) bundleInfo)->setSoBundle(true);
220 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
221 registerSoBundle(bundleInfo);
224 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
225 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
226 registerExtBundle(bundleInfo);
228 // other cases might be for example .apk for android, which are loaded in the wrapper
231 void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo)
233 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
234 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
236 if (bundleInfoInternal->getSoBundle())
238 unregisterBundleSo(bundleInfo->getID());
240 else if(bundleInfoInternal->getJavaBundle())
243 unregisterBundleJava(bundleInfo->getID());
249 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
251 void *bundleHandle = m_bundles[id]->getBundleHandle();
253 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
254 std::string(m_bundles[id]->getID()).c_str());
257 dlclose(bundleHandle);
259 if ((error = dlerror()) != NULL)
261 OC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error);
265 delete m_bundles[id];
270 void ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
272 string strUri = resource->m_uri;
273 string strResourceType = resource->m_resourceType;
274 RCSResourceObject::Ptr server = nullptr;
276 OC_LOG_V(INFO, CONTAINER_TAG, "Registration 2 of resource (%s)" ,
277 std::string(strUri + ", " + strResourceType).c_str());
279 registrationLock.lock();
280 if (m_mapResources.find(strUri) == m_mapResources.end())
282 server = buildResourceObject(strUri, strResourceType);
284 if (server != nullptr)
286 m_mapServers[strUri] = server;
287 m_mapResources[strUri] = resource;
288 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
290 server->setGetRequestHandler(
291 std::bind(&ResourceContainerImpl::getRequestHandler, this,
292 std::placeholders::_1, std::placeholders::_2));
294 server->setSetRequestHandler(
295 std::bind(&ResourceContainerImpl::setRequestHandler, this,
296 std::placeholders::_1, std::placeholders::_2));
298 OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
299 std::string(strUri + ", " +
300 strResourceType).c_str());
302 if (m_config->isHasInput(resource->m_bundleId))
304 discoverInputResource(strUri);
307 // to get notified if bundle resource attributes are updated
308 resource->registerObserver((NotificationReceiver *) this);
313 OC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
314 std::string(strUri + " already exists.").c_str());
316 registrationLock.unlock();
319 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
321 string strUri = resource->m_uri;
322 string strResourceType = resource->m_resourceType;
324 OC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
325 std::string(resource->m_uri + ", " +
326 resource->m_resourceType).c_str());
328 if (m_config->isHasInput(resource->m_bundleId))
330 undiscoverInputResource(strUri);
333 if (m_mapServers.find(strUri) != m_mapServers.end())
335 m_mapServers[strUri].reset();
337 m_mapResources.erase(m_mapResources.find(strUri));
338 m_mapBundleResources[resource->m_bundleId].remove(strUri);
342 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
343 configInfo *configOutput)
347 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
351 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
352 std::vector< resourceInfo > *configOutput)
356 m_config->getResourceConfiguration(bundleId, configOutput);
360 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
361 const RCSResourceAttributes &)
363 RCSResourceAttributes attr;
364 std::string strResourceUri = request.getResourceUri();
365 OC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s",strResourceUri.c_str());
367 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
368 && m_mapResources.find(strResourceUri) != m_mapResources.end())
370 if (m_mapResources[strResourceUri])
372 auto getFunction = [this, &attr, &strResourceUri]()
374 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest();
376 boost::thread getThread(getFunction);
377 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
381 OC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %d attributes",strResourceUri.c_str(), attr.size());
383 return RCSGetResponse::create(std::move(attr), 200);
386 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
387 const RCSResourceAttributes &attributes)
389 RCSResourceAttributes attr;
390 std::list<std::string> lstAttributes;
391 std::string strResourceUri = request.getResourceUri();
393 OC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %d attributes",strResourceUri.c_str(), attributes.size());
395 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
396 && m_mapResources.find(strResourceUri) != m_mapResources.end())
398 if (m_mapResources[strResourceUri])
400 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
402 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
404 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
405 itor != attributes.end(); itor++)
407 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
408 != lstAttributes.end())
410 attr[itor->key()] = itor->value();
414 OC_LOG_V(INFO, CONTAINER_TAG, "Calling handleSetAttributeRequest");
415 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
417 boost::thread setThread(setFunction);
418 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
422 return RCSSetResponse::create(std::move(attr), 200);
425 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
427 OC_LOG_V(INFO, CONTAINER_TAG,
428 "notification from (%s)", std::string(strResourceUri + ".").c_str());
430 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
432 m_mapServers[strResourceUri]->notify();
436 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
438 static ResourceContainerImpl m_instance;
442 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
443 const std::string &strResourceType)
445 return RCSResourceObject::Builder(strUri, strResourceType,
446 "oic.if.baseline").setObservable(
447 true).setDiscoverable(true).build();
450 void ResourceContainerImpl::startBundle(const std::string &bundleId)
452 if (m_bundles.find(bundleId) != m_bundles.end())
454 if (!m_bundles[bundleId]->isActivated())
455 activateBundle(m_bundles[bundleId]);
458 OC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
463 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
464 std::string(bundleId + "\' is not registered.").c_str());
468 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
470 if (m_bundles.find(bundleId) != m_bundles.end())
472 if (m_bundles[bundleId]->isActivated())
473 deactivateBundle(m_bundles[bundleId]);
476 OC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
481 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
482 std::string(bundleId + "\' is not registered.").c_str());
486 void ResourceContainerImpl::addBundle(const std::string &bundleId,
487 const std::string &bundleUri, const std::string &bundlePath,
488 const std::string &activator, std::map< string, string > params)
492 if (m_bundles.find(bundleId) != m_bundles.end())
493 OC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
497 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
498 bundleInfo->setID(bundleId);
499 bundleInfo->setPath(bundlePath);
500 bundleInfo->setActivatorName(activator);
501 if (params.find("libraryPath") != params.end())
503 string activatorName = activator; // modify activator for Java bundle
504 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
505 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
506 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
509 OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
510 std::string(bundleInfo->getID() + "; " +
511 bundleInfo->getPath()).c_str());
513 registerBundle(bundleInfo);
517 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
519 if (m_bundles.find(bundleId) != m_bundles.end())
521 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
522 if (bundleInfo->isActivated())
523 deactivateBundle(bundleInfo);
525 if (bundleInfo->isLoaded())
526 unregisterBundle(bundleInfo);
530 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
531 std::string(bundleId + "\' is not ced.").c_str());
535 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
537 OC_LOG_V(INFO, CONTAINER_TAG,
538 "list bundles (%d)", m_bundles.size());
539 std::list<std::unique_ptr<RCSBundleInfo> > ret;
540 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
541 it != m_bundles.end(); ++it)
544 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
545 (bundleInfo)->setBundleInfo(it->second);
546 ret.push_back(std::move(bundleInfo));
552 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
553 const std::string &resourceUri, std::map< string, string > params)
555 if (m_bundles.find(bundleId) != m_bundles.end())
557 if (!m_bundles[bundleId]->getJavaBundle())
559 resourceInfo newResourceInfo;
560 newResourceInfo.uri = resourceUri;
562 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
563 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
564 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
565 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
566 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
567 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
569 addSoBundleResource(bundleId, newResourceInfo);
574 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
575 std::string(bundleId + "\' is not registered.").c_str());
579 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
580 const std::string &resourceUri)
582 if (m_bundles.find(bundleId) != m_bundles.end())
584 if (m_bundles[bundleId]->getSoBundle())
586 removeSoBundleResource(bundleId, resourceUri);
591 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
592 std::string(bundleId + "\' is not registered.").c_str());
596 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
598 std::list < string > ret;
600 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
602 ret = m_mapBundleResources[bundleId];
609 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
611 OC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle");
614 activator_t *bundleActivator = NULL;
615 deactivator_t *bundleDeactivator = NULL;
616 resourceCreator_t *resourceCreator = NULL;
617 resourceDestroyer_t *resourceDestroyer = NULL;
618 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
619 void *bundleHandle = NULL;
620 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
621 if ((error = dlerror()) != NULL)
623 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
626 if (bundleHandle != NULL)
628 OC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s", bundleInfoInternal->getActivatorName().c_str());
630 (activator_t *) dlsym(bundleHandle,
631 ("" + bundleInfoInternal->getActivatorName()
632 + "_externalActivateBundle").c_str());
633 if ((error = dlerror()) != NULL)
635 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
638 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
639 + "_externalActivateBundle").c_str());
642 (deactivator_t *) dlsym(bundleHandle,
643 ("" + bundleInfoInternal->getActivatorName()
644 + "_externalDeactivateBundle").c_str());
645 if ((error = dlerror()) != NULL)
647 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
650 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
651 + "_externalDeactivateBundle").c_str());
654 (resourceCreator_t *) dlsym(bundleHandle,
655 ("" + bundleInfoInternal->getActivatorName()
656 + "_externalCreateResource").c_str());
657 if ((error = dlerror()) != NULL)
659 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
662 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
663 + "_externalCreateResource").c_str());
666 (resourceDestroyer_t *) dlsym(bundleHandle,
667 ("" + bundleInfoInternal->getActivatorName()
668 + "_externalDestroyResource").c_str());
669 if ((error = dlerror()) != NULL)
671 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
674 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
675 + "_externalDestroyResource").c_str());
679 if ((error = dlerror()) != NULL)
681 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
685 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
686 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
687 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
688 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
689 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
690 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
692 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
697 if ((error = dlerror()) != NULL)
699 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
702 OC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished");
705 void ResourceContainerImpl::registerExtBundle(RCSBundleInfo *bundleInfo){
706 OC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)",
707 std::string(bundleInfo->getID()).c_str());
708 OC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)",
709 std::string(bundleInfo->getActivatorName()).c_str());
711 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
713 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
716 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
718 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
720 if (bundleActivator != NULL)
722 bundleActivator(this, m_bundles[bundleId]->getID());
723 m_bundles[bundleId]->setActivated(true);
727 //Unload module and return error
728 OC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
731 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
732 bundleInfoInternal->setActivated(true);
736 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
738 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
739 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
741 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
745 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
747 OC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource%s", outputResourceUri.c_str());
748 auto foundOutputResource = m_mapResources.find(outputResourceUri);
749 auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
753 resourceProperty.at(INPUT_RESOURCE);
755 catch (std::out_of_range &e)
760 for (auto iter : resourceProperty)
762 if (iter.first.compare(INPUT_RESOURCE) == 0)
764 for (auto it : iter.second)
766 auto makeValue = [&](const std::string & reference) mutable -> std::string
768 std::string retStr = "";
771 retStr = it.at(reference);
773 catch (std::out_of_range &e)
779 std::string uri = makeValue(INPUT_RESOURCE_URI);
780 std::string type = makeValue(INPUT_RESOURCE_TYPE);
781 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
783 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
784 < DiscoverResourceUnit > (outputResourceUri);
785 newDiscoverUnit->startDiscover(
786 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
788 std::bind(&SoftSensorResource::onUpdatedInputResource,
789 std::static_pointer_cast< SoftSensorResource > (foundOutputResource->second),
790 std::placeholders::_1, std::placeholders::_2));
792 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
794 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
796 foundDiscoverResource->second.push_back(newDiscoverUnit);
800 m_mapDiscoverResourceUnits.insert(
801 std::make_pair(outputResourceUri,
802 std::list< DiscoverResourceUnit::Ptr >
803 { newDiscoverUnit }));
810 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
812 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
814 OC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
815 m_bundles[id]->getID()).c_str());
817 if (bundleDeactivator != NULL)
820 m_bundles[id]->setActivated(false);
824 //Unload module and return error
825 OC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
829 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
830 resourceInfo newResourceInfo)
832 resourceCreator_t *resourceCreator;
834 resourceCreator = m_bundles[bundleId]->getResourceCreator();
836 if (resourceCreator != NULL)
838 resourceCreator(newResourceInfo);
842 OC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
846 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
847 const std::string &resourceUri)
849 if (m_mapResources.find(resourceUri) != m_mapResources.end())
851 resourceDestroyer_t *resourceDestroyer =
852 m_bundles[bundleId]->getResourceDestroyer();
854 if (resourceDestroyer != NULL)
856 resourceDestroyer(m_mapResources[resourceUri]);
860 OC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
865 void ResourceContainerImpl::activateBundleThread(const std::string &id)
867 OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
868 std::string(m_bundles[id]->getID()).c_str());
870 if (m_bundles[id]->getJavaBundle())
873 activateJavaBundle(id);
876 else if(m_bundles[id]->getSoBundle())
878 activateSoBundle (id);
881 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
882 std::string(m_bundles[id]->getID()).c_str());
886 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
888 return m_bundleVM[bundleId];
891 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
893 OC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
894 std::string(bundleInfo->getID()).c_str());
897 JavaVMInitArgs vm_args;
898 JavaVMOption options[3];
900 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
902 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
906 OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
907 std::string(bundleInfo->getPath() +
908 " available.").c_str());
912 OC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
913 std::string(bundleInfo->getPath() + " not available.").c_str());
918 char optionString[] = "-Djava.compiler=NONE";
919 options[0].optionString = optionString;
920 char classpath[1000];
921 strcpy(classpath, "-Djava.class.path=");
922 strcat(classpath, bundleInfo->getPath().c_str());
924 OC_LOG(INFO, CONTAINER_TAG,
925 std::string("Configured classpath: ").append(classpath).c_str());
927 options[1].optionString = classpath;
929 char libraryPath[1000];
930 strcpy(libraryPath, "-Djava.library.path=");
931 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
932 options[2].optionString = libraryPath;
934 OC_LOG(INFO, CONTAINER_TAG,
935 std::string("Configured library path: ").append(libraryPath).c_str());
937 vm_args.version = JNI_VERSION_1_4;
938 vm_args.options = options;
939 vm_args.nOptions = 3;
940 vm_args.ignoreUnrecognized = 1;
943 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
947 OC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
952 OC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
955 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
957 const char *className = bundleInfoInternal->getActivatorName().c_str();
959 OC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
960 bundleInfoInternal->getActivatorName() + "|").c_str());
962 jclass bundleActivatorClass = env->FindClass(className);
964 if (bundleActivatorClass == NULL)
966 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
967 std::string( bundleInfoInternal->getID()
968 + " bundle activator(" + bundleInfoInternal->getActivatorName()
969 + ") not found ").c_str());
973 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
976 if (activateMethod == NULL)
978 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
979 std::string( bundleInfoInternal->getID()
980 + " activate bundle method not found ").c_str());
983 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
985 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
988 if (deactivateMethod == NULL)
990 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
991 std::string( bundleInfoInternal->getID()
992 + " deactivate bundle method not found ").c_str());
996 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
998 jmethodID constructor;
1000 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
1002 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
1004 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
1006 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
1008 bundleInfoInternal->setLoaded(true);
1010 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
1013 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
1018 void ResourceContainerImpl::activateJavaBundle(string bundleId)
1020 OC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
1022 JavaVM *vm = getJavaVM(bundleId);
1023 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1025 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1027 if (envStat == JNI_EDETACHED)
1029 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1031 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1034 else if (envStat == JNI_EVERSION)
1036 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1039 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1040 bundleInfoInternal->getJavaBundleActivatorMethod());
1042 m_bundles[bundleId]->setActivated(true);
1045 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
1047 OC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
1049 JavaVM *vm = getJavaVM(bundleId);
1050 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1052 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1054 if (envStat == JNI_EDETACHED)
1056 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1058 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1061 else if (envStat == JNI_EVERSION)
1063 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1066 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1067 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1069 m_bundles[bundleId]->setActivated(false);
1072 void ResourceContainerImpl::unregisterBundleJava(string id)
1074 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1075 m_bundles[id]->getID()).c_str());
1077 OC_LOG_V(INFO, CONTAINER_TAG, "Destroying JVM");
1079 m_bundleVM[id]->DestroyJavaVM();
1081 delete m_bundles[id];
1082 m_bundles.erase(id);