Thursday, November 4, 2010

JavaScript Dependency Management and JSLint Tools

I've been focusing on JavaScript Rich Internet Application development for some time now and have felt that while JavaScript is a capable language, it has sorely lacked tooling. This situation is changing of course and we're seeing more JavaScript tools come from the likes of Mozilla, Apple and Google quite frequently.

I feel that the lack of dependency management with good version control in any language is painful and JavaScript was no exception. I recently spent a good part of my day sorting out dependencies in a .NET environment; I sorely missed Apache Maven there. Given my JavaScript development activities I decided to provide the dependency management functionality of Maven to JavaScript via a Maven Plugin.

Imagine writing just the following in your JavaScript code when you want to ensure that jQuery or Prototype.js is present at runtime:

var $;

Alternatively when there are no global variables declared by a JavaScript dependency you can import it without being concerned about its file location and version:

/**
 * @import com.jqueryui:jquery-ui
 */

The com.jqueryui:jquery-ui artifact along with its version is declared in Maven's POM file.

This frictionless approach to declaring JavaScript dependency requirements is the motivation for the Maven JavaScript Import Plugin.

I'm pleased to announce the availability of my JavaScript Import Plugin at Codehaus where I have now also become a committer (a great honour!).

Along the way  I decided to create a plugin that provided efficient JSLint invocation during JavaScript development. This plug is also released at the Codehaus and is named the JSLint Plugin.

Please help the professional JavaScript developer community by downloading and building the projects from source and try out some development using the plugins.

Maven crashing with a bus error/invalid memory access given Java 10.6 update 3

I had this very annoying scenario where all of a sudden the following happened:

mvn
Invalid memory access of location 0x133cd800a rip=0x100504444
Bus error

This seemed to happen as a result of updating to Java 10.6 update 3 on Mac OS X Snow Leopard; but not straight away!

After a while I started to wonder if the update was installed correctly. So, I manually downloaded the update from here:

http://support.apple.com/kb/dl972

...and presto, things appear to be ok again... let's see how long that lasts!

I understand that as of this update, Apple are handing over control to Oracle in terms of managing the distribution of Java on Mac OS X. I think that this is generally good given that Apple has always been a little behind in terms of the latest Java release. Let's hope that the quality of the distro on Mac OS X does not go south though!


Wednesday, October 6, 2010

When is a unit test not a unit test?

There's a great deal written on this topic already, but obviously not enough. I had a very interesting chat with a colleague the other day about what constitutes a unit test vs integration, system etc.

I think the boundaries are mirky, but for me at least, a unit test is something that can be constructed with little friction and it can execute fast. I don't really care much about whether I'm testing things that are technically outside of the "unit" I'm working on e.g. if there's an in-memory database available and I can easily construct a test of my code, I don't think I'm crossing the boundary into integration testing. In fact what I've found is that it is sometimes easier to set up an  in memory database for the purposes of testing rather than mocking it.

Now of course when you start crossing process boundaries you are moving into the world of integration tests. This falls into the category of "being slow to run" though.

At the end of the day I'm not too worried about whether my test is a unit test so long as its easy to write and quick to execute. If it isn't then I'm happy to have it fall into another category of testing.

Saturday, August 7, 2010

A MessageBodyWriter isWriteable method for a collection example

I can’t believe how much time I’ve spent today on providing an isWritable method for my JAX-RS MessageBodyWriter class. I just couldn't find a good example out there that did what I wanted to do. Maybe Google search wasn't behaving today!

My goal was to have a provider that translates a List<GPSTrackerCollection> object into an application/xml output stream. GPSTrackerCollection is my own class.

The tricky thing was learning how to test for the presence of a List of GPSTrackerCollection objects within my provider.

First things first, I needed to wrap my collection in a GenericEntity object in order to preserve information on the parameterised type; the runtime strips this information of course (this is known as erasure). Here's an example service implementation from my code base:

@GET
@Path("/history")
public Response getHistory() {
List<GPSTrackerCollection> list = new ArrayList<GPSTrackerCollection>();
GenericEntity<List<GPSTrackerCollection>> entity =
new GenericEntity<List<GPSTrackerCollection>>(list) {};
return Response.ok(entity).build();
}

My MessageBodyWriter ends up looking something like the following:

@Produces("application/xml")
@Provider
public class GPSTrackerCollectionProvider implements
MessageBodyWriter<List<GPSTrackerCollection>> {

@Override
public long getSize(List<GPSTrackerCollection> arg0, Class<?> arg1,
Type arg2, Annotation[] arg3, MediaType arg4) {
return -1;
}

@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] arg2, MediaType arg3) {

// Ensure that we're handling only List<GPSTrackerCollection> objects.
boolean isWritable;
if (List.class.isAssignableFrom(type)
&& genericType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericType;
Type[] actualTypeArgs = (parameterizedType.getActualTypeArguments());
isWritable = (actualTypeArgs.length == 1 && actualTypeArgs[0]
.equals(GPSTrackerCollection.class));
} else {
isWritable = false;
}

return isWritable;
}

@Override
public void writeTo(List<GPSTrackerCollection> gpsTrackerCollections,
Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
MultivaluedMap<String, Object> arg5, OutputStream os)
throws IOException {

...
}
}

The key thing here is that we check the parameterised type argument for equality against the class of GPSTrackerCollection. My intuition was that I could do an instanceof operation here, but this is not permitted. Check out this FAQ as to why.

Enjoy.

Saturday, July 31, 2010

Application development feelings 2010

Every now and again I review my practice of developing web based software. Here is a summary of how I write my applications in the context of a multi-tier architecture.

The presentation layer

In essence I'm a big fan of RIA (Rich Internet Application) for situations where I'm interested in developing a web based application as distinct from a web site full of HTML pages. I feel that this is a distinction lost on many people and I also think that those same people struggle with combining HTML/CSS/JS to make a web application.

I'm quite keen on developing something with Sproutcore as in a way, it blows away HTML and CSS and gives you the widgets to build a fully fledged MVC style app that works within the browser; its very similar to developing a native desktop application. The same can be said for Google Web Toolkit (GWT) however I slightly favour Sproutcore as it embraces JavaScript (JS) as a language whereas GWT embraces Java that produces JS i.e. Sproutcore "feels" lighter. Perhaps I should get over that... what Google have developed in the form of Google Wave is impressive.

There are other JS MVC frameworks out there (JavascriptMVC and PureMVC for starters), but I'm not sure that they drop HTML/CSS as a dependency for the developer to consider. I suppose what I'm feeling is that HTML and CSS do not belong in a web based application. Given HTML's start in life as a mark up language for documents I'm comfortable in acknowledging that it does not have a place in my web application.

The biggest thing for me right now with regards to JS RIA is the lack of development tooling. I've been using the Jetbrains WebStorm IDE for a couple months and while I'm impressed on its Window's implementation, it doesn't feel like a native Mac OS X application. Given that Mac OS X is where I like to spend a lot of my time this unfortunately creates a problem for me.

In my ideal world I'd be using Apache Maven for building and deploying my JS RIA's. I've really come to appreciate Maven in the Java space and it is just waiting for someone (may be even me!) to extend it for JS development (I'm familiar with JS Maven Tools but I understand that this project hasn't had activity for a long time). The pressures of family life and my daily job curtails the development of developer tools.

I shall pursue the use of SproutCore for my next RIA. So far I've just used HTML/CSS/JS and jQuery having implemented MVC in the raw. However when it comes to developing a website with perhaps a few dynamic requirements, may be I'll stay with trusty old JSP/Struts that communicates with my logic layer. When it comes to mobile and desktop, I will continue to do as the Romans do and develop native applications that communicate with my logic layer.

The logic layer

I've used Apache Camel on several projects now and just love it. What I end up with is a Java based application that can be managed as a service typically deployed to some brand of Unix. I've been using VMware's Hyperic to monitor the applications and given JMX I get some very deep monitoring capabilities.

I just love the Spring IoC framework and associated toolkits; particularly Spring JPA DAOs and Spring Test.

My applications tend to consist to lots of components that can exist independently of each other, and each with minimal dependencies on other frameworks and toolkits. For example, my application services containing the important business logic tend to be written as Java beans and have no or very little dependence on external libraries.

I lean towards creating RESTful services and look to leverage Apache CXF in this regard. I'm not yet using a toolkit to scaffold some of my RESTful DAO functionality but will at some point. I'm also a big fan of JSON as a payload format but XML's schema definitions have an important place in my heart. JSON with schemas would be a killer and I've just got to get into this.

Messaging is key and Active/MQ has been a great performer. I'm really looking forward to v.5.4 and its support of HTML 5 web sockets. This will allow messaging to be utilised by the JS RIA and I find that prospect quite exciting.

Apache Maven plays a huge role in developing and deploying my logic layer applications.

The data layer

I've been very happy with Postgres as a database and look forward to its forthcoming redundancy features. I'm also curious about Big table style approaches and Document Oriented Databases. I'm not quite sure why SQL should continue to play an application development standpoint (reporting, data mining etc. yes, but perhaps not within the application itself). ORMs have pushed me to the "NoSQL" conclusion and I like not having to deal with queries as much.

Summary

Multi-tier works for me. RIA appears to be the way of the future for web applications as distinct from web documents/pages. If you've not seen Apache Camel check it out. Postgres is great but I'm curious about Document Oriented Databases.

Please share your thoughts.

Friday, July 30, 2010

Development using Salesforce

I'm presently on a short contract developing using the Salesforce platform.

I've been using Salesforce for over 2 months now and I'm afraid to conclude that it doesn't make it as a viable platform for cloud application deployment. The reason for this is quite simple: it forces you to develop in the cloud as distinct from deploying to the cloud.

Salesforce thought that it would be great to re-invent the wheel, introduce a new language (Apex) and introduce the associated tooling. From a developer perspective this means that all of your regular tooling such as version control, continuos integration, deployment management, dependency management... I could go on... simply isn't there. Let me re-state a bit of that: no version control!

Developing on the Salesforce platform also means that you're dealing with 300ms (ish) round trips for every resource in their web development screens. This is because the platform is hosted in the US and the speed of light being what it is. A developer can quite commonly be waiting for several seconds while the development web app is doing its thing; reminds of my COBOL days 25 years ago when we had to submit stuff to a shared compiler resource and wait.

I'm quite turned on by the idea of deploying to the cloud and I think that VMforce and Google App Engine have chosen the right approach; they of course allow you to develop locally and then deploy to the cloud. Full scale application development in the cloud though is another thing and frankly Salesforce have got it wrong.

Friday, June 4, 2010

Invoking Maven and having it ignore your local settings

I recently found myself working remotely from my network and needed to perform a build using Maven. My ~/.m2/settings.xml file is configured with information about my network's Nexus repository and a few other things. I therefore needed to find a way to tell Maven to ignore my regular settings; preferably without mutating my settings.xml file. In particular I just wanted to have Maven pull out a plugin from the Central repo.

The documentation on the Maven command doesn't mention how it can be invoked to ignore your settings.xml. However if you type the following then it will ignore it (in my case I wanted to download the wonderful Cobertura plugin and produce a code coverage report) :

mvn -s/dev/null cobertura:cobertura

All good.