Skip to content

Commit

Permalink
yaap during connectphase, express-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
warmuuh committed Apr 3, 2013
1 parent ec05aee commit 6aaaf44
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 23 deletions.
19 changes: 19 additions & 0 deletions express-example/MyService.js
@@ -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;



20 changes: 20 additions & 0 deletions express-example/main.js
@@ -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);});

3 changes: 3 additions & 0 deletions yaap/wire/express/package.json
@@ -0,0 +1,3 @@
{
"main":"./wireplugin"
}
58 changes: 58 additions & 0 deletions yaap/wire/express/plugins/VerbProcessors.js
@@ -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);}));});
37 changes: 37 additions & 0 deletions yaap/wire/express/wireplugin.js
@@ -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));
}));
6 changes: 3 additions & 3 deletions yaap/wire/plugins/AutowireProcessor.js
Expand Up @@ -32,16 +32,16 @@ return {
var refName = param.annotation.parameters.length == 1 var refName = param.annotation.parameters.length == 1
? param.annotation.parameters[0] ? param.annotation.parameters[0]
: param.name; : param.name;

refs[param.name] = {$ref: refName}; refs[param.name] = {$ref: refName};
} }

console.log("@Autowire "+ JSON.stringify(refs));
var promise = when.defer(); var promise = when.defer();
context.promises.push(promise.promise); context.promises.push(promise.promise);
context.wire(refs).then(function (resolvedRefs) { context.wire(refs).then(function (resolvedRefs) {


var origFn = obj[fnDescription.name]; var origFn = obj[fnDescription.name];

//console.log("@Autowire "+ JSON.stringify(resolvedRefs));
obj[fnDescription.name] = function(){ obj[fnDescription.name] = function(){
for(var i = 0; i < annotatedParameters.length; ++i){ for(var i = 0; i < annotatedParameters.length; ++i){
var param = annotatedParameters[i]; var param = annotatedParameters[i];
Expand Down
31 changes: 19 additions & 12 deletions yaap/wire/wireplugin.js
Expand Up @@ -21,14 +21,20 @@
function processAnnotations(resolver, facet, wire) { function processAnnotations(resolver, facet, wire) {
var options = facet.options; var options = facet.options;
var obj = facet.target; 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) { function afterProcessing(resolver, facet, wire) {
Expand All @@ -46,15 +52,16 @@


return { return {
wire$plugin: function(ready, destroyed, options) { wire$plugin: function(ready, destroyed, options) {

yaap.register(autowire); //register annotation processor for @Autowire yaap.register(autowire); //register annotation processor for @Autowire
yaap.register(postConstruct); //register annotation processor for @Initialize yaap.register(postConstruct); //register annotation processor for @Initialize
yaap.register(preDestroy); //register annotation processor for @PreDestroy yaap.register(preDestroy); //register annotation processor for @PreDestroy



return { return {
configure: processAnnotations, "connect": processAnnotations,
"ready": afterProcessing, "ready": afterProcessing,
"destroy": beforeDestroying "destroy": beforeDestroying
}; };
} }
}; };
Expand Down
10 changes: 2 additions & 8 deletions yaap/yaap.js
Expand Up @@ -78,16 +78,10 @@ function(_, registry, NotNullProcessor, DefaultProcessor, wire, PanPG_util, es5,


for(var f in obj) for(var f in obj)
{ {
if (!_( obj[f]).isFunction()) return; if (!_(obj[f]).isFunction()) return;


var source = obj[f].toString(); 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); var ast = es5.Program(source);


//console.log(PanPG_util.showTree(ast)); //console.log(PanPG_util.showTree(ast));
Expand Down

0 comments on commit 6aaaf44

Please sign in to comment.