ffead.server.doc
ComponentGen.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  * ComponentGen.cpp
18  *
19  * Created on: Mar 24, 2010
20  * Author: sumeet
21  */
22 
23 #include "ComponentGen.h"
24 
25 ComponentGen::ComponentGen() {
26  logger = Logger::getLogger("ComponentGen");
27 }
28 
29 ComponentGen::~ComponentGen() {
30  // TODO Auto-generated destructor stub
31 }
32 
33 string convertfrom(string val)
34 {
35  StringUtil::toUpper(val);
36  if(val=="TRUE")
37  return "true";
38  else if(val=="FALSE")
39  return "false";
40  else
41  {
42  throw "Conversion exception";
43  }
44 }
45 
46 
47 string ComponentGen::generateComponentCU(string fileName,string &cudata,string &cuheader,string &curemote,string &curemoteheaders)
48 {
49  string file;
50  /*int s,en;
51  s = fileName.find_last_of("/")+1;
52  dir = fileName.substr(0,s-1);
53  en = fileName.find_last_of(".");*/
54  string initheaders,cuinithdrs,name;
55  /*file = fileName.substr(s,en-s);
56  file = ("Component_"+file);
57  string initheaders = "#include \"Component.h\"\n";
58  string cuheader = "class "+file + ": public Component \n{\n";
59  string cudata = "#include \""+file+".h\"\n\n";
60  string constr;
61  string servs,servsh;
62  cuheader += "public:\n";
63  cuheader += "~"+file+"();\n";
64  cuheader += file+"();\n";
65  cudata += file+"::~"+file+"()\n{}\n";
66  cudata += file+"::"+file+"()\n{\n";*/
67  PropFileReader pread;
68  propMap srprps = pread.getProperties(fileName);
69  if(srprps.size()>2)
70  {
71  try
72  {
73  name = srprps["@CMP_NAME"];
74  if(name=="")
75  throw "Component Name must be specified";
76  file = ("Component_"+name);
77  string cudef(file);
78  StringUtil::toUpper(cudef);
79  initheaders = "#ifndef "+cudef+"_H\n#define "+cudef+"_H\n";
80  cuinithdrs = "#ifndef "+cudef+"_REMOTE_H\n#define "+cudef+"_REMOTE_H\n";
81  initheaders += "#include \"Component.h\"\n";
82  initheaders += "#include \"AppContext.h\"\n";
83  cuheader = "class "+file + ": public Component \n{\n";
84  curemote = "#include \""+file+"_Remote.h\"\n";
85  curemoteheaders = "#include \"RemoteComponent.h\"\n";
86  curemoteheaders += "class "+file + "_Remote : public RemoteComponent \n{\n";
87 
88  curemoteheaders += "public:\n";
89  curemoteheaders += "~"+file+"_Remote();\n";
90  curemoteheaders += file+"_Remote();\n";
91 
92  cudata = "#include \""+file+".h\"\n\n";
93 
94  string constr;
95  string servs,servsh,rsrvs;
96  cuheader += "public:\n";
97  cuheader += "~"+file+"();\n";
98  cuheader += file+"();\n";
99  cudata += file+"::~"+file+"()\n{}\n";
100  cudata += file+"::"+file+"()\n{\n";
101  cudata += "AppContext::lookup(\""+name+"\");\n;";
102 
103  curemote += file+"_Remote::~"+file+"_Remote()\n{}\n";
104  curemote += file+"_Remote::"+file+"_Remote()\n{\n";
105  curemote += ("this->setBeanName(\""+name+"\");\n");
106 
107  cudata+= ("this->setName(\""+name+"\");\n");
108 
109  string desc = srprps["@CMP_DESC"];
110  cudata+= ("this->setDesc(\""+desc+"\");\n");
111 
112  cudata+= ("this->setAjaxAvail("+convertfrom(srprps["@AJAX_AVAIL"])+");\n");
113 
114  cudata+= ("this->setWebsAvail("+convertfrom(srprps["@WEBS_AVAIL"])+");\n");
115 
116  cudata+= ("this->setMethinvAvail("+convertfrom(srprps["@MINV_AVAIL"])+");\n");
117 
118  cudata+= ("this->setAutoTranx("+convertfrom(srprps["@AUTO_TRANS"])+");\n");
119 
120  cudata+= ("this->setSession("+convertfrom(srprps["@SESSION"])+");\n");
121 
122  cudata+= ("this->setThrdPerReq("+convertfrom(srprps["@THRD_PER_REQ"])+");\n");
123 
124  cudata+= ("this->setThrdPoolSize("+srprps["@THRD_POOL_NUM"]+");\n");
125 
126  cudata+= ("this->setAuthMode("+convertfrom(srprps["@AUTH_ALL"])+");\n");
127 
128  string alwd = srprps["@USR_GRP_ALWD"];
129  Cont1 alwdls;
130  StringUtil::split(alwdls, alwd, (","));
131  for (unsigned int var = 0; var < alwdls.size(); var++)
132  {
133  cudata+= ("this->getAllwList().push_back(\""+alwdls.at(var)+"\");\n");
134  }
135 
136  string blkd = srprps["@BLOCK_USERS"];
137  Cont1 blkls;
138  StringUtil::split(blkls, blkd, (","));
139  for (unsigned int var = 0; var < blkls.size(); var++)
140  {
141  cudata+= ("this->getBlkdList().push_back(\""+blkls.at(var)+"\");\n");
142  }
143 
144  string prots = srprps["@PROTO_ALWD"];
145  Cont1 protls;
146  StringUtil::split(protls, prots, (","));
147  for (unsigned int var = 0; var < protls.size(); var++)
148  {
149  cudata+= ("this->getProtocols().push_back(\""+protls.at(var)+"\");\n");
150  }
151 
152  ConnectionSettings consts;
153  consts.mode = srprps["@AUTH_DETS"];
154  cudata += ("ConnectionSettings conset(");
155  cudata+= ("\""+srprps["@AUTH_DETS"]+"\",");
156  consts.source = srprps["@AUTH_CONN_SRC"];
157  cudata+= ("\""+srprps["@AUTH_CONN_SRC"]+"\",");
158  consts.username = srprps["@AUTH_USR_NAME"];
159  cudata+= ("\""+srprps["@AUTH_USR_NAME"]+"\",");
160  consts.passwd = srprps["@AUTH_USR_PASS"];
161  cudata+= ("\""+srprps["@AUTH_USR_PASS"]+"\",");
162  consts.address = srprps["@AUTH_ADD"];
163  cudata+= ("\""+srprps["@AUTH_ADD"]+"\");\n");
164  cudata+= ("this->setAuthDets(conset);\n");
165 
166  cudata+= ("this->setDbconpoolSize("+srprps["@DB_CONN_POOL_NUM"]+");\n");
167 
168  int cnt =1;
169  while(srprps["@SERVICE"+CastUtil::lexical_cast<string>(cnt)]!="")
170  {
171  string srvc = srprps["@SERVICE"+CastUtil::lexical_cast<string>(cnt++)];
172  ComponentService cmps;
173  if(srvc.find("@USR_GRP_ALWD(")!=string::npos)
174  {
175  int st = srvc.find("@USR_GRP_ALWD(");
176  string temp = srvc.substr(st+14);
177  size_t sp = temp.find_first_of(" ");
178  size_t cl = temp.find_first_of(")");
179  if((sp!=string::npos && sp<cl) || cl==string::npos)
180  {
181  throw "No spaces allowed between in value";
182  }
183  else
184  {
185  string temtem = temp.substr(0,cl);
186  if(temtem.find("@BLOCK_USERS")!=string::npos ||
187  temtem.find("@USR_GRP_ALWD")!=string::npos ||
188  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
189  temtem.find("@SRV_RET")!=string::npos ||
190  temtem.find("@USERS_ALWD")!=string::npos ||
191  temtem.find("@NAME")!=string::npos ||
192  temtem.find("@ARGS")!=string::npos ||
193  temtem.find("@SIGNATURE")!=string::npos)
194  {
195  throw "Invalid start of property within another property";
196  }
197  else
198  {
199  Cont1 alwd;
200  StringUtil::split(alwd, temtem, (","));
201  }
202  }
203  }
204  if(srvc.find("@BLOCK_USERS(")!=string::npos)
205  {
206  int st = srvc.find("@BLOCK_USERS(");
207  string temp = srvc.substr(st+13);
208  size_t sp = temp.find_first_of(" ");
209  size_t cl = temp.find_first_of(")");
210  if((sp!=string::npos && sp<cl) || cl==string::npos)
211  {
212  throw "No spaces allowed between in value";
213  }
214  else
215  {
216  string temtem = temp.substr(0,cl);
217  if(temtem.find("@BLOCK_USERS")!=string::npos ||
218  temtem.find("@USR_GRP_ALWD")!=string::npos ||
219  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
220  temtem.find("@SRV_RET")!=string::npos ||
221  temtem.find("@USERS_ALWD")!=string::npos ||
222  temtem.find("@NAME")!=string::npos ||
223  temtem.find("@ARGS")!=string::npos ||
224  temtem.find("@SIGNATURE")!=string::npos)
225  {
226  throw "Invalid start of property within another property";
227  }
228  else
229  {
230  Cont1 alwd;
231  StringUtil::split(alwd, temtem, (","));
232  }
233  }
234  }
235  if(srvc.find("@BLOCK_USR_GRPS(")!=string::npos)
236  {
237  int st = srvc.find("@BLOCK_USR_GRPS(");
238  string temp = srvc.substr(st+16);
239  size_t sp = temp.find_first_of(" ");
240  size_t cl = temp.find_first_of(")");
241  if((sp!=string::npos && sp<cl) || cl==string::npos)
242  {
243  throw "No spaces allowed between in value";
244  }
245  else
246  {
247  string temtem = temp.substr(0,cl);
248  if(temtem.find("@BLOCK_USERS")!=string::npos ||
249  temtem.find("@USR_GRP_ALWD")!=string::npos ||
250  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
251  temtem.find("@SRV_RET")!=string::npos ||
252  temtem.find("@USERS_ALWD")!=string::npos ||
253  temtem.find("@NAME")!=string::npos ||
254  temtem.find("@ARGS")!=string::npos ||
255  temtem.find("@SIGNATURE")!=string::npos)
256  {
257  throw "Invalid start of property within another property";
258  }
259  else
260  {
261  Cont1 alwd;
262  StringUtil::split(alwd, temtem, (","));
263  }
264  }
265  }
266  string srvret = "void";
267  if(srvc.find("@SRV_RET(")!=string::npos)
268  {
269  int st = srvc.find("@SRV_RET(");
270  string temp = srvc.substr(st+9);
271  size_t sp = temp.find_first_of(" ");
272  size_t cl = temp.find_first_of(")");
273  if((sp!=string::npos && sp<cl) || cl==string::npos)
274  {
275  throw "No spaces allowed between in value";
276  }
277  else
278  {
279  string temtem = temp.substr(0,cl);
280  if(temtem.find("@BLOCK_USERS")!=string::npos ||
281  temtem.find("@USR_GRP_ALWD")!=string::npos ||
282  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
283  temtem.find("@SRV_RET")!=string::npos ||
284  temtem.find("@USERS_ALWD")!=string::npos ||
285  temtem.find("@NAME")!=string::npos ||
286  temtem.find("@ARGS")!=string::npos ||
287  temtem.find("@SIGNATURE")!=string::npos)
288  {
289  throw "Invalid start of property within another property";
290  }
291  else
292  {
293  cmps.retType = temtem;
294  srvret = temtem;
295  }
296  }
297  }
298  if(srvc.find("@USERS_ALWD(")!=string::npos)
299  {
300  int st = srvc.find("@USERS_ALWD(");
301  string temp = srvc.substr(st+12);
302  size_t sp = temp.find_first_of(" ");
303  size_t cl = temp.find_first_of(")");
304  if((sp!=string::npos && sp<cl) || cl==string::npos)
305  {
306  throw "No spaces allowed between in value";
307  }
308  else
309  {
310  string temtem = temp.substr(0,cl);
311  if(temtem.find("@BLOCK_USERS")!=string::npos ||
312  temtem.find("@USR_GRP_ALWD")!=string::npos ||
313  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
314  temtem.find("@SRV_RET")!=string::npos ||
315  temtem.find("@USERS_ALWD")!=string::npos ||
316  temtem.find("@NAME")!=string::npos ||
317  temtem.find("@ARGS")!=string::npos ||
318  temtem.find("@SIGNATURE")!=string::npos)
319  {
320  throw "Invalid start of property within another property";
321  }
322  else
323  {
324  Cont1 alwd;
325  StringUtil::split(alwd, temtem, (","));
326  }
327  }
328  }
329  string srvcls,srvmeth;
330  if(srvc.find("@SIGNATURE(")!=string::npos)
331  {
332  int st = srvc.find("@SIGNATURE(");
333  string temp = srvc.substr(st+11);
334  size_t sp = temp.find_first_of(" ");
335  size_t cl = temp.find_first_of(")");
336  if((sp!=string::npos && sp<cl) || cl==string::npos)
337  {
338  throw "No spaces allowed between in value";
339  }
340  else
341  {
342  string temtem = temp.substr(0,cl);
343  if(temtem.find("@BLOCK_USERS")!=string::npos ||
344  temtem.find("@USR_GRP_ALWD")!=string::npos ||
345  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
346  temtem.find("@SRV_RET")!=string::npos ||
347  temtem.find("@USERS_ALWD")!=string::npos ||
348  temtem.find("@NAME")!=string::npos ||
349  temtem.find("@ARGS")!=string::npos ||
350  temtem.find("@SIGNATURE")!=string::npos)
351  {
352  throw "Invalid start of property within another property";
353  }
354  else
355  {
356  Cont1 alwd;
357  StringUtil::split(alwd, temtem, ("."));
358  if(alwd.size()==2)
359  {
360  srvcls = alwd.at(0);
361  srvmeth = alwd.at(1);
362  cmps.classn = alwd.at(0);
363  cmps.methName = alwd.at(1);
364  }
365  else
366  {
367  throw "Invalid Service Signature";
368  }
369  }
370  }
371  }
372  string srvname;
373  if(srvc.find("@NAME(")!=string::npos)
374  {
375  int st = srvc.find("@NAME(");
376  string temp = srvc.substr(st+6);
377  size_t sp = temp.find_first_of(" ");
378  size_t cl = temp.find_first_of(")");
379  if((sp!=string::npos && sp<cl) || cl==string::npos)
380  {
381  throw "No spaces allowed between in value";
382  }
383  else
384  {
385  string temtem = temp.substr(0,cl);
386  if(temtem.find("@BLOCK_USERS")!=string::npos ||
387  temtem.find("@USR_GRP_ALWD")!=string::npos ||
388  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
389  temtem.find("@SRV_RET")!=string::npos ||
390  temtem.find("@USERS_ALWD")!=string::npos ||
391  temtem.find("@NAME")!=string::npos ||
392  temtem.find("@ARGS")!=string::npos ||
393  temtem.find("@SIGNATURE")!=string::npos)
394  {
395  throw "Invalid start of property within another property";
396  }
397  else
398  {
399  srvname = temtem;
400  }
401  }
402  }
403  string srvargs,srvmcarg,srvmcargremote,srvmcargremotevec="vector<Object> _vec;\n";
404  if(srvc.find("@ARGS(")!=string::npos)
405  {
406  int st = srvc.find("@ARGS(");
407  string temp = srvc.substr(st+6);
408  size_t sp = temp.find_first_of(" ");
409  size_t cl = temp.find_first_of(")");
410  if((sp!=string::npos && sp<cl) || cl==string::npos)
411  {
412  throw "No spaces allowed between in value";
413  }
414  else
415  {
416  string temtem = temp.substr(0,cl);
417  if(temtem.find("@BLOCK_USERS")!=string::npos ||
418  temtem.find("@USR_GRP_ALWD")!=string::npos ||
419  temtem.find("@BLOCK_USR_GRPS")!=string::npos ||
420  temtem.find("@SRV_RET")!=string::npos ||
421  temtem.find("@USERS_ALWD")!=string::npos ||
422  temtem.find("@NAME")!=string::npos ||
423  temtem.find("@ARGS")!=string::npos ||
424  temtem.find("@SIGNATURE")!=string::npos)
425  {
426  throw "Invalid start of property within another property";
427  }
428  else
429  {
430  if(temtem=="void")
431  {}
432  else
433  {
434  Cont1 alwd;
435  StringUtil::split(alwd, temtem, (","));
436  for (unsigned int var = 0; var < alwd.size(); ++var)
437  {
438  if(alwd.at(var)!="" && alwd.at(var).find("*")==string::npos && alwd.at(var).find("&")==string::npos)
439  {
440  //srvargs += alwd.at(var) + "*_"+CastUtil::lexical_cast<string>(var+1)+" = ("+alwd.at(var)+"*)args.at("+CastUtil::lexical_cast<string>(var)+");\n";
441  srvmcarg += "_"+CastUtil::lexical_cast<string>(var+1)+"";
442  srvargs += alwd.at(var)+ " _"+CastUtil::lexical_cast<string>(var+1);
443  srvmcargremote += alwd.at(var)+ " &_"+CastUtil::lexical_cast<string>(var+1);
444  srvmcargremotevec += "Object __"+CastUtil::lexical_cast<string>(var+1)+";\n";
445  srvmcargremotevec += "__"+CastUtil::lexical_cast<string>(var+1)+" << _"+CastUtil::lexical_cast<string>(var+1)+";\n";
446  srvmcargremotevec += "_vec.push_back(__"+CastUtil::lexical_cast<string>(var+1)+");\n";
447  if(var!=alwd.size()-1)
448  {
449  srvmcarg += ",";
450  srvmcargremote += ",";
451  srvargs += ",";
452  }
453  }
454  else
455  {
456  throw "Invalid service arguments";
457  }
458  }
459  }
460  }
461  }
462  }
463  cuinithdrs += "#include \""+srvcls+".h\"\n";
464  initheaders += "#include \""+srvcls+".h\"\n";
465  servsh += srvret+" "+srvname+"("+srvargs+");\n";
466  servs += srvret+" "+file+"::"+srvname+"("+srvargs+")\n{\n";
467  servs += srvcls +" _serviceImpl;\n";
468  if(srvret!="" && srvret!="void")
469  servs += srvret + " _retvalue;\n";
470  servs += "try{\n";
471  if(convertfrom(srprps["@AUTO_TRANS"])=="true")
472  servs += "this->startTransaction();\n";
473  if(srvret!="" && srvret!="void")
474  servs += "_retvalue = _serviceImpl."+srvmeth+"("+srvmcarg+");\n}\n";
475  else
476  servs += "_serviceImpl."+srvmeth+"("+srvmcarg+");\n}\n";
477  servs += "catch(...){\n";
478  if(convertfrom(srprps["@AUTO_TRANS"])=="true")
479  servs += "this->rollback();\n";
480  servs += "}\n";
481  if(convertfrom(srprps["@AUTO_TRANS"])=="true")
482  servs += "this->endTransaction();\n";
483  if(srvret!="" && srvret!="void")
484  servs += "return _retvalue;\n}\n\n";
485  else
486  servs += "}\n\n";
487  curemoteheaders += srvret+" "+srvname+"("+srvmcargremote+");\n";
488  rsrvs += srvret+" "+file+"_Remote::"+srvname+"("+srvmcargremote+")\n{\n";
489  rsrvs += srvmcargremotevec;
490  if(srvret!="" && srvret!="void")
491  rsrvs += "return ";
492  rsrvs += "*("+srvret+"*)this->invoke(\""+srvname+"\",_vec,\""+srvret+"\");\n";
493  rsrvs += "}\n";
494  }
495  curemoteheaders += "};\n";
496  cudata += "}\n"+servs;
497  curemote += "}\n"+rsrvs;
498  cuheader += servsh + "};\n";
499  cuheader = initheaders + cuheader+"#endif";
500  curemoteheaders = cuinithdrs + curemoteheaders+"#endif";
501  /*logger << cuheader << flush;
502  logger << "\n\n\n\n\n\n------------------------------------------------------------------\n\n\n\n\n\n" << flush;
503  logger << cudata << flush;
504  logger << "\n\n\n\n\n\n------------------------------------------------------------------\n\n\n\n\n\n" << flush;
505  logger << curemoteheaders << flush;
506  logger << "\n\n\n\n\n\n------------------------------------------------------------------\n\n\n\n\n\n" << flush;
507  logger << curemote << flush;*/
508 
509  }
510  catch(const char* ex)
511  {
512  throw ex;
513  logger << "Exception occurred" << flush;
514  }
515 
516  }
517  else
518  throw "Error Parsing Component";
519  return file;
520 }