srvoa
Version:
Infrastructure for service oriented architecture.
228 lines (184 loc) • 8.89 kB
JavaScript
/**
* srvoa - soa infrastructure for node js
*
* @copyright Copyright (c) 2015, Alrik Zachert
* @license https://gitlab.com/kermit-js/kermit/blob/support/srvoa/LICENSE BSD-2-Clause
*/
;
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Service = require('./Service'),
ConfigService = require('./ConfigService'),
ServiceManager = require('./ServiceManager');
/**
* The srvoa application class.
*/
var Application = (function (_Service) {
_inherits(Application, _Service);
_createClass(Application, null, [{
key: 'APP_CONFIG_SERVICE_KEY',
/**
* The config hash passed to configure.
*
* @property applicationConfig {Object}
*/
/**
* The reference to the application config service.
*
* @property configService {ConfigService}
*/
/**
* The config service key within the service manager.
*
* @returns {string}
*/
get: function get() {
return 'app.config';
}
/**
* @inheritDoc
*/
}]);
function Application(serviceManager) {
_classCallCheck(this, Application);
_get(Object.getPrototypeOf(Application.prototype), 'constructor', this).call(this, serviceManager);
this.applicationConfig = null;
this.configService = null;
}
/**
* Fetch and return the serviceManager.
*
* @return {ServiceManager}
*/
_createClass(Application, [{
key: 'getServiceManager',
value: function getServiceManager() {
if (this.serviceManager === null) {
this.serviceManager = new ServiceManager();
}
return this.serviceManager;
}
/**
* @return {ConfigService}
*/
}, {
key: 'getConfigService',
value: function getConfigService() {
return this.configService;
}
/**
* @param {ConfigService} configService
* @return {Application}
*/
}, {
key: 'setConfigService',
value: function setConfigService(configService) {
this.configService = configService;
return this;
}
/**
* @inheritDoc
*/
}, {
key: 'configure',
value: function configure(config) {
this.applicationConfig = config || {};
return this;
}
/**
* Initialize the app config service. Try to fetch an instance from the service manager, otherwise
* create a new one. Configure and launch the config service,
*
* @inheritDoc
*/
}, {
key: 'bootstrap',
value: function bootstrap() {
var serviceManager = this.getServiceManager(),
configService,
applicationConfig = this.applicationConfig;
if (serviceManager.has(Application.APP_CONFIG_SERVICE_KEY) === false) {
configService = new ConfigService();
serviceManager.set(Application.APP_CONFIG_SERVICE_KEY, configService);
} else {
configService = serviceManager.get(Application.APP_CONFIG_SERVICE_KEY);
}
if (typeof applicationConfig.files !== 'undefined') {
configService.configure({
files: applicationConfig.files
}).bootstrap().launch();
} else if (typeof applicationConfig.configs !== 'undefined') {
configService.setConfig.apply(configService, applicationConfig.configs);
}
serviceManager.setStrictMode(configService.get(ServiceManager.STRICT_MODE_CONFIG_KEY, false));
this.configService = configService;
return this;
}
/**
* Look for services (configurable as 'app.services') to configure, bootstrap and launch.
*
* @inheritDoc
*/
}, {
key: 'launch',
value: function launch() {
var configService = this.configService,
serviceManager = this.getServiceManager(),
servicesConfig = configService.get('app.services', {}),
servicesToLoad = [];
for (var serviceKey in servicesConfig) {
var serviceDefinition = servicesConfig[serviceKey];
if (typeof serviceDefinition === 'function') {
serviceDefinition = {
service: serviceDefinition
};
}
if (serviceDefinition !== null && typeof serviceDefinition.service === 'function') {
// instantiate the fetched service class.
var service = new serviceDefinition.service(serviceManager);
serviceManager.set(serviceKey, service);
// determine the configkey
if (typeof serviceDefinition.configKey !== 'string' && serviceDefinition.configKey !== null) {
if (typeof service.configKey === 'string' || service.configKey === null) {
serviceDefinition.configKey = service.configKey;
} else if (typeof serviceDefinition.service.CONFIG_KEY === 'string' || serviceDefinition.service.CONFIG_KEY === null) {
serviceDefinition.configKey = serviceDefinition.service.CONFIG_KEY;
} else {
serviceDefinition.configKey = serviceKey;
}
}
servicesToLoad.push({
key: serviceKey,
instance: service,
definition: serviceDefinition.service,
configKey: serviceDefinition.configKey
});
}
}
// configure all required services
for (var i = 0, l = servicesToLoad.length; i < l; ++i) {
var service = servicesToLoad[i];
// in case the service has a config key defined
// try to load the service specific config and pass this to the configure method.
if (typeof service.configKey === 'string') {
service.instance.configure(configService.get(service.configKey));
} else {
service.instance.configure();
}
}
// bootstrap all required services
for (var i = 0, l = servicesToLoad.length; i < l; ++i) {
servicesToLoad[i].instance.bootstrap();
}
// launch all required services
for (var i = 0, l = servicesToLoad.length; i < l; ++i) {
servicesToLoad[i].instance.launch();
}
return this;
}
}]);
return Application;
})(Service);
module.exports = Application;