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 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 OIC_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 OIC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path.");
110 OIC_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();
125 void ResourceContainerImpl::stopContainer()
127 OIC_LOG(INFO, CONTAINER_TAG, "Stopping resource container.");
129 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
130 it != m_bundles.end(); ++it)
132 BundleInfoInternal *bundleInfo = it->second;
133 deactivateBundle(bundleInfo);
134 unregisterBundle(bundleInfo);
137 if (!m_mapServers.empty())
139 map< std::string, RCSResourceObject::Ptr >::iterator itor = m_mapServers.begin();
141 while (itor != m_mapServers.end())
143 (itor++)->second.reset();
146 m_mapResources.clear();
147 m_mapBundleResources.clear();
154 void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo)
156 activationLock.lock();
157 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
159 if (bundleInfoInternal->isLoaded())
161 activateBundle(bundleInfo->getID());
163 activationLock.unlock();
166 void ResourceContainerImpl::deactivateBundle(RCSBundleInfo *bundleInfo)
168 if (((BundleInfoInternal *) bundleInfo)->isActivated())
170 deactivateBundle(bundleInfo->getID());
174 void ResourceContainerImpl::activateBundle(const std::string &id)
176 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
177 std::string(m_bundles[id]->getID()).c_str());
178 activationLock.lock();
179 auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this,
181 boost::thread activator(f);
182 activator.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
183 activationLock.unlock();
184 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
185 std::string(m_bundles[id]->getID()).c_str());
188 void ResourceContainerImpl::deactivateBundle(const std::string &id)
190 if (m_bundles[id]->getJavaBundle())
193 deactivateJavaBundle(id);
198 deactivateSoBundle(id);
203 void ResourceContainerImpl::registerBundle(RCSBundleInfo *bundleInfo)
205 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)",
206 std::string(bundleInfo->getPath()).c_str());
208 if (has_suffix(bundleInfo->getPath(), ".jar"))
211 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true);
212 registerJavaBundle(bundleInfo);
217 ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
218 registerSoBundle(bundleInfo);
222 void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo)
224 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
225 if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated())
227 if (!bundleInfoInternal->getJavaBundle())
229 unregisterBundleSo(bundleInfo->getID());
234 unregisterBundleJava(bundleInfo->getID());
240 void ResourceContainerImpl::unregisterBundleSo(const std::string &id)
242 void *bundleHandle = m_bundles[id]->getBundleHandle();
244 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)",
245 std::string(m_bundles[id]->getID()).c_str());
248 dlclose(bundleHandle);
250 if ((error = dlerror()) != NULL)
252 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error);
256 delete m_bundles[id];
261 int ResourceContainerImpl::registerResource(BundleResource::Ptr resource)
263 string strUri = resource->m_uri;
264 string strResourceType = resource->m_resourceType;
265 string strInterface = resource->m_interface;
266 RCSResourceObject::Ptr server = nullptr;
269 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" ,
270 std::string(strUri + ", " + strResourceType).c_str());
272 registrationLock.lock();
273 if (m_mapResources.find(strUri) == m_mapResources.end())
275 if (strInterface.empty()) {
276 strInterface = "oic.if.baseline";
279 server = buildResourceObject(strUri, strResourceType, strInterface);
281 if (server != nullptr)
283 m_mapServers[strUri] = server;
284 m_mapResources[strUri] = resource;
285 m_mapBundleResources[resource->m_bundleId].push_back(strUri);
287 server->setGetRequestHandler(
288 std::bind(&ResourceContainerImpl::getRequestHandler, this,
289 std::placeholders::_1, std::placeholders::_2));
291 server->setSetRequestHandler(
292 std::bind(&ResourceContainerImpl::setRequestHandler, this,
293 std::placeholders::_1, std::placeholders::_2));
295 OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)",
296 std::string(strUri + ", " +
297 strResourceType).c_str());
299 if (m_config && m_config->isHasInput(resource->m_bundleId))
301 discoverInputResource(strUri);
304 // to get notified if bundle resource attributes are updated
305 resource->registerObserver(this);
311 OIC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)",
312 std::string(strUri + " already exists.").c_str());
315 registrationLock.unlock();
320 void ResourceContainerImpl::unregisterResource(BundleResource::Ptr resource)
322 string strUri = resource->m_uri;
323 string strResourceType = resource->m_resourceType;
325 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)",
326 std::string(resource->m_uri + ", " +
327 resource->m_resourceType).c_str());
329 if (m_config && m_config->isHasInput(resource->m_bundleId))
331 undiscoverInputResource(strUri);
334 if (m_mapServers.find(strUri) != m_mapServers.end())
336 m_mapServers[strUri].reset();
338 m_mapResources.erase(m_mapResources.find(strUri));
339 m_mapBundleResources[resource->m_bundleId].remove(strUri);
343 void ResourceContainerImpl::getBundleConfiguration(const std::string &bundleId,
344 configInfo *configOutput)
348 m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
352 void ResourceContainerImpl::getResourceConfiguration(const std::string &bundleId,
353 std::vector< resourceInfo > *configOutput)
357 m_config->getResourceConfiguration(bundleId, configOutput);
361 RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request,
362 const RCSResourceAttributes &)
364 RCSResourceAttributes attr;
365 std::string strResourceUri = request.getResourceUri();
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));
382 return RCSGetResponse::create(std::move(attr), 200);
385 RCSSetResponse ResourceContainerImpl::setRequestHandler(const RCSRequest &request,
386 const RCSResourceAttributes &attributes)
388 RCSResourceAttributes attr;
389 std::list<std::string> lstAttributes;
390 std::string strResourceUri = request.getResourceUri();
392 if (m_mapServers.find(strResourceUri) != m_mapServers.end()
393 && m_mapResources.find(strResourceUri) != m_mapResources.end())
395 if (m_mapResources[strResourceUri])
397 auto setFunction = [this, &lstAttributes, &strResourceUri, &attributes, &attr]()
399 lstAttributes = m_mapResources[strResourceUri]->getAttributeNames();
401 for (RCSResourceAttributes::const_iterator itor = attributes.begin();
402 itor != attributes.end(); itor++)
404 if (std::find(lstAttributes.begin(), lstAttributes.end(), itor->key())
405 != lstAttributes.end())
407 attr[itor->key()] = itor->value();
411 m_mapResources[strResourceUri]->handleSetAttributesRequest(attr);
413 boost::thread setThread(setFunction);
414 setThread.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC));
418 return RCSSetResponse::create(std::move(attr), 200);
421 void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri)
423 OIC_LOG_V(INFO, CONTAINER_TAG,
424 "notification from (%s)", std::string(strResourceUri + ".").c_str());
426 if (m_mapServers.find(strResourceUri) != m_mapServers.end())
428 m_mapServers[strResourceUri]->notify();
432 ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
434 static ResourceContainerImpl m_instance;
438 RCSResourceObject::Ptr ResourceContainerImpl::buildResourceObject(const std::string &strUri,
439 const std::string &strResourceType, const std::string &strInterface)
441 return RCSResourceObject::Builder(strUri, strResourceType,
442 strInterface).setObservable(
443 true).setDiscoverable(true).build();
446 void ResourceContainerImpl::startBundle(const std::string &bundleId)
448 if (m_bundles.find(bundleId) != m_bundles.end())
450 if (!m_bundles[bundleId]->isActivated())
451 activateBundle(m_bundles[bundleId]);
454 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle already started");
459 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
460 std::string(bundleId + "\' is not registered.").c_str());
464 void ResourceContainerImpl::stopBundle(const std::string &bundleId)
466 if (m_bundles.find(bundleId) != m_bundles.end())
468 if (m_bundles[bundleId]->isActivated())
469 deactivateBundle(m_bundles[bundleId]);
472 OIC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated");
477 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
478 std::string(bundleId + "\' is not registered.").c_str());
482 void ResourceContainerImpl::addBundle(const std::string &bundleId,
483 const std::string &bundleUri,
484 const std::string &bundlePath,
485 const std::string &activator,
486 std::map< string, string > params)
490 if (m_bundles.find(bundleId) != m_bundles.end())
491 OIC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
495 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
496 bundleInfo->setID(bundleId);
497 bundleInfo->setPath(bundlePath);
498 bundleInfo->setActivatorName(activator);
499 if (params.find("libraryPath") != params.end())
501 string activatorName = activator; // modify activator for Java bundle
502 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
503 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
504 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
507 OIC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
508 std::string(bundleInfo->getID() + "; " +
509 bundleInfo->getPath()).c_str());
511 registerBundle(bundleInfo);
515 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
517 if (m_bundles.find(bundleId) != m_bundles.end())
519 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
520 if (bundleInfo->isActivated())
521 deactivateBundle(bundleInfo);
523 if (bundleInfo->isLoaded())
524 unregisterBundle(bundleInfo);
528 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
529 std::string(bundleId + "\' is not registered.").c_str());
533 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
535 std::list<std::unique_ptr<RCSBundleInfo> > ret;
536 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
537 it != m_bundles.end(); ++it)
540 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
541 (bundleInfo)->setBundleInfo(it->second);
542 ret.push_back(std::move(bundleInfo));
548 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
549 const std::string &resourceUri, std::map< string, string > params)
551 if (m_bundles.find(bundleId) != m_bundles.end())
553 if (!m_bundles[bundleId]->getJavaBundle())
555 resourceInfo newResourceInfo;
556 newResourceInfo.uri = resourceUri;
558 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
559 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
560 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
561 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
562 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
563 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
565 addSoBundleResource(bundleId, newResourceInfo);
570 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
571 std::string(bundleId + "\' is not registered.").c_str());
575 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
576 const std::string &resourceUri)
578 if (m_bundles.find(bundleId) != m_bundles.end())
580 if (!m_bundles[bundleId]->getJavaBundle())
582 removeSoBundleResource(bundleId, resourceUri);
587 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
588 std::string(bundleId + "\' is not registered.").c_str());
592 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
594 std::list < string > ret;
596 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
598 ret = m_mapBundleResources[bundleId];
605 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
609 activator_t *bundleActivator = NULL;
610 deactivator_t *bundleDeactivator = NULL;
611 resourceCreator_t *resourceCreator = NULL;
612 resourceDestroyer_t *resourceDestroyer = NULL;
613 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
614 void *bundleHandle = NULL;
615 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
617 if (bundleHandle != NULL)
620 (activator_t *) dlsym(bundleHandle,
621 ("" + bundleInfoInternal->getActivatorName()
622 + "_externalActivateBundle").c_str());
624 (deactivator_t *) dlsym(bundleHandle,
625 ("" + bundleInfoInternal->getActivatorName()
626 + "_externalDeactivateBundle").c_str());
628 (resourceCreator_t *) dlsym(bundleHandle,
629 ("" + bundleInfoInternal->getActivatorName()
630 + "_externalCreateResource").c_str());
632 (resourceDestroyer_t *) dlsym(bundleHandle,
633 ("" + bundleInfoInternal->getActivatorName()
634 + "_externalDestroyResource").c_str());
637 if ((error = dlerror()) != NULL)
639 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
643 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
644 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
645 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
646 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
647 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
648 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
650 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
655 if ((error = dlerror()) != NULL)
657 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
662 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
664 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
666 if (bundleActivator != NULL)
668 bundleActivator(this, m_bundles[bundleId]->getID());
669 m_bundles[bundleId]->setActivated(true);
673 //Unload module and return error
674 OIC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
677 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
678 bundleInfoInternal->setActivated(true);
682 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
684 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
685 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
687 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
691 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
693 auto foundOutputResource = m_mapResources.find(outputResourceUri);
694 auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
698 resourceProperty.at(INPUT_RESOURCE);
700 catch (std::out_of_range &e)
705 for (auto iter : resourceProperty)
707 if (iter.first.compare(INPUT_RESOURCE) == 0)
709 for (auto it : iter.second)
711 auto makeValue = [&](const std::string & reference) mutable -> std::string
713 std::string retStr = "";
716 retStr = it.at(reference);
718 catch (std::out_of_range &e)
724 std::string uri = makeValue(INPUT_RESOURCE_URI);
725 std::string type = makeValue(INPUT_RESOURCE_TYPE);
726 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
728 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
729 < DiscoverResourceUnit > (outputResourceUri);
730 newDiscoverUnit->startDiscover(
731 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
733 std::bind(&SoftSensorResource::onUpdatedInputResource,
734 std::static_pointer_cast< SoftSensorResource > (foundOutputResource->second),
735 std::placeholders::_1, std::placeholders::_2));
737 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
739 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
741 foundDiscoverResource->second.push_back(newDiscoverUnit);
745 m_mapDiscoverResourceUnits.insert(
746 std::make_pair(outputResourceUri,
747 std::list< DiscoverResourceUnit::Ptr >
748 { newDiscoverUnit }));
755 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
757 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
759 OIC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
760 m_bundles[id]->getID()).c_str());
762 if (bundleDeactivator != NULL)
765 m_bundles[id]->setActivated(false);
769 //Unload module and return error
770 OIC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
774 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
775 resourceInfo newResourceInfo)
777 resourceCreator_t *resourceCreator;
779 resourceCreator = m_bundles[bundleId]->getResourceCreator();
781 if (resourceCreator != NULL)
783 resourceCreator(newResourceInfo);
787 OIC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
791 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
792 const std::string &resourceUri)
794 if (m_mapResources.find(resourceUri) != m_mapResources.end())
796 resourceDestroyer_t *resourceDestroyer =
797 m_bundles[bundleId]->getResourceDestroyer();
799 if (resourceDestroyer != NULL)
801 resourceDestroyer(m_mapResources[resourceUri]);
805 OIC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
810 void ResourceContainerImpl::activateBundleThread(const std::string &id)
812 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
813 std::string(m_bundles[id]->getID()).c_str());
815 if (m_bundles[id]->getJavaBundle())
818 activateJavaBundle(id);
823 activateSoBundle (id);
826 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
827 std::string(m_bundles[id]->getID()).c_str());
831 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
833 return m_bundleVM[bundleId];
836 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
838 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
839 std::string(bundleInfo->getID()).c_str());
842 JavaVMInitArgs vm_args;
843 JavaVMOption options[3];
845 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
847 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
851 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
852 std::string(bundleInfo->getPath() +
853 " available.").c_str());
857 OIC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
858 std::string(bundleInfo->getPath() + " not available.").c_str());
863 char optionString[] = "-Djava.compiler=NONE";
864 options[0].optionString = optionString;
865 char classpath[1000];
866 strcpy(classpath, "-Djava.class.path=");
867 strcat(classpath, bundleInfo->getPath().c_str());
869 OIC_LOG(INFO, CONTAINER_TAG,
870 std::string("Configured classpath: ").append(classpath).c_str());
872 options[1].optionString = classpath;
874 char libraryPath[1000];
875 strcpy(libraryPath, "-Djava.library.path=");
876 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
877 options[2].optionString = libraryPath;
879 OIC_LOG(INFO, CONTAINER_TAG,
880 std::string("Configured library path: ").append(libraryPath).c_str());
882 vm_args.version = JNI_VERSION_1_4;
883 vm_args.options = options;
884 vm_args.nOptions = 3;
885 vm_args.ignoreUnrecognized = 1;
888 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
892 OIC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
897 OIC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
900 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
902 const char *className = bundleInfoInternal->getActivatorName().c_str();
904 OIC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
905 bundleInfoInternal->getActivatorName() + "|").c_str());
907 jclass bundleActivatorClass = env->FindClass(className);
909 if (bundleActivatorClass == NULL)
911 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
912 std::string( bundleInfoInternal->getID()
913 + " bundle activator(" + bundleInfoInternal->getActivatorName()
914 + ") not found ").c_str());
918 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
921 if (activateMethod == NULL)
923 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
924 std::string( bundleInfoInternal->getID()
925 + " activate bundle method not found ").c_str());
928 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
930 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
933 if (deactivateMethod == NULL)
935 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
936 std::string( bundleInfoInternal->getID()
937 + " deactivate bundle method not found ").c_str());
941 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
943 jmethodID constructor;
945 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
947 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
949 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
951 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
953 bundleInfoInternal->setLoaded(true);
955 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
958 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
961 void ResourceContainerImpl::activateJavaBundle(string bundleId)
963 OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
965 JavaVM *vm = getJavaVM(bundleId);
966 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
968 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
970 if (envStat == JNI_EDETACHED)
972 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
974 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
977 else if (envStat == JNI_EVERSION)
979 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
982 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
983 bundleInfoInternal->getJavaBundleActivatorMethod());
985 m_bundles[bundleId]->setActivated(true);
988 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
990 OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
992 JavaVM *vm = getJavaVM(bundleId);
993 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
995 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
997 if (envStat == JNI_EDETACHED)
999 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
1001 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1004 else if (envStat == JNI_EVERSION)
1006 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1009 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1010 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1012 m_bundles[bundleId]->setActivated(false);
1015 void ResourceContainerImpl::unregisterBundleJava(string id)
1017 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1018 m_bundles[id]->getID()).c_str());
1020 OIC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1022 m_bundleVM[id]->DestroyJavaVM();
1024 delete m_bundles[id];
1025 m_bundles.erase(id);