8 | var fs=require("fs"),simpleportal=require("./../simpleportal"),util=require("./../util"),editorUtil=require("./../editor").Util,Service=require("./../service/service"),Routerclass=require("./router");var ServiceLoaderConstructor=function(b,d){b=b||{};var a=this;if(d){var c={templatedir:d.getServerCorePath("resources/templates/service"),systemapidir:d.getServerCorePath("api")};b=util.extendJSON(c,b)}Routerclass.call(this,b,d);a.routerid=b.routerid||"serviceloader";a.serviceUrl=b.serviceUrl||"/api";a.viewUrl=b.viewUrl||"/view";a.servicedir=b.servicedir||"api";a.templatedir=b.templatedir||__dirname+"/../../server/resources/templates/service";a.systemapidir=b.systemapidir||__dirname+"/../../server/api";a.systemServiceUrl=b.systemServiceUrl||"system";a.systemServices=[];a.on("load.router",function(e){if(e&&e.plugin&&e.servicedir){a.loadServices(e,function(f){a.emit("router.loaded."+e.plugin,f)})}});return this};require("util").inherits(ServiceLoaderConstructor,Routerclass);ServiceLoaderConstructor.prototype.loadRouter=function(c,b){var a=this;loadSystemServices(a,function(){a.loadServices(a.configuration,function(){a.emit("router.loaded");if(b){b()}})})};ServiceLoaderConstructor.prototype.loadServices=function(e,d){var b=this;var a=e&&e.servicedir?e.servicedir:b.servicedir;b.getLogger().debug(b.routerid+":loadServices","loading from << "+a);if(!fs.existsSync(a)){if(d){d()}}else{var c=fs.lstatSync(a);if(c.isDirectory()){if(b.configuration&&b.configuration.services&&e&&e.services){if(e.services&&e.services.extend){delete e.services.extend}util.extendJSON(b.configuration.services,e.services)}loadServices(b,a,false,d,e.plugin,e.serviceprefix)}else{if(d){d()}}}};ServiceLoaderConstructor.prototype.unLoadServices=function(e,d){var b=this;var a=e&&e.servicedir?e.servicedir:b.servicedir;b.getLogger().debug(b.routerid+":unLoadServices"," unoading from << -- "+a);if(!fs.existsSync(a)){if(d){d()}}else{var c=fs.lstatSync(a);if(c.isDirectory()){unLoadServices(b,a,false,d)}else{if(d){d()}}}};ServiceLoaderConstructor.prototype.getServiceDetails=function(c){var a=this;var d;if(!a.routers[c]&&a.routers["system_"+c]){c="system_"+c}var b=a.routers[c];if(b){d=b.exportConfig()}return d};ServiceLoaderConstructor.prototype.initRouter=function(b,e){var a=this;a.getLogger().info(a.routerid+":initRouter","initializing serviceloader routers.");b.dispatch.addServiceHandlers(a,a.serviceUrl,function(f,j,i,g,h){a.call(f,j,i,g,h)});var d=b.dispatch.addServiceGlobalHandlers(a,a.serviceUrl+"/"+a.systemServiceUrl,function(f,j,i,g,h){a.call(f,j,i,g,h)},a.viewUrl+"/"+a.systemServiceUrl);registerPluginUrls(a,d,a.serviceUrl+"/"+a.systemServiceUrl,a.viewUrl+"/"+a.systemServiceUrl);var c=b.dispatch.addServiceGlobalHandlers(a,a.serviceUrl,function(f,j,i,g,h){a.call(f,j,i,g,h)},a.viewUrl);registerPluginUrls(a,c,a.serviceUrl,a.viewUrl);updateTemporaryTemplateHandler(a,a.viewUrl+"/"+a.systemServiceUrl);updateTemporaryTemplateHandler(a,a.viewUrl);if(e){e()}};ServiceLoaderConstructor.prototype.getPreference=function(f,b,d){var a=this;var c={};if(f&&f.name){var e=a.getService(f.name,f.plugin);if(e){return e.getPreference(b,d)}}return c};ServiceLoaderConstructor.prototype.removeSavedPreference=function(e,b,d){var a=this;if(e&&e.name){var c=a.getService(e.name,e.plugin);if(c){return c.removeSavedPreference(b,d)}}else{if(d){d()}}};function removePluginParam(d,a){var b=0;var e=Object.keys(d.params);for(var c=2;c<=e.length;c++){d.params["group"+(c-1)]=d.path["group"+(c-1)]=d.params["group"+(c)]}delete d.params["group"+(e.length)];delete d.path["group"+(e.length)];d.pathGroup=d.params["group1"]}function setDynamicparams(d){if(d&&d.query&&d.query.template){d.servicetemplate=d.query.template}if(arguments.length>3){d.params=d.params||{};d.path=d.path||{};var a=1,e;var c=false;var f="";if(arguments.length>4){for(var b=4;b<arguments.length;b++){if(a==1){a++;d.params["group"+a]=d.path["group"+a]=arguments[b]}else{if(/jquerymobile|bootstrap/.test(arguments[b])||(b==5&&arguments.length==8)){a++;if(!c){c=true;d.params["group"+a]=d.path["group"+a]=f}a++;d.params["group"+a]=d.path["group"+a]=arguments[b]}else{if(!c){f=(f?f+"/":"")+arguments[b]}else{e=(e?e+"/":"")+arguments[b]}}}}}d.params["group1"]=d.path["group1"]=d.pathGroup=arguments[3];if(arguments.length>3){a++;d.params["group"+a]=e}}}function updateTemporaryTemplateHandler(a,e,d){var c={};c["GET "+e+"/:service/:subfield/:subfield1/:subfield2/:subfield3/templates/:template/:url"]=function(h,f,i){h.viewservice=true;setDynamicparams.apply(this,arguments);a.getLogger().info(a.routerid,"Dynamic templates with sub template type - ");var g=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/:subfield/:subsubfield/templates/:templatedir/:jsmodel",h,f,i)};c["GET "+e+"/:service/:subfield/:subfield1/:subfield2/templates/:template/:url"]=function(h,f,i){h.viewservice=true;setDynamicparams.apply(this,arguments);a.getLogger().info(a.routerid,"Dynamic templates with sub template type - ");var g=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/:subfield/:subsubfield/templates/:templatedir/:jsmodel",h,f,i)};c["GET "+e+"/:service/:subfield/:subfield1/templates/:template/:url"]=function(h,f,i){h.viewservice=true;setDynamicparams.apply(this,arguments);a.getLogger().info(a.routerid,"Dynamic templates with sub template type - ");var g=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/:subfield/:subsubfield/templates/:templatedir/:jsmodel",h,f,i)};c["GET "+e+"/:service/:subfield/templates/:template/:url"]=function(h,f,i){h.viewservice=true;setDynamicparams.apply(this,arguments);a.getLogger().info(a.routerid,"Dynamic templates with sub template type - ");var g=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/:subfield/templates/:templatedir/:jsmodel",h,f,i)};c["GET "+e+"/:service/templates/:template/:url"]=function(k,g,l,j,h,f){a.getLogger().info(a.routerid,"Dynamic templates with template type - "+j+" -- "+h+" -- "+f);k.viewservice=true;setDynamicparams(k,g,l,j,h,f);var i=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/templates/:templatedir/:jsmodel",k,g,l)};c["GET /:plugin"+e+"/:service/templates/:template/:url"]=function(h,g,n,i,j,k,f){a.getLogger().info(a.routerid,"Dynamic templates with template type - "+i+" / "+j+" / "+k+" / "+f);h.viewservice=true;var m=a.getPluginloader().getPluginDetails(i,"webapp");if(m){h.plugin=i;m.resourcepath=m.installeddir+"/resources/templates";h.webappsetting=m}setDynamicparams.apply(this,arguments);removePluginParam(h);var l=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/templates/:templatedir/:jsmodel",h,g,n)};c["GET /:plugin"+e+"/:service/:subfield/templates/:template/:url"]=function(h,g,o,i,j,n,k,f){a.getLogger().info(a.routerid,"Dynamic templates with template type - "+i+" / "+j+" / "+k+" / "+f);h.viewservice=true;var m=a.getPluginloader().getPluginDetails(i,"webapp");if(m){h.plugin=i;m.resourcepath=m.installeddir+"/resources/templates";h.webappsetting=m}setDynamicparams.apply(this,arguments);removePluginParam(h);var l=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/:subfield/templates/:templatedir/:jsmodel",h,g,o)};c["GET /:plugin"+e+"/:service/:subfield/*/templates/:template/:url"]=function(h,g,o,i,j,n,p,k,f){a.getLogger().info(a.routerid,"Dynamic templates with template type - "+i+" / "+j+" / "+k+" / "+f);h.viewservice=true;var m=a.getPluginloader().getPluginDetails(i,"webapp");if(m){h.plugin=i;m.resourcepath=m.installeddir+"/resources/templates";h.webappsetting=m}setDynamicparams.apply(this,arguments);removePluginParam(h);var l=a.getService("apiservice");a.call("apiservice","GET /dynamictemplate/:id/:subfield/:subsubfield/templates/:templatedir/:jsmodel",h,g,o)};var b=a.getServerInstance().getRouter("router").dispatch.addUrlHandler(c)}function updatePluginViewHanler(a,c,b){return(function(d,f,e){return function(m){m.viewservice=true;d.getLogger().debug(d.routerid,"Plugin uri :"+f);if(arguments.length>0){var l=arguments[3];var h=d.getPluginloader().getPluginDetails(l,"webapp");if(h){m.plugin=l;h.resourcepath=h.installeddir+"/resources/templates";m.webappsetting=h;var g=[];var j=0;for(var k in arguments){if(k!=3){g[j++]=arguments[k]}}e.apply(this,g)}else{e.apply(this,arguments)}}else{e.apply(this,arguments)}}})(a,c,b)}function registerPluginUrls(a,h,g,e){if(e&&h&&Object.keys(h).length>0){var c={};for(var b in h){if(b.indexOf("GET ")!=-1||b.indexOf("/")==0){var d=b.replace(g,"/:pluginid"+e);var f=b.replace(g,"/:pluginid/mobile"+e);c[d]=updatePluginViewHanler(a,d,h[b]);c[f]=updatePluginViewHanler(a,f,h[b]);if(b=="/api/:service"){c[d+"/"]=updatePluginViewHanler(a,d+"/",h[b]);c[f+"/"]=updatePluginViewHanler(a,f+"/",h[b])}}}a.getServerInstance().getRouter("router").dispatch.addUrlHandler(c)}}ServiceLoaderConstructor.prototype.updateServiceRouter=function(b,d,c){var a=this;if(d&&a.configuration&&a.configuration.services&&d&&d.services){util.extendJSON(a.configuration.services,d.services)}a.getServerInstance().getRouter("router").dispatch.addServiceRouter(b,a.serviceUrl,function(){a.call.apply(a,arguments)})};ServiceLoaderConstructor.prototype.invoke=function(e,a,c,i,d){console.log("INFO: Calling service - "+e+" - "+a);var f={},h=this;if(typeof c=="function"){i=c;c={query:{}}}else{if(typeof i=="object"){d=i;i=null}}if(a.indexOf("?")!=-1){a=a.substring(0,a.indexOf("?"));f=util.getParamValues({url:a});if(!c){c={query:f}}c=util.extendJSON(c,{query:f})}var g=h.getService(e);if(!g){i("service-not-found-"+e,{})}else{var b=g.service[a];if(!b&&g[a]){b=g[a]}if(!b){if(i){i("service-not-found-"+a,{})}}else{if(typeof b=="string"){b=g.service[b]}if(b){b(c,{},i,d)}else{if(i){i("service-not-found-"+a,{})}}}}};ServiceLoaderConstructor.prototype.call=function(b,g,e,c,d){var a=this;a.getLogger().debug(a.routerid,"Calling service - "+b+" - "+g+">>> "+e.url);var f=a.getService(b);if(!f){if(d){d()}else{util.sendServiceResponse(c,"Service Not found",{});return}}else{a.getLogger().debug(a.routerid,"Calling service - "+b+" - "+g);f.call(g,e,c,function(h,i,j){if(d&&h&&typeof h=="string"&&h.indexOf("no-such-method")==0){d()}else{util.sendServiceResponse(c,h,i,j)}})}};ServiceLoaderConstructor.prototype.getUpdatedConfiguration=function(e){var a=this;var d={};for(var b in a){var c=getServiceDetails(a,b);if(c&&(!e||(c.plugin==e))){d[c.name]=c.configuration}}return util.extendJSON({},d)};ServiceLoaderConstructor.prototype.registerService=function(c,b,d){b=b||{};var a=this;fs.stat(c,function(g,h){if(!g&&h.isFile()){var e=util.getResourceFromUrl(c).substr(0,c.lastIndexOf(".js"));var f=__loadService(a,{path:c,name:b.name,plugin:b.plugin,serviceprefix:b.serviceprefix,servicetype:b.servicetype});if(d){d(!f?"Service not loaded!.":null)}}else{if(d){d()}}})};ServiceLoaderConstructor.prototype.getService=function(e,f){var b=this;if(!b.routers[e]&&b.routers["system_"+e]){e="system_"+e}var a=b.routers[e];if((!a||!a.get)){var d;if(typeof f=="string"){d=f}else{if(f instanceof Service){d=f.plugin}else{if(f&&f.plugin){d=f.plugin}else{if(f&&f.pluginid){d=f.pluginid}}}}if(d){var c=b.getPluginloader().getPluginDetails(d,"webapp");if(c&&c.serviceprefix){b.getLogger().debug(b.routerid,"getting api service --- "+c.serviceprefix+e);a=b.routers[c.serviceprefix+e]}}}return a};var registerEvents=function(a,d){if(d&&a.routers[d].get&&a.routers[d].on){a.routers[d].on("init",function(f){a.updateServiceRouter(a.routers[d]);a.emit("service-init",this)});a.routers[d].once("startup",function(){a.emit("servicestartup",this)});a.routers[d].on("backup",function(f){a.emit("service.backup",f)});var e=[];for(var c in simpleportal.Constants.ServiceEvents){e.push(simpleportal.Constants.ServiceEvents[c])}for(var c in simpleportal.Constants.CommonAPIEvents){e.push(simpleportal.Constants.CommonAPIEvents[c])}for(var b in e){registerServiceEvent(a,d,e[b])}}};var registerServiceEvent=function(a,c,b){if(a.routers[c]){a.routers[c].on(b,function(d){a.emit("service."+c+"."+b,d)})}};function loadServices(j,b,a,l,f,m){var g=getServices(j,b,a,f,m);var e=g.length;var k=0;if(g&&g.length>0){for(var d in g){var h=g[d];var c=__loadService(j,h);if(e-1==k++){if(l){l(g)}}}}else{if(l){l()}}}function getSystemServices(a){if(a.systemServices.length==0){a.systemServices=getServices(a,a.systemapidir,false)}return a.systemServices}function getServices(a,d,c,f,b){c=c||false;var e=[];var g="system";if(d==a.servicedir){g="server"}if(fs.existsSync(d)){fs.readdirSync(d).forEach(function(j){if(/\.js$/.test(j)){if((c&&j=="index.js")||j!="index.js"){var m=fs.lstatSync(d+"/"+j);var k=j.substr(0,j.lastIndexOf("."));var h=k;if(k=="index"){h=d.substring(d.lastIndexOf("/")+1,d.length)}var n=fs.realpathSync(d+"/"+k+".js");e.push({path:n,name:h,plugin:f,servicetype:g,serviceprefix:b,stats:m})}}else{if(!/^\./.test(j)){var m=fs.lstatSync(d+"/"+j);if(m.isDirectory()&&!(j=="html5"||j=="lib"||j=="util"||j=="models"||j=="views")){var o=getServices(a,d+"/"+j,true,f);if(o.length>0){for(var l in o){e.push(o[l])}}}}}})}return e}function __loadService(a,c){var f=c.name;if(c.system){f="system_"+c.name}if(c.serviceprefix){f=c.serviceprefix+f}else{if(a.routers[f]){if(c.plugin){f=c.plugin+"_"+c.name}}}a.getLogger().debug(a.routerid+":__loadService",c.name+" << "+f+" << "+c.path);if(!a.routers[f]){try{a.routers.__defineGetter__(f,function(){return require(c.path)});a.routers[f].plugin=c.plugin;if(c.plugin&&c.plugin!=""){a.routers[f].servicetype="plugin"}else{if(c.servicetype){a.routers[f].servicetype=c.servicetype}}if(c.system){a.routers[f].serviceurl=a.systemServiceUrl+"/"+c.name}else{if(c.serviceprefix&&f!=c.name){a.routers[f].serviceurl=f}}a.routers[f].servicepath=c.path;if(c.stats){a.routers[f].stats=c.stats}if(c.path){a.routers[f].servicefile=c.path}a.routers[f].servicename=f;registerEvents(a,f);return true}catch(d){delete a.routers[f];console.error(d);c.error=d;a._routererrors[f]={plugin:c.plugin,error:d};if(d.code=="MODULE_NOT_FOUND"&&c.plugin){var b=a.getPluginloader().getPluginErrors()[c.plugin]||{};if(!b.missingnpmdependancies){b.missingnpmdependancies=[]}var e=util.getTextBetween(d.message,"'","'");b.missingnpmdependancies.push(e);if(!a.getPluginloader().getPluginErrors()[c.plugin]){a.getPluginloader().getPluginErrors()[c.plugin]=b}}a.getLogger().error(a.routerid+":"+f,d);return false}}else{a.getLogger().error(a.routerid+":__loadService",c.name+" << "+f+" already registered.");return false}}function loadSystemServices(a,h){if(a.systemServices!=0){h()}else{var g=getSystemServices(a);var f=g.length;var c=0;if(g&&g.length>0){for(var e in g){var b=g[e];b.system=true;var d=__loadService(a,b);if(f-1==c++){if(h){h(g)}}}}else{if(h){h()}}}}function unLoadServices(g,b,a,h){var e=getServices(g,b,a);var d=e.length;var j=0;if(e&&e.length>0){for(var c in e){var f=e[c];delete g.routers[f.name]}}if(h){h()}}function getServiceDetails(a,c){var d;if(typeof a.routers[c]=="string"||typeof a.routers[c]=="function"){}else{if(a.routers[c]){if(!a.routers[c]&&a.routers["system_"+c]){c="system_"+c}var b=a.routers[c];if(b){d=b.exportConfig()}}}return d}var ExportFunction=(function(){return ServiceLoaderConstructor}());module.exports=ExportFunction; |