void ServiceRegistration::SetProperties(const ServiceProperties& props) { if (!d) throw std::logic_error("ServiceRegistration object invalid"); MutexLocker lock(d->eventLock); ServiceListeners::ServiceListenerEntries before; // TBD, optimize the locking of services { //MutexLocker lock2(d->module->coreCtx->globalFwLock); if (d->available) { // NYI! Optimize the MODIFIED_ENDMATCH code int old_rank = 0; int new_rank = 0; std::list<std::string> classes; { MutexLocker lock3(d->propsLock); Any any = d->properties[ServiceConstants::SERVICE_RANKING()]; if (any.Type() == typeid(int)) old_rank = any_cast<int>(any); d->module->coreCtx->listeners.GetMatchingServiceListeners(d->reference, before, false); classes = ref_any_cast<std::list<std::string> >(d->properties[ServiceConstants::OBJECTCLASS()]); long int sid = any_cast<long int>(d->properties[ServiceConstants::SERVICE_ID()]); d->properties = ServiceRegistry::CreateServiceProperties(props, classes, sid); any = d->properties[ServiceConstants::SERVICE_RANKING()]; if (any.Type() == typeid(int)) new_rank = any_cast<int>(any); } if (old_rank != new_rank) { d->module->coreCtx->services.UpdateServiceRegistrationOrder(*this, classes); } } else { throw std::logic_error("Service is unregistered"); } } ServiceListeners::ServiceListenerEntries matchingListeners; d->module->coreCtx->listeners.GetMatchingServiceListeners(d->reference, matchingListeners); d->module->coreCtx->listeners.ServiceChanged(matchingListeners, ServiceEvent(ServiceEvent::MODIFIED, d->reference), before); d->module->coreCtx->listeners.ServiceChanged(before, ServiceEvent(ServiceEvent::MODIFIED_ENDMATCH, d->reference)); }
bool LDAPExpr::Compare( const Any& obj, int op, const std::string& s ) const { if (obj.Empty()) return false; if (op == EQ && s == WILDCARD_STRING) return true; try { const std::type_info& objType = obj.Type(); if (objType == typeid(std::string)) { return CompareString(ref_any_cast<std::string>(obj), op, s); } else if (objType == typeid(std::vector<std::string>)) { const std::vector<std::string>& list = ref_any_cast<std::vector<std::string> >(obj); for (std::size_t it = 0; it != list.size(); it++) { if (CompareString(list[it], op, s)) return true; } } else if (objType == typeid(std::list<std::string>)) { const std::list<std::string>& list = ref_any_cast<std::list<std::string> >(obj); for (std::list<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) { if (CompareString(*it, op, s)) return true; } } else if (objType == typeid(char)) { return CompareString(std::string(1, ref_any_cast<char>(obj)), op, s); } else if (objType == typeid(bool)) { if (op==LE || op==GE) return false; std::string boolVal = any_cast<bool>(obj) ? "true" : "false"; return std::equal(s.begin(), s.end(), boolVal.begin(), stricomp); } else if (objType == typeid(Byte) || objType == typeid(int)) { int sInt; std::stringstream ss(s); ss >> sInt; int intVal = any_cast<int>(obj); switch(op) { case LE: return intVal <= sInt; case GE: return intVal >= sInt; default: /*APPROX and EQ*/ return intVal == sInt; } } else if (objType == typeid(float)) { float sFloat; std::stringstream ss(s); ss >> sFloat; float floatVal = any_cast<float>(obj); switch(op) { case LE: return floatVal <= sFloat; case GE: return floatVal >= sFloat; default: /*APPROX and EQ*/ float diff = floatVal - sFloat; return (diff < std::numeric_limits<float>::epsilon()) && (diff > -std::numeric_limits<float>::epsilon()); } }
bool LDAPExpr::Compare( const Any& obj, int op, const std::string& s ) const { if (obj.Empty()) return false; if (op == EQ && s == WILDCARD_STRING) return true; try { const std::type_info& objType = obj.Type(); if (objType == typeid(std::string)) { return CompareString(ref_any_cast<std::string>(obj), op, s); } else if (objType == typeid(std::vector<std::string>)) { const std::vector<std::string>& list = ref_any_cast<std::vector<std::string> >(obj); for (std::size_t it = 0; it != list.size(); it++) { if (CompareString(list[it], op, s)) return true; } } else if (objType == typeid(std::list<std::string>)) { const std::list<std::string>& list = ref_any_cast<std::list<std::string> >(obj); for (std::list<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) { if (CompareString(*it, op, s)) return true; } } else if (objType == typeid(char)) { return CompareString(std::string(1, ref_any_cast<char>(obj)), op, s); } else if (objType == typeid(bool)) { if (op==LE || op==GE) return false; std::string boolVal = any_cast<bool>(obj) ? "true" : "false"; return std::equal(s.begin(), s.end(), boolVal.begin(), stricomp); } else if (objType == typeid(short)) { return CompareIntegralType<short>(obj, op, s); } else if (objType == typeid(int)) { return CompareIntegralType<int>(obj, op, s); } else if (objType == typeid(long int)) { return CompareIntegralType<long int>(obj, op, s); } else if (objType == typeid(long long int)) { return CompareIntegralType<long long int>(obj, op, s); } else if (objType == typeid(unsigned char)) { return CompareIntegralType<unsigned char>(obj, op, s); } else if (objType == typeid(unsigned short)) { return CompareIntegralType<unsigned short>(obj, op, s); } else if (objType == typeid(unsigned int)) { return CompareIntegralType<unsigned int>(obj, op, s); } else if (objType == typeid(unsigned long int)) { return CompareIntegralType<unsigned long int>(obj, op, s); } else if (objType == typeid(unsigned long long int)) { return CompareIntegralType<unsigned long long int>(obj, op, s); } else if (objType == typeid(float)) { errno = 0; char* endptr = 0; double sFloat = strtod(s.c_str(), &endptr); if ((errno == ERANGE && (sFloat == 0 || sFloat == HUGE_VAL || sFloat == -HUGE_VAL)) || (errno != 0 && sFloat == 0) || endptr == s.c_str()) { return false; } double floatVal = static_cast<double>(any_cast<float>(obj)); switch(op) { case LE: return floatVal <= sFloat; case GE: return floatVal >= sFloat; default: /*APPROX and EQ*/ double diff = floatVal - sFloat; return (diff < std::numeric_limits<float>::epsilon()) && (diff > -std::numeric_limits<float>::epsilon()); } } else if (objType == typeid(double)) { errno = 0; char* endptr = 0; double sDouble = strtod(s.c_str(), &endptr); if ((errno == ERANGE && (sDouble == 0 || sDouble == HUGE_VAL || sDouble == -HUGE_VAL)) || (errno != 0 && sDouble == 0) || endptr == s.c_str()) { return false; } double doubleVal = any_cast<double>(obj); switch(op) { case LE: return doubleVal <= sDouble; case GE: return doubleVal >= sDouble; default: /*APPROX and EQ*/ double diff = doubleVal - sDouble; return (diff < std::numeric_limits<double>::epsilon()) && (diff > -std::numeric_limits<double>::epsilon()); } } else if (objType == typeid(std::vector<Any>)) { const std::vector<Any>& list = ref_any_cast<std::vector<Any> >(obj); for (std::size_t it = 0; it != list.size(); it++) { if (Compare(list[it], op, s)) return true; } } } catch (...) { // This might happen if a std::string-to-datatype conversion fails // Just consider it a false match and ignore the exception } return false; }
ModulePrivate::ModulePrivate(Module* qq, CoreModuleContext* coreCtx, ModuleInfo* info) : coreCtx(coreCtx) , info(*info) , resourceContainer(info) , moduleContext(0) , moduleActivator(0) , q(qq) { // Check if the module provides a manifest.json file and if yes, parse it. if (resourceContainer.IsValid()) { ModuleResource manifestRes("/manifest.json", resourceContainer); if (manifestRes) { ModuleResourceStream manifestStream(manifestRes); try { moduleManifest.Parse(manifestStream); } catch (const std::exception& e) { US_ERROR << "Parsing of manifest.json for module " << info->location << " failed: " << e.what(); } } } // Check if we got version information and validate the version identifier if (moduleManifest.Contains(Module::PROP_VERSION())) { Any versionAny = moduleManifest.GetValue(Module::PROP_VERSION()); std::string errMsg; if (versionAny.Type() != typeid(std::string)) { errMsg = std::string("The version identifier must be a string"); } try { version = ModuleVersion(versionAny.ToString()); } catch (const std::exception& e) { errMsg = std::string("The version identifier is invalid: ") + e.what(); } if (!errMsg.empty()) { throw std::invalid_argument(std::string("The Json value for ") + Module::PROP_VERSION() + " for module " + info->location + " is not valid: " + errMsg); } } std::stringstream propId; propId << this->info.id; moduleManifest.SetValue(Module::PROP_ID(), propId.str()); moduleManifest.SetValue(Module::PROP_LOCATION(), this->info.location); moduleManifest.SetValue(Module::PROP_NAME(), this->info.name); if (moduleManifest.Contains(Module::PROP_AUTOLOAD_DIR())) { this->info.autoLoadDir = moduleManifest.GetValue(Module::PROP_AUTOLOAD_DIR()).ToString(); } else { this->info.autoLoadDir = this->info.name; moduleManifest.SetValue(Module::PROP_AUTOLOAD_DIR(), Any(this->info.autoLoadDir)); } }