Permalink
Browse files

yaap during connectphase, express-integration

  • Loading branch information...
1 parent ec05aee commit 6aaaf4467d86e1f694be8335dbf5352a32976997 @warmuuh committed Apr 3, 2013
@@ -0,0 +1,19 @@
+"use strict";
+
+function MyService() {
+}
+
+
+MyService.prototype = {
+
+ handleGet: function (req, res, next)/*@GET("/")*/ {
+ res.send('Look ma, no HTML!');
+ }
+
+};
+
+
+module.exports = MyService;
+
+
+
View
@@ -0,0 +1,20 @@
+/*jshint node:true*/
+"use strict";
+
+
+var wire = require("wire");
+
+wire({
+ app: { create: 'express' , ready:{listen:[8000]}},
+ myService: { create: './MyService' },
+
+ plugins: [
+ {module: "../yaap/wire"},
+ {module: "../yaap/wire/express", server: "app"}
+ ]
+}, {require: require}).then(function(ctx){
+
+ console.log("----- initialized -----");
+ //ctx.destroy();
+}, function(err){console.error(err);});
+
@@ -0,0 +1,3 @@
+{
+ "main":"./wireplugin"
+}
@@ -0,0 +1,58 @@
+/** @license MIT License (c) copyright 2013 original author or authors */
+
+/**
+ * @Autowired annotation processor.
+ *
+ * Autowires parameters of function calls based on a given or the parameter name.
+ *
+ * needs wire to be in the configuration object.
+ *
+ *
+ * Licensed under the MIT License at:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * @author Peter Mucha
+ *
+ * @version 0.0.3
+ */
+"use strict";
+(function(define) {
+define([ "when"],
+function(when) {
+
+return [
+{
+ annotation: "@GET",
+ processFunction: function(obj, fnDescription, annotationParams, context){
+ context.express.get(annotationParams[0], function(req, res, next){
+ obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
+ });
+ }
+},
+{
+ annotation: "@PUT",
+ processFunction: function(obj, fnDescription, annotationParams, context){
+ context.express.put(annotationParams[0], function(req, res, next){
+ obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
+ });
+ }
+},
+{
+ annotation: "@POST",
+ processFunction: function(obj, fnDescription, annotationParams, context){
+ context.express.post(annotationParams[0], function(req, res, next){
+ obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
+ });
+ }
+},
+{
+ annotation: "@DELETE",
+ processFunction: function(obj, fnDescription, annotationParams, context){
+ context.express["delete"](annotationParams[0], function(req, res, next){
+ obj[fnDescription.name](req, res, next); //dynamic call because there could be other annotations
+ });
+ }
+}
+];
+
+});})(typeof define == 'function'? define: function(deps, factory) {module.exports = factory.apply(this, deps.map(function(x) {return require(x);}));});
@@ -0,0 +1,37 @@
+/** @license MIT License (c) copyright Peter Mucha */
+
+/**
+ * yaap/wire plugin
+ * wire plugin that provides annotation processing of components, e.g. @Autowired
+ *
+ * wire is part of the cujo.js family of libraries (http://cujojs.com/)
+ *
+ * Licensed under the MIT License at:
+ * http://www.opensource.org/licenses/mit-license.php
+ */
+(function(define) {
+ define(["underscore", "../../yaap",
+ "./plugins/VerbProcessors",
+ "wire"], function(_, yaap, VERBS, wire) {
+ "use strict";
+
+
+ return {
+ wire$plugin: function(ready, destroyed, options) {
+ if (!options.server)
+ throw "yaap/wire/express needs server reference";
+
+ //register for wiring
+ yaap.config = yaap.config || {};
+ yaap.config.express = {$ref: options.server};
+ for(var i = 0; i < VERBS.length; ++i)
+ yaap.register(VERBS[i]);
+ return {};
+ }
+ };
+
+ });
+}(
+typeof define == 'function' && define.amd ? define : function(deps, factory) {
+ module.exports = factory.apply(this, deps.map(require));
+}));
@@ -32,16 +32,16 @@ return {
var refName = param.annotation.parameters.length == 1
? param.annotation.parameters[0]
: param.name;
-
+
refs[param.name] = {$ref: refName};
}
-
+ console.log("@Autowire "+ JSON.stringify(refs));
var promise = when.defer();
context.promises.push(promise.promise);
context.wire(refs).then(function (resolvedRefs) {
var origFn = obj[fnDescription.name];
-
+ //console.log("@Autowire "+ JSON.stringify(resolvedRefs));
obj[fnDescription.name] = function(){
for(var i = 0; i < annotatedParameters.length; ++i){
var param = annotatedParameters[i];
View
@@ -21,14 +21,20 @@
function processAnnotations(resolver, facet, wire) {
var options = facet.options;
var obj = facet.target;
- var ctx = {
- wire: wire, //feed in context, so Autowire can do its work
- promises: [] //plugins can save promises here
- };
- yaap.process(obj, ctx);
-
- when.all(ctx.promises).then(resolver.resolve,resolver.reject);
+ yaap.config = yaap.config || {};
+ wire(yaap.config).then(function(ctx){
+ ctx.wire= wire; //feed in context, so Autowire can do its work
+ ctx.promises= []; //plugins can save promises here
+ yaap.process(obj, ctx);
+
+ when.all(ctx.promises).then(resolver.resolve,resolver.reject);
+
+ }, function(err){
+ console.log("wiring yaap-config failed:" + err);
+ resolver.reject();
+ } );
+
}
function afterProcessing(resolver, facet, wire) {
@@ -46,15 +52,16 @@
return {
wire$plugin: function(ready, destroyed, options) {
-
+
yaap.register(autowire); //register annotation processor for @Autowire
yaap.register(postConstruct); //register annotation processor for @Initialize
yaap.register(preDestroy); //register annotation processor for @PreDestroy
-
+
+
return {
- configure: processAnnotations,
- "ready": afterProcessing,
- "destroy": beforeDestroying
+ "connect": processAnnotations,
+ "ready": afterProcessing,
+ "destroy": beforeDestroying
};
}
};
View
@@ -78,16 +78,10 @@ function(_, registry, NotNullProcessor, DefaultProcessor, wire, PanPG_util, es5,
for(var f in obj)
{
- if (!_( obj[f]).isFunction()) return;
+ if (!_(obj[f]).isFunction()) return;
var source = obj[f].toString();
-
-
- source = source.substring(0, source.indexOf("{")); //strip body //TODO: this is not secure, if comments contain '{'
-
-
-
-
+ source = source.substring(0, source.indexOf("{")); //strip body //TODO: this is not secure, if comments contain '{'
var ast = es5.Program(source);
//console.log(PanPG_util.showTree(ast));

0 comments on commit 6aaaf44

Please sign in to comment.