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((NotificationReceiver *) 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, const std::string &bundlePath,
484 const std::string &activator, std::map< string, string > params)
488 if (m_bundles.find(bundleId) != m_bundles.end())
489 OIC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist");
493 BundleInfoInternal *bundleInfo = new BundleInfoInternal();
494 bundleInfo->setID(bundleId);
495 bundleInfo->setPath(bundlePath);
496 bundleInfo->setActivatorName(activator);
497 if (params.find("libraryPath") != params.end())
499 string activatorName = activator; // modify activator for Java bundle
500 std::replace(activatorName.begin(), activatorName.end(), '.', '/');
501 ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
502 ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]);
505 OIC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)",
506 std::string(bundleInfo->getID() + "; " +
507 bundleInfo->getPath()).c_str());
509 registerBundle(bundleInfo);
513 void ResourceContainerImpl::removeBundle(const std::string &bundleId)
515 if (m_bundles.find(bundleId) != m_bundles.end())
517 BundleInfoInternal *bundleInfo = m_bundles[bundleId];
518 if (bundleInfo->isActivated())
519 deactivateBundle(bundleInfo);
521 if (bundleInfo->isLoaded())
522 unregisterBundle(bundleInfo);
526 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
527 std::string(bundleId + "\' is not registered.").c_str());
531 std::list<std::unique_ptr<RCSBundleInfo>> ResourceContainerImpl::listBundles()
533 std::list<std::unique_ptr<RCSBundleInfo> > ret;
534 for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
535 it != m_bundles.end(); ++it)
538 std::unique_ptr<BundleInfoInternal> bundleInfo(new BundleInfoInternal);
539 (bundleInfo)->setBundleInfo(it->second);
540 ret.push_back(std::move(bundleInfo));
546 void ResourceContainerImpl::addResourceConfig(const std::string &bundleId,
547 const std::string &resourceUri, std::map< string, string > params)
549 if (m_bundles.find(bundleId) != m_bundles.end())
551 if (!m_bundles[bundleId]->getJavaBundle())
553 resourceInfo newResourceInfo;
554 newResourceInfo.uri = resourceUri;
556 if (params.find(OUTPUT_RESOURCE_NAME) != params.end())
557 newResourceInfo.name = params[OUTPUT_RESOURCE_NAME];
558 if (params.find(OUTPUT_RESOURCE_TYPE) != params.end())
559 newResourceInfo.resourceType = params[OUTPUT_RESOURCE_TYPE];
560 if (params.find(OUTPUT_RESOURCE_ADDR) != params.end())
561 newResourceInfo.address = params[OUTPUT_RESOURCE_ADDR];
563 addSoBundleResource(bundleId, newResourceInfo);
568 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
569 std::string(bundleId + "\' is not registered.").c_str());
573 void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId,
574 const std::string &resourceUri)
576 if (m_bundles.find(bundleId) != m_bundles.end())
578 if (!m_bundles[bundleId]->getJavaBundle())
580 removeSoBundleResource(bundleId, resourceUri);
585 OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)",
586 std::string(bundleId + "\' is not registered.").c_str());
590 std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId)
592 std::list < string > ret;
594 if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
596 ret = m_mapBundleResources[bundleId];
603 void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo)
607 activator_t *bundleActivator = NULL;
608 deactivator_t *bundleDeactivator = NULL;
609 resourceCreator_t *resourceCreator = NULL;
610 resourceDestroyer_t *resourceDestroyer = NULL;
611 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
612 void *bundleHandle = NULL;
613 bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY);
615 if (bundleHandle != NULL)
618 (activator_t *) dlsym(bundleHandle,
619 ("" + bundleInfoInternal->getActivatorName()
620 + "_externalActivateBundle").c_str());
622 (deactivator_t *) dlsym(bundleHandle,
623 ("" + bundleInfoInternal->getActivatorName()
624 + "_externalDeactivateBundle").c_str());
626 (resourceCreator_t *) dlsym(bundleHandle,
627 ("" + bundleInfoInternal->getActivatorName()
628 + "_externalCreateResource").c_str());
630 (resourceDestroyer_t *) dlsym(bundleHandle,
631 ("" + bundleInfoInternal->getActivatorName()
632 + "_externalDestroyResource").c_str());
635 if ((error = dlerror()) != NULL)
637 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
641 ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator);
642 ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator);
643 ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator);
644 ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer);
645 ((BundleInfoInternal *) bundleInfo)->setLoaded(true);
646 ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle);
648 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
653 if ((error = dlerror()) != NULL)
655 OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error);
660 void ResourceContainerImpl::activateSoBundle(const std::string &bundleId)
662 activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
664 if (bundleActivator != NULL)
666 bundleActivator(this, m_bundles[bundleId]->getID());
667 m_bundles[bundleId]->setActivated(true);
671 //Unload module and return error
672 OIC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful.");
675 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
676 bundleInfoInternal->setActivated(true);
680 void ResourceContainerImpl::undiscoverInputResource(const std::string &outputResourceUri)
682 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri);
683 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
685 m_mapDiscoverResourceUnits.erase(foundDiscoverResource);
689 void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri)
691 auto foundOutputResource = m_mapResources.find(outputResourceUri);
692 auto resourceProperty = foundOutputResource->second->m_mapResourceProperty;
696 resourceProperty.at(INPUT_RESOURCE);
698 catch (std::out_of_range &e)
703 for (auto iter : resourceProperty)
705 if (iter.first.compare(INPUT_RESOURCE) == 0)
707 for (auto it : iter.second)
709 auto makeValue = [&](const std::string & reference) mutable -> std::string
711 std::string retStr = "";
714 retStr = it.at(reference);
716 catch (std::out_of_range &e)
722 std::string uri = makeValue(INPUT_RESOURCE_URI);
723 std::string type = makeValue(INPUT_RESOURCE_TYPE);
724 std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME);
726 DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared
727 < DiscoverResourceUnit > (outputResourceUri);
728 newDiscoverUnit->startDiscover(
729 DiscoverResourceUnit::DiscoverResourceInfo(uri, type,
731 std::bind(&SoftSensorResource::onUpdatedInputResource,
732 std::static_pointer_cast< SoftSensorResource > (foundOutputResource->second),
733 std::placeholders::_1, std::placeholders::_2));
735 auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(
737 if (foundDiscoverResource != m_mapDiscoverResourceUnits.end())
739 foundDiscoverResource->second.push_back(newDiscoverUnit);
743 m_mapDiscoverResourceUnits.insert(
744 std::make_pair(outputResourceUri,
745 std::list< DiscoverResourceUnit::Ptr >
746 { newDiscoverUnit }));
753 void ResourceContainerImpl::deactivateSoBundle(const std::string &id)
755 deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
757 OIC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string(
758 m_bundles[id]->getID()).c_str());
760 if (bundleDeactivator != NULL)
763 m_bundles[id]->setActivated(false);
767 //Unload module and return error
768 OIC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful.");
772 void ResourceContainerImpl::addSoBundleResource(const std::string &bundleId,
773 resourceInfo newResourceInfo)
775 resourceCreator_t *resourceCreator;
777 resourceCreator = m_bundles[bundleId]->getResourceCreator();
779 if (resourceCreator != NULL)
781 resourceCreator(newResourceInfo);
785 OIC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful.");
789 void ResourceContainerImpl::removeSoBundleResource(const std::string &bundleId,
790 const std::string &resourceUri)
792 if (m_mapResources.find(resourceUri) != m_mapResources.end())
794 resourceDestroyer_t *resourceDestroyer =
795 m_bundles[bundleId]->getResourceDestroyer();
797 if (resourceDestroyer != NULL)
799 resourceDestroyer(m_mapResources[resourceUri]);
803 OIC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful.");
808 void ResourceContainerImpl::activateBundleThread(const std::string &id)
810 OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)",
811 std::string(m_bundles[id]->getID()).c_str());
813 if (m_bundles[id]->getJavaBundle())
816 activateJavaBundle(id);
821 activateSoBundle (id);
824 OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)",
825 std::string(m_bundles[id]->getID()).c_str());
829 JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
831 return m_bundleVM[bundleId];
834 void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo)
836 OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)",
837 std::string(bundleInfo->getID()).c_str());
840 JavaVMInitArgs vm_args;
841 JavaVMOption options[3];
843 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
845 if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
849 OIC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)",
850 std::string(bundleInfo->getPath() +
851 " available.").c_str());
855 OIC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)",
856 std::string(bundleInfo->getPath() + " not available.").c_str());
861 char optionString[] = "-Djava.compiler=NONE";
862 options[0].optionString = optionString;
863 char classpath[1000];
864 strcpy(classpath, "-Djava.class.path=");
865 strcat(classpath, bundleInfo->getPath().c_str());
867 OIC_LOG(INFO, CONTAINER_TAG,
868 std::string("Configured classpath: ").append(classpath).c_str());
870 options[1].optionString = classpath;
872 char libraryPath[1000];
873 strcpy(libraryPath, "-Djava.library.path=");
874 strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
875 options[2].optionString = libraryPath;
877 OIC_LOG(INFO, CONTAINER_TAG,
878 std::string("Configured library path: ").append(libraryPath).c_str());
880 vm_args.version = JNI_VERSION_1_4;
881 vm_args.options = options;
882 vm_args.nOptions = 3;
883 vm_args.ignoreUnrecognized = 1;
886 res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
890 OIC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM.");
895 OIC_LOG(INFO, CONTAINER_TAG, "JVM successfully created.");
898 m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
900 const char *className = bundleInfoInternal->getActivatorName().c_str();
902 OIC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string(
903 bundleInfoInternal->getActivatorName() + "|").c_str());
905 jclass bundleActivatorClass = env->FindClass(className);
907 if (bundleActivatorClass == NULL)
909 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
910 std::string( bundleInfoInternal->getID()
911 + " bundle activator(" + bundleInfoInternal->getActivatorName()
912 + ") not found ").c_str());
916 jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
919 if (activateMethod == NULL)
921 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
922 std::string( bundleInfoInternal->getID()
923 + " activate bundle method not found ").c_str());
926 bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
928 jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
931 if (deactivateMethod == NULL)
933 OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)",
934 std::string( bundleInfoInternal->getID()
935 + " deactivate bundle method not found ").c_str());
939 bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
941 jmethodID constructor;
943 constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
945 jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
947 jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
949 bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
951 bundleInfoInternal->setLoaded(true);
953 m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
956 OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered");
959 void ResourceContainerImpl::activateJavaBundle(string bundleId)
961 OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle");
963 JavaVM *vm = getJavaVM(bundleId);
964 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
966 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
968 if (envStat == JNI_EDETACHED)
970 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
972 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
975 else if (envStat == JNI_EVERSION)
977 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
980 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
981 bundleInfoInternal->getJavaBundleActivatorMethod());
983 m_bundles[bundleId]->setActivated(true);
986 void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
988 OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle");
990 JavaVM *vm = getJavaVM(bundleId);
991 BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
993 int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
995 if (envStat == JNI_EDETACHED)
997 if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
999 OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach ");
1002 else if (envStat == JNI_EVERSION)
1004 OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported ");
1007 env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
1008 bundleInfoInternal->getJavaBundleDeactivatorMethod());
1010 m_bundles[bundleId]->setActivated(false);
1013 void ResourceContainerImpl::unregisterBundleJava(string id)
1015 OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string(
1016 m_bundles[id]->getID()).c_str());
1018 OIC_LOG(INFO, CONTAINER_TAG, "Destroying JVM");
1020 m_bundleVM[id]->DestroyJavaVM();
1022 delete m_bundles[id];
1023 m_bundles.erase(id);