Tuesday, March 15, 2011

4.0 l/100km in Skoda Fabia Combi TDi

Today we're not talking about computers. Well, maybe just a little. But not in developer's kind of way but more like a consumer. You know? The kind of person that actually buys the stuff you created :)

So it all started on a gas station as I needed to pump some gas into my car's tank as I was running dangerously near the big far E letter. With 5.09 PLN a liter it was just just crazy so I decided to get only as little as I need to get home plus some extra in case of trouble. My car usually takes around 5l/100km, this is a 125-ish kilometer distance so I tanked for 50.03 PLN and was on my way home from work.

The traffic was as it is usually around 5pm in the slowest town in Poland. So it took me around 15 minutes to get through the worst 2km. What I noticed was an unusually low usage (around 7.9l/100km) on my on-board computer. I mean for such traffic conditions with and a cold diesel engine. This was the moment I decided to see how low can I go on the gas usage.

This time I drove precisely according to rules. 50km/h in town (or 40 when the signs said so), 90km/h outside of towns. By the time I reached 90th kilometer my usage dropped to 4.1 l/100km so it was already my personal record.

After going through small cities I finally arrive at a point where I joined the A4. Normally I'm a "pedal to the metal" kind of guy but not this time. I followed a truck going about 90 km/h, didn't accelerate much.. and after the 100th kilometer I've reached the usage of 4.0 liter per 100km.

For the rest of the road I've tried as hard as I could to keep my foot light (which is kind of freaky when you're one of the slowest cars on the highway and normally it's more/less the opposite).

Here's the route in details:

A - My workplace
B - Gas station (damn you BP! 5.09PLN for a liter of diesel gasoline!)
C - Complete stop due to traffic lights
D - There's actually a roundabout and I took a wrong turn
E - Here I've made a U-turn after driving this God forsaken "road"
F - Just before the traffic lights I've noticed a 3.9 l/100km usage!
G - My joy didn't last long (damn traffic lights...)

So it is possible. I've done it. 4.0l, it's a 124km drive (according to the readings of the on-board computer) so it worth 25.25 PLN. By comparison taking a train costs arount 35PLN and takes 4 hours 30 minutes (if you're lucky).

Did anyone had similar experiences? Please share!

Sunday, March 13, 2011

Ext JS, RowExpander and Ajax

Ext JS contains almost everything a body needs. If there's some functionality that's missing there's always the option to create plugins to extend the core functionality.

For example there's a RowExpander plugin that allows you to create details for rows in a grid, which is very neat. The only problem with that is that it needs the details loaded with the parent row which (if you need to perform more exhaustive queries) is bad.

So after googling a bit I've came up with the idea of extending the original plugin so that it allows for loading content from an external source.

The original idea is taken from this old post.

The idea is simple: return a placeholder instead of template, load the content using an Ajax call and store the retrieved content in the placeholder once it arrives.

I needed this plugin to be able to process the content using template as well so there's the "mode" config option which can be set to 'html' (as-returned-by-server) or 'tpl' (which means that the tpl template will receive an object with 2 fields: parent - with the parent record data, and details - with the parsed object or list returned by server).

The example is here

And finally the sources can be downloaded from my Git repository here.

Monday, March 7, 2011

Grails, Tomcat and additional contexts

I needed to create a modular application today. It was nice and easy up to the point where I needed to make some ajax calls and due to the same-origin principle it was all failing miserably in development. In production, since both apps are on the same tomcat instance everything works just fine.

To be able to add more contexts to the very lonely tomcat instance fired by Grails create a _Events.groovy file in your scripts folder with content like this
eventConfigureTomcat = {tomcat ->
def ctxName = "secondary"
def ctx = tomcat.addContext("/${ctxName}", System.getProperty("java.io.tmpdir"))
def servlet = tomcat.addServlet(ctx, "my-servlet", new MyServlet())
servlet.addInitParameter("some-param", "some-value")
ctx.addServletMapping("/*", "my-servlet")
}


Here's some explanation:

1. This will bring up a secondary context called "secondary" (so you can access it under http://localhost:8080/secondary)
2. It will bind to all requests below this url to MyServlet instance

Using the HttpProxyServlet I'm now able to have the secondary applications available on the same tomcat instance:
import com.jsos.httpproxy.HttpProxyServlet

def map(tomcat, ctxName, port) {
def ctx = tomcat.addContext("/${ctxName}", System.getProperty("java.io.tmpdir"))
def servlet = tomcat.addServlet(ctx, "proxy", new HttpProxyServlet())
servlet.addInitParameter("host", "http://localhost:${port}/")
servlet.addInitParameter("uri", "/")
ctx.addServletMapping("/*", "proxy")
}

eventConfigureTomcat = {tomcat ->
println "Configuring tomcat"
map(tomcat, "first", 8081)
map(tomcat, "second", 8082)
}


Hope this will give you more flexibility doing stuff with Grails!

Sunday, March 6, 2011

Grails, Ext JS and desktop-like applications

I've just finished work on the Ext JS resource integration for Grails. This plugin's main purpose is to start coding Ext JS applications in no time.

An added bonus with this plugin is the distribution of the ux-all package (it comes in the package with Ext JS) as well as a slightly modified version one of the examples - the Desktop application.

So what I did to the Desktop example was to externalize the creation of desktop icons (it was really stupid to code all the icons directly in HTML) as well as the inclusion of ScriptLoader utility that allows to load additional scripts on demand.

If you'd like to give it a try there's a load of information on the plugin's page on how to create a desktop-like application in under 5 minutes :)

And if you'd like to get the sources here's the git repo with them: http://dev.aplaline.com/git/grails-extjs.git

The sources also contain some documentation (which is basically the one from plugin's page but dissected into smaller chunks for easier navigation). To generate the final version run grails doc in the main plugin folder and in a matter of seconds target/docs you'll find a ready to browse version of the documentation. I might put it online some day :)

Ext.Direct and Grails

I just started a new Grails plugin that will (in theory) allow Grails service's method to be called using Ext.Direct JavaScript client. Obviously I've looked at existing libraries to do this (the directjngine and extdirect4java) but as far as my taste goes they are not groovy enough. My thinking is that Groovy allows for typeless declarations for parameters and JavaScript does the same. So additional metadata (like annotations in case of the 2 engines mentioned above) should not be needed. All is there in the code already.

We'll see where this will take me :)

You can follow my endeavors on GitHub

Friday, February 25, 2011

RIA in Grails using Ext JS

I've been looking for quite some time now for a client-side library that'd allow for creation of rich internet applications (RIA). I've evaluated (however briefly) Flex and Silverlight at first but this is not what I've had in mind. I wanted something that will just work, without any sophisticated plugins that get outdated (Flash) or are not supported on all platforms (Silverlight).

Knowing what Google did to make real applications possible in the browser I've looked at GWT and again this is not what I've had in mind. I dismissed Silverlight and Flash for the reason that I wanted to code in JavaScript and have the whole power of that language at hand with the ability to use CSS and HTML as I please. Coding in Java is, how should I put it, a pain in the a$$. I personally hate this language and there's nothing that will change it. Oh, and btw this also means that all the frameworks that build on GWT (like Vaadin to name just one) fall into the same category.

And then I stumbled upon Ext JS. Man that was a surprise! First the object model baked in into the framework turned out to be perfectly suited for real-life applications. The GUI itself looks just gorgeous! There's a client-side part for direct communication with server (Ext.Direct) and a model for data access (the Ext.data.Store and descendants). But that's not all! There's a JsonStore that can be configured to work with RESTful JSON service!

As you might have guessed I'm using Ext JS with a Grails backend. Those two play so nicely together it's almost to good to be true but to make things even simpler I've created a Grails plugin called json-rest-api that taps into the domain objects and exposes them in a RESTful way for the JsonStore to consume.

Now creating an application with editable grid and all the usual CRUD functionality takes less than 5 minutes!

Check out the plugin here and the two examples here and here. You'll be amazed!

Stay tuned for more examples and tricks on using Ext JS with Grails!

Sunday, February 20, 2011

Creating desktop-like web applications

For the longest time there's been a gap between web developers and the so called desktop developers. Both branches used different tools and different techniques to get things done.

What has changed in the past couple of years is the web itself and what's being expected from web application. With the change that occurred in the field of RIA (Rich Internet Applications) many products that would previously just be a set of web pages turned into SPAs (Single-Page Applications), utilizing the power of Ajax, dynamic DOM manipulation and recognizing the full potential of all the 3 core technologies (HTML, CSS and JavaScript).

The Misery



With that in the picture it is worth taking a moment and looking at the options we have when creating a new application (be it a web app or a desktop one):

1. Operating system.



Nowadays if someone targets their product at one OS it means pretty much that they allow for competition right from the beginning. And not only allow for it but in a sense ask for it. Sure there's Java that compiles and runs on everything that has a CPU but talking seriously about creating an usable GUI in Swing or SWT is plain and simple asking for trouble.
Then there's .NET and Mono as its multi-platform solution. It all looks nice and dandy but the truth of the matter is that WinForms is still not 100% usable and with WPF in the game it's going to be very hard for Mono guys to keep up. So even though creating breath-taking and maintainable UI under .NET is feasible it narrows down the potential clients to Microsoft Windows.

2. Windowing System.



Platform (and operating system) aside there are some great windowing toolkits. WX, GTK (and even recently GTK#) and even SWT - those are all viable options and should be considered as candidates for new projects. There's only a couple of small issue with most of them: they look and feel different on different platforms and have very little support. Don't get me wrong here - with the right amount of desperation one can work out miracles with those - it's just not something that you'd take, toy around it for a day or two and start using it. WPF, WinForms and VCL included!

3. Maintenance.



Even though we developers don't like to think about what our application will do in 3 years and how those new features will be delivered to the customer it is vital for the continued success of our efforts that the deployment of new versions is as painless as it gets. Different platforms (and I'm talking managed platforms only here) have different solutions to this problem. On .NET you get the "Click once" capability which pretty much covers it. On Java you get the web launch capability which is close to the Click Once, but less feature-rich.

4. Access to developers.



Finally if you're so damn successful like Facebook or YouTube you might want to hire some new developers to do your dirty work. Choosing (or as I like to call it: closing yourself on) one technology means that the amount of people you can hire is not enough to find the right people for the job in a reasonable amount of time.

So it seems that all is lost and the fragmentation of what's available and what's struggling with the rest of the world brings an end to reason in software development.

The Solutuion.



As it turns out not all is lost. Let's examine what's available on the market to make our future more predictable.

Desktop Web Applications.



As weird as it might sound it is not science fiction at all. Right now with the capabilities of HTML5 this actually is a viable option.
Redeployment is as easy as it gets (just hit the damn F5 button and you're done), technology is already there (just take a look at Ext JS to know what I'm talking about) and the actual browser platform is getting faster and faster every day (look at Internet Explorer 9 and the direct hardware access they are doing - it's amazing!!! - not to mention Google Chrome and FireFox that kick ass since god knows when already).

If the flexibility of CSS+HTML+JS is not good enough (for whatever reason) and you just need to go all crazy there's Flash (and Flex), Silverlight and JavaFX. My personal choice would be Silverlight but that's mine .NET fetish talking :D

So what's the future going to bring? Who knows? But one thing is certain: After the web revolution that happened since Ajax the world is never going to be the same. The browser attack is not going to go anytime soon and in fact (with Google's Chromium in the picture) it might very well be that the OS to win users is not going to have the two whacky glossy panels or an animal but a circle in the logo.