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);
223 // other cases might be for example .apk for android, which are loaded in the wrapper
225 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
226 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
227 registerExtBundle(bundleInfo);
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 of resource (%s)" ,
277 std::string(strUri + ", " + strResourceType + "," + resource->m_bundleId).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));
300 if (m_config->isHasInput(resource->m_bundleId))
302 OC_LOG_V(INFO, CONTAINER_TAG, "Resource has input (%s)",
303 std::string(strUri + ", " +
304 strResourceType).c_str());
305 discoverInputResource(strUri);
308 OC_LOG_V(INFO, CONTAINER_TAG, "Resource has no input (%s)",
309 std::string(strUri + ", " +
310 strResourceType).c_str());
312 OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
313 std::string(strUri + ", " +
314 strResourceType).c_str());
316 // to get notified if bundle resource attributes are updated
317 resource->registerObserver((NotificationReceiver *) this);
322 OC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
323 std::string(strUri + " already exists.").c_str());
325 registrationLock.unlock();
328 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
330 string strUri = resource->m_uri;
331 string strResourceType = resource->m_resourceType;
333 OC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
334 std::string(resource->m_uri + ", " +
335 resource->m_resourceType).c_str());
337 if (m_config->isHasInput(resource->m_bundleId))
339 undiscoverInputResource(strUri);
342 if (m_mapServers.find(strUri) != m_mapServers.end())
344 m_mapServers[strUri].reset();
346 m_mapResources.erase(m_mapResources.find(strUri));
347 m_mapBundleResources[resource->m_bundleId].remove(strUri);
351 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
352 configInfo *configOutput)
356 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
360 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
361 std::vector< resourceInfo > *configOutput)
365 m_config->getResourceConfiguration(bundleId, configOutput);
368 OC_LOG_V(DEBUG, CONTAINER_TAG, "no config present ");
372 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
373 const RCSResourceAttributes &)
375 RCSResourceAttributes attr;
376 std::string strResourceUri = request.getResourceUri();
377 OC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s",strResourceUri.c_str());
379 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
380 && m_mapResources.find(strResourceUri) != m_mapResources.end())
382 if (m_mapResources[strResourceUri])
384 auto getFunction = [this, &attr, &strResourceUri]()
386 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest();
388 boost::thread getThread(getFunction);
389 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
393 OC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %d attributes",strResourceUri.c_str(), attr.size());
395 return RCSGetResponse::create(std::move(attr), 200);
398 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
399 const RCSResourceAttributes &attributes)
401 RCSResourceAttributes attr;
402 std::list<std::string> lstAttributes;
403 std::string strResourceUri = request.getResourceUri();
405 OC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %d attributes",strResourceUri.c_str(), attributes.size());
407 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
408 && m_mapResources.find(strResourceUri) != m_mapResources.end())
410 if (m_mapResources[strResourceUri])
412 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
414 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
416 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
417 itor != attributes.end(); itor++)
419 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
420 != lstAttributes.end())
422 attr[itor->key()] = itor->value();
426 OC_LOG_V(INFO, CONTAINER_TAG, "Calling handleSetAttributeRequest");
427 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
429 boost::thread setThread(setFunction);
430 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
434 return RCSSetResponse::create(std::move(attr), 200);
437 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
439 OC_LOG_V(INFO, CONTAINER_TAG,
440 "notification from (%s)", std::string(strResourceUri + ".").c_str());
442 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
444 m_mapServers[strResourceUri]->notify();
448 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
450 static ResourceContainerImpl m_instance;
454 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
455 const std::string &strResourceType)
457 return RCSResourceObject::Builder(strUri, strResourceType,
458 "oic.if.baseline").setObservable(
459 true).setDiscoverable(true).build();
462 void ResourceContainerImpl::startBundle(const std::string &bundleId)
464 OC_LOG_V(INFO, CONTAINER_TAG, "startBundle %s",bundleId.c_str());
465 if (m_bundles.find(bundleId) != m_bundles.end())
467 if (!m_bundles[bundleId]->isActivated())
468 activateBundle(m_bundles[bundleId]);
471 OC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
476 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
477 std::string(bundleId + "\' is not registered.").c_str());
481 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
483 OC_LOG_V(INFO, CONTAINER_TAG, "stopBundle %s",bundleId.c_str());
484 if (m_bundles.find(bundleId) != m_bundles.end())
486 if (m_bundles[bundleId]->isActivated())
487 deactivateBundle(m_bundles[bundleId]);
490 OC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
495 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
496 std::string(bundleId + "\' is not registered.").c_str());
500 void ResourceContainerImpl::addBundle(const std::string &bundleId,
501 const std::string &bundleUri, const std::string &bundlePath,
502 const std::string &activator, std::map< string, string > params)
506 if (m_bundles.find(bundleId) != m_bundles.end())
507 OC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
511 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
512 bundleInfo->setID(bundleId);
513 bundleInfo->setPath(bundlePath);
514 bundleInfo->setActivatorName(activator);
515 if (params.find("libraryPath") != params.end())
517 string activatorName = activator; // modify activator for Java bundle
518 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
519 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
520 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
523 OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
524 std::string(bundleInfo->getID() + "; " +
525 bundleInfo->getPath()).c_str());
527 registerBundle(bundleInfo);
531 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
533 OC_LOG_V(INFO, CONTAINER_TAG, "removeBundle %s",bundleId.c_str());
534 if (m_bundles.find(bundleId) != m_bundles.end())
536 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
537 if (bundleInfo->isActivated())
538 deactivateBundle(bundleInfo);
540 if (bundleInfo->isLoaded())
541 unregisterBundle(bundleInfo);
545 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
546 std::string(bundleId + "\' is not ced.").c_str());
550 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
552 OC_LOG_V(INFO, CONTAINER_TAG,
553 "list bundles (%d)", m_bundles.size());
554 std::list<std::unique_ptr<RCSBundleInfo> > ret;
555 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
556 it != m_bundles.end(); ++it)
559 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
560 (bundleInfo)->setBundleInfo(it->second);
561 ret.push_back(std::move(bundleInfo));
567 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
568 const std::string &resourceUri, std::map< string, string > params)
570 if (m_bundles.find(bundleId) != m_bundles.end())
572 if (!m_bundles[bundleId]->getJavaBundle())
574 resourceInfo newResourceInfo;
575 newResourceInfo.uri = resourceUri;
577 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
578 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
579 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
580 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
581 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
582 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
584 addSoBundleResource(bundleId, newResourceInfo);
589 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
590 std::string(bundleId + "\' is not registered.").c_str());
594 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
595 const std::string &resourceUri)
597 OC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(), resourceUri.c_str());
598 if (m_bundles.find(bundleId) != m_bundles.end())
600 if (m_bundles[bundleId]->getSoBundle())
602 removeSoBundleResource(bundleId, resourceUri);
607 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
608 std::string(bundleId + "\' is not registered.").c_str());
612 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
614 OC_LOG_V(INFO, CONTAINER_TAG, "listBundleResources %s",bundleId.c_str());
615 std::list < string > ret;
617 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
619 ret = m_mapBundleResources[bundleId];
626 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
628 OC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle");
631 activator_t *bundleActivator = NULL;
632 deactivator_t *bundleDeactivator = NULL;
633 resourceCreator_t *resourceCreator = NULL;
634 resourceDestroyer_t *resourceDestroyer = NULL;
635 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
636 void *bundleHandle = NULL;
637 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
638 if ((error = dlerror()) != NULL)
640 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
643 if (bundleHandle != NULL)
645 OC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s", bundleInfoInternal->getActivatorName().c_str());
647 (activator_t *) dlsym(bundleHandle,
648 ("" + bundleInfoInternal->getActivatorName()
649 + "_externalActivateBundle").c_str());
650 if ((error = dlerror()) != NULL)
652 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
655 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
656 + "_externalActivateBundle").c_str());
659 (deactivator_t *) dlsym(bundleHandle,
660 ("" + bundleInfoInternal->getActivatorName()
661 + "_externalDeactivateBundle").c_str());
662 if ((error = dlerror()) != NULL)
664 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
667 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
668 + "_externalDeactivateBundle").c_str());
671 (resourceCreator_t *) dlsym(bundleHandle,
672 ("" + bundleInfoInternal->getActivatorName()
673 + "_externalCreateResource").c_str());
674 if ((error = dlerror()) != NULL)
676 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
679 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
680 + "_externalCreateResource").c_str());
683 (resourceDestroyer_t *) dlsym(bundleHandle,
684 ("" + bundleInfoInternal->getActivatorName()
685 + "_externalDestroyResource").c_str());
686 if ((error = dlerror()) != NULL)
688 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
691 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
692 + "_externalDestroyResource").c_str());
696 if ((error = dlerror()) != NULL)
698 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
702 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
703 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
704 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
705 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
706 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
707 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
709 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
714 if ((error = dlerror()) != NULL)
716 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
719 OC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished");
722 void ResourceContainerImpl::registerExtBundle(RCSBundleInfo *bundleInfo){
723 OC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)",
724 std::string(bundleInfo->getID()).c_str());
725 OC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)",
726 std::string(bundleInfo->getActivatorName()).c_str());
728 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
729 // in this case at least the resource configuration needs to be loaded
730 // in order to mark potential input resources for soft sensors
731 std::vector< resourceInfo > temp;
732 OC_LOG_V(INFO, CONTAINER_TAG, "Loading resource config(%s)",
733 std::string(bundleInfo->getID()).c_str());
734 getResourceConfiguration(bundleInfo->getID(),
737 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
740 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
742 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
744 if (bundleActivator != NULL)
746 bundleActivator(this, m_bundles[bundleId]->getID());
747 m_bundles[bundleId]->setActivated(true);
751 //Unload module and return error
752 OC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
755 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
756 bundleInfoInternal->setActivated(true);
760 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
762 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
763 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
765 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
769 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
771 OC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource%s", outputResourceUri.c_str());
772 auto foundOutputResource = m_mapResources.find(outputResourceUri);
773 auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
777 resourceProperty.at(INPUT_RESOURCE);
779 catch (std::out_of_range &e)
784 for (auto iter : resourceProperty)
786 if (iter.first.compare(INPUT_RESOURCE) == 0)
788 for (auto it : iter.second)
790 auto makeValue = [&](const std::string & reference) mutable -> std::string
792 std::string retStr = "";
795 retStr = it.at(reference);
797 catch (std::out_of_range &e)
803 std::string uri = makeValue(INPUT_RESOURCE_URI);
804 std::string type = makeValue(INPUT_RESOURCE_TYPE);
805 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
807 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
808 < DiscoverResourceUnit > (outputResourceUri);
809 newDiscoverUnit->startDiscover(
810 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
812 std::bind(&SoftSensorResource::onUpdatedInputResource,
813 std::static_pointer_cast< SoftSensorResource > (foundOutputResource->second),
814 std::placeholders::_1, std::placeholders::_2));
816 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
818 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
820 foundDiscoverResource->second.push_back(newDiscoverUnit);
824 m_mapDiscoverResourceUnits.insert(
825 std::make_pair(outputResourceUri,
826 std::list< DiscoverResourceUnit::Ptr >
827 { newDiscoverUnit }));
834 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
836 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
838 OC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
839 m_bundles[id]->getID()).c_str());
841 if (bundleDeactivator != NULL)
844 m_bundles[id]->setActivated(false);
848 //Unload module and return error
849 OC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
853 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
854 resourceInfo newResourceInfo)
856 resourceCreator_t *resourceCreator;
858 resourceCreator = m_bundles[bundleId]->getResourceCreator();
860 if (resourceCreator != NULL)
862 resourceCreator(newResourceInfo);
866 OC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
870 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
871 const std::string &resourceUri)
873 if (m_mapResources.find(resourceUri) != m_mapResources.end())
875 resourceDestroyer_t *resourceDestroyer =
876 m_bundles[bundleId]->getResourceDestroyer();
878 if (resourceDestroyer != NULL)
880 resourceDestroyer(m_mapResources[resourceUri]);
884 OC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
889 void ResourceContainerImpl::activateBundleThread(const std::string &id)
891 OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
892 std::string(m_bundles[id]->getID()).c_str());
894 if (m_bundles[id]->getJavaBundle())
897 activateJavaBundle(id);
900 else if(m_bundles[id]->getSoBundle())
902 activateSoBundle (id);
905 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
906 std::string(m_bundles[id]->getID()).c_str());
910 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
912 return m_bundleVM[bundleId];
915 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
917 OC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
918 std::string(bundleInfo->getID()).c_str());
921 JavaVMInitArgs vm_args;
922 JavaVMOption options[3];
924 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
926 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
930 OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
931 std::string(bundleInfo->getPath() +
932 " available.").c_str());
936 OC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
937 std::string(bundleInfo->getPath() + " not available.").c_str());
942 char optionString[] = "-Djava.compiler=NONE";
943 options[0].optionString = optionString;
944 char classpath[1000];
945 strcpy(classpath, "-Djava.class.path=");
946 strcat(classpath, bundleInfo->getPath().c_str());
948 OC_LOG(INFO, CONTAINER_TAG,
949 std::string("Configured classpath: ").append(classpath).c_str());
951 options[1].optionString = classpath;
953 char libraryPath[1000];
954 strcpy(libraryPath, "-Djava.library.path=");
955 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
956 options[2].optionString = libraryPath;
958 OC_LOG(INFO, CONTAINER_TAG,
959 std::string("Configured library path: ").append(libraryPath).c_str());
961 vm_args.version = JNI_VERSION_1_4;
962 vm_args.options = options;
963 vm_args.nOptions = 3;
964 vm_args.ignoreUnrecognized = 1;
967 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
971 OC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
976 OC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
979 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
981 const char *className = bundleInfoInternal->getActivatorName().c_str();
983 OC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
984 bundleInfoInternal->getActivatorName() + "|").c_str());
986 jclass bundleActivatorClass = env->FindClass(className);
988 if (bundleActivatorClass == NULL)
990 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
991 std::string( bundleInfoInternal->getID()
992 + " bundle activator(" + bundleInfoInternal->getActivatorName()
993 + ") not found ").c_str());
997 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
1000 if (activateMethod == NULL)
1002 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1003 std::string( bundleInfoInternal->getID()
1004 + " activate bundle method not found ").c_str());
1007 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
1009 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
1012 if (deactivateMethod == NULL)
1014 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1015 std::string( bundleInfoInternal->getID()
1016 + " deactivate bundle method not found ").c_str());
1020 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
1022 jmethodID constructor;
1024 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
1026 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
1028 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
1030 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
1032 bundleInfoInternal->setLoaded(true);
1034 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
1037 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
1042 void ResourceContainerImpl::activateJavaBundle(string bundleId)
1044 OC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
1046 JavaVM *vm = getJavaVM(bundleId);
1047 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1049 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1051 if (envStat == JNI_EDETACHED)
1053 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1055 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1058 else if (envStat == JNI_EVERSION)
1060 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1063 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1064 bundleInfoInternal->getJavaBundleActivatorMethod());
1066 m_bundles[bundleId]->setActivated(true);
1069 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
1071 OC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
1073 JavaVM *vm = getJavaVM(bundleId);
1074 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1076 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1078 if (envStat == JNI_EDETACHED)
1080 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1082 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1085 else if (envStat == JNI_EVERSION)
1087 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1090 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1091 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1093 m_bundles[bundleId]->setActivated(false);
1096 void ResourceContainerImpl::unregisterBundleJava(string id)
1098 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1099 m_bundles[id]->getID()).c_str());
1101 OC_LOG_V(INFO, CONTAINER_TAG, "Destroying JVM");
1103 m_bundleVM[id]->DestroyJavaVM();
1105 delete m_bundles[id];
1106 m_bundles.erase(id);