X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fresource-container%2Fsrc%2FResourceContainerImpl.cpp;h=5042f0c827687c4da47993cee80ad3ed4977211b;hb=442026128ead8780fa45d0db8a6f17be7c9220e0;hp=c6a27d19714fee0a69f9a524e5dac7bcc2cd8a3e;hpb=084ac4fa5ac4792acb4d5123e13bcbadab803001;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/resource-container/src/ResourceContainerImpl.cpp b/service/resource-container/src/ResourceContainerImpl.cpp index c6a27d1..5042f0c 100644 --- a/service/resource-container/src/ResourceContainerImpl.cpp +++ b/service/resource-container/src/ResourceContainerImpl.cpp @@ -68,71 +68,64 @@ namespace OIC activationLock.lock(); - if (!configFile.empty()) - { - m_config = new Configuration(configFile); - - if (m_config->isLoaded()) + try{ + if (!configFile.empty()) { - configInfo bundles; - m_config->getConfiguredBundles(&bundles); + m_config = new Configuration(configFile); - for (unsigned int i = 0; i < bundles.size(); i++) + if (m_config->isLoaded()) { - BundleInfoInternal *bundleInfo = new BundleInfoInternal(); - bundleInfo->setPath(bundles[i][BUNDLE_PATH]); - bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]); - bundleInfo->setID(bundles[i][BUNDLE_ID]); - if (!bundles[i][BUNDLE_ACTIVATOR].empty()) + configInfo bundles; + m_config->getConfiguredBundles(&bundles); + + for (unsigned int i = 0; i < bundles.size(); i++) { - string activatorName = bundles[i][BUNDLE_ACTIVATOR]; - std::replace(activatorName.begin(), activatorName.end(), '.', '/'); - ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName); - ((BundleInfoInternal *) bundleInfo)->setLibraryPath( - bundles[i][BUNDLE_LIBRARY_PATH]); - } + shared_ptr bundleInfo(new BundleInfoInternal); + bundleInfo->setPath(bundles[i][BUNDLE_PATH]); + bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]); + bundleInfo->setID(bundles[i][BUNDLE_ID]); + if (!bundles[i][BUNDLE_ACTIVATOR].empty()) + { + string activatorName = bundles[i][BUNDLE_ACTIVATOR]; + std::replace(activatorName.begin(), activatorName.end(), '.', '/'); + bundleInfo->setActivatorName(activatorName); + bundleInfo->setLibraryPath(bundles[i][BUNDLE_LIBRARY_PATH]); + } - OIC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)", - std::string(bundles[i][BUNDLE_ID] + ";" + - bundles[i][BUNDLE_PATH]).c_str()); + OIC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)", + std::string(bundles[i][BUNDLE_ID] + ";" + + bundles[i][BUNDLE_PATH]).c_str()); - registerBundle(bundleInfo); - activateBundle(bundleInfo); + registerBundle(bundleInfo); + activateBundle(bundleInfo); + } + } + else + { + OIC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path."); } } else { - OIC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path."); + OIC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided."); } - } - else - { - OIC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided."); - } - - map::iterator activatorIterator; - for (activatorIterator = m_activators.begin(); activatorIterator != m_activators.end(); - activatorIterator++) - { - activatorIterator->second.timed_join( - boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC)); - // wait for bundles to be activated + OIC_LOG(INFO, CONTAINER_TAG, "Resource container started."); + }catch(...){ + OIC_LOG(INFO, CONTAINER_TAG, "Resource container failed starting."); } activationLock.unlock(); - OIC_LOG(INFO, CONTAINER_TAG, "Resource container started."); } void ResourceContainerImpl::stopContainer() { OIC_LOG(INFO, CONTAINER_TAG, "Stopping resource container."); - for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin(); + for (std::map< std::string, shared_ptr >::iterator it = m_bundles.begin(); it != m_bundles.end(); ++it) { - BundleInfoInternal *bundleInfo = it->second; - deactivateBundle(bundleInfo); - unregisterBundle(bundleInfo); + deactivateBundle(it->second); + unregisterBundle(it->second); } if (!m_mapServers.empty()) @@ -152,10 +145,12 @@ namespace OIC delete m_config; } - void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::activateBundle(shared_ptr bundleInfo) { activationLock.lock(); - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); if (bundleInfoInternal->isLoaded()) { @@ -164,11 +159,13 @@ namespace OIC activationLock.unlock(); } - void ResourceContainerImpl::deactivateBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::deactivateBundle(shared_ptr bundleInfo) { - if (((BundleInfoInternal *) bundleInfo)->isActivated()) + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + if (bundleInfoInternal->isActivated()) { - deactivateBundle(bundleInfo->getID()); + deactivateBundle(bundleInfoInternal->getID()); } } @@ -176,11 +173,15 @@ namespace OIC { OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)", std::string(m_bundles[id]->getID()).c_str()); + activationLock.lock(); - auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this, - id); - boost::thread activator(f); - activator.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC)); + try{ + activateBundleThread(id); + } + catch(...){ + OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s) failed", + std::string(m_bundles[id]->getID()).c_str()); + } activationLock.unlock(); OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)", std::string(m_bundles[id]->getID()).c_str()); @@ -201,41 +202,44 @@ namespace OIC } // loads the bundle - void ResourceContainerImpl::registerBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::registerBundle(shared_ptr bundleInfo) { OIC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)", std::string(bundleInfo->getPath()).c_str()); + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); if (has_suffix(bundleInfo->getPath(), ".jar")) { #if(JAVA_SUPPORT) - ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true); - ((BundleInfoInternal *) bundleInfo)->setSoBundle(false); + bundleInfoInternal->setJavaBundle(true); + bundleInfoInternal->setSoBundle(false); registerJavaBundle(bundleInfo); #else // android .jar library - ((BundleInfoInternal *) bundleInfo)->setSoBundle(false); - ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false); + bundleInfoInternal->setSoBundle(false); + bundleInfoInternal->setJavaBundle(false); registerExtBundle(bundleInfo); #endif } else if(has_suffix(bundleInfo->getPath(), ".so")) { - ((BundleInfoInternal *) bundleInfo)->setSoBundle(true); - ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false); + bundleInfoInternal->setSoBundle(true); + bundleInfoInternal->setJavaBundle(false); registerSoBundle(bundleInfo); } // other cases might be for example .apk for android, which are loaded in the wrapper else{ - ((BundleInfoInternal *) bundleInfo)->setSoBundle(false); - ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false); + bundleInfoInternal->setSoBundle(false); + bundleInfoInternal->setJavaBundle(false); registerExtBundle(bundleInfo); } } - void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::unregisterBundle(shared_ptr bundleInfo) { - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated()) { if (bundleInfoInternal->getSoBundle()) @@ -267,7 +271,6 @@ namespace OIC } else { - delete m_bundles[id]; m_bundles.erase(id); } } @@ -418,7 +421,7 @@ namespace OIC } } - OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %d attributes",strResourceUri.c_str(), attr.size()); + OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %zu attributes",strResourceUri.c_str(), attr.size()); return RCSGetResponse::create(std::move(attr), 200); } @@ -430,7 +433,7 @@ namespace OIC std::list lstAttributes; std::string strResourceUri = request.getResourceUri(); - OIC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %d attributes",strResourceUri.c_str(), attributes.size()); + OIC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %zu attributes",strResourceUri.c_str(), attributes.size()); if (m_mapServers.find(strResourceUri) != m_mapServers.end() && m_mapResources.find(strResourceUri) != m_mapResources.end()) @@ -538,7 +541,7 @@ namespace OIC else { - BundleInfoInternal *bundleInfo = new BundleInfoInternal(); + shared_ptr bundleInfo = std::make_shared(); bundleInfo->setID(bundleId); bundleInfo->setPath(bundlePath); bundleInfo->setActivatorName(activator); @@ -546,8 +549,8 @@ namespace OIC { string activatorName = activator; // modify activator for Java bundle std::replace(activatorName.begin(), activatorName.end(), '.', '/'); - ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName); - ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]); + bundleInfo->setActivatorName(activatorName); + bundleInfo->setLibraryPath(params[BUNDLE_LIBRARY_PATH]); } OIC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)", @@ -563,7 +566,7 @@ namespace OIC OIC_LOG_V(INFO, CONTAINER_TAG, "removeBundle %s",bundleId.c_str()); if (m_bundles.find(bundleId) != m_bundles.end()) { - BundleInfoInternal *bundleInfo = m_bundles[bundleId]; + shared_ptr bundleInfo = m_bundles[bundleId]; if (bundleInfo->isActivated()) deactivateBundle(bundleInfo); @@ -582,12 +585,12 @@ namespace OIC OIC_LOG_V(INFO, CONTAINER_TAG, "list bundles (%d)", m_bundles.size()); std::list > ret; - for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin(); + for (std::map< std::string, shared_ptr >::iterator it = m_bundles.begin(); it != m_bundles.end(); ++it) { { std::unique_ptr bundleInfo(new BundleInfoInternal); - (bundleInfo)->setBundleInfo(it->second); + bundleInfo->setBundleInfo(it->second); ret.push_back(std::move(bundleInfo)); } } @@ -624,7 +627,8 @@ namespace OIC void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId, const std::string &resourceUri) { - OIC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(), resourceUri.c_str()); + OIC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(), + resourceUri.c_str()); if (m_bundles.find(bundleId) != m_bundles.end()) { if (m_bundles[bundleId]->getSoBundle()) @@ -653,7 +657,7 @@ namespace OIC } - void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::registerSoBundle(shared_ptr bundleInfo) { OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle"); const char *error; @@ -662,7 +666,9 @@ namespace OIC deactivator_t *bundleDeactivator = NULL; resourceCreator_t *resourceCreator = NULL; resourceDestroyer_t *resourceDestroyer = NULL; - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + void *bundleHandle = NULL; bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY); if ((error = dlerror()) != NULL) @@ -672,7 +678,8 @@ namespace OIC if (bundleHandle != NULL) { - OIC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s", bundleInfoInternal->getActivatorName().c_str()); + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s", + bundleInfoInternal->getActivatorName().c_str()); bundleActivator = (activator_t *) dlsym(bundleHandle, ("" + bundleInfoInternal->getActivatorName() @@ -682,7 +689,8 @@ namespace OIC OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); } else{ - OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName() + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + "_externalActivateBundle").c_str()); } bundleDeactivator = @@ -694,7 +702,8 @@ namespace OIC OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); } else{ - OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName() + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + "_externalDeactivateBundle").c_str()); } resourceCreator = @@ -706,7 +715,8 @@ namespace OIC OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); } else{ - OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName() + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + "_externalCreateResource").c_str()); } resourceDestroyer = @@ -718,7 +728,8 @@ namespace OIC OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); } else{ - OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + bundleInfoInternal->getActivatorName() + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + "_externalDestroyResource").c_str()); } @@ -729,14 +740,14 @@ namespace OIC } else { - ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator); - ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator); - ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator); - ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer); - ((BundleInfoInternal *) bundleInfo)->setLoaded(true); - ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle); - - m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo); + bundleInfoInternal->setBundleActivator(bundleActivator); + bundleInfoInternal->setBundleDeactivator(bundleDeactivator); + bundleInfoInternal->setResourceCreator(resourceCreator); + bundleInfoInternal->setResourceDestroyer(resourceDestroyer); + bundleInfoInternal->setLoaded(true); + bundleInfoInternal->setBundleHandle(bundleHandle); + + m_bundles[bundleInfo->getID()] = bundleInfoInternal; } } else @@ -749,13 +760,16 @@ namespace OIC OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished"); } - void ResourceContainerImpl::registerExtBundle(RCSBundleInfo *bundleInfo){ + void ResourceContainerImpl::registerExtBundle(shared_ptr bundleInfo){ OIC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)", std::string(bundleInfo->getID()).c_str()); OIC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)", std::string(bundleInfo->getActivatorName()).c_str()); - m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo); + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + + m_bundles[bundleInfo->getID()] = bundleInfoInternal; // in this case at least the resource configuration needs to be loaded // in order to mark potential input resources for soft sensors std::vector< resourceInfo > temp; @@ -782,7 +796,7 @@ namespace OIC OIC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful."); } - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId]; + shared_ptr bundleInfoInternal = m_bundles[bundleId]; bundleInfoInternal->setActivated(true); } @@ -802,11 +816,10 @@ namespace OIC { OIC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource %s", outputResourceUri.c_str()); auto foundOutputResource = m_mapResources.find(outputResourceUri); - // auto resourceProperty = foundOutputResource->second->m_mapResourceProperty; resourceInfo info; m_config->getResourceConfiguration(foundOutputResource->second->m_bundleId, - foundOutputResource->second->m_name, &info); + outputResourceUri, &info); map< string, vector< map< string, string > > > resourceProperty = info.resourceProperty; try @@ -954,7 +967,7 @@ namespace OIC return m_bundleVM[bundleId]; } - void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::registerJavaBundle(shared_ptr bundleInfo) { OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)", std::string(bundleInfo->getID()).c_str()); @@ -963,7 +976,9 @@ namespace OIC JavaVMInitArgs vm_args; JavaVMOption options[3]; - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r")) { @@ -985,7 +1000,7 @@ namespace OIC options[0].optionString = optionString; char classpath[1000]; strcpy(classpath, "-Djava.class.path="); - strcat(classpath, bundleInfo->getPath().c_str()); + strncat(classpath, bundleInfo->getPath().c_str(), BUNDLE_PATH_MAXLEN); OIC_LOG(INFO, CONTAINER_TAG, std::string("Configured classpath: ").append(classpath).c_str()); @@ -994,7 +1009,7 @@ namespace OIC char libraryPath[1000]; strcpy(libraryPath, "-Djava.library.path="); - strcat(libraryPath, bundleInfo->getLibraryPath().c_str()); + strncat(libraryPath, bundleInfo->getLibraryPath().c_str(), BUNDLE_PATH_MAXLEN); options[2].optionString = libraryPath; OIC_LOG(INFO, CONTAINER_TAG, @@ -1074,7 +1089,7 @@ namespace OIC bundleInfoInternal->setLoaded(true); - m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo); + m_bundles[bundleInfo->getID()] = bundleInfoInternal; OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered"); @@ -1087,7 +1102,7 @@ namespace OIC OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle"); JavaVM *vm = getJavaVM(bundleId); - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId]; + bundleInfoInternal = m_bundles[bundleId]; JNIEnv *env; int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4); @@ -1114,7 +1129,7 @@ namespace OIC OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle"); JavaVM *vm = getJavaVM(bundleId); - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId]; + shared_ptrbundleInfoInternal = m_bundles[bundleId]; JNIEnv *env; int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4); @@ -1145,7 +1160,6 @@ namespace OIC m_bundleVM[id]->DestroyJavaVM(); - delete m_bundles[id]; m_bundles.erase(id); } #endif