Using HK2 with Jersey (with auto-scanning!)

Overview

HK2 has been an interesting ride. For the most part, if anyone asks, I’m still going to suggest Spring for Dependency Injection, because the documentation is a thing of wonder. Plus, Spring Inversion of Control (IoC) and Aspect Oriented Programming (AOP) are the wheelhouse that makes all of the other Spring stuff possible.  However, there are some situations where using these technologies are requested/required, so it’s good to know how to use them.

Understanding Dependency Injection

Dependency Injection is a great way to decouple your code from the implementation.  For example, if you create an interface in your core program, and you can leave the implementation for creation in a different library.  How is that useful?  One example of this would be for database access.  If you create a simple interface, like the following:

Now, use this interface in some form of implementation. This should provide a simple passthrough implementation usage of the interface:

You can then make two different implementations that function very differently, and put them in separate jars.

The first could access a database, such as PostgreSQL:

The second implementation could be a MongoDB implementation:

Now for the interesting part… You want it to be able to change at runtime. So your customer can easily change and use PostgreSQL or MongoDB with ease. Heck, they can even add a Redis store with ease and without having to recompile a single line of code. It seems like magic, but it’s not. It’s just smart decoupling.

Continue reading

The New Hotness – Spring 4’s Spring Boot

The guys who make the Spring framework libraries are no slouches.  They know the ups and downs of their own framework, as well as Java as a whole.  They are also on top of all of the newer JVM technologies, such as Groovy, Scala, Gradle, Spock, etc.  Heck, if they weren’t making money hand over fist, I’d think they would be great core language developers.  The would progress the language to a point that I’d love using it again.

The reason I say all of this is because the developers of the Spring framework realized that there are a couple other frameworks out there (Ruby on Rails, Grails, Django, etc.) that favor convention over configuration.  Meaning, I don’t want to have to set up 50 XML files to configure a project just so that I can get it running as a simple web service.  It shouldn’t be that complicated.  Spring 4 has some answers for you.  Spring introduced Spring Boot, a group of annotations that will favor convention over configuration… at least until you need to change it to something more complicated. The way it acts, I think they may have backported some of the stuff they did for Grails right into Java. Continue reading

My New Favorite Toys

OK, I’m going to admit it.  I tend to get really excited about Configuration Management (CM) type work and I have no idea why.  This is one of those.

Machine Images/Virtual Machines

Ok, these have been around.  If you don’t know what a machine image is, then stop and go download Oracle’s VirtualBox, because it is something that everyone should know.  These were revolutionary when they first were introduced.   So much so that most Operating Systems support some form of images (disk or machine) right out of the box.  Especially because a number of companies, including VMWare and Parallels, found out that they could manage your hardware better than the Operating System (which was originally supposed to do just that, but has really turned into something much more complex and more like a user interface). So, it turned out that if you used machine images, you could have a snapshot to a point in time. Sure, it still required some configuration after getting it going, but after that, it was cake. IT departments (and Dell/Norton, with their Ghost Image partition) were loving it. It really cut the mind-numbing monotony of setting up new boxes.

Puppet and Chef

Essentially, Puppet and Chef do the same thing.  Both start on the assumption that you have a base image installed on your box. Both represent a language that gives you the ability to install the software, users, and whatever else is necessary on a box to have it ready for tasking.  Both have claimed to support all major OS options.  This way, you could install the same users on the Windows boxes and Unix boxes in one script!  Of course the multiplatform argument is crap, but it still is a great option to finish what Machine Images started.

Take passwords for example.  Each OS handles this so completely differently that you would have to create a different module for the password for each OS.  Too much hassle.  Most companies are going to pick an OS and stick with it.  Heck, most are going to pick the same hardware too. This is the suggested use for almost all companies anyways.  Google, Yahoo, Amazon, etc. all use proprietary hardware.  But it’s THE SAME proprietary hardware.  Every machine has the same specifications.  (If you want, check out the Google server machines or the data center).  And I’m guessing most of them aren’t Windows boxes.  Just sayin’.

So, you create a new machine configuration for each type of machine.  One for your Web servers, one for the database servers, etc. Makes scripting all boxes that you might want to make really easy. Plus, in a cloud environment, like Amazon EC2 or privately held HDFS clusters, this makes setting up servers a breeze.

The difference between Puppet and Chef is their target audience.  Although they are technically competing products, both companies have pretty much stated that their real competition is the rats net of batch/shell scripts that you’ve probably written, or even better yet the documented instructions like the following.  We’ve all written these before.  And they even sometimes work.  Sometimes. Continue reading

ImpressNotes

A bunch of people enjoy the new Slide technologies that are coming out using HTML5.  You can check some of them out here at Html5Rocks.  Unless you are doing something fancy, though, you probably shouldn’t write all of the JavaScript to do this yourself.  This is for the same reason that you shouldn’t write a DOM manipulation tool yourself (cause #1 – don’t do work that you don’t have to do, and #2 – JQuery already did it and they did it better than you or I likely will.)  A little while ago, I posted about a friend using ImpressJS.  I got a chance to use it myself now.  It’s a really useful library that does the slide moving, using any number of 3D transforms (kinda like Prezi on crack*).  Also, my friend had coded against an older version.  The newer versions allow you to have triggers to kick off other pre/css and post/css, so if you want to do some cool transforms like Apple showcases, you can do that.

Also, something that my buddy noticed is that with any presentation anyone gives, you usually have notes to read from.  These are really helpful if you are the presenter and you need help remembering (that would be me) or for those viewing after the presentation has happened.  This is standard in a presentation tool like PowerPoint or Keynote.  I took some of the code that he wrote and modified it to be a bit less intrusive to the library, which follows a more Aspect Oriented approach.  So, if you ever need to use notes with your ImpressJs presentation, please feel free to use/modify the ImpressNotes project.  I still want to add a timer to it too, so I’ll probably tinker with it some more.

*Obviously, you’re going to still have to be a developer to use this, vs Prezi, which you can be non-technical.  So don’t get me wrong.  Not knocking Prezi.

SourceMaps are the new Sliced Bread!

What are SourceMaps?  Basically, it maps the code in your minified/obfuscated/compiled file to the original source code.

Think about that for a second.  If you are a JavaScript developer, you are constantly dealing with minified files, or in the case of some of the fun newer technologies, such as Google’s Closure Tools or CoffeeScript, you are actually compiling the code provided.  In each of these cases, it is changing from JavaScript to another form of JavaScript.  Most the time, once we start using the minified/obfuscated/compiled, we have no idea what’s going on and have to switch back to the “debug” version if we want to figure anything out.  And we assume that the Minifier/Compiler has done nothing wrong to our code in the process (sometimes a stretch, especially with some of the newer ones).

The SourceMap is a separate file that can be provided that will give general descriptions to help the browser map the files back to the original codebase.  That way, if you have issues in the compiled code, you step through the code, and the browser knows exactly where to put you in the debug code.  How sweet is that?

They are something simple that was developed by the Mozilla group (check out the mozilla SourceMap), that was gobbled up by Google Chrome as well because of how unbelievably useful it is.  Thanks to a fellow developer (Patrcik Gilmore) in Oslo, Norway, we now have this available in the Maven ClosureJavaScriptFramework Plugin.  So for anyone interested, feel free to start using the plugin.

Obviously, I didn’t provide a ton of information on how to use it.  Turns out there isn’t much out there yet either.  But, there is a nice link on how to use SourceMaps in your browsers at html5rocks.  Check that out and it should get you using it pretty quickly.  Another downside, it’s not supported by everyone yet.  But just you wait, everyone’s going to be jumping on this bandwagon.  There are just too many people that will find this useful.  So, if you are using some of these minifiers/compilers, put in a request to get that little .smap file added!

JavaScript primer

For those starting on JavaScript, I’m sorry.  This one’s going to hurt a little bit.  First off, I’d suggest you start with Pro JavaScript Design Patterns.  Learn everything about why it’s right.  Then throw it away.  Because you’ll never need the code presented in that book (it’s been done a hundred times in a bunch of libraries.)  But, the concepts in it are invaluable.  Also, it’s hard to find out what’s built in to all of the browsers.  I’d start here.  Hands down the best site for really learning JavaScript built in functions, though not as pretty as some of the others.  Then I’d move on to Mozilla, which is a lot to take in, but also amazing.

Typing in JavaScript is implicit and it is handled automatically.  All you do is say “var x = 5” and it knows that x is an int.  It’s magic.  Magic that is hard to understand when stuff goes wrong.  Here’s an article on understanding type conversion in JavaScript.
http://jibbering.com/faq/notes/type-conversion/

Also, people in Java and C seem to love ternary operators.  I think it’s because everyone thinks it’s clever.  For a newbie, it’s hard to understand though, so I tend to avoid them in most cases.  However, JavaScript supports this, so you can be clever all you want in your code.  Heck, JavaScript allows you to be so clever that I know some folks that don’t know what their own code does once they are done with it.  Which is just awesome to support, by the way.  But I digress.  JavaScript also expands upon the ternary operator with the conditional-or (“||”) operator, which can be used in some pretty fancy ways.  For anyone interested in learning about the || operator in javascript:
http://addyosmani.com/blog/exploring-javascripts-logical-or-operator/

OK, a bunch of people don’t understand how scoping in JavaScript works.  One of the reasons for this is that most people are trained nowadays as Java developers (I myself was trained in C/C++, so I have my own set of issues…).  This is an article that explains closures, for all those that need help with it.  It includes the terrible “closure loop issue”.
http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/
Basically, because JavaScript functions can be passed around as objects, you don’t know who is calling it or where they are calling it from.  This means that the JavaScript needs to carry everything it needs with it.  This is why people call this a memory leak.  And they aren’t entirely wrong.  Especially when people use closures like they are using Java private.  Then it’s definitely a memory leak.
http://www.ibm.com/developerworks/web/library/wa-memleak/

Awesome site that allows you to code a little snippet and see if it works, as well as share with others. The coolest part is that you can include a bunch of the more commonly used libraries, such as JQuery, Dojo, ExtJS/Sencha, Underscore, and Raphael (plus a TON more, but I thought I’d throw out the big ones/my favorites).  You can even get a plugin or two for your IDE or text editor to throw it directly up there when having issues.
http://jsfiddle.net/

Well, that’s about it.  I just thought I’d share some resources for the beginners out there.

jQuery and ImpressJs

So my buddy did a JQuery rehash for people in our company.  In my opinion, it doesn’t need to be sold.  People like JQuery cause it is easy and the learning curve to do some things is easy.  It follows the old Perl mantra: “Try to make simple things easy, and complex things possible”.  Few folks can argue with that.

More interesting, in my opinion, than the subject material, was his choice of presentation tools.  He chose to use the Impress JS library. It functions like prezi, but with WAY more configuration options (pretty much anything you can think of doing to the DOM).

He posted his presentation on his website here and put the code up on GitHub here.