ffead.server.doc
ConfigurationHandler.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * ConfigurationHandler.cpp
18  *
19  * Created on: 19-Jun-2012
20  * Author: sumeetc
21  */
22 
23 #include "ConfigurationHandler.h"
24 
25 ConfigurationHandler::ConfigurationHandler() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 ConfigurationHandler::~ConfigurationHandler() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 
35 void ConfigurationHandler::listi(string cwd,string type,bool apDir,strVec &folders)
36 {
37  Logger logger = Logger::getLogger("ConfigurationHandler");
38  FILE *pipe_fp;
39  string command;
40  if(chdir(cwd.c_str())!=0)
41  return;
42  if(type=="/")
43  command = ("find . \\( ! -name . -prune \\) \\( -type d -o -type l \\) 2>/dev/null");
44  else
45  command = ("find . \\( ! -name . -prune \\) \\( -type f -o -type l \\) -name '*"+type+"' 2>/dev/null");
46  //command = ("ls -F1 "+cwd+"|grep '"+type+"'");
47  logger << ("Searching directory " + cwd + " for pattern " + type) << endl;
48  if ((pipe_fp = popen(command.c_str(), "r")) == NULL)
49  {
50  printf("pipe open error in cmd_list\n");
51  }
52  int t_char;
53  string folderName;
54  while ((t_char = fgetc(pipe_fp)) != EOF)
55  {
56  if(t_char!='\n')
57  {
58  stringstream ss;
59  ss << (char)t_char;
60  string temp;
61  ss >> temp;
62  folderName.append(temp);
63  }
64  else if(folderName!="")
65  {
66  StringUtil::replaceFirst(folderName,"*","");
67  StringUtil::replaceFirst(folderName,"./","");
68  if(folderName.find("~")==string::npos)
69  {
70  if(apDir)
71  {
72  if(type=="/")
73  {
74  folderName = cwd+"/"+folderName+"/";
75  StringUtil::replaceFirst(folderName,"//","/");
76  folders.push_back(folderName);
77  }
78  else
79  {
80  folderName = cwd+"/"+folderName;
81  StringUtil::replaceFirst(folderName,"//","/");
82  folders.push_back(folderName);
83  }
84  }
85  else
86  folders.push_back(folderName);
87  }
88  folderName = "";
89  }
90  }
91  pclose(pipe_fp);
92 }
93 
94 ConfigurationData ConfigurationHandler::handle(strVec webdirs,strVec webdirs1,string incpath,string rtdcfpath,string pubpath,string respath,bool isSSLEnabled)
95 {
96  Logger logger = Logger::getLogger("ConfigurationHandler");
97  ConfigurationData configurationData;
98  configurationData.resourcePath = respath;
99  strVec all,dcps,afcd,appf,wspath,compnts,handoffVec,tpes;
100  string includeRef;
101  TemplateEngine templ;
102  StringContext cntxt;
103  string libs,ilibs,isrcs,iobjs,ideps;
104  Reflection ref;
105  vector<bool> stat;
106  strVec vecvp,pathvec;
107  map<string, string> ajintpthMap;
108  propMap srp;
109  PropFileReader pread;
110  XmlParser parser("Parser");
111  ComponentGen gen;
112  if(isSSLEnabled)
113  {
114  propMap sslsec = pread.getProperties(respath+"/security.prop");
115  if(sslsec.size()>0)
116  {
117  configurationData.key_file = sslsec["KEYFILE"];
118  configurationData.dh_file = sslsec["DHFILE"];
119  configurationData.ca_list = sslsec["CA_LIST"];
120  configurationData.rand_file = sslsec["RANDOM"];
121  configurationData.sec_password = sslsec["PASSWORD"];
122  string tempcl = sslsec["CLIENT_SEC_LEVEL"];
123  configurationData.srv_auth_prvd = sslsec["SRV_AUTH_PRVD"];
124  configurationData.srv_auth_mode = sslsec["SRV_AUTH_MODE"];
125  configurationData.srv_auth_file = sslsec["SRV_AUTH_FILE"];
126  if(tempcl!="")
127  {
128  try
129  {
130  configurationData.client_auth = CastUtil::lexical_cast<int>(tempcl);
131  }
132  catch(...)
133  {
134  logger << "\nInvalid client auth level defined" << flush;
135  configurationData.client_auth = 1;
136  }
137  }
138  }
139  }
140  string rundyncontent;
141  for(unsigned int var=0;var<webdirs.size();var++)
142  {
143  //logger << webdirs.at(0) << flush;
144  string defpath = webdirs.at(var);
145  string dcppath = defpath + "dcp/";
146  string tmplpath = defpath + "tpe/";
147  string cmppath = defpath + "components/";
148  string usrincludes = defpath + "include/";
149  //propMap srp = pread.getProperties(defpath+"config/app.prop");
150 
151  string name = webdirs1.at(var);
152  StringUtil::replaceAll(name,"/","");
153  rundyncontent += "cp -Rf $FEAD_CPP_PATH/public/* $FEAD_CPP_PATH/web/"+name+"/public/\n";
154  configurationData.cntMap[name] = "true";
155  listi(dcppath,".dcp",true,dcps);
156  listi(tmplpath,".tpe",true,tpes);
157  listi(cmppath,".cmp",true,compnts);
158  all.push_back(usrincludes);
159  appf.push_back(defpath+"app.xml");
160 
161  libs += ("-l"+ name+" ");
162  ilibs += ("-I" + usrincludes+" ");
163  wspath.push_back(name);
164 
165  logger << "started reading application.xml " << endl;
166  Element root = parser.getDocument(defpath+"config/application.xml").getRootElement();
167  if(root.getTagName()=="app" && root.getChildElements().size()>0)
168  {
169  ElementList eles = root.getChildElements();
170  for (unsigned int apps = 0; apps < eles.size(); apps++)
171  {
172  if(eles.at(apps).getTagName()=="controllers")
173  {
174  ElementList cntrls = eles.at(apps).getChildElements();
175  for (unsigned int cntn = 0; cntn < cntrls.size(); cntn++)
176  {
177  if(cntrls.at(cntn).getTagName()=="controller")
178  {
179  string url = cntrls.at(cntn).getAttribute("url");
180  string clas = cntrls.at(cntn).getAttribute("class");
181  if(url!="" && clas!="")
182  {
183  if(cntrls.at(cntn).getAttribute("url").find("*")!=string::npos)
184  {
185  if(url=="*.*")
186  configurationData.urlpattMap[name+url] = clas;
187  else
188  {
189  url = url.substr(url.find("*")+1);
190  configurationData.urlMap[name+url] = clas;
191  }
192  }
193  else if(clas!="")
194  configurationData.urlMap[name+url] = clas;
195  logger << ("Adding Controller for " + (name + url) + " :: " + clas) << endl;
196  }
197  else
198  {
199  string from = cntrls.at(cntn).getAttribute("from");
200  string to = cntrls.at(cntn).getAttribute("to");
201  if(to.find("*")!=string::npos && to!="")
202  to = to.substr(to.find("*")+1);
203  if(from.find("*")!=string::npos && to!="")
204  {
205  if(from=="*.*")
206  configurationData.mappattMap[name+from] = to;
207  else
208  {
209  from = from.substr(from.find("*")+1);
210  configurationData.mapMap[name+from] = to;
211  }
212  }
213  else if(to!="")
214  {
215  configurationData.mapMap[name+from] = to;
216  }
217  logger << ("Adding Mapping for " + (name + from) + " :: " + to) << endl;
218  }
219  }
220  }
221  }
222  else if(eles.at(apps).getTagName()=="authhandlers")
223  {
224  ElementList cntrls = eles.at(apps).getChildElements();
225  for (unsigned int cntn = 0; cntn < cntrls.size(); cntn++)
226  {
227  if(cntrls.at(cntn).getTagName()=="authhandler")
228  {
229  string url = cntrls.at(cntn).getAttribute("url");
230  string provider = cntrls.at(cntn).getAttribute("provider");
231  if(url!="" && provider!="")
232  {
233  if(url.find("*")!=string::npos)
234  {
235  if(url=="*.*")
236  configurationData.autpattMap[name+url] = provider;
237  else
238  {
239  url = url.substr(url.find("*")+1);
240  configurationData.autMap[name+url] = provider;
241  }
242  }
243  else if(provider!="")
244  configurationData.autMap[name+url] = provider;
245  logger << ("Adding Authhandler for " + (name + url) + " :: " + provider) << endl;
246  }
247  }
248  }
249  }
250  else if(eles.at(apps).getTagName()=="filters")
251  {
252  ElementList cntrls = eles.at(apps).getChildElements();
253  for (unsigned int cntn = 0; cntn < cntrls.size(); cntn++)
254  {
255  if(cntrls.at(cntn).getTagName()=="filter")
256  {
257  string url = cntrls.at(cntn).getAttribute("url");
258  string clas = cntrls.at(cntn).getAttribute("class");
259  string type = cntrls.at(cntn).getAttribute("type");
260  if(clas!="" && (type=="in" || type=="out"))
261  {
262  if(url=="")url="*.*";
263  if(url.find("*")!=string::npos)
264  {
265  if(url=="*.*")
266  {
267  configurationData.filterMap[name+url+type].push_back(clas);
268  }
269  else
270  {
271  url = url.substr(url.find("*")+1);
272  configurationData.filterMap[name+url+type].push_back(clas);
273  }
274  }
275  logger << ("Adding Filter for " + (name + url + type) + " :: " + clas) << endl;
276  }
277  }
278  }
279  }
280  else if(eles.at(apps).getTagName()=="templates")
281  {
282  ElementList tmplts = eles.at(apps).getChildElements();
283  for (unsigned int tmpn = 0; tmpn < tmplts.size(); tmpn++)
284  {
285  if(tmplts.at(tmpn).getTagName()=="template")
286  {
287  configurationData.tmplMap[name+tmplts.at(tmpn).getAttribute("file")] = tmplts.at(tmpn).getAttribute("class");
288  tpes.push_back(defpath+tmplts.at(tmpn).getAttribute("file"));
289  logger << ("Adding Template for " + (name+tmplts.at(tmpn).getAttribute("file")) + " :: " + tmplts.at(tmpn).getAttribute("class")) << endl;
290  }
291  }
292  }
293  else if(eles.at(apps).getTagName()=="dviews")
294  {
295  ElementList dvs = eles.at(apps).getChildElements();
296  for (unsigned int dn = 0; dn < dvs.size(); dn++)
297  {
298  if(dvs.at(dn).getTagName()=="dview")
299  {
300  configurationData.vwMap[name+dvs.at(dn).getAttribute("path")] = dvs.at(dn).getAttribute("class");
301  logger << ("Adding Dynamic View for " + (name+dvs.at(dn).getAttribute("path")) + " :: " + dvs.at(dn).getAttribute("class")) << endl;
302  }
303  }
304  }
305  else if(eles.at(apps).getTagName()=="ajax-interfaces")
306  {
307  ElementList ajintfs = eles.at(apps).getChildElements();
308  for (unsigned int dn = 0; dn < ajintfs.size(); dn++)
309  {
310  if(ajintfs.at(dn).getTagName()=="ajax-interface")
311  {
312  string url = ajintfs.at(dn).getAttribute("url");
313  if(url.find("*")==string::npos)
314  {
315  if(url=="")
316  url = "/";
317  else if(url.at(0)!='/')
318  url = "/" + url;
319  configurationData.ajaxIntfMap[name+url] = ajintfs.at(dn).getAttribute("class");
320  pathvec.push_back(name);
321  vecvp.push_back(usrincludes);
322  stat.push_back(false);
323  ajintpthMap[ajintfs.at(dn).getAttribute("class")] = "/" + name+url;
324  afcd.push_back(ajintfs.at(dn).getAttribute("class"));
325  logger << ("Adding Ajax Interface for " + (name+url) + " :: " + ajintfs.at(dn).getAttribute("class")) << endl;
326  }
327  }
328  }
329  }
330  else if(eles.at(apps).getTagName()=="restcontrollers")
331  {
332  ElementList cntrls = eles.at(apps).getChildElements();
333  for (unsigned int cntn = 0; cntn < cntrls.size(); cntn++)
334  {
335  if(cntrls.at(cntn).getTagName()=="restcontroller")
336  {
337  string url = cntrls.at(cntn).getAttribute("urlpath");
338  string clas = cntrls.at(cntn).getAttribute("class");
339  string rname = cntrls.at(cntn).getAttribute("name");
340  ElementList resfuncs = cntrls.at(cntn).getChildElements();
341  for (unsigned int cntn1 = 0; cntn1 < resfuncs.size(); cntn1++)
342  {
343  if(resfuncs.at(cntn1).getTagName()=="restfunction")
344  {
345  RestFunction restfunction;
346  restfunction.name = resfuncs.at(cntn1).getAttribute("name");
347  restfunction.alias = resfuncs.at(cntn1).getAttribute("alias");
348  restfunction.clas = clas;
349  restfunction.meth = resfuncs.at(cntn1).getAttribute("meth");
350  restfunction.baseUrl = resfuncs.at(cntn1).getAttribute("baseUrl");
351  if(restfunction.baseUrl!="")
352  {
353  if(restfunction.baseUrl.at(0)!='/')
354  restfunction.baseUrl = "/" + restfunction.baseUrl;
355  }
356  restfunction.icontentType = resfuncs.at(cntn1).getAttribute("icontentType");
357  restfunction.ocontentType = resfuncs.at(cntn1).getAttribute("ocontentType");
358  ElementList resfuncparams = resfuncs.at(cntn1).getChildElements();
359  bool hasBodyParam = false, invalidParam = false;;
360  for (unsigned int cntn2 = 0; cntn2 < resfuncparams.size(); cntn2++)
361  {
362  if(resfuncparams.at(cntn2).getTagName()=="param")
363  {
364  RestFunctionParams param;
365  param.type = resfuncparams.at(cntn2).getAttribute("type");
366  param.from = resfuncparams.at(cntn2).getAttribute("from");
367  param.name = resfuncparams.at(cntn2).getAttribute("name");
368  if(param.from=="body")
369  {
370  hasBodyParam = true;
371  }
372  if(StringUtil::trimCopy(param.type)=="")
373  {
374  invalidParam = true;
375  logger << "no type specified for param" << endl;
376  }
377  else if(param.from!="body" && StringUtil::trimCopy(param.name)=="")
378  {
379  invalidParam = true;
380  logger << "no name specified for param" << endl;
381  }
382  else if(param.from=="body" && (restfunction.meth=="GET" || restfunction.meth=="OPTIONS" || restfunction.meth=="TRACE"
383  || restfunction.meth=="HEAD"))
384  {
385  invalidParam = true;
386  logger << ("skipping param " + param.type + ", from is body and method is " + restfunction.meth) << endl;
387  }
388  else if(!(param.type=="int" || param.type=="short" || param.type=="long" || param.type=="float" || param.type=="string"
389  || param.type=="std::string" || param.type=="double" || param.type=="bool") && param.from!="body")
390  {
391  invalidParam = true;
392  logger << ("skipping param " + param.type + ", from is body input is complex type") << endl;
393  }
394  else if(param.from=="postparam" && (restfunction.meth=="GET" || restfunction.meth=="OPTIONS" || restfunction.meth=="TRACE"
395  || restfunction.meth=="HEAD"))
396  {
397  invalidParam = true;
398  logger << ("skipping param " + param.type + ", from is postparam and method is " + restfunction.meth) << endl;
399  }
400  else
401  restfunction.params.push_back(param);
402  }
403  }
404  if(hasBodyParam && restfunction.params.size()>1)
405  {
406  continue;
407  }
408  if(invalidParam)
409  {
410  continue;
411  }
412  if(clas!="")
413  {
414  if(url.find("*")==string::npos)
415  {
416  if(url=="")
417  url = "/";
418  else if(url.at(0)!='/')
419  url = "/" + url;
420  string urlmpp;
421  if(rname!="")
422  {
423  if(restfunction.alias!="")
424  {
425  if(restfunction.baseUrl=="")
426  urlmpp = name+url+rname+"/"+restfunction.alias;
427  else
428  urlmpp = name+restfunction.baseUrl;
429  StringUtil::replaceFirst(urlmpp,"//","/");
430  configurationData.rstCntMap[urlmpp] = restfunction;
431  }
432  else
433  {
434  if(restfunction.baseUrl=="")
435  urlmpp = name+url+rname+"/"+restfunction.name;
436  else
437  urlmpp = name+restfunction.baseUrl;
438  StringUtil::replaceFirst(urlmpp,"//","/");
439  configurationData.rstCntMap[urlmpp] = restfunction;
440  }
441  }
442  else
443  {
444  if(restfunction.alias!="")
445  {
446  if(restfunction.baseUrl=="")
447  urlmpp = name+url+clas+"/"+restfunction.alias;
448  else
449  urlmpp = name+restfunction.baseUrl;
450  StringUtil::replaceFirst(urlmpp,"//","/");
451  configurationData.rstCntMap[urlmpp] = restfunction;
452  }
453  else
454  {
455  if(restfunction.baseUrl=="")
456  urlmpp = name+url+clas+"/"+restfunction.name;
457  else
458  urlmpp = name+restfunction.baseUrl;
459  StringUtil::replaceFirst(urlmpp,"//","/");
460  configurationData.rstCntMap[urlmpp] = restfunction;
461  }
462  }
463  logger << ("Adding rest-controller => " + urlmpp + " , class => " + clas) << endl;
464  }
465  }
466  }
467  }
468  }
469  }
470  }
471  else if(eles.at(apps).getTagName()=="security")
472  {
473  ElementList cntrls = eles.at(apps).getChildElements();
474  for (unsigned int cntn = 0; cntn < cntrls.size(); cntn++)
475  {
476  if(cntrls.at(cntn).getTagName()=="login-handler")
477  {
478  string provider = cntrls.at(cntn).getAttribute("provider");
479  string url = cntrls.at(cntn).getAttribute("url");
480  string sessionTimeoutV = cntrls.at(cntn).getAttribute("sessionTimeout");
481  Security securityObject;
482  securityObject.loginProvider = provider;
483  StringUtil::replaceFirst(url,"//","/");
484  if(url.at(0)=='/' && url.length()>1)
485  url = url.substr(1);
486  securityObject.loginUrl = url;
487  try {
488  securityObject.sessTimeout = CastUtil::lexical_cast<long>(sessionTimeoutV);
489  } catch (...) {
490  securityObject.sessTimeout = 3600;
491  logger << "\nInvalid session timeout value defined, defaulting to 1hour/3600sec" << endl;
492  }
493  configurationData.securityObjectMap[name] = securityObject;
494  }
495  else if(cntrls.at(cntn).getTagName()=="secure")
496  {
497  if(configurationData.securityObjectMap.find(name)!=configurationData.securityObjectMap.end())
498  {
499  Security securityObject = configurationData.securityObjectMap[name];
500  string path = cntrls.at(cntn).getAttribute("path");
501  string role = cntrls.at(cntn).getAttribute("role");
502  SecureAspect secureAspect;
503  secureAspect.path = path;
504  secureAspect.role = role;
505  securityObject.secures.push_back(secureAspect);
506  configurationData.securityObjectMap[name] = securityObject;
507  }
508  }
509  else if(cntrls.at(cntn).getTagName()=="welcome")
510  {
511  string welcomeFile = cntrls.at(cntn).getAttribute("file");
512  if(configurationData.securityObjectMap.find(name)!=configurationData.securityObjectMap.end())
513  {
514  Security securityObject = configurationData.securityObjectMap[name];
515  securityObject.welocmeFile = welcomeFile;
516  configurationData.securityObjectMap[name] = securityObject;
517  }
518  }
519  }
520  }
521  else if(eles.at(apps).getTagName()=="handoffs")
522  {
523  ElementList cntrls = eles.at(apps).getChildElements();
524  for (unsigned int cntn = 0; cntn < cntrls.size(); cntn++)
525  {
526  if(cntrls.at(cntn).getTagName()=="handoff")
527  {
528  string app = cntrls.at(cntn).getAttribute("app");
529  string def = cntrls.at(cntn).getAttribute("default");
530  string ext = cntrls.at(cntn).getAttribute("ext");
531  configurationData.handoffs[app] = def;
532  configurationData.handoffs[app+"extension"] = ext;
533  handoffVec.push_back("-l"+ app+" ");
534  }
535  }
536  }
537  }
538  }
539  logger << "done reading application.xml " << endl;
540 
541  /*logger << "started reading cibernate.xml " << endl;
542  Mapping* mapping = new Mapping;
543  smstrMap appTableColMapping;
544  strMap maptc,maptcl;
545  relMap appTableRelMapping;
546  Element dbroot = parser.getDocument(defpath+"config/cibernate.xml").getRootElement();
547  if(dbroot.getTagName()=="cibernate")
548  {
549  ElementList dbeles = dbroot.getChildElements();
550  for (unsigned int dbs = 0; dbs < dbeles.size(); dbs++)
551  {
552  if(dbeles.at(dbs).getTagName()=="config")
553  {
554  ElementList confs = dbeles.at(dbs).getChildElements();
555  string uid,pwd,dsn;
556  int psize= 2;
557  for (unsigned int cns = 0; cns < confs.size(); cns++)
558  {
559  if(confs.at(cns).getTagName()=="uid")
560  {
561  uid = confs.at(cns).getText();
562  }
563  else if(confs.at(cns).getTagName()=="pwd")
564  {
565  pwd = confs.at(cns).getText();
566  }
567  else if(confs.at(cns).getTagName()=="dsn")
568  {
569  dsn = confs.at(cns).getText();
570  }
571  else if(confs.at(cns).getTagName()=="pool-size")
572  {
573  if(confs.at(cns).getText()!="")
574  psize = CastUtil::lexical_cast<int>(confs.at(cns).getText());
575  }
576  }
577  CibernateConnPools::addPool(psize,uid,pwd,dsn,name);
578  }
579  else if(dbeles.at(dbs).getTagName()=="tables")
580  {
581  ElementList tabs = dbeles.at(dbs).getChildElements();
582  for (unsigned int dn = 0; dn < tabs.size(); dn++)
583  {
584  if(tabs.at(dn).getTagName()=="table")
585  {
586  vector<DBRel> relv;
587  maptcl[tabs.at(dn).getAttribute("class")] = tabs.at(dn).getAttribute("name");
588  ElementList cols = tabs.at(dn).getChildElements();
589  for (unsigned int cn = 0; cn < cols.size(); cn++)
590  {
591  if(cols.at(cn).getTagName()=="hasOne")
592  {
593  DBRel relation;
594  relation.clsName = cols.at(cn).getText();
595  relation.type = 1;
596  relation.fk = cols.at(cn).getAttribute("fk");
597  relation.pk = cols.at(cn).getAttribute("pk");
598  relv.push_back(relation);
599  }
600  else if(cols.at(cn).getTagName()=="hasMany")
601  {
602  DBRel relation;
603  relation.clsName = cols.at(cn).getText();
604  relation.type = 2;
605  relation.fk = cols.at(cn).getAttribute("fk");
606  relation.pk = cols.at(cn).getAttribute("pk");
607  relation.field = cols.at(cn).getAttribute("field");
608  relv.push_back(relation);
609  }
610  else if(cols.at(cn).getTagName()=="many")
611  {
612  DBRel relation;
613  relation.clsName = cols.at(cn).getText();
614  relation.type = 3;
615  relation.fk = cols.at(cn).getAttribute("fk");
616  relation.pk = cols.at(cn).getAttribute("pk");
617  relv.push_back(relation);
618  }
619  else if(cols.at(cn).getTagName()=="col")
620  {
621  maptc[cols.at(cn).getAttribute("obf")] = cols.at(cn).getAttribute("dbf");
622  }
623  }
624  appTableColMapping[tabs.at(dn).getAttribute("class")] = maptc;
625  appTableRelMapping[tabs.at(dn).getAttribute("class")] = relv;
626  }
627  }
628  }
629  }
630  }
631  mapping->setAppTableColMapping(appTableColMapping);
632  mapping->setAppTableClassMapping(maptcl);
633  mapping->setAppTableRelMapping(appTableRelMapping);
634  CibernateConnPools::addMapping(name,mapping);
635  logger << "done reading cibernate.xml " << endl;*/
636  configureCibernate(name, defpath+"config/cibernate.xml");
637 
638  /*logger << "started reading afc.prop " << endl;
639  propMap afc = pread.getProperties(defpath+"config/afc.prop");
640  string filepath;
641  if(afc.size()>0)
642  {
643  string objs = afc["PROP"];
644  strVec objv;
645  StringUtil::split(objv, objs, (","));
646  for (unsigned int var1 = 0;var1<objv.size();var1++)
647  {
648  if(objv.at(var1)!="")
649  {
650  //strVec info = ref.getAfcObjectData(usrincludes+objv.at(var)+".h", true);
651  pathvec.push_back(name);
652  vecvp.push_back(usrincludes);
653  stat.push_back(true);
654  afcd.push_back(objv.at(var1));
655  }
656  }
657  objs = afc["INTF"];
658  objv.clear();
659  StringUtil::split(objv, objs, (","));
660  for (unsigned int var1 = 0;var1<objv.size();var1++)
661  {
662  if(objv.at(var1)!="")
663  {
664  //strVec info = ref.getAfcObjectData(usrincludes+objv.at(var)+".h", false);
665  pathvec.push_back(name);
666  vecvp.push_back(usrincludes);
667  stat.push_back(false);
668  afcd.push_back(objv.at(var1));
669  }
670  }
671  }
672  logger << "done reading afc.prop " << endl;*/
673 
674  logger << "started reading fviews.xml " << endl;
675  root = parser.getDocument(defpath+"config/fviews.xml").getRootElement();
676  if(root.getTagName()=="fview" && root.getChildElements().size()>0)
677  {
678  ElementList eles = root.getChildElements();
679  for (unsigned int apps = 0; apps < eles.size(); apps++)
680  {
681  if(eles.at(apps).getTagName()=="page")
682  {
683  string fvw = eles.at(apps).getAttribute("htm");
684  StringUtil::replaceFirst(fvw,".html",".fview");
685  configurationData.fviewmap[eles.at(apps).getAttribute("htm")] = eles.at(apps).getAttribute("class");
686  pathvec.push_back(name);
687  vecvp.push_back(usrincludes);
688  stat.push_back(false);
689  afcd.push_back(eles.at(apps).getAttribute("class"));
690  ElementList elese = eles.at(apps).getChildElements();
691  string nsfns = "\nvar _fview_namespace = {";
692  string js = "\n\nwindow.onload = function(){";
693  for (int appse = 0; appse < (int)elese.size(); appse++)
694  {
695  if(elese.at(appse).getTagName()=="event")
696  {
697  nsfns += "\n\"_fview_cntxt_global_js_callback"+CastUtil::lexical_cast<string>(appse)+"\" : function(response){" + elese.at(appse).getAttribute("cb") + "},";
698  js += "\ndocument.getElementById('"+elese.at(appse).getAttribute("eid")+"').";
699  js += elese.at(appse).getAttribute("type") + " = function(){";
700  js += eles.at(apps).getAttribute("class")+"."+elese.at(appse).getAttribute("func")+"(";
701  string args = elese.at(appse).getAttribute("args");
702  if(args!="")
703  args += ",";
704  js += args + "\"_fview_cntxt_global_js_callback"+CastUtil::lexical_cast<string>(appse)+"\",\"/"+name+"/"+fvw+"\",_fview_namespace);}";
705  }
706  else if(elese.at(appse).getTagName()=="form")
707  {
708  pathvec.push_back(name);
709  vecvp.push_back(usrincludes);
710  stat.push_back(true);
711  afcd.push_back(elese.at(appse).getAttribute("bean"));
712  configurationData.formMap[elese.at(appse).getAttribute("name")] = elese.at(appse);
713  }
714  }
715  js += "\n}\n\n";
716  nsfns = nsfns.substr(0,nsfns.length()-1) + "\n}\n";
717  js = nsfns + js;
718  for (unsigned int appse = 0; appse < elese.size(); appse++)
719  {
720  if(elese.at(appse).getTagName()=="functions")
721  {
722  js += elese.at(appse).getText();
723  }
724  }
725  AfcUtil::writeTofile(pubpath+eles.at(apps).getAttribute("class")+".js",js,true);
726  }
727  }
728  }
729  logger << "done reading fviews.xml " << endl;
730  }
731  logger << "started generating component code" <<endl;
732  for (unsigned int var1 = 0;var1<compnts.size();var1++)
733  {
734  string cudata,cuheader,curemote,curemoteheaders;
735  try
736  {
737  string file = gen.generateComponentCU(compnts.at(var1),cudata,cuheader,curemote,curemoteheaders);
738  AfcUtil::writeTofile(rtdcfpath+file+".h",cuheader,true);
739  AfcUtil::writeTofile(rtdcfpath+file+".cpp",cudata,true);
740  AfcUtil::writeTofile(rtdcfpath+file+"_Remote.h",curemoteheaders,true);
741  AfcUtil::writeTofile(rtdcfpath+file+"_Remote.cpp",curemote,true);
742  isrcs += "./"+file+".cpp \\\n"+"./"+file+"_Remote.cpp \\\n";
743  iobjs += "./"+file+".o \\\n"+"./"+file+"_Remote.o \\\n";
744  ideps += "./"+file+".d \\\n"+"./"+file+"_Remote.d \\\n";
745  configurationData.cmpnames.push_back(file);
746  }
747  catch(const char* ex)
748  {
749  logger << ("Exception occurred during component code generation : ") << ex << endl;
750  }
751  }
752  for (unsigned int cntn = 0; cntn < handoffVec.size(); cntn++)
753  {
754  StringUtil::replaceFirst(libs, handoffVec.at(cntn), "");
755  }
756  logger << "done generating component code" <<endl;
757  logger << "started generating reflection/serialization code" <<endl;
758  string ret = ref.generateClassDefinitionsAll(all,includeRef);
759  string objs, ajaxret, headers,typerefs;
760  AfcUtil::writeTofile(rtdcfpath+"ReflectorInterface.cpp",ret,true);
761  ret = ref.generateSerDefinitionAll(all,includeRef, true, objs, ajaxret, headers,typerefs);
762  AfcUtil::writeTofile(rtdcfpath+"SerializeInterface.cpp",ret,true);
763  logger << "done generating reflection/serialization code" <<endl;
764  cntxt["RUNTIME_LIBRARIES"] = libs;
765  ret = templ.evaluate(rtdcfpath+"objects.mk.template",cntxt);
766  AfcUtil::writeTofile(rtdcfpath+"objects.mk",ret,true);
767  cntxt.clear();
768  cntxt["USER_DEFINED_INC"] = ilibs;
769  cntxt["RUNTIME_COMP_SRCS"] = isrcs;
770  cntxt["RUNTIME_COMP_OBJS"] = iobjs;
771  cntxt["RUNTIME_COMP_DEPS"] = ideps;
772  ret = templ.evaluate(rtdcfpath+"subdir.mk.template",cntxt);
773  AfcUtil::writeTofile(rtdcfpath+"subdir.mk",ret,true);
774  configurationData.dcpsss = dcps;
775  logger << "started generating dcp code" <<endl;
776  ret = DCPGenerator::generateDCPAll(dcps);
777  AfcUtil::writeTofile(rtdcfpath+"DCPInterface.cpp",ret,true);
778  logger << "done generating dcp code" <<endl;
779  configurationData.tpes = tpes;
780  logger << "started generating template code" <<endl;
781  ret = TemplateGenerator::generateTempCdAll(tpes);
782  //logger << ret << endl;
783  AfcUtil::writeTofile(rtdcfpath+"TemplateInterface.cpp",ret,true);
784  logger << "done generating template code" <<endl;
785  string infjs;
786  logger << endl<< "started generating ajax code" <<endl;
787  string ajaxHeaders;
788  ret = AfcUtil::generateJsObjectsAll(vecvp,afcd,stat,ajaxHeaders,objs,infjs,pathvec,ajaxret,typerefs,ajintpthMap);
789  AfcUtil::writeTofile(rtdcfpath+"AjaxInterface.cpp",ret,true);
790  AfcUtil::writeTofile(pubpath+"_afc_Objects.js",objs,true);
791  AfcUtil::writeTofile(pubpath+"_afc_Interfaces.js",infjs,true);
792  AfcUtil::writeTofile(incpath+"AfcInclude.h",(ajaxHeaders+headers),true);
793  logger << "done generating ajax code" <<endl;
794  ApplicationUtil apputil;
795  webdirs.clear();
796  logger << "started generating application code" <<endl;
797  ret = apputil.buildAllApplications(appf,webdirs1,configurationData.appMap);
798  AfcUtil::writeTofile(rtdcfpath+"ApplicationInterface.cpp",ret,true);
799  logger << "done generating application code" <<endl;
800  WsUtil wsu;
801  logger << "started generating web-service code" <<endl;
802  ret = wsu.generateAllWSDL(wspath,respath,configurationData.wsdlmap);
803  AfcUtil::writeTofile(rtdcfpath+"WsInterface.cpp",ret,true);
804  logger << "done generating web-service code" <<endl;
805  TemplateEngine engine;
806  cntxt.clear();
807  cntxt["TARGET_LIB"] = "all";
808  cntxt["Dynamic_Public_Folder_Copy"] = rundyncontent;
809  string cont = engine.evaluate(respath+"/rundyn_template.sh", cntxt);
810  AfcUtil::writeTofile(respath+"/rundyn.sh", cont, true);
811  cntxt.clear();
812  cntxt["TARGET_LIB"] = "libdinter";
813  cntxt["Dynamic_Public_Folder_Copy"] = rundyncontent;
814  cont = engine.evaluate(respath+"/rundyn_template.sh", cntxt);
815  AfcUtil::writeTofile(respath+"/rundyn_dinter.sh", cont, true);
816  return configurationData;
817 }
818 
819 void ConfigurationHandler::configureCibernate(string name, string configFile)
820 {
821  Logger logger = Logger::getLogger("ConfigurationHandler");
822  XmlParser parser("Parser");
823  logger << ("started reading cibernate config file " + configFile) << endl;
824  Mapping* mapping = new Mapping;
825  smstrMap appTableColMapping;
826  strMap maptc,maptcl;
827  relMap appTableRelMapping;
828  Element dbroot = parser.getDocument(configFile).getRootElement();
829  if(dbroot.getTagName()=="cibernate")
830  {
831  ElementList dbeles = dbroot.getChildElements();
832  for (unsigned int dbs = 0; dbs < dbeles.size(); dbs++)
833  {
834  if(dbeles.at(dbs).getTagName()=="config")
835  {
836  ElementList confs = dbeles.at(dbs).getChildElements();
837  string uid,pwd,dsn;
838  int psize= 2;
839  for (unsigned int cns = 0; cns < confs.size(); cns++)
840  {
841  if(confs.at(cns).getTagName()=="uid")
842  {
843  uid = confs.at(cns).getText();
844  }
845  else if(confs.at(cns).getTagName()=="pwd")
846  {
847  pwd = confs.at(cns).getText();
848  }
849  else if(confs.at(cns).getTagName()=="dsn")
850  {
851  dsn = confs.at(cns).getText();
852  }
853  else if(confs.at(cns).getTagName()=="pool-size")
854  {
855  if(confs.at(cns).getText()!="")
856  psize = CastUtil::lexical_cast<int>(confs.at(cns).getText());
857  }
858  }
859  CibernateConnPools::addPool(psize,uid,pwd,dsn,name);
860  }
861  else if(dbeles.at(dbs).getTagName()=="tables")
862  {
863  ElementList tabs = dbeles.at(dbs).getChildElements();
864  for (unsigned int dn = 0; dn < tabs.size(); dn++)
865  {
866  if(tabs.at(dn).getTagName()=="table")
867  {
868  vector<DBRel> relv;
869  maptcl[tabs.at(dn).getAttribute("class")] = tabs.at(dn).getAttribute("name");
870  ElementList cols = tabs.at(dn).getChildElements();
871  for (unsigned int cn = 0; cn < cols.size(); cn++)
872  {
873  if(cols.at(cn).getTagName()=="hasOne")
874  {
875  DBRel relation;
876  relation.clsName = cols.at(cn).getText();
877  relation.type = 1;
878  relation.fk = cols.at(cn).getAttribute("fk");
879  relation.pk = cols.at(cn).getAttribute("pk");
880  relation.field = cols.at(cn).getAttribute("field");
881  relv.push_back(relation);
882  }
883  else if(cols.at(cn).getTagName()=="hasMany")
884  {
885  DBRel relation;
886  relation.clsName = cols.at(cn).getText();
887  relation.type = 2;
888  relation.fk = cols.at(cn).getAttribute("fk");
889  relation.pk = cols.at(cn).getAttribute("pk");
890  relation.field = cols.at(cn).getAttribute("field");
891  relv.push_back(relation);
892  }
893  else if(cols.at(cn).getTagName()=="many")
894  {
895  DBRel relation;
896  relation.clsName = cols.at(cn).getText();
897  relation.type = 3;
898  relation.fk = cols.at(cn).getAttribute("fk");
899  relation.pk = cols.at(cn).getAttribute("pk");
900  relv.push_back(relation);
901  }
902  else if(cols.at(cn).getTagName()=="col")
903  {
904  maptc[cols.at(cn).getAttribute("obf")] = StringUtil::toLowerCopy(cols.at(cn).getAttribute("dbf"));
905  }
906  }
907  appTableColMapping[tabs.at(dn).getAttribute("class")] = maptc;
908  appTableRelMapping[tabs.at(dn).getAttribute("class")] = relv;
909  }
910  }
911  }
912  }
913  }
914  mapping->setAppTableColMapping(appTableColMapping);
915  mapping->setAppTableClassMapping(maptcl);
916  mapping->setAppTableRelMapping(appTableRelMapping);
917  CibernateConnPools::addMapping(name,mapping);
918  logger << "done reading cibernate config file " + configFile << endl;
919 }
920 
921 
922 void ConfigurationHandler::destroyCibernate()
923 {
924  map<string,Mapping*> mappings = CibernateConnPools::getMappings();
925  map<string,Mapping*>::iterator it;
926  for(it=mappings.begin();it!=mappings.end();it++)
927  {
928  CibernateConnectionPool* pool = CibernateConnPools::getPool(it->first);
929  delete pool;
930  delete it->second;
931  }
932 }