Saturday, March 29, 2014

What's up lately

1. I've been getting ugly and mini with gulp.js.

2. Cool REST API project at work with Jetty, Jersey, Guice (DI), and JAXB. A little mvn too of course. 

3. Some C# and Groovy explorartion gists. 

4. Implemented a Clock object in JS. It's a gist, but it's easy to throw into a fiddle. 

The gists are here:

Wednesday, March 19, 2014

Tar install

Here is a sweet command for all of your tar installing needs.

  • sudo tar --directory=/opt -xv f <file>.tar.[bz2|gz]
  • add the directory to your path: export PATH=$PATH:/opt/[package_name]/bin


Monday, March 17, 2014

Comparing Embed-able Java HTTP/REST Servers

I have a project where I want to create a REST service in Java, but embed the HTTP and REST server in the application itself.  Why embedded?  Because we can't install a servlet container on any of the machines we need to run on.  I don't want to serve web pages, just REST services.  Maybe in the future I would like to support web sockets, but that is just a "nice to have".  This is not a public REST API, so we don't need to worry about firewalls.

There are a lot of embed-able java HTTP/REST servers out there.  The good thing is that REST framework part of it seems like a no brainer; Use the reference implementation, Jersey.  We already have a project using Jersey, so it's an easy choice.

But the embed-able HTTP server is not an easy choice.  Here is a list with some features as of 2014-03-17. Options that support NIO and an async model would be nice.

I wish I could offer a conclusion, but you will probably need to research these to meet your own needs.  As far as popularity I think it boils down to Jetty and Simple.

We chose Jetty.  Here are the steps to get your hello world Jetty/Jersey embedded server up and running:

The hard way:
  1. Embed Jetty
  2. Using Jersey with embedded Jetty HTTP Server.  Search for section Jetty HTTP Server.
The easy way:

Saturday, March 15, 2014

Private variables in Javascript classes

So, in most classical languages, you can declare a private variable in a class.  No big deal.  You can declare a private function too.  Private is good as well all know.  You can't really have encapsulation without private/protected variables.  Encapsulation is maybe the most important part of Classical Object Oriented Design.

In javascript, you can create a private variable in a constructor as a local var like this:

Person = function(age) {
var _age = age; this.getAge = function() { return _age; } }; var p = new Person(4); p._age; // undefined p.getAge(); // 4

Cool!  I have encapsulation, but one thing we know about javascript is that functions declared withing the constructor are duplicated for each object.  That's not good.  So if I create another person, it has its own getAge function!


Person {getAgefunction}
  1. getAgefunction () { return _age; }
  2. __proto__Object

var q = new Person(5);

q.getAge = function() { 2; };
function () { 2; }

Person {getAgefunction}
  1. getAgefunction () { 2; }
  2. __proto__Object

Protoype functions solve the problem of function duplication among many objects.  BUT only functions that are declared in the same scope as the local variable will have access to that variable (via a closure).  So prototype functions cannot access private variables.

So I got to thinking.  Coffeescript implements classes.  How does it implement private variables?  Answer.  It does not .  You put an underscore in front of variables and functions you want to "act" as private and then hope that developers are smart enough not to access them directly.

But there is a way to implement private functions!  See this answer on stack overflow.
The comments are interesting too.  It is impossible to unit test private functions, but then maybe you just need to implement a public wrapper around the private one.

Reading this post at,the private variable is not an instance variable, but a class level private variable.  That is almost never what we are looking for unless we are implementing a singleton.  He comes up with a pretty reasonable conclusion.  Don't implement private variables and functions.  It's not worth the trouble.  I am a little let-down by that, but I guess we just move on and keep it simple.

Friday, March 7, 2014