Eclipse Hudson Plugin
Context - Continuous Integration (CI) is a must. And Hudson is a free, decent, and quick to setup CI server.
To get Hudson into Eclipse - there's a plugin. The recommended way of installing is of course, via the plugin's update URL - http://hudson-eclipse.googlecode.com/svn/trunk/hudson-update/.
After installing it, you'll need to configure it:

Once that is out of the way, you'll get this:

Guice or Spring?
Just today, someone mentioned that because Spring already does what Guice does (and more, I might add) that I should stick to the latter. I beg to differ. Each has its own purpose, I wouldn't put these 2 in the same league.
Spring is almost a complete container. From security, a web MVC model, batch framework, persistence, JMX, scheduling, web services and many more, Spring offers (or is able to piece together) an almost complete stack to build enterprise applications. In other words, it's a JEE competitor. Its selling point has always been as a lightweight enterprise stack anyway.
Guice on the other hand is a lightweight DI enabler, so to speak. It offers to take away DI code that a developer could otherwise write themselves. This is probably a gross over-simplication but that's how I see it. When I want DI or AOP but I don't want the whole she-bang (and I don't want to do DI manually), Guice is the solution. On the other hand, if I'm looking to develop enterprise software, I could use Guice, but Spring offers so many bundled services that it's hard to ignore.
I like how Guice encourages constructor injection (which, along with final fields, encourages immutability) and that it returns new instances by default - both of which should allow better performing apps when paired with powerful hardware (read: multi-core friendly). I also like how Guice encourages thinking of applications in Modules, its promotion of testability and its potential reusability. I'm a seasoned Spring developer but it was Guice that really made me step back and think about DI, testability and modularity.
After a few weeks of implementing a small library in Guice, it seems there's no turning back. For non-trivial stuff, there's Spring but for everything else, Guice may be the better choice for me. At the end of the day, Guice is yet another tool under my belt. To dismiss it entirely because I already know Spring is just foolish (even if I've been using Spring for 2 years). Of course, I'll also keep my eye on the recently announced JSR that will bring DI to standard Java.
Guice and Spring are but tools. It's DI, AOP and testability that I need.
Google Guice @Provider Gotcha
Note: Best read after some background reading on Google Guice and dependency injection.
I spent many hours pouring through my codes to resolve this so I'm posting this as a guide to others.
Scenario:
Google Guice allows Providers to be defined, either using the @Provider annotation or by implementing the Provider interface. To those new to Guice, here's a simple explanation on providers:
When you need code to create an object, use an @Provides method
In other words, when you need to inject an object that needs some sort of setup code, or when you need to customize what is injected, you resort to using a Provider. Here's a code snippet that injects a chain of handlers (read: Chain of Responsibility pattern):
@Provides
private ErResponseHandler provideResponseHandlers() {
// builds the handlers manually - chain of responsibility pattern
ErResponseHandler handler = new InactivateAccountResponseHandler(
new UsageAuthRateChargeResponseHandler());
return handler;
}
And this is BaseResponseHandler - a base class which implements ErResponseHandler.
public abstract class BaseResponseHandler implements ErResponseHandler {
@Inject
protected XmlBinder xmlbinder;
public BaseResponseHandler() {
}
private ErResponseHandler next;
public BaseResponseHandler(final ErResponseHandler nextHandler) {
this.next = nextHandler;
}
public final boolean start(final ErResponse response) {
boolean handled = this.handleResponse(response);
if (next != null && !handled) {
next.start(response);
}
return handled;
}
public abstract boolean handleResponse(ErResponse resp);
}
Everything seems to be in order, right? Straightforward dependency injection of xmlbinder, except of course, this does not work. Xmlbinder is always null. The problem is that once you take over the job of providing your own objects to be injected, injection doesn't work automagically any more. You'll have to wire up your objects manually too.
Solution:
The provider method has to be changed to:
@Provides
//XmlBinder has been bound using the application Module
private ErResponseHandler provideResponseHandlers(XmlBinder xmlbinder) {
// builds the handlers manually - chain of responsibility pattern
ErResponseHandler handler = new InactivateAccountResponseHandler(
new UsageAuthRateChargeResponseHandler());
handler.setXmlBinder(xmlbinder);
return handler;
}
Of course, the BaseResponseHandler class now has to set the xmlbinder accordingly:
public abstract class BaseResponseHandler implements ErResponseHandler {
protected XmlBinder xmlbinder;
public BaseResponseHandler() {
}
private ErResponseHandler next;
public BaseResponseHandler(final ErResponseHandler nextHandler) {
this.next = nextHandler;
}
public void setXmlBinder(final XmlBinder binder) {
this.xmlbinder = binder;
if (next != null) {
next.setXmlBinder(binder);
}
}
public final boolean start(final ErResponse response) {
boolean handled = this.handleResponse(response);
if (next != null && !handled) {
next.start(response);
}
return handled;
}
public abstract boolean handleResponse(ErResponse resp);
}
Unfortunately, Guice's documentation mentions this only in passing:
Dependencies can be passed in as parameters to the method. The injector will exercise the bindings for each of these before invoking the method
It should really be phrased as
Dependencies must be passed in as parameters to the method. Only then will the injector exercise the bindings for each of these before invoking the method.
Replacing conventional loops with Closures in Java 6
A useful way to promote a low-level code reuse is to use Closures. Think of it as writing generic functions that can be passed around in method calls.
Here's a code snippet that replaces a loop with a Closure:
Closure buildNode = new Closure() {
//do processing in execute method
public void execute(final Object obj) {
nodes = new ArrayList();
NodeBuilderType builderType = (NodeBuilderType) obj;
NodeBuilderStrategy strategy = strategies.get(builderType
.getClass());
if (strategy == null) {
throw new NodeBuilderException(
"No strategy found for Node type:" + builderType);
}
nodes.add(strategy.build(builderType, document));
}
};
//runs a 'for' closure
ClosureUtils.forClosure(builderTypes.size(), buildNode);
Commons Collections also provides utility classes which allows for Closures to be very useful for Collections processing. Here's one that applies a Closure on a Collection:
//define your closure
Closure appendChild = new Closure() {
//do processing in execute method
public void execute(final Object obj) {
Node child = (Node) obj;
usageAttr.appendChild(child);
}
};
//executes appendChild against nodes
CollectionUtils.forAllDo(nodes, appendChild);
For more information, look at Apache's Commons Collections. Credit goes to Chris Shayan for sharing this a few weeks back. Update: I'll have to say that this way of achieving closures in JDK<7 is a hack, and feels like a hack but it's one of the solutions available. I'm still eagerly waiting for JDK to allow a more 'native' closure.
Dependency Injection Fails With Spring 3.0.0 RC2, JUnit 4.7, Mockito
Following text-book examples of using SpringJUnit4ClassRunner.class with Mockito, I couldn't get DI to work. It doesn't matter if I use the old-school way of XML configuration + setter injection or if I use annotations, they just don't work. Oddly though, the log shows that the application context did start, and beans were instantiated, they were just not injected and the beans are null. The beans are not mocks, but real classes that I need to use.
Curious, I created a simple main method to test the program and true enough, things work. As of now, I'm unable to determine why this is failing and I'm already delayed, so I'm abandoning Spring 3.0 for now. JUnit and Mockito works as I've only recently added Spring to get some AOP stuff working.
If you've got a similar combination working, I'm interested to know how this was done. Oddly enough, I can't seem to Google for any similar problems.
5 Simple Rules To Effective Debugging
1. Use your IDE to debug

As odd as this will sound, many, many programmers I know still does System.out.println() (which easily incurs insults from yours truly) for debugging; slightly smarter ones will use logs. Both will never be as fast or as convenient as using a real debugger in your IDE. I was lucky enough to pick up this valuable skill from my first senior and for that I'll be forever grateful. IDE debugging often allows the following cool features
- hot swapping of code
- object inspection (it doesn't freaking matter if it's a plain object or some fancy data structure)
- execution stack monitoring
- break when variable equals [some value]
- real-time modification of variable value

2. Remember thy shortcut keys
This is applicable to programming in general - mastery of your IDE's shortcut keys isn't a luxury, it's a necessity. What this means is that you need to always be able to step-in, step-out, step-over and continue debugging without even touching your mouse or looking at your keyboard. Memorize each key-stroke for those operations folks (that's F5, F7, F6 and F8 for Eclipse users). This is probably the simplest rule, but it's important nevertheless.
3. Write Unit Tests

How is this related to debugging? Simple. Unit tests by nature are fast to execute and are executable within the IDE. Yes, even if you're using Maven or some other build tools, it is very important to allow tests to be run from within the IDE to facilitate quick debugging. Unit tests are also very atomic, allowing a very narrow execution path, which in turn enables you to zoom in to a small portion of code to debug. Often I've seen folks without unit tests having to fire-up an application server to debug. You lose plenty of time waiting for it to start, only to have to go through the entire waiting game if you should need to change code (naturally this happens often in the debugging cycle). And you know what they say about tests that takes a long time to run... (they don't get executed)
4. Set Correct Breakpoints

If you find yourself having to hit the keyboard to many times to advance the stack, something is wrong. Don't be afraid to start with many breakpoints but start to clear the older ones once you have identify a general flow. I general I try not to have more than 5, 7 at most. If you're using too many breakpoints, your test execution path might be too wide. Consider rewriting a narrower test. You may also wish to consider using conditional breakpoints.

5. You Can Debug Remotely

Yes, it isn't ideal, but if you've got the server up, and the application is acting weird, why not seize the chance to quickly examine the state of the application? Of course this assumes that during development, you have the smarts to start the application using remote debugging VM options:
-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
You'll lose the ability to hot swap code, and there's the need to redeploy once you changed code, but these are small inconveniences compared to the ability to view the internals of your application when a bug surfaces (you still retain the other benefits of debugging). Once the examination is done, actual bug fixing and debugging should be carried out via unit tests anyway.