UNPKG

@ccms/container

Version:

MiaoScript container package

194 lines 7.94 kB
"use strict"; /// <reference types="@ccms/nashorn" /> Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultContainer = exports.reduceMetadata = exports.loadMavenDepend = exports.MavenDepend = exports.Resource = exports.Autowired = exports.JSClass = exports.JavaClass = exports.provideSingletonNamed = exports.provideSingleton = exports.provideNamed = void 0; var tslib_1 = require("tslib"); require("reflect-metadata"); var decorators_1 = require("./decorators"); var inversify_1 = require("inversify"); var inversify_binding_decorators_1 = require("inversify-binding-decorators"); var constants_1 = require("./constants"); var utils_1 = require("./utils"); /** * 注册一个命名对象 * @param identifier 标识符 * @param name 名称 */ var provideNamed = function (identifier, name) { return (0, inversify_binding_decorators_1.fluentProvide)(identifier).whenTargetNamed(name).done(); }; exports.provideNamed = provideNamed; /** * 注册一个单例对象 * @param identifier 标识符 */ var provideSingleton = function (identifier) { return (0, inversify_binding_decorators_1.fluentProvide)(identifier).inSingletonScope().done(); }; exports.provideSingleton = provideSingleton; /** * 注册一个单例对象 * @param identifier 标识符 */ var provideSingletonNamed = function (identifier, name) { return (0, inversify_binding_decorators_1.fluentProvide)(identifier).inSingletonScope().whenTargetNamed(name).done(); }; exports.provideSingletonNamed = provideSingletonNamed; /** * 获得一个 java.lang.Class * @param className Java全类名 */ var JavaClass = function (className) { return function (target, propertyKey, index) { try { target[propertyKey] = Java.type(className).class; return; } catch (error) { } try { target[propertyKey] = base.getClass(className); return; } catch (error) { } console.warn('JavaClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!'); }; }; exports.JavaClass = JavaClass; /** * 获得一个JS的Java类 * @param className Java 全类名 */ var JSClass = function (className) { return function (target, propertyKey, index) { (0, utils_1._proxyGetter)(target, propertyKey, function () { try { return Java.type(className); } catch (error) { } try { return base.getClass(className).static; } catch (error) { } console.warn('JSClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!'); }, true); }; }; exports.JSClass = JSClass; /** * 自动注入实例由平台实现 * @param className 类名 */ var Autowired = function (className) { return function (target, propertyKey, index) { var container = (0, decorators_1.getContainer)(); if (className && (className instanceof Symbol || className instanceof Function)) { return (0, inversify_1.inject)(className)(target, propertyKey, index); } var type = Reflect.getMetadata('design:type', target, propertyKey); if (type && type !== Object && !Java.isJavaObject(type)) { (0, inversify_1.inject)(type)(target, propertyKey, index); (0, inversify_1.named)(className || propertyKey)(target, propertyKey, index); } else if (container.isBound(constants_1.ioc.Autowired)) { console.debug('Autowired', 'ioc.Autowired', 'named', className || propertyKey); target[propertyKey] = container.getNamed(constants_1.ioc.Autowired, className || propertyKey); } else { throw new Error("No matching bindings found for target: ".concat(target.constructor.name, " type: ").concat(type, " named: ").concat(className || propertyKey)); } }; }; exports.Autowired = Autowired; /** * 自动注入资源由平台实现 * @param className 类名 */ var Resource = function (resourceName) { return function (target, propertyKey, index) { target[propertyKey] = (0, decorators_1.getContainer)().getNamed(constants_1.ioc.Resource, resourceName || propertyKey); }; }; exports.Resource = Resource; var DocumentBuilderFactory = Java.type('javax.xml.parsers.DocumentBuilderFactory'); var MavenDepend = function (groupId, artifactId, version, recursion) { if (recursion === void 0) { recursion = false; } return function (target) { loadMavenDepend(groupId, artifactId, version, recursion); }; }; exports.MavenDepend = MavenDepend; var loadedMavenDepend = new Set(); function loadMavenDepend(groupId, artifactId, version, recursion) { if (recursion === void 0) { recursion = false; } var key = "".concat(groupId, ":").concat(artifactId, ":").concat(version); try { if (loadedMavenDepend.has(key)) { return; } console.info('loading maven dependency', key); var _a = tslib_1.__read(base.loadMavenDepend(groupId, artifactId, version), 2), pom = _a[0], _ = _a[1]; loadedMavenDepend.add(key); if (recursion) { var doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(pom); var dependencies = doc.getElementsByTagName("dependency"); var size = dependencies.length; if (!size) { return; } console.debug(key, 'found', size, 'dependencies loading...'); for (var i = 0; i < size; i++) { var dependency = dependencies.item(i); var gav = dependency.getChildNodes(); var length = gav.length; var dependencyVersion = { groupId: '', artifactId: '', version: '' }; for (var j = 0; j < length; j++) { var prop = gav.item(j); switch (prop.getNodeName()) { case "groupId": dependencyVersion.groupId = prop.getTextContent(); break; case "artifactId": dependencyVersion.artifactId = prop.getTextContent(); break; case "version": dependencyVersion.version = prop.getTextContent(); break; } } loadMavenDepend(dependencyVersion.groupId, dependencyVersion.artifactId, dependencyVersion.version, recursion); } } } catch (error) { console.warn('load maven dependency', key, 'failed. Error:', error); if (global.debug) { console.ex(error); } } } exports.loadMavenDepend = loadMavenDepend; var reduceMetadata = function (ctx) { return ctx.currentRequest.target.metadata.reduce(function (result, entry, index) { result[entry.key] = entry.value; return result; }, {}); }; exports.reduceMetadata = reduceMetadata; function initAutowired(container) { container.bind(constants_1.ioc.Autowired).toDynamicValue(function (ctx) { var metadata = (0, exports.reduceMetadata)(ctx); var key = Object.toString.call(metadata.named); if (key === "[object Function]" || key === "[object Symbol]") { return container.get(metadata.named); } return undefined; }); } exports.DefaultContainer = new inversify_1.Container(); (0, decorators_1.initContainer)(exports.DefaultContainer); initAutowired(exports.DefaultContainer); tslib_1.__exportStar(require("inversify"), exports); tslib_1.__exportStar(require("./constants"), exports); tslib_1.__exportStar(require("./decorators"), exports); tslib_1.__exportStar(require("inversify-binding-decorators"), exports); //# sourceMappingURL=index.js.map