esdlcmd_common.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2013 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef ESDLCMD_COMMON_HPP
  14. #define ESDLCMD_COMMON_HPP
  15. #include "jprop.hpp"
  16. #include "jargv.hpp"
  17. #include "common.hpp"
  18. #include "esdl_def.hpp"
  19. #include "esdl_def_helper.hpp"
  20. #include "ws_esdlconfig_esp.ipp"
  21. #include "esdl2xml.hpp"
  22. #define COMPONENTS_DIR_NAME "componentfiles"
  23. #define HIGHER_DIR_RELATIVE ".."
  24. //=========================================================================================
  25. interface IEsdlCommand : extends IInterface
  26. {
  27. virtual bool parseCommandLineOptions(ArgvIterator &iter)=0;
  28. virtual bool finalizeOptions(IProperties *globals)=0;
  29. virtual int processCMD()=0;
  30. virtual void usage()=0;
  31. };
  32. typedef IEsdlCommand *(*EsdlCommandFactory)(const char *cmdname);
  33. #define ESDLOPT_HELP "--help"
  34. #define ESDLOPT_HELP_s "--?"
  35. #define ESDLARG_HELP "help"
  36. #define ESDL_OPTION_VERBOSE "--verbose"
  37. #define ESDL_OPT_VERBOSE "-v"
  38. #define ESDL_CONVERT_SOURCE "--source"
  39. #define ESDL_CONVERT_OUTDIR "--outdir"
  40. #define ESDL_CONVERT_ALL "--all"
  41. #define ESDL_CONVERT_EXPANDEDXML "--expandedxml"
  42. #define ESDL_CONVERT_EXPANDEDXML_x "-x"
  43. #define HPCC_COMPONENT_FILES_DIR "--compfilesdir"
  44. #define HPCC_COMPONENT_FILES_DIR_CDE "--CDE"
  45. #define ESDLOPT_XSLT_PATH "--xslt"
  46. #define ESDLOPT_VERSION "--version"
  47. #define ESDLOPT_SERVICE "--service"
  48. #define ESDLOPT_METHOD "--method"
  49. #define ESDLOPT_PREPROCESS_OUT "--preprocess-output"
  50. #define ESDLOPT_ANNOTATE "--annotate"
  51. #define ESDLOPT_NO_OPTIONAL_ATTRIBUTES "--noopt"
  52. #define ESDLOPT_OPT_PARAM_VAL "-opt"
  53. #define ESDLOPT_OPTIONAL_PARAM_VAL "--optional"
  54. #define ESDLOPT_TARGET_NS "-tns"
  55. #define ESDLOPT_TARGET_NAMESPACE "--target-namespace"
  56. #define ESDLOPT_NUMBER "-n"
  57. #define ESDLOPT_NO_COLLAPSE "--show-inheritance"
  58. #define ESDLOPT_NO_ARRAYOF "--no-arrayof"
  59. #define ESDLOPT_WSDL_ADDRESS "--wsdl-address"
  60. #define ESDLBINDING_URN_BASE "urn:hpccsystems:ws"
  61. #define ESDLOPTLIST_DELIMITER ";"
  62. #define ESDL_OPT_SERVICE_SERVER "-s"
  63. #define ESDL_OPTION_SERVICE_SERVER "--server"
  64. #define ESDL_OPTION_SERVICE_PORT "--port"
  65. #define ESDL_OPT_SERVICE_USER "-u"
  66. #define ESDL_OPTION_SERVICE_USER "--username"
  67. #define ESDL_OPTION_SERVICE_PORT "--port"
  68. #define ESDL_OPT_SERVICE_PORT "-p"
  69. #define ESDL_OPT_SERVICE_PASS "-pw"
  70. #define ESDL_OPTION_SERVICE_PASS "--password"
  71. #define ESDL_OPTION_ESP_PROC_NAME "--esp-proc-name"
  72. #define ESDL_OPT_ESP_PROC_NAME "-epn"
  73. #define ESDL_OPTION_TARGET_ESP_ADDRESS "--esp-ip"
  74. #define ESDL_OPTION_TARGET_ESP_PORT "--esp-port"
  75. #define ESDL_OPTION_CONFIG "--config"
  76. #define ESDL_OPTION_OVERWRITE "--overwrite"
  77. bool matchVariableOption(ArgvIterator &iter, const char prefix, IArrayOf<IEspNamedValue> &values);
  78. enum esdlCmdOptionMatchIndicator
  79. {
  80. EsdlCmdOptionNoMatch=0,
  81. EsdlCmdOptionMatch=1,
  82. EsdlCmdOptionCompletion=2
  83. };
  84. class EsdlCmdCommon : public CInterface, implements IEsdlCommand
  85. {
  86. public:
  87. IMPLEMENT_IINTERFACE;
  88. EsdlCmdCommon() : optVerbose(false)
  89. {}
  90. virtual esdlCmdOptionMatchIndicator matchCommandLineOption(ArgvIterator &iter, bool finalAttempt=false);
  91. virtual bool finalizeOptions(IProperties *globals);
  92. virtual void usage()
  93. {
  94. fprintf(stdout,
  95. " --help display usage information for the given command\n"
  96. " -v,--verbose output additional tracing information\n"
  97. );
  98. }
  99. public:
  100. bool optVerbose;
  101. };
  102. class EsdlCmdHelper : public CInterface
  103. {
  104. protected:
  105. Owned<IEsdlDefinition> esdlDef;
  106. Owned<IEsdlDefinitionHelper> helper;
  107. Owned<IFile> serviceDefFile;
  108. EsdlCmdHelper()
  109. {
  110. esdlDef.set(createEsdlDefinition());
  111. helper.set(createEsdlDefinitionHelper());
  112. }
  113. public:
  114. IMPLEMENT_IINTERFACE;
  115. static EsdlCmdHelper * createEsdlHelper()
  116. {
  117. return new EsdlCmdHelper();
  118. }
  119. void getServiceESXDL(const char * sourceFileName, const char * serviceName, StringBuffer & xmlOut, double version, IProperties *opts=NULL, unsigned flags=0)
  120. {
  121. if (!esdlDef.get())
  122. esdlDef.set(createEsdlDefinition());
  123. if(!esdlDef->hasFileLoaded(sourceFileName))
  124. {
  125. StringBuffer extension;
  126. StringBuffer filename;
  127. splitFilename(sourceFileName, NULL, NULL, &filename, &extension);
  128. if (stricmp(extension.str(),LEGACY_FILE_EXTENSION)==0 || stricmp(extension.str(),ESDL_FILE_EXTENSION)==0)
  129. {
  130. StringBuffer esxml;
  131. EsdlCmdHelper::convertECMtoESXDL(sourceFileName, filename.str(), esxml, true, true, false, true);
  132. esdlDef->addDefinitionFromXML(esxml, serviceName, (int)version);
  133. }
  134. else
  135. {
  136. loadEsdlDef(sourceFileName);
  137. }
  138. }
  139. if (esdlDef)
  140. {
  141. IEsdlDefObjectIterator *deps = esdlDef->getDependencies(serviceName, "", version, opts, flags);
  142. if( deps )
  143. {
  144. xmlOut.appendf( "<esxdl name=\"%s\">", serviceName);
  145. helper->toXML( *deps, xmlOut, version, opts, flags );
  146. xmlOut.append("</esxdl>");
  147. }
  148. else
  149. throw( MakeStringException(0, "Could not get ESDL structure") );
  150. }
  151. }
  152. static void convertECMtoESXDL(const char * filepath, const char * esxdlname, StringBuffer & esxml, bool recursive, bool verbose, bool outputincludes, bool isIncludedESDL)
  153. {
  154. if (verbose)
  155. fprintf(stdout,"Converting ESDL file %s to XML\n", filepath);
  156. Owned<Esdl2Esxdl> cmd = new Esdl2Esxdl(recursive, verbose);
  157. esxml.setf( "<esxdl name=\"%s\">", esxdlname);
  158. cmd->transform(filepath, "", &esxml, outputincludes, isIncludedESDL); //output to buffer
  159. esxml.append("</esxdl>");
  160. }
  161. static void outputMultiExceptions(const IMultiException &me)
  162. {
  163. fprintf(stderr, "\nException(s):\n");
  164. aindex_t count = me.ordinality();
  165. for (aindex_t i=0; i<count; i++)
  166. {
  167. IException& e = me.item(i);
  168. StringBuffer msg;
  169. fprintf(stderr, "%d: %s\n", e.errorCode(), e.errorMessage(msg).str());
  170. }
  171. fprintf(stderr, "\n");
  172. }
  173. static IClientWsESDLConfig * getWsESDLConfigSoapService(const char *server, const char *port, const char *username, const char *password)
  174. {
  175. if(server == NULL)
  176. throw MakeStringException(-1, "Server url not specified");
  177. VStringBuffer url("http://%s:%s/WsESDLConfig", server, port);
  178. IClientWsESDLConfig * esdlConfigClient = createWsESDLConfigClient();
  179. esdlConfigClient->addServiceUrl(url.str());
  180. esdlConfigClient->setUsernameToken(username, password, NULL);
  181. return esdlConfigClient;
  182. }
  183. protected:
  184. void loadEsdlDef(const char * sourceFileName)
  185. {
  186. serviceDefFile.setown( createIFile(sourceFileName) );
  187. if( serviceDefFile->exists() )
  188. {
  189. if( serviceDefFile->isFile() )
  190. {
  191. if( serviceDefFile->size() > 0 )
  192. {
  193. // Realized a subtle source of potential problems. Because there
  194. // can be multiple EsdlStruct definitions with the same name
  195. // in multiple files, you need to be careful that only those files
  196. // explicitly included by your service are loaded to the
  197. // EsdlDefinition object that you'll getDependencies() on. If not,
  198. // you could inadvertently getDependencies() from a different structure
  199. // with the same name. This means we can only reliably process one
  200. // Web Service at a time, and must load files by explicitly loading
  201. // only the top-level ws_<service> definition file, and allowing the
  202. // load code to handle loading only the minimal set of required includes
  203. esdlDef->addDefinitionsFromFile( serviceDefFile->queryFilename() );
  204. }
  205. else
  206. {
  207. throw( MakeStringException(0, "ESDL definition file source %s is empty", sourceFileName) );
  208. }
  209. }
  210. else
  211. {
  212. throw( MakeStringException(0, "ESDL definition file source %s is not a file", sourceFileName) );
  213. }
  214. }
  215. else
  216. {
  217. throw( MakeStringException(0, "ESDL definition file source %s does not exist", sourceFileName) );
  218. }
  219. }
  220. };
  221. class EsdlConvertCmd : public EsdlCmdCommon
  222. {
  223. public:
  224. EsdlConvertCmd() {}
  225. virtual esdlCmdOptionMatchIndicator matchCommandLineOption(ArgvIterator &iter, bool finalAttempt=false);
  226. virtual bool finalizeOptions(IProperties *globals);
  227. virtual bool parseCommandLineOptions(ArgvIterator&);
  228. virtual bool parseCommandLineOption(ArgvIterator&);
  229. virtual void usage()
  230. {
  231. EsdlCmdCommon::usage();
  232. puts(" --outdir=<out dir path> Location to generate output\n");
  233. }
  234. public:
  235. StringAttr optSource;
  236. StringAttr optOutDirPath;
  237. };
  238. class EsdlHelperConvertCmd : public EsdlConvertCmd
  239. {
  240. protected:
  241. Owned<IEsdlDefinition> esdlDef;
  242. Owned<IEsdlDefinitionHelper> helper;
  243. public:
  244. EsdlHelperConvertCmd()
  245. {
  246. esdlDef.set(createEsdlDefinition());
  247. helper.set(createEsdlDefinitionHelper());
  248. }
  249. virtual void usage()
  250. {
  251. EsdlConvertCmd::usage();
  252. }
  253. virtual void doTransform(IEsdlDefObjectIterator& objs, StringBuffer &target, double version=0, IProperties *opts=NULL, const char *ns=NULL, unsigned flags=0 )=0;
  254. virtual void loadTransform( StringBuffer &xsltpath, IProperties *params )=0;
  255. virtual void setTransformParams(IProperties *params )=0;
  256. };
  257. static bool getCurrentFolder(StringBuffer & path)
  258. {
  259. StringBuffer folder;
  260. splitDirTail(queryCurrentProcessPath(), folder);
  261. removeTrailingPathSepChar(folder);
  262. if (folder.length())
  263. {
  264. path = folder;
  265. return true;
  266. }
  267. return false;
  268. }
  269. static bool getComponentFilesRelPathFromBin(StringBuffer & path)
  270. {
  271. if (getCurrentFolder(path))
  272. {
  273. path.appendf("%c%s%c%s", PATHSEPCHAR, HIGHER_DIR_RELATIVE,PATHSEPCHAR,COMPONENTS_DIR_NAME);
  274. return true;
  275. }
  276. return false;
  277. }
  278. #endif