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);
216 // android .jar library
217 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
218 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
219 registerExtBundle(bundleInfo);
222 else if(has_suffix(bundleInfo->getPath(), ".so"))
224 ((BundleInfoInternal *) bundleInfo)->setSoBundle(true);
225 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
226 registerSoBundle(bundleInfo);
228 // other cases might be for example .apk for android, which are loaded in the wrapper
230 ((BundleInfoInternal *) bundleInfo)->setSoBundle(false);
231 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
232 registerExtBundle(bundleInfo);
236 void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo)
238 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
239 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
241 if (bundleInfoInternal->getSoBundle())
243 unregisterBundleSo(bundleInfo->getID());
245 else if(bundleInfoInternal->getJavaBundle())
248 unregisterBundleJava(bundleInfo->getID());
254 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
256 void *bundleHandle = m_bundles[id]->getBundleHandle();
258 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
259 std::string(m_bundles[id]->getID()).c_str());
262 dlclose(bundleHandle);
264 if ((error = dlerror()) != NULL)
266 OC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error);
270 delete m_bundles[id];
275 void ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
277 string strUri = resource->m_uri;
278 string strResourceType = resource->m_resourceType;
279 RCSResourceObject::Ptr server = nullptr;
281 OC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" ,
282 std::string(strUri + ", " + strResourceType + "," +
283 resource->m_bundleId).c_str());
285 registrationLock.lock();
286 if (m_mapResources.find(strUri) == m_mapResources.end())
288 server = buildResourceObject(strUri, strResourceType);
290 if (server != nullptr)
292 m_mapServers[strUri] = server;
293 m_mapResources[strUri] = resource;
294 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
296 server->setGetRequestHandler(
297 std::bind(&ResourceContainerImpl::getRequestHandler, this,
298 std::placeholders::_1, std::placeholders::_2));
300 server->setSetRequestHandler(
301 std::bind(&ResourceContainerImpl::setRequestHandler, this,
302 std::placeholders::_1, std::placeholders::_2));
306 if (m_config->isHasInput(resource->m_bundleId))
308 OC_LOG_V(INFO, CONTAINER_TAG, "Resource has input (%s)",
309 std::string(strUri + ", " +
310 strResourceType).c_str());
311 discoverInputResource(strUri);
314 OC_LOG_V(INFO, CONTAINER_TAG, "Resource has no input (%s)",
315 std::string(strUri + ", " +
316 strResourceType).c_str());
318 OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
319 std::string(strUri + ", " +
320 strResourceType).c_str());
322 // to get notified if bundle resource attributes are updated
323 resource->registerObserver((NotificationReceiver *) this);
328 OC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
329 std::string(strUri + " already exists.").c_str());
331 registrationLock.unlock();
334 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
336 string strUri = resource->m_uri;
337 string strResourceType = resource->m_resourceType;
339 OC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
340 std::string(resource->m_uri + ", " +
341 resource->m_resourceType).c_str());
343 if (m_config->isHasInput(resource->m_bundleId))
345 OC_LOG_V(INFO, CONTAINER_TAG, "Calling undiscover (%s)",
346 std::string(resource->m_uri + ", " +
347 resource->m_resourceType).c_str());
348 undiscoverInputResource(strUri);
351 if (m_mapServers.find(strUri) != m_mapServers.end())
353 OC_LOG_V(INFO, CONTAINER_TAG, "Resetting server (%s)",
354 std::string(resource->m_uri + ", " +
355 resource->m_resourceType).c_str());
356 m_mapServers[strUri].reset();
358 m_mapResources.erase(m_mapResources.find(strUri));
360 OC_LOG_V(INFO, CONTAINER_TAG, "Remove bundle resource (%s)",
361 std::string(resource->m_uri + ", " +
362 resource->m_resourceType).c_str());
363 m_mapBundleResources[resource->m_bundleId].remove(strUri);
367 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
368 configInfo *configOutput)
372 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
376 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
377 std::vector< resourceInfo > *configOutput)
381 m_config->getResourceConfiguration(bundleId, configOutput);
384 OC_LOG_V(DEBUG, CONTAINER_TAG, "no config present ");
388 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
389 const RCSResourceAttributes &)
391 RCSResourceAttributes attr;
392 std::string strResourceUri = request.getResourceUri();
393 OC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s",strResourceUri.c_str());
395 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
396 && m_mapResources.find(strResourceUri) != m_mapResources.end())
398 if (m_mapResources[strResourceUri])
400 auto getFunction = [this, &attr, &strResourceUri]()
402 attr = m_mapResources[strResourceUri]->handleGetAttributesRequest();
404 boost::thread getThread(getFunction);
405 getThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
409 OC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %d attributes",strResourceUri.c_str(), attr.size());
411 return RCSGetResponse::create(std::move(attr), 200);
414 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
415 const RCSResourceAttributes &attributes)
417 RCSResourceAttributes attr;
418 std::list<std::string> lstAttributes;
419 std::string strResourceUri = request.getResourceUri();
421 OC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %d attributes",strResourceUri.c_str(), attributes.size());
423 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
424 && m_mapResources.find(strResourceUri) != m_mapResources.end())
426 if (m_mapResources[strResourceUri])
428 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
430 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
432 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
433 itor != attributes.end(); itor++)
435 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
436 != lstAttributes.end())
438 attr[itor->key()] = itor->value();
442 OC_LOG_V(INFO, CONTAINER_TAG, "Calling handleSetAttributeRequest");
443 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
445 boost::thread setThread(setFunction);
446 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
450 return RCSSetResponse::create(std::move(attr), 200);
453 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
455 OC_LOG_V(INFO, CONTAINER_TAG,
456 "notification from (%s)", std::string(strResourceUri + ".").c_str());
458 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
460 m_mapServers[strResourceUri]->notify();
464 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
466 static ResourceContainerImpl m_instance;
470 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
471 const std::string &strResourceType)
473 return RCSResourceObject::Builder(strUri, strResourceType,
474 "oic.if.baseline").setObservable(
475 true).setDiscoverable(true).build();
478 void ResourceContainerImpl::startBundle(const std::string &bundleId)
480 OC_LOG_V(INFO, CONTAINER_TAG, "startBundle %s",bundleId.c_str());
481 if (m_bundles.find(bundleId) != m_bundles.end())
483 if (!m_bundles[bundleId]->isActivated())
484 activateBundle(m_bundles[bundleId]);
487 OC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
492 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
493 std::string(bundleId + "\' is not registered.").c_str());
497 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
499 OC_LOG_V(INFO, CONTAINER_TAG, "stopBundle %s",bundleId.c_str());
500 if (m_bundles.find(bundleId) != m_bundles.end())
502 if (m_bundles[bundleId]->isActivated())
503 deactivateBundle(m_bundles[bundleId]);
506 OC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
511 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
512 std::string(bundleId + "\' is not registered.").c_str());
516 void ResourceContainerImpl::addBundle(const std::string &bundleId,
517 const std::string &bundleUri, const std::string &bundlePath,
518 const std::string &activator, std::map< string, string > params)
522 if (m_bundles.find(bundleId) != m_bundles.end())
523 OC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
527 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
528 bundleInfo->setID(bundleId);
529 bundleInfo->setPath(bundlePath);
530 bundleInfo->setActivatorName(activator);
531 if (params.find("libraryPath") != params.end())
533 string activatorName = activator; // modify activator for Java bundle
534 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
535 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
536 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
539 OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
540 std::string(bundleInfo->getID() + "; " +
541 bundleInfo->getPath()).c_str());
543 registerBundle(bundleInfo);
547 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
549 OC_LOG_V(INFO, CONTAINER_TAG, "removeBundle %s",bundleId.c_str());
550 if (m_bundles.find(bundleId) != m_bundles.end())
552 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
553 if (bundleInfo->isActivated())
554 deactivateBundle(bundleInfo);
556 if (bundleInfo->isLoaded())
557 unregisterBundle(bundleInfo);
561 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
562 std::string(bundleId + "\' is not ced.").c_str());
566 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
568 OC_LOG_V(INFO, CONTAINER_TAG,
569 "list bundles (%d)", m_bundles.size());
570 std::list<std::unique_ptr<RCSBundleInfo> > ret;
571 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
572 it != m_bundles.end(); ++it)
575 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
576 (bundleInfo)->setBundleInfo(it->second);
577 ret.push_back(std::move(bundleInfo));
583 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
584 const std::string &resourceUri, std::map< string, string > params)
586 if (m_bundles.find(bundleId) != m_bundles.end())
588 if (!m_bundles[bundleId]->getJavaBundle())
590 resourceInfo newResourceInfo;
591 newResourceInfo.uri = resourceUri;
593 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
594 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
595 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
596 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
597 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
598 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
600 addSoBundleResource(bundleId, newResourceInfo);
605 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
606 std::string(bundleId + "\' is not registered.").c_str());
610 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
611 const std::string &resourceUri)
613 OC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(), resourceUri.c_str());
614 if (m_bundles.find(bundleId) != m_bundles.end())
616 if (m_bundles[bundleId]->getSoBundle())
618 removeSoBundleResource(bundleId, resourceUri);
623 OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
624 std::string(bundleId + "\' is not registered.").c_str());
628 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
630 OC_LOG_V(INFO, CONTAINER_TAG, "listBundleResources %s",bundleId.c_str());
631 std::list < string > ret;
633 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
635 ret = m_mapBundleResources[bundleId];
642 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
644 OC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle");
647 activator_t *bundleActivator = NULL;
648 deactivator_t *bundleDeactivator = NULL;
649 resourceCreator_t *resourceCreator = NULL;
650 resourceDestroyer_t *resourceDestroyer = NULL;
651 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
652 void *bundleHandle = NULL;
653 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
654 if ((error = dlerror()) != NULL)
656 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
659 if (bundleHandle != NULL)
661 OC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s", bundleInfoInternal->getActivatorName().c_str());
663 (activator_t *) dlsym(bundleHandle,
664 ("" + bundleInfoInternal->getActivatorName()
665 + "_externalActivateBundle").c_str());
666 if ((error = dlerror()) != NULL)
668 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
671 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
672 + "_externalActivateBundle").c_str());
675 (deactivator_t *) dlsym(bundleHandle,
676 ("" + bundleInfoInternal->getActivatorName()
677 + "_externalDeactivateBundle").c_str());
678 if ((error = dlerror()) != NULL)
680 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
683 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
684 + "_externalDeactivateBundle").c_str());
687 (resourceCreator_t *) dlsym(bundleHandle,
688 ("" + bundleInfoInternal->getActivatorName()
689 + "_externalCreateResource").c_str());
690 if ((error = dlerror()) != NULL)
692 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
695 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
696 + "_externalCreateResource").c_str());
699 (resourceDestroyer_t *) dlsym(bundleHandle,
700 ("" + bundleInfoInternal->getActivatorName()
701 + "_externalDestroyResource").c_str());
702 if ((error = dlerror()) != NULL)
704 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
707 OC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName()
708 + "_externalDestroyResource").c_str());
712 if ((error = dlerror()) != NULL)
714 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
718 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
719 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
720 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
721 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
722 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
723 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
725 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
730 if ((error = dlerror()) != NULL)
732 OC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error);
735 OC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished");
738 void ResourceContainerImpl::registerExtBundle(RCSBundleInfo *bundleInfo){
739 OC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)",
740 std::string(bundleInfo->getID()).c_str());
741 OC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)",
742 std::string(bundleInfo->getActivatorName()).c_str());
744 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
745 // in this case at least the resource configuration needs to be loaded
746 // in order to mark potential input resources for soft sensors
747 std::vector< resourceInfo > temp;
748 OC_LOG_V(INFO, CONTAINER_TAG, "Loading resource config(%s)",
749 std::string(bundleInfo->getID()).c_str());
750 getResourceConfiguration(bundleInfo->getID(),
753 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
756 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
758 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
760 if (bundleActivator != NULL)
762 bundleActivator(this, m_bundles[bundleId]->getID());
763 m_bundles[bundleId]->setActivated(true);
767 //Unload module and return error
768 OC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
771 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
772 bundleInfoInternal->setActivated(true);
776 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
778 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
779 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
781 OC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource.");
782 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
783 OC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource done.");
787 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
789 OC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource %s", outputResourceUri.c_str());
790 auto foundOutputResource = m_mapResources.find(outputResourceUri);
791 // auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
794 m_config->getResourceConfiguration(foundOutputResource->second->m_bundleId,
795 foundOutputResource->second->m_name, &info);
796 map< string, vector< map< string, string > > > resourceProperty = info.resourceProperty;
800 resourceProperty.at(INPUT_RESOURCE);
802 catch (std::out_of_range &e)
804 OC_LOG_V(DEBUG, CONTAINER_TAG, "No input resource %s", outputResourceUri.c_str());
808 for (auto iter : resourceProperty)
810 if (iter.first.compare(INPUT_RESOURCE) == 0)
812 for (auto it : iter.second)
814 auto makeValue = [&](const std::string & reference) mutable -> std::string
816 std::string retStr = "";
819 retStr = it.at(reference);
821 catch (std::out_of_range &e)
827 std::string uri = makeValue(INPUT_RESOURCE_URI);
828 std::string type = makeValue(INPUT_RESOURCE_TYPE);
829 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
832 OC_LOG_V(DEBUG, CONTAINER_TAG, "Start discovery: %s, %s, %s", uri.c_str(),
833 type.c_str(), attributeName.c_str());
834 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
835 < DiscoverResourceUnit > (outputResourceUri);
836 newDiscoverUnit->startDiscover(
837 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
839 std::bind(&SoftSensorResource::onUpdatedInputResource,
840 std::static_pointer_cast< SoftSensorResource >
841 (foundOutputResource->second),
842 std::placeholders::_1, std::placeholders::_2));
844 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
846 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
848 foundDiscoverResource->second.push_back(newDiscoverUnit);
852 m_mapDiscoverResourceUnits.insert(
853 std::make_pair(outputResourceUri,
854 std::list< DiscoverResourceUnit::Ptr >
855 { newDiscoverUnit }));
862 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
864 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
866 OC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
867 m_bundles[id]->getID()).c_str());
869 if (bundleDeactivator != NULL)
872 m_bundles[id]->setActivated(false);
876 //Unload module and return error
877 OC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
881 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
882 resourceInfo newResourceInfo)
884 resourceCreator_t *resourceCreator;
886 resourceCreator = m_bundles[bundleId]->getResourceCreator();
888 if (resourceCreator != NULL)
890 resourceCreator(newResourceInfo);
894 OC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
898 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
899 const std::string &resourceUri)
901 if (m_mapResources.find(resourceUri) != m_mapResources.end())
903 resourceDestroyer_t *resourceDestroyer =
904 m_bundles[bundleId]->getResourceDestroyer();
906 if (resourceDestroyer != NULL)
908 resourceDestroyer(m_mapResources[resourceUri]);
912 OC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
917 void ResourceContainerImpl::activateBundleThread(const std::string &id)
919 OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
920 std::string(m_bundles[id]->getID()).c_str());
922 if (m_bundles[id]->getJavaBundle())
925 activateJavaBundle(id);
928 else if(m_bundles[id]->getSoBundle())
930 activateSoBundle (id);
933 OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
934 std::string(m_bundles[id]->getID()).c_str());
938 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
940 return m_bundleVM[bundleId];
943 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
945 OC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
946 std::string(bundleInfo->getID()).c_str());
949 JavaVMInitArgs vm_args;
950 JavaVMOption options[3];
952 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
954 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
958 OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
959 std::string(bundleInfo->getPath() +
960 " available.").c_str());
964 OC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
965 std::string(bundleInfo->getPath() + " not available.").c_str());
970 char optionString[] = "-Djava.compiler=NONE";
971 options[0].optionString = optionString;
972 char classpath[1000];
973 strcpy(classpath, "-Djava.class.path=");
974 strcat(classpath, bundleInfo->getPath().c_str());
976 OC_LOG(INFO, CONTAINER_TAG,
977 std::string("Configured classpath: ").append(classpath).c_str());
979 options[1].optionString = classpath;
981 char libraryPath[1000];
982 strcpy(libraryPath, "-Djava.library.path=");
983 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
984 options[2].optionString = libraryPath;
986 OC_LOG(INFO, CONTAINER_TAG,
987 std::string("Configured library path: ").append(libraryPath).c_str());
989 vm_args.version = JNI_VERSION_1_4;
990 vm_args.options = options;
991 vm_args.nOptions = 3;
992 vm_args.ignoreUnrecognized = 1;
995 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
999 OC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
1004 OC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
1007 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
1009 const char *className = bundleInfoInternal->getActivatorName().c_str();
1011 OC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
1012 bundleInfoInternal->getActivatorName() + "|").c_str());
1014 jclass bundleActivatorClass = env->FindClass(className);
1016 if (bundleActivatorClass == NULL)
1018 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1019 std::string( bundleInfoInternal->getID()
1020 + " bundle activator(" +
1021 bundleInfoInternal->getActivatorName()
1022 + ") not found ").c_str());
1026 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
1029 if (activateMethod == NULL)
1031 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1032 std::string( bundleInfoInternal->getID()
1033 + " activate bundle method not found ").c_str());
1036 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
1038 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
1041 if (deactivateMethod == NULL)
1043 OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
1044 std::string( bundleInfoInternal->getID()
1045 + " deactivate bundle method not found ").c_str());
1049 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
1051 jmethodID constructor;
1053 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
1055 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
1057 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
1059 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
1061 bundleInfoInternal->setLoaded(true);
1063 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
1066 OC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
1071 void ResourceContainerImpl::activateJavaBundle(string bundleId)
1073 OC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
1075 JavaVM *vm = getJavaVM(bundleId);
1076 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1078 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1080 if (envStat == JNI_EDETACHED)
1082 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1084 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1087 else if (envStat == JNI_EVERSION)
1089 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1092 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1093 bundleInfoInternal->getJavaBundleActivatorMethod());
1095 m_bundles[bundleId]->setActivated(true);
1098 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
1100 OC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
1102 JavaVM *vm = getJavaVM(bundleId);
1103 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
1105 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
1107 if (envStat == JNI_EDETACHED)
1109 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1111 OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1114 else if (envStat == JNI_EVERSION)
1116 OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1119 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1120 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1122 m_bundles[bundleId]->setActivated(false);
1125 void ResourceContainerImpl::unregisterBundleJava(string id)
1127 OC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1128 m_bundles[id]->getID()).c_str());
1130 OC_LOG_V(INFO, CONTAINER_TAG, "Destroying JVM");
1132 m_bundleVM[id]->DestroyJavaVM();
1134 delete m_bundles[id];
1135 m_bundles.erase(id);