void* FFEADContext::getBean(const std::string& beanName, const std::string& appName) { if(beanName!="" && beans.find(appName+beanName)!=beans.end()) { Bean& bean = beans[appName+beanName]; return getBean(bean); } return NULL; }
T getBean(std::string beanName) { return boost::reflection::arg_cast<T>(getBean(beanName)); }
void FFEADContext::initializeAllSingletonBeans(const std::map<std::string, bool>& servingContexts) { if(reflector==NULL) { reflector = new Reflector; } std::map<std::string,Bean>::iterator beanIter; logger << "Initializing singleton beans..." << std::endl; for (beanIter=beans.begin();beanIter!=beans.end();beanIter++) { Bean& bean = beanIter->second; std::string type; if(bean.inbuilt!="" && bean.value!="") { type = bean.inbuilt; } else { type = bean.clas; } std::string _k = bean.appName + bean.name + ";" + type; if(servingContexts.find(bean.appName)!=servingContexts.end() && StringUtil::toLowerCopy(bean.scope)!="prototype" && !objects.contains(type)) { logger << ("Initializing Bean [appName = "+bean.appName+", name = "+bean.name+ ", class = "+bean.clas+ ", value = 0x" + StringUtil::toHEX((long long)getBean(bean))) << std::endl; } } }
void* FFEADContext::getBean(const Bean& bean) { void *_temp = NULL; std::string type; if(bean.inbuilt!="" && bean.value!="") { type = bean.inbuilt; } else if(bean.inbuilt!="" && bean.value=="") { throw std::runtime_error("Invalid value for inbuilt type"); } else { type = bean.clas; } std::string k = bean.name+";"+type; if(StringUtil::toLowerCopy(bean.scope)!="prototype") { std::string _k = bean.appName + k; if(objects.contains(_k)) { return objects.find(_k); } } if(bean.inbuilt!="" && bean.value!="") { if(bean.inbuilt=="string" || bean.inbuilt=="std::string") { std::string *in = new std::string(bean.value); _temp = in; } else if(bean.inbuilt=="int") { int *in = new int; *in = CastUtil::lexical_cast<int>(bean.value); _temp = in; } else if(bean.inbuilt=="float") { float *in = new float; *in = CastUtil::lexical_cast<float>(bean.value); _temp = in; } else if(bean.inbuilt=="double") { double *in = new double; *in = CastUtil::lexical_cast<double>(bean.value); _temp = in; } else if(bean.inbuilt=="long") { long *in = new long; *in = CastUtil::lexical_cast<long>(bean.value); _temp = in; } else if(bean.inbuilt=="long long") { long long *in = new long long; *in = CastUtil::lexical_cast<long long>(bean.value); _temp = in; } else if(bean.inbuilt=="bool") { bool *in = new bool; if(bean.value=="true") *in = true; else if(bean.value=="false") *in = false; _temp = in; } else if(bean.inbuilt=="char") { char *in = new char; *in = bean.value[0]; _temp = in; } } else if(bean.injectAs=="" || bean.injs.size()==0) { args argus; ClassInfo clas = reflector->getClassInfo(bean.clas, bean.appName); const Constructor& ctor = clas.getConstructor(argus); _temp = reflector->newInstanceGVP(ctor); } else if(bean.injectAs=="prop") { args argus; vals valus; ClassInfo clas = reflector->getClassInfo(bean.clas, bean.appName); const Constructor& ctor = clas.getConstructor(argus); _temp = reflector->newInstanceGVP(ctor); for (unsigned int var = 0; var < bean.injs.size(); var++) { Bean& beanc = injbns[bean.injs.at(var)]; if(beanc.name=="") beanc = beans[bean.injs.at(var)]; std::string methodName("set"); methodName += StringUtil::capitalizedCopy(bean.names.at(var)); if(beanc.inbuilt!="") argus.push_back(beanc.inbuilt); else if(beanc.clas!="") argus.push_back(beanc.clas); else throw std::runtime_error("Invalid or no bean type defined"); Method meth = clas.getMethod(methodName,argus); void *value = getBean(beanc); valus.push_back(value); reflector->invokeMethod<void*>(_temp,meth,valus); valus.clear(); argus.clear(); } } else if(bean.injectAs=="cons") { args argus; vals valus; ClassInfo clas = reflector->getClassInfo(bean.clas, bean.appName); for (unsigned int var = 0; var < bean.injs.size(); var++) { Bean& beanc = injbns[bean.injs.at(var)]; if(beanc.name=="") beanc = beans[bean.injs.at(var)]; if(beanc.inbuilt!="") argus.push_back(beanc.inbuilt); else if(beanc.clas!="") argus.push_back(beanc.clas); else throw std::runtime_error("Invalid or no bean type defined"); void *value = getBean(beanc); valus.push_back(value); } const Constructor& ctor = clas.getConstructor(argus); _temp = reflector->newInstanceGVP(ctor,valus); } else if(bean.injectAs=="intf") { args argus; vals valus; ClassInfo clas = reflector->getClassInfo(bean.clas, bean.appName); const Constructor& ctor = clas.getConstructor(argus); _temp = reflector->newInstanceGVP(ctor); for (unsigned int var = 0; var < bean.injs.size(); var++) { Bean& beanc = injbns[bean.injs.at(var)]; if(beanc.name=="") beanc = beans[bean.injs.at(var)]; std::string methodName("set"); methodName += StringUtil::capitalizedCopy(bean.names.at(var)); if(bean.types.at(var)!="") argus.push_back(bean.types.at(var)); else throw std::runtime_error("Invalid or no bean type defined"); Method meth = clas.getMethod(methodName,argus); void *value = getBean(beanc); valus.push_back(value); reflector->invokeMethod<void*>(_temp,meth,valus); valus.clear(); argus.clear(); } } if(StringUtil::toLowerCopy(bean.scope)!="prototype") { std::string _k = bean.appName + k; if(!objects.contains(_k)) { objects.insert(_k, _temp); } } return _temp; }