Tuesday, May 17, 2011

Explorative Programming

Recently I've been struggling with a friend of mine about the actual meaning of Explorative Programming (or short ExP). The term Explorative is used here so that the pressure is put on the "exploring" part of it. I actually know it's not a proper English word but who cares :D

Anyways... Explorative programming - what is it all about?

Let's say you need to write the infamous "Calculator" class with the (also infamous) "add" method. In this particular case there's little to explore, right? You know (hopefully) that 1+3 equals 4 and there's nothing in the whole universe that will convince you otherwise :)
Let's explore this simple case in a little more TDD/ExP way. In TDD you'd write a class called (let's say) CalculatorTest. In that class you'd write a method with a @Test annotation (if you're using JUnit). That method would then instantiate your Calculator class (which does not exist yet) and call the method "add" (which does not exist yet) with some well known parameters that produce a well known output.

All good so far, meaning your test does not compile :).

So what you do next is that you add your class (preferably by means of some IDE's refactoring or what have you), then add the method (same as before - you actually can do this manually but who would do that when Eclipse and all the others are there to help you), fix the parameters (didn't you just write "def" to declare your variable to state that you don't really care what the fricken type is?) and finally after all those IDE-enabled steps (which let's be honest didn't take all that long) your test is compiling but failing. Great success!!!
The next obvious thing is to add the simplest thing possible which is to return the proper value from your function, then to add next test, see it failing, add the proper logic to satisfy both test cases.... you get the idea. This is TDD in its purest form as described by many mentors out there.

Where ExP is different is where the IDE needs to come in. Why for the love of god do you need a sophisticated tool to create a file for you when you actually don't need it? What you need is a class - not a separate file!

In ExP I propose that what you do is you do have the class (or method) you're testing but in the same file as your assertions. So for example:
def add(a, b) {
//the actuall implementation whatever it is goes here
}

assert add(1, 2) == 3
assert add(4, 5) == 9

Ain't that nice? You can sort of "explore" your domain "in place" without the need to have a sophisticated IDE to create unnecessary files for you (you're just creating a method for all you'd care at this point).

Let's examine a slightly more proper example for the actual "explorative" part of the thing, shall we?

Let's assume you're not alone in the world, which is a pretty good assumption in my personal opinion. Next let's assume you didn't write the code you're working on all by yourself (which in my personal experience is more than likely, or actually it's taken for granted if you add to the fact that you didn't write Spring or Hibernate all by yourself).

What you can't do at this point is to wave a magic wand and say "let it do what the client wants!". Instead what you can do is the next good thing which is to say "let's see what the user actually wants". And in the spirit of ExP what you'd do is to spawn a console of some sort within the currently latest version of your application (hello rails and grails!), sit down either alone (with tons of useless documentation trying to make your way thorough it) or with your actual customer, code 5 or 10 lines, write the assertions when the user says that it is the desired result.

Once you have it all figured out there's tons of knowledge that you better store for posterity! To do that you move the necessary bits and pieces where they belong, cutting your assertions in oh so many places into separate methods that'd satisfy your good taste for testing a single thing at a time and of course moving the class you just wrote to a separate file. From this moment on it's TDD only, my friend. You've got to keep'em separated.

Oh, and before I forget: the best thing in all this is you can actually use your eyes and brain to interpret the imperfect results before writing any assertions. Let's be hones: the computer (or the assertions rather) are only as good as you wrote them. If you already know what the actual outcome is (like in the case of some tests you might find on the net) it's fine to do it the old fashion way with separate tests and all that. What you do have to take into consideration while working with real life stuff is mostly the imperfect steps in between and how you can first spot them with sort of debuglets (I love this term!) or other means like the outcome of your script which is a debuglet all along.

I hope I've shed some light into this ridiculous term that I've tried to coin. Just as a word of caution: it might make no sense whatsoever when applied to your situation. What I strongly believe in is that it gives you the possibility to use simple text editor to do the TDD kind of thing with more freedom and options to choose from. That being said I officially declare that I don't use an IDE for anything and I mean it. IDE is bad for oh so many reasons I won't even go there. They make you a cripple when you need to actually come up with an idea of your own!

Oh! and before I forget: Once you've moved stuff back to where they belong you need to "integrate" your class with the rest of the system. That's where the regular TDD, IDEs and all kind of stuff that's floating around comes in.

Would you believe this all came to be from a single conversation with a supervisor that didn't actually end up being my supervisor at all? Funny how things play out...

There's a number of people that have advocated this style of programming. Recently on the 33rd degree conference Dierk Koenig showed how you'd go about writing your quicksort implementation. Previously Luca Bolognese showed the same kind of approach while presenting F# to the audience. It's all about learning, exploring and happiness :)

Have fun!

Sunday, May 15, 2011

The easy but you have to know it...

Yeah, this is the one that is probably make me look like a complete newbie. I've been developing apps in Ruby mostly on Ubuntu but lately I've turned back to Windows and found out that on this platform not everything is taken for granted like it was on Ubuntu. So for that reason I'm making this note so that I don't forget.

If you're installing Ruby gems on Windows and they require compilation of some native extensions (like the JSON thing or Thin) then you need a C compiler to do that. In fact you can do that in multiple ways but there's one easy one that'll take the pain out of your neck in seconds.

What you do is you download the DevKit package, un-7zip it (it's actually a self-extracting archive so that's easy), cd to the directory where you unzipped it and execute the following 3 commands:
ruby dk.rb init
ruby dk.rb review (and make sure your Ruby installation is at correct place)
ruby dk.rb install
With that in place test it by issuing for example:
gem install json
and you should see the following
Fetching: json-1.5.1.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
Successfully installed json-1.5.1
1 gem installed
Have fun!

Wednesday, May 11, 2011

Cross-origin resource sharing

Today I've stumbled upon quite an interesting topic while watching this video from MIX11. Around 40th minute Giorgio Sardo brought up the topic of cross-site Ajax calls. At first I thought "OK, yet another proprietary extension in IE9" but then I started googling and what I found (and subsequently here) has exceeded my wildest expectations. As it turns out it is implemented in all modern browsers. There's a catch to it: the server needs to be aware that such a request will come and attach a specific HTTP header in response.

Since I don't do well with plain text explanations I've put together a Sinatra client and a Rack server to demonstrate this feature.

Client


This is the client code using jQuery's load method to do the Ajax heavy lifting:
$("body").load("http://localhost:9292/"); 

No surprise there, right? It looks like a regular Ajax request. The difference here is that it queries localhost on a different port (9292 - default Rack port) than the one that the request originated from (4567 - default Sinatra port).

Server


Let's see how we can respond to such a request using a pure Rack application.
run lambda { |env| [ 
200, 
{ 
  'Content-Type'=>'text/html', 
  'Access-Control-Allow-Origin' => env['HTTP_ORIGIN'] || '*',
  'Access-Control-Allow-Headers' => 'X-Requested-With'
}, 
env.inspect 
] }

The key here is the line that defines the Access-Control-Allow-Origin response header. When it's set to * it means that everyone can access this resource from everywhere. It's a sort of catch-all if you will. The value of env['HTTP_ORIGIN'] is the value sent by the browser to the server saying "Hey, I'm calling from here. Can I access your resource please?" And if the server agrees (which it does from everywhere at this point) the browser will honor the response and return the data back to the script. The Access-Control-Allow-Headers header is sometimes required for example by ExtJS' Ajax calls.

Here's the example for your convenience. You can start it by issuing the following commands:
cd server
start ruby -S rackup
cd ..\client
start ruby client.rb

After that open your favorite browser (I sure hope it's not IE < 8), navigate to http://localhost:4567 and observe the environment string from the server dumped onto your screen.
The client is obviously written in Sinatra, the best micro web framework ever and the server is a pure Rack application.

Sunday, May 8, 2011

Going ultra simple with JRuby, Tomcat and Rack

This one is for those that don't believe that web applications can be written in a single line :D With Rack and Warbler it is actually possible :D

First install the prerequisites and required gems as described in this post.

Create a file called config.ru with the following content
run lambda { |env| [ 200, { 'Content-Type'=>'text/plain' }, "Hello World!\n" ] }

Then run warbler like this:
jruby -S warble
... and deploy the resulting war file to tomcat. DONE!

Oh! And in case you were wondering: the performance of this application is just incredible! On a virtual machine with 2 cores at 2.19GHz each and 512MB RAM (Dell D830 laptop) it delivers around 2000 requests per second!

Ruby rocks!

Running Sinatra application on Tomcat

My recent fascination with the Sinatra framework has made me look for viable deployment options. At first I started with JRuby and Tomcat6.

So what do I need (top-to-bottom with only JDK installed) to successfully deploy a hello-world style application on Tomcat?

Installing prerequisites.

First you need a copy of JRuby and Tomcat6. Unzip both of them some place you'll have easy access to. In this tutorial I'll be using the root of drive C: because this is a folder everyone will have.
After doing so you should have 2 folders on your disk with the appropriate applications:

C:\apache-tomcat-6.0.29
C:\jruby-1.6.1

Installing required gems:

  C:\jruby-1.6.1\bin\jruby -S gem install sinatra sinatra-reloader warbler bundler

Creating the application.

Creating the hello-world style application (a bit extended version) is really simple. First you create a folder in a place of your choosing. In that folder you just need to create 2 files:

hello.rb:
  require 'rubygems'
require 'sinatra'
require 'sinatra/reloader' if development?

get '/' do
@message = "Hello, world! from Sinatra running JRuby on Tomcat"
erb :index
end

views/index.erb
  <h1><%= @message %></h1>
The hello.rb is the main application file and views/index.erb is the supporting view.
All further command line commands must be executed from the folder you've just created!

Testing if everything works as expected.

To test if the application performs as expected issue the following command inside the folder you have created:
  C:\jruby-1.6.1\bin\jruby hello.rb
and if there are no errors you'll have the application available on port 4567 right away. Test it with your favorite browser navigating to http://localhost:4567

Creating deployment files.

This is a little bit more involving. We'll create 3 files now:

Gemfile:
  source :rubygems
gem "sinatra"

config.ru:
  require 'rubygems'
require 'hello'

set :run, false

run Sinatra::Application

config/warble.rb:
  Warbler::Config.new do |config|
config.dirs = %w(views)
config.includes = FileList["hello.rb"]
config.jar_name = "hello"
end

The Gemfile is just the definition which gems are required by the application. The config.ru file defines how rack application is supposed to start. config/warble.rb is to tell the warbler tool which files go into the actual WAR file and what the output file name should be.

Create hello.war

To create the output hello.war file issue the following command:
C:\jruby-1.6.1\bin\jruby -S warble
At this point you should have the hello.war file ready for deployment.

Deploying to Tomcat.

This is the easy part. Simply copy the file hello.war to C:\apache-tomcat-6.0.29\webapps and start Tomcat.

Conclusion

It wasn't that hard, was it? Next time we'll see how we can do exactly the same with Apache HTTP Server and Passenger (a.k.a. mod_rails) running on a freshly installed Ubuntu 10.04.1.

And last but not least (because I know you've been waiting for it): the source code is obviously here for your entertainment :)

Have fun!

Edit on May 9th, 2011
I found out why specifying gem dependencies in config/warbler.rb doesn't work as it should and why the workaround with Gemfile is needed. Obviously for any bigger project it is better to use Gemfile and bundler as it solves the problem when another developer jumps in and wants to start hacking the application. For simple projects the config/warbler.rb should be enough though. Up until now specifying gems didn't work as expected due to an obvious bug in warbler. Here's a fix that I proposed to solve the problem:

https://github.com/padcom/warbler/commit/b4b24e17dee5bb98525203c82519a8901874ef81

With that in place the config/warbler.rb would look like this:
Warbler::Config.new do |config|
config.jar_name = "hello"
config.dirs = %w(views)
config.includes = FileList["hello.rb"]

config.gems = ["sinatra"]
end
and naturally the Gemfile would go away (one file less to keep track of :D in such a simple application)

Friday, May 6, 2011

Sinatra - the power is back!

I honestly don't know what I've been doing for the past 5 years. Really. I've been trying to solve simple problems by learning "corporate" bullshit allowing the really good stuff go unnoticed. But that has changed. I'm a new person now. I'm changed, changed forever.

Today I've been working on my json-rest-api plugin to allow for excluding some properties if you really wanted to. Ok I admit, I needed that in the application I'm writing for the corporate. And so I've been browsing, looking, searching because I somehow knew that something is wrong with the world around me. I couldn't quite tell what it was but it felt like a splinter in my mind that I just couldn't get rid of.

I've stumbled upon Graffiti micro-framework for building web applications in Groovy. I was so amazed with the immediate results it gave me that I started digging deeper and deeper into the roots of where this damn thing came from. And so I found Sinatra...

And the world will never be the same...

Sinatra is a micro-framework giving a DSL style to create simple web applications. Since I'm a windows addict (don't shoot me please!) I needed to find something else than shotgun to do the hot reloading of my app. And so I found sinatra/reloader...

And the world will never be the same...

Fortunately Sinatra is not married with any ORM so I could find a tool that's equally simple and does the heavy lifting of moving data from and to the database for me. And so I found DataMapper...

And the world will never be the same...

At this point I'm after re-writing the ihatejsf.com application (34 lines of Ruby code!!!) using Sinatra and DataMapper and after deployment on Heroku. It was an experience I'll never forget. It has changed me forever and ever.

I'm a changed man.

Wednesday, April 27, 2011

Running test on a live test instance of the application

I've faced a unique challenge today: test my json-rest-api plugin. Unique, isn't it? :D

The problem


Well all would be really easy if not for the fact that I wanted to do an end-to-end test just like a client would use the generated API for a domain class. In this case using integration tests was out of the question because the actual http listener is not running. And I wanted to test it all the way through...

After some digging I've found out that there's a number of plugins that provide this kind of functionality where the application starts and tests are being executed. Those plugins are functional-test, easyb-acceptance-test and webtest.

The unhappy seeker


I've tried to play around with those and one thing struck me down the most: they are mostly targeted at testing web pages and not RESTful services. So they were not as nice for my case as I'd like them to be.

What made me wonder though is that all of those plugins do exactly the same thing to actually spawn the application before tests:
eventAllTestsStart = {
if (getBinding().variables.containsKey("functionalTests")) {
functionalTests << "functional"
}
}
Obviously different plugins use different names for their tests but the idea remains the same. So I asked myself what would happen if I'd introduce the same thing in my scripts/_Events.groovy without those plugins installed... Much to my surprise Grails build system started the application right after spitting out "Starting integration test phase ..." :)

The road continues


Now all I had to do was to find out a nice way to actually get the test going. As it turns out there's a fantastic utility called HTTPBuilder that does exactly what I needed! Just a couple of lines in BuildConfig.groovy later...
test('org.codehaus.groovy.modules.http-builder:http-builder:0.5.0') {
excludes "commons-logging", "xml-apis", "groovy"
}
... and testing RESTful services has never been easier!
import groovy.util.GroovyTestCase
import org.junit.Test

import groovyx.net.http.*

class RESTfulTests extends GroovyTestCase {
@Test
void willReturnErrorIfSavingNotSuccessfull() {
def client = new RESTClient("http://localhost:8080/example/api/")
def response = client.post(
path: "person",
body: [ data: [ firstName: 'John', lastName: 'Doe' ] ],
requestContentType : ContentType.JSON.toString())
assert response.status == 200
}
}

If you want to see a more sophisticated usage of this approach with more assertions I encourage you to take a look at the basic application in json-rest-api-examples project.

The obvious


One note to add: if you'd like to run those integration tests as part of your continuous integration it'll most probably fail because the port is already in use by Hudson... To overcome this issue set the server.port parameter to some port value that's not in use. In Hudson it looks more/less like this:



Obviously you need to use this port in your tests instead of the default 8080...

Have fun!

Sunday, April 24, 2011

Hosting a Git repository on Ubuntu 10.04 LTS

I thought I'll post this recipe for anyone having issues hosting their own Git repos via HTTP. It's not that hard but a few things have to happen to actually make it work.

The automated way


If you're as lazy as I am here's a script that you can just download, execute as root and all the steps below will be executed for you. This means that you can turn a plain vanilla Ubuntu 10.04 installation into Git host in a matter of seconds:

https://gist.github.com/1034042 (raw)

The manual way


Install packages

sudo apt-get install apache2 git-core gitweb

Create folder to host your repositories

sudo mkdir /srv/git

Configure Apache to serve the repositories

Add the following to /etc/apache2/conf.d/git (create the file if it does not exist!):

# GIT
SetEnv GIT_PROJECT_ROOT /srv/git/
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAliasMatch \
"(?x)^/git/(.*/(HEAD | \
info/refs | \
objects/(info/[^/]+ | \
[0-9a-f]{2}/[0-9a-f]{38} | \
pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
git-(upload|receive)-pack))$" \
/usr/lib/git-core/git-http-backend/$1
ScriptAlias /git /usr/share/gitweb/index.cgi

<LocationMatch "^/git/.*/git-receive-pack$">
AuthType Basic
AuthName "Git repository"
AuthUserFile /srv/git/users
Require valid-user
</LocationMatch>

Create a link to gitweb.js

This step is required so that the proper javascript library is available for GitWeb:
sudo ln -s /usr/share/gitweb/gitweb.js /var/www

Create users file

sudo htpasswd -c /srv/git/users john
and type the password twice

Create script to create repositories

Create a file /srv/git/create.sh with the following content:
#!/bin/sh
git init --bare $1.git
cd $1.git
git config http.receivepack true
git config gitweb.owner "John Doe"
cd ..
chown www-data:www-data -R $1.git

Use the create.sh script to create a repository

cd /srv/git
sudo sh create.sh example

Update path to repos for gitweb

In /etc/gitweb.conf update the $projectroot to:
$projectroot = "/srv/git"

Restart apache

sudo invoke-rc.d apache2 restart

That's it! you should now be able to clone and browse your repository using the smart HTTP transport!

The outcome


Remember that when you use this setup you'll have a Git repository that than be cloned by everyone but only users specified in /srv/git/users can push to this repo! This means that you'll have to provide username in the URL like this:
http://john@myserver/git/example.git

If you don't do that you'll see the following error while trying to push:
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 1.05 KiB, done.
Total 3 (delta 0), reused 0 (delta 0)
error: RPC failed; result=22, HTTP code = 401
fatal: The remote end hung up unexpectedly
fatal: The remote end hung up unexpectedly

Hope this will save someone some time :)

Saturday, April 23, 2011

I'm finally a Git convert

Well, yes... It happened. I don't know how or when but it just did. I'm now officially a Git convert. Period.

For those of you who know me you might also know that I was a huge Subversion advocate. This was also seen in the role of Subversion trainer that I inherited after a friend of mine in my current company. I was teaching and preaching SVN for the better part of this millennium. So why?

Well, there are a couple of things that distinct SVN from Git and the distributed/centralized part of it ain't the most important one. There are some key features that come out of the fact how Git works but they could work in Subversion OK if this damn thing wouldn't be so eager to use the fricken network... In this case the distributed part of Git is not so much about giving commit access to nobody besides me. It's also not about the "I can commit when I'm on the plane". I mean, come on, geeks! How many of regular developers do the coding at FL300 to avoid wiretaps? :D

The best part of Git is speed. I mean speed as in fricken space rocket! Like it or not in a corporate environment there's always going to be this "everybody on the team has commit access" thing and nothing is going to change that. Get over it! The best thing about Git is that it works just as good in a centralized environment as Subversion or even better, faster, lighter, jezzier, sexier and all that :D

One more thing: setting up Git server to host the one true version of the repository in a centralized environment is so damn easy! There's like 10 different ways with the one truly blessed prescription (Smart HTTP Transfer). Just go google it. It's amazing how incredibly fast it is! And it plays so nicely with Trac and Redmine!

Now that I have that out of my chest I'd like to say a few words about other counterparts: Bazaar, Mercurial, Monotone

If you're still using Bazaar - I'm so sorry you didn't see the light yet. There's definitely a guy sitting next to you that already knows what this Git thing is so you better ask him before you'll be left alone in the dark.

Hail to all the HG users! You're also great :D But not as jazzy and sexy as the Gitters :D

Monotone... Well.. letsnotbetohardonthoseguysastheyareprobablytheonesthatwrotethisthing...

Thursday, April 14, 2011

I still hate JSF - but a little less :D

If you know me you also know this for a fact: I hate JSF with a passion! There are no bad enough words that you could use to express the hatred I feel. What can I say - I'm broken this way...

Today I finally gave JSF2 a spin. This time I already knew that working with Java EE is a pain in the neck so I've decided to use my usual environment (Grails) to do the test.

JSF / JSF2 has some major drawbacks out of the box. It's heavy and it's a misery to introduce changes (again, out of the box). Sure you can use like JRebel and stuff to make your life easier but then JRebel costs money and if you're just playing with the technology with no apparent reason spending any money is just not justified at all.

So back to the topic: JSF2 on Grails. Sounds, scary, right? Yes, I've got the goosebumps too :)

What's in the box? (cited after http://grails.org/plugin/jsf2):

- Bean By convention (no need for faces-config)
- Bean access to controller parameters (session,request,params...)
- Beans dynamic methods for navigation redirect(action?,bean?,view?,uri?,url?) and - render(action?,view?,bean?),
- Automatic bean and service properties resolution
- - 'void init()' called at bean initialization if present
- 'void dispose()' called at bean destruction if present
- JSF Extended request scope ('view')
- Access to web.xml configuration via Jsf2Config.groovy
- Access to faces-config generated in web-app/faces-config.xml
- Converters for Locale, Closure, Currency, Timezone
- i18n ready, fast access with #{m['key']}
- create-bean script
- Hibernate session managed from view rendering to view response
- Execute groovy code in EL expression ( #{gtag.groov[' def i = 2; 3.times{ i++ }; i; ']} )
- Support JSF2 Components - @ManagedBean ...

And so my test went like this. I've installed the plugin the usual way, I've create a class called StatusBean (in a .groovy file!), a little bit of copy-paste from the plugin's page to figure out the signature of an event listener did the heavy lifting for me, I've checked the FAQ section to see where should I put my xhtml files and it all worked right out of the box!

Well, I'd definitely NOT go as far as saying that I actually like JSF2. Hell no! I despise it just as much as I did before. I do however recognize that there's world outside of GSPs and SiteMesh and the alternatives are good for some things.

Sunday, April 10, 2011

Quicksort - the groovy way

I must confess: I didn't write quicksort by myself ever. I always felt it's one of those things that I don't really have to do. Well, Dierk Koenig showed that it might be useful to write your own implementation of quicksort. Not for regular use but for training purposes.

You all know the Collections.sort method, right? We all know it's damn fast, well tested and all that stuff. Let's see what does it take in Groovy to implement quicksort ourselves:
def qs(list) {
if (list.size() < 2) return list
def pivot = list[0]
def items = list.groupBy { it <=> pivot }.withDefault { [] }
qs(items[-1]) + items[0] + qs(items[1])
}

WTF? 4 lines plus function definition? And it can easily be shrunk to 3 lines by inlining the pivot variable:
def qs(list) {
if (list.size() < 2) return list
def items = list.groupBy { it <=> list[0] }.withDefault { [] }
qs(items[-1]) + items[0] + qs(items[1])
}


One of my colleges told me that before he saw the implementation of quicksort in Groovy he didn't quite get the idea, what it does and all. Same here :) I never tried to dig into the details, I just accepted that it works. But with this implementation I just see what's going on! There's nothing to understand, just 3 lines to read :D
By comparison an implementation of bubble sort is around 10 lines long and is about 100 times slower :D
If you're wondering how does this implementation perform by comparison to the Collections.sort() method here are the results (sort a 100 elements list 1000 times):

def qs(list)Collections.sort()
1938ms19ms

The actual timing varies but the ratio more/less 100:1 sticks.

It's interesting to note that there's a 10 times difference in timing when invoking list.sort() and Collections.sort(list) in favor for the latter one. That's probably due to the dynamic stuff going on in between.

Saturday, April 9, 2011

33rd Degree - best place to be (EVER!!!)

Hi there folks!

I've just returned from the 33rd Degree conference and I simply need to share the positive energy I've brought home with me.

Before we start please bear in mind that the 3 days of the conference were packed with best-of-breed talks and since there's only one of me (I soooo wish this wasn't so) I was able to see only a small portion of all the great materials and performances available there. I'm sure your opinions might vary depending on what you attended :)

First of all - a fantastic surprise! I've been kind of skeptical about Dr Venkat Subramaniam which I should never ever, ever EVER have been to. This was one of the most brilliant, encouraging, humorous and inspiring talks I've ever seen on polyglot programming! Venkat, I hope you'll not forget your exercises again :) That was an absolutely brilliant metaphor to TDD. In overall it was the best source of ideas to encourage people to learn I've ever encountered! Best talk of the conference in my opinion!

Then there was the forever funny and painfully cynic Ted Neward. I just love the way he presents his ideas like a preacher in the church of St. Programmer :) He's just the one and only! Ted, great talk about the unanswerable question and believe me even if someone didn't speak Scala your talk on design patterns was still the place to be :)

There was also the best polish speaker, Sławek Sobótka with his deep-zoom like presentation about layers in CqRS architecture. Sławek: you rock! And pleeeease! Don't put the architect label on t-shirt :) Don't ever turn over to the dark side!

I must also say there was one talk that threw me completely out of balance. That was the talk by Dierk Koenig about ULC (by Canoo) framework. To give you a little bit of background here I'll tell you this: I'm usually fond of "pure" solutions. This means that mixing Flex/Flash, Silverlight or desktop Java with web development doesn't seem like a good idea to me. There's tons of stuff that you can use HTML5 for and you don't need any extras on top of the browser to make things work. That's the main reason I'm such an advocate of jQuery, its UI framework and above all the Ext JS library. Nevertheless the ULC framework by Canoo has proven itself to be a viable alternative to plain-browser design in cases where more sophisticated interoperability with client's environment is required. That includes for example interoperability with peripherals other than localStorage, sessionStorage and the other ones provided by default by HTML.
Dierk also gave a very nice and pretty advanced talk on programming in Groovy (my favorite language!). Great stuff! And very very inspiring!

Hamlet D'Arcy gave talked about ways to remove boiler plate code from your sources so that you can write more conscious code. Really good stuff, man! Thanks for the enlightenment! And thanks for bringing Groovy to the mix :) Groovy rocks!
I've also had the opportunity to exchange some thoughts with Hamlet about the applicability of the ULC toolkit. He's been extremely helpful in helping me understand the business needs this toolkit tries to address. Thanks for sharing!

If you're into the psychology kind of things then you're absolutely bound to visit Nathaniel Schutta's talk about hacking your brain. Man, that was really awesome to see so obvious and ridiculously simple ideas put into familiar context to which I could relate as a simple developer.

And last but not least: if you're into "the latest and the greatest", or in other words if you're like me striving to make your other colleagues look corny, then the talk that Jarek Pałka gave on NoSQL databases and their applicability in solving certain challenges was definitely the one to be at. Jarek is known for his specific humor and to-the-point observations he makes. This time was no different and quite frankly I've expected nothing less from this guy. Great appearance, man!

Overall this was a time very well spent. I encourage you to visit this conference next year - you'll be absolutely amazed!

I'd also like to congratulate Grzegorz Duda, the mastermind behind all of this on putting together by far the best conference ever!

Friday, April 1, 2011

Grails gotcha - controllers with same name in different packages

Well... it's tough, isn't it? You get a controller with the plugin you use and this damn controller has the same damn name as your controller. And this name is just perfect for what your controller does...

I've just found myself in this kind of situation. I asked on the Grails users group and found out only what I already knew - those damn classes have to have different names. Period.

But I'm the wild one. I need to bend software to my will. I just have to do it :D After a couple of hours of reading Grails sources and experimenting with Grails console (fricken best thing EVER!!!) I came up with this:
class BootStrap {

def grailsApplication

def init = { servletContext ->
grailsApplication.getArtefactInfo("Controller").grailsClasses.each {
it.uri2closureMap.put('/' + it.fullName, 'index')
it.uri2closureMap.put('/' + it.fullName + '/', "index")
it.uri2closureMap.put('/' + it.fullName + '/index', "index")
it.uri2closureMap.put('/' + it.fullName + '/index/**', "index")
it.configureURIsForCurrentState()
}
}

def destroy = {
}
}
With this I can use fully qualified class names in my UrlMappings.groovy!

And as usual life is good again :D

Tuesday, March 29, 2011

Ext JS/Core and formatting date for Grails backend - reloaded

Here's the ultimate solution for modern browsers (works in FireFox and Chrome, there's a fix for IE):
Ext.util.JSON.encodeDate = function(o) { return JSON.stringify(o); }
I feel this needs some explanation :D So here it is:

The JSON object in modern browsers is a serializer/deserializer for JSON format. It uses all the necessary stuff (like the UTC version of date methods and adds the "Z" at the end denoting that this in fact is a Zulu time).

What about Internet Explorer? Well... There's this "fix" for this browser called json2.js. You can download it at https://github.com/douglascrockford/JSON-js/raw/master/json2.js.

The beauty of this solution however lies in the fact that it's an extremely fast solution because the browser's manufacturers are in fact able to provide an optimized version of the stringify and parse methods!

Ext core, Grails and formating dates

In my previous post I've show how to provide the Grails date JSON deserializer with proper date format from Ext JS. This time we're going to look at the same problem but with a solution that also works for Ext-Core:
Ext.util.JSON.encodeDate = function(o) {
function pad(n) {
return n < 10 ? "0" + n : n;
}
return '"' +
o.getUTCFullYear() + '-' +
pad(o.getUTCMonth() + 1) + '-' +
pad(o.getUTCDate()) + 'T' +
pad(o.getUTCHours()) + ':' +
pad(o.getUTCMinutes()) + ':' +
pad(o.getUTCSeconds()) + 'Z"';
}

This function performs much better and uses methods exposed by JavaScript's Date object instead of custom Ext JS date operations. I'd suggest this function instead of the previous one for both Ext-Core and Ext JS.

ExtJS, Grails and dates

Recently I've been made aware of a problem with interoperability between ExtJS and Grails in regards to date serialization. Here's the deal:

1. Grails expects the date to be UTC
2. Grails expects the date to end with a 'Z' (denoting that it actually is UTC)
3. ExtJS by default when parsing date thinks just concatenates the year, month, day, hour, minutes and seconds and does no other processing.

To change this behavior and make it work across different time zones right out of the box simply provide the following implementation of Ext.util.JSON.encodeDate:
Ext.util.JSON.encodeDate = function(d) {
return d
.add(Date.MINUTE, d.getTimezoneOffset())
.format('"Y-m-d\\TG:H:i\\Z"');
};

That's it when it comes to passing dates from client to server. More on serializing date on the server next time!

Friday, March 25, 2011

Ext JS licensing - BE EXTRA CAREFUL!

It looks like the licensing terms for Ext JS library (as stated in an email conversation with Ted Driscoll) are for developers and for the once you bought is only. There would be nothing unusual about this but you have to understand that if a developer leaves the company then he TAKES THIS LICENSE WITH HIM and in best case scenario you're around $329 lighter if you hire a new developer.

Here's the conversation I've had with the said VP:

Me

I'd like to buy 2 ExtJS licenses for developers in my team. There is however a
question of what will happen if a developer leaves the team and someone new is
hired? Will the license stay with the team or with it move away with the developer?

Ted Driscoll

The license is assigned to a specific user, so you can not move them.  
they are not floating. Hope this helps

Me

Does this mean that when a developer leaves the company he leaves with the
license?

Ted Driscoll

No,  but no one else can use that license.  Now having said that, if you ask
once, we will allow it, but if this happens more then one ,then we will not.
Our licenses is are per named developer
Like I said - you need to know what you're getting into. Ext JS is a fantastic library but the licensing terms are just cruel for companies willing to make software with it. Especially in today's world where developers are changing jobs often which is not only normal but also desired to attain high technical skills.

Wednesday, March 16, 2011

OOP and OOA - the new age

How come programmers have advanced so far in the evolution that the architects that lead the overall design have been left so far behind? How come that they teach and preach the same stuff their grandpa would (if he'd be still alive)?

It might seem like a strong opinion to you. Tough one if it does. You might be one of the architects that I mentioned above.

So what's the deal?

Let's examine two of the many OOP techniques at our disposal: Inheritance and Composition.

Inheritance (quoted after Wikipedia):

"In object-oriented programming (OOP), inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects. In classical inheritance where objects are defined by classes, classes can inherit other classes. [...]
The inheritance concept was invented in 1967 for Simula."

Composition (again - from wikipedia):

"In computer science, object composition (not to be confused with function composition) is a way to combine simple objects or data types into more complex ones. Compositions are a critical building block of many basic data structures, including the tagged union, the linked list, and the binary tree, as well as the object used in object-oriented programming.
Composited (composed) objects are often referred to as having a "has a" relationship."

So it's dead simple, right? Son is his father's child but a car has wheels and eingine, right?

So please anyone tell me why the F so many enterprise-grade systems are created in a monolithic way? For haven's sake the 80's are gone, it's the year 2011!.

And don't you go about telling me that the technology isn't there yet! Oh it's there. In fact it's been there for a couple of years now. Starting with the first servlet containers!

So you'll say that creating a system that shares common security configuration (and thus uses some sort of single-sign-on feature) is not easy? Wake up, dude! It's been ready for ages!

So maybe it's the build time that's causing you to lean towards a monolithic design? Goddamn it! Is it really faster to build and deploy a 200MB War file than to build just one single part of it and dynamically restart the context?

Ok - I get it. You still need to cover your part of the deal, right?. If everything would be so dead simple you'd not be needed at all and that'd mean that you'd be without work. So who the hell would control what parts would need to emerge for the whole system to work??? Well, we (programmers) would't have to finally do your job.

All and all my point is: keep it simple and small.

PS. I hope you did get the irony :D

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.

Saturday, January 29, 2011

Moving to new server...

Due to the move of server from one physical location to another resources on this page (meaning examples linked to posts, etc.) will not be available until some time next week. Sorry for the inconvenience.

Tuesday, January 25, 2011

Setting up SQL Server 2008 Express to work with Grails

If you'll find yourself in need to configure a SQL Server database connection for your next grails application and you'd like to use the Express edition for development here's how you can do it:

1. Change login mode:
a) open regedit
b) navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQLServer
c) change the value of LoginMode from 1 (windows authorization) to 2 (mixed mode)

2. Enable TCP/IP connectivity:
a) open the SQL Server Configuration Manager (this one comes with the SQL Server itself)
b) click on "SQL Server Network Configuration / Protocols for SQLEXPRESS
c) right-click on TCP/IP and select "Enable"
d) double-click on TCP/IP, go to second tab (IP Addresses"), scroll down to the bottom of the property list and in the "IP All" section fill in the blank "TCP Port" with 1433

3. Restart SQL Server:
a) select SQL Server Services
b) in the list to the right select SQL Server (SQLEXPRESS)
c) right-click it and select "Restart"

The next step requires any form of ability to execute SQL statements against the server. What I used was the Visual Web Developer 2010 and its "Database connections" view.

4. Enable "sa" user:
a) ALTER LOGIN sa ENABLE
b) ALTER LOGIN sa WITH PASSWORD = ''

5. Create a database:
a) Create database your_database_name_here

6. Download and install the JDBC driver for Microsoft SQL Server (you just need the sqljdbc4.jar in your lib folder)

7. Configure your DataSource.groovy:
driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
dialect = "org.hibernate.dialect.SQLServerDialect"
url = "jdbc:sqlserver://localhost:1433;databaseName=your_database_name_here"
username = "sa"
password = "your_password_as_defined_in_4b"

With all that in place everything should work just fine :)

You might wonder why I've decided to mess around in windows registry instead of using proper tools (like the SQL Server Management Tools or whatever..). The answer is quite simple: It felt like a total overkill to install this humongous tool just to set one property :)

Saturday, January 22, 2011

Handling exceptions in Grails

If you ever find yourself in need to provide custom exception handling in your application but you need to do more than just show the view (send an email for example) there's a really simple way to do it. It consists of 2 steps (of which the documentation mentions only one and remains completely silence about the other one):

1. Create a mapping in UrlMappings.groovy:
"500"(controller: 'errors')

2. Create a controller that will handle it:
class ErrorsController {
def index = {
def exception = request.exception
// do some processing here
}
}


It's very unfortunate that the request.exception is not mentioned in the right spot in Grails documentation. It leads to confusion that the actual exception instance is available only in views which is clearly not the case.

Friday, January 21, 2011

Grails FAQ

This one you definitely have to look at :)

https://fbflex.wordpress.com/2011/01/11/grails-interview-questions/

Not because you're looking for a job, not because you recruit someone... simply because it's good stuff and you most likely don't know all of it by heart.

Thursday, January 20, 2011

Why JSF is the most used framework

Today I've stumbled upon an article supposedly written by one of the developers of JSF standards.

http://www.zeroturnaround.com/blog/ed-burns-on-why-jsf-is-the-most-popular-framework/

Before I'll go any further I need to make it clear: I hate JSF! Yes, this is my personal opinion, my personal choice. I don't encourage you to do so as well - maybe it's good for you and saves you tons of hours of work - it didn't do so for me.

I'd like to point out that JSF is in my opinion a failed attempt to reproduce ASP.NET WebForms technology. Where WebForms succeeded (component market, easy design, easy event-driven programming model) JSF has failed miserably...

JSF is neither easy nor standard (if you look from a broader point of view and not keep your head in the Java box). I think JSF has the one and only advantage that allows inexperienced web application developers to stay inexperienced and not use core web technologies (HTML, CSS and EcmaScript). This is the reason why JSF is so damn popular - and so damn evil. Once you step into it there's no going back. You're stuck with it!

Like it or not the browser is a viewer that uses all 3 domain specific languages to achieve what it does so what you can do is either learn yet another DSL (the facelets DSL in this case) to do the job or you can simply go through the effort of learning the core ones which will pay off in the next and next and next web application projects no matter what technology you'll use (besides JSF of course)
To my liking it is unbearably sad that people dismiss the simple ideas and take a shot at frameworks like JSF. I wonder when the era will come with something else will replace JSF like WebServices are being replaced with REST.

Funny thing is that Ed draws a hard line between client-side-only frameworks (jQuery, Flex...) because they don't fit into his agenda but at the same time asks if "Wicket, Grails? Play? Echo2? Tapestry? Stripes?" will fulfill the goals that JSF is supposed to. It's funny because there's for example no Grails application that wouldn't use either Prototype or jQuery (or anything else for client-side programming). Funny :)

One final note: "jQuery+REST"? If you know what the author meant with it please leave a comment and explain it to me.

Sunday, January 16, 2011

Keep your data local

"Keep your data local" - said once a good friend of mine. It didn't mean much to me until some time last week when I was given a task to optimize a .NET application that used way to much memory.

Keeping the story short: if you want to keep your data local use an embedded database like SQLite, SQL Server CE or Firebird Embedded and don't keep all your data in memory at once. Split the process into 3 parts (populate data, process the data, send updates on user's demand) and don't even think about using DataSet for anything that's larger than say 10000 records. It's way to bloated to use it on those amounts of data.

So, if you think that sorting and filtering within a DataView is faster because there's no network overhead and no additional disk IO please be advised that it is way slower than reading records on demand from any of the embedded databases mentioned above and uses a mere fraction of memory by comparison.

And btw - SQLite rules, but its lack of locking on record level is very much annoying...

Tuesday, January 11, 2011

Old times...

Today I've been browsing through my old applications and libraries. You know... the kind of code you'd never look back at :)

It's funny that after all those years (3 to be exact) when I look at the code it still looks nice and fresh. I must have known what I was doing :D

Jokes aside here's a story to think about:

Imagine a project involving 2 to 3 developers, based on bad assumptions and lack of a good idea. This project went on and on for years (2 years to be exact). The idea was simple: take an application that used Pervasive's BTRV API and make it work under some RDBMS. Like I said it took many developers (for this kind of idea to come true) and many years... And in the end it didn't work... In the mean time (since I'm the kind of guy that 'knows better') I've decided to do a little spike and to see how much effort it'd take to create something like that but in a more, let's say, clever fasion. As it turned out it took me 3 days to get the whole idea to run in read-only mode (I never got so far to implement the update operations even though everything is ready to do them). 3 days!!! I mean, are people really so blinded by the fact that they don't want to do rewrites that it forces them to spend years of wasted work and hundreds of kilos of dollars just not to say they've made a mistake???

Man that was really fun to look at again and to remember those old times...

Monday, January 10, 2011

camelCase for human beings

Reading lots of characters glued together is a very good thing for the compiler. Human beings tend to prefer spaces rather than a set of lowercase characters separated with uppercase characters to distinguish words. It's just how we were taught since we were very young...

In case of unit test in Groovy we can actually name our tests with spaces! Here's an example:
import org.junit.Test

class ExampleTests {
@Test void 'This test verifies nothing but is a good example'() {
assert 1 == 1
}
}
When later on you're reading the name of the test that failed it's a whole lot easier than to read the same in camelCase.

Converting camelCase to human text

I thought I'll give a string an additional method so that whenever I have a camelCase string I can convert it easily to something that's easier to read:
String.metaClass.humanify = {
def r = ""

delegate.eachWithIndex { c, i ->
if (i == 0) r += c.toUpperCase()
else if (c in 'A'..'Z') r += ' ' + c.toLowerCase()
else r += c
}

return r
}

From now on it's easy to read camelCase when it's humanized :D

Sunday, January 9, 2011

Testing sending emails with Grails

If you'll ever find yourself in need to test some mail sending piece of code in Grails be advised that there are 2 ways of doing it (both options work only in integration tests!)

The grails-mail way

Once you have your piece of code in place what you can do is to set the
grails.mail.disabled=true
setting in Config.groovy and skip sending emials entirely. You can then use the return value from mailService.sendMail call to inspect what would have been sent to the server.

The "all-the-way-through" way

If you'd like to see if everything really goes through to the mail server you can always use Dumpster. It's a very elegant library giving you basically 3 things:

  • A SimpleSmtpServer.start() method

  • The SimpleSmtpServer instance as a result of the method call above

  • SimpleSmtpServer.stop()
So what you can do is to start the server at the beginning of your test, send the mail simply to localhost:25 (assuming you don't have sendmail listening on the same port of course) and shut it down after you're done with it.

To sort of enable Dumpster in your tests either add the dumbster-1.6.jar to your lib folder or add the following dependency to BuildConfig.groovy
dependencies {
test 'dumbster:dumbster:1.6'
}
Make sure you enable repoCentral()!

Here's how easy it is to use Dumpster to test sending emails in a grails integration test:
import org.junit.Test
import com.dumbster.smtp.SimpleSmtpServer

class ExampleIntegrationTests {

def mailService

@Test void willReceiveEmail() {
def server = SimpleSmtpServer.start()

mailService.sendMail {
to "someone@some-server.com"
from "me@server.it"
body "This is the body"
}

server.stop()

assert server.receivedEmail.toList().size() == 1

// a little dump to see what's in the email :)
server.receivedEmail.each { println it }
}
}
Check out the Dumpster examples for more. I like it!

Final warning

A word of caution related to the grails-mail plugin: in version 1.0-SNAPSHOT you need to manually add a repository for the required dependencies to be properly resolved. In general I recommend using version 0.9 because it works out-of-the-box. To do that either specify the version while installing the plugin like this:
grails install-plugin mail 0.9
or if you've already installed it in the default version you can always edit the application.properties file and change the plugin version there.

Saturday, January 8, 2011

Grails, IDEs and productivity

Disclaimer

The following opinion might do you harm, offend you or your colleagues or might seem like a talk of a crazy man. Below way of proceeding with daily job is just one of many, probably as many as there are developers out there.

You have been warned!

It all started a long time ago

A long time ago there was Turbo Pascal. It had (as for those old times) an amazing IDE where you could write and debug code and if you made a typo the IDE would tell you immediately that you made a mistake. It had yet another property: it was blazing fast even on those old PCs.

Microsoft VisualStudio, Delphi and Java IDEs

After some time the era of graphical user interfaces kicked in. Environment for programmers started to grow and grow and grow... Especially those for Java. What they brought to the table was supposedly productivity enhancements. One would code faster by use of wizards, code completion, squiggly lines and other "gooye" goodness...

Everything comes for a price

Since those IDEs at the very beginning were quite simple they started fast. PCs were getting faster and faster every year so even if the startup time was around 20 seconds it'd still not be a problem because the next year it'd be down to 3-4 seconds and life would be good again.
The only problem with that was that the next year's release had even longer startup time and this tendency didn't seem to stop until today.

Rational developer point of view

Imagine a situation where all you need is to change 2 lines of code, run the tests and be done with it. What would it take to do this in an IDE like Eclipse or NetBeans and how would this look like when using a less sophisticated tool like let's say TextMate.

Eclipse

Ok, so let's start Eclipse. It's actually a good thing it starts so long because I can go and make myself a coffee before it's ready to work. Oh! but wait! Starting is not everything! Since those brainiacs that created Eclipse thought that it's actually better to delay loading stuff you don't need at first it is actually not only the startup time but you need to add to this a long long time for every window to pop up the first time. Not to mention that the PC I'm running on is quite a powerful one (2 64 bit cores @ 2.8GHz each, fast HDD, lots of ultra fast RAM) Well, most games (and those were the applications that demanded the most of hardware) work like a charm.
Code completion, the biggest hype of all the features in modern IDEs, seems to work only with dull, very formal languages. There's a reason for that: dynamic languages tend to change the behavior of existing code in runtime so there's no way to properly predict what will happen in this particular place in code.
Take Ruby or Groovy for example. Eclipse is doing a pretty good job in giving you hints on what's available but it still is not powerful enough to support some of the nice features like Mixins for example.

NetBeans 6.9.1

The story with that one is exactly the same as Eclipse but worse. Imagine to wait for over 40 seconds just for the list to appear so that you can choose what you want to call. NIGHTMARE!!!

Visual Web Developer 2010

One nice thing about this IDE is that it is focused on one task, and one task only: building web applications. To that end there's no Windows Forms designer built in to slow down the IDE when I don't need it. There's also this one and only (best of breed my friends) JavaScript editor that recognizes extension patterns in many libraries (like ExtJS or jQuery) and provides intelli-sense for them. Let me tell you: it's a F1 car to work with JS!
Unfortunately many of the other things from Eclipse and NetBeans still apply. Startup time is longer and longer with every version and PCs just don't catch up with it.

The worst nightmare: Delphi

Imagine saying that Eclipse, or even NetBeans, starts in no time by comparison. The Delphi 2009 startup time was around 1.5 minutes!!! I mean come on, guys! What were you thinking when you allowed such a piece of crap to see the light of the day?

TextMate to the rescue

These days it is very common for the actual tooling support to come together with a particular library. Take Ruby on Rails or Grails for example. All you need to do is to install the thing and have an editor with contextual text highlighting so that you can see better what's going on. Hell, you can even code directly in the browser!
I took TextMate as a general-purpose example but the same holds true for most of the other plain text editors. Vi, Vim, Emacs (ok, that one is a hardcore one :)), Gedit, Midnight Commander's editor, Far Manager 2 with Colorer plugin... You name it! And like I mentioned before, the code completion itself once you're productive in a platform will only slow you down.
You might say: and what about debugging? Well, as the good uncle Bob said "you don't! what you should do is to create unit tests to see if it all works as expected". But that's a topic for a completely different post...

Is it really all lost?

Ok, so let's give the IDEs some credit they deserve. I do use them, from time to time, when what I need is to do some refactoring, especially to rename classes in files. The process is just too cumbersome to do it manually. I've seen people do some pretty amazing things with Java code in Eclipse too. I guess at some point it becomes like a second skin for you... I prefer to go "naked" :)

Conclusion

When you go to see a doctor and tell him that when you bang your head against the wall then the head begins to hurt what he'll most probably say is "Don't do it". So if you'll ever find yourself complaining about the speed of your IDE try some alternate approach. You'll be amazed what productivity boost it will bring after just a few days of "getting used to". Remember: chemistry doesn't happen over night :)

Grails, Camel and adding routes at runtime

Since it's been of some interest on the net to figure out how to add routes at runtime to Camel provided by the grails-routing plugin here's an example of how it might be implemented:
import org.apache.camel.builder.*
import org.apache.camel.model.*

class HomeController {
// 1
def camelContext

def index = {
// 2
println camelContext.routes.size()
// 3
if (camelContext.routes.size() == 0)
// 4
camelContext.addRouteDefinitions(
new MyRouteBuilder().routeDefinitions
)
render text: camelContext.routes.size().toString(), contentType: "text/plain"
}

def send = {
// 5
sendMessage("seda:input", "Hello, world! from dynamically added rotue")
render text: "sent", contentType: "text/plain"
}
}

// 6
class MyRouteBuilder extends RouteBuilder {
// need to override this to make RouteBuilder inheritance happy
void configure() { 
} 

// generate a new route
List getRouteDefinitions() {
[ from("seda:input").to("stream:out") ]
}
}

1 - Make the CamelContext instance available


the def camelContext makes the bean of class CamelContext available for us in this controller

2 - Debugging


For debugging purposes we're printing out to console how many routes are defined. The first time it will print 0 as there are no routes defined yet.

3 - Check if the route has already been added


We don't want to add the same route over and over again, right? :)

4 - Adding a new route


Using an instance of MyRouteBuilder get a new route definition and stuff it into the current set of routes.

5 - The send action


In this action we'll test the newly created route to see if it works

As you can see the only odd thing is that MyRouteBuilder doesn't do anything in configure since it's not required. We're inheriting from RouteBuilder only to have access to specific DSL methods (like the from one in this example).

To try it out make sure you have no other routes defined in your application, fire up the /home/index and note the number of routes printed out to console and displayed on the page. The first one should read 0 and the second one (since it's evaluated after the new route has been added) should read 1.
Then navigate to /home/send and observe in console a nice message received on seda:input and forwarded to stream:out

EDIT on 21 January 2012

There's a better way of doing this sort of things that works with all kind of routes:
def index = {
    if (camelContext.routes.size() == 0) {
        camelContext.addRoutes(new RouteBuilder() {
            @Override void configure() {
                from("seda:input")
                    .filter({ it.in.body.contains('from') })
                    .to("stream:out")
            }
        });
    }
    render text: camelContext.routes.size().toString(), contentType: "text/plain"
}


Hope this will help :)

Friday, January 7, 2011

Groovy/Grails DSL testing

Today I faced a usual task of testing a controller action that utilizes the withCriteria call to fetch some data from the database. Usually what I do in situation like this is I write an integration test, have the database mocked using in-memory instance of HSQLDb and all is nice and dandy.

This time I didn't have the luxury to have the database in place. I will not bore you why that is the case - it simply is. What I didn't want to do was to just let the thing be untested so I've decided to create a ultra-minimalistic framework for testing such DSLs in Groovy.

First of all, it all bases on the assumption that you don't care about the actual results the DSL brings. What you do care about is the fact that certain parts of the DSL have been called in a particular order and with proper arguments. To put this in context: you don't care that the withCriteria DSL will be in reality transformed into SQL but you do care that what you wrote will be as you expect it to be.

So the following class has emerged:
package groovy.test

class DSLTester {
static mock(clazz, method, result) {
def tester = new DSLTester()
clazz.metaClass.static."${method}" = { Closure dsl ->
dsl.delegate = tester
dsl.resolveStrategy = Closure.DELEGATE_FIRST
dsl()
return result
}
return tester
}

def result = []

def methodMissing(String name, args) {
result << [ name: name, args: args.toList() ]
}
}

What it does is it executes the closure passed on as the DSL to whatever static method we decide to test and catches all the missing calls as if they were proper DSL calls. You might find the args.toList() part to be a little bit strange. This is to allow use simple comparison in tests - read on to find how.

To give you an example first let's define the Person class:
class Person {
String firstName
String lastName
}

With that in hand let's create a home controller that will just print all the people ordered by firstName:
class HomeController {
def index = {
def people = Person.withCriteria {
order "lastName"
}

[ people: people ]
}
}

To test the withCriteria closure let's utilize the DSLTester class:
import groovy.test.DSLTester
import grails.test.ControllerUnitTestCase
import org.junit.Test

class HomeControllerTests extends ControllerUnitTestCase {
void testExecuteDSL() {
def mock = DSLTester.mock(Person, 'withCriteria', [])
def actual = controller.index()
assert mock.result == [ [ name: 'order', args: [ 'lastName' ] ] ]
}
}

As you can see the only assertion that I make here is that the order will be properly set to 'lastName' which is what we wanted in the first place.

There's tons of ways you could improve this. You could for example come up with a nice way to verify expected methods have been called with the proper arguments instead of declaring a huge list with hashes containing everything. Sky is the limit :)

The main motivation behind this sort of test is to provide means to test only your code and to test it in complete isolation. It might be perfectly understandable that since the code that uses withCriteria has such a strong dependency on the database that testing it in isolation makes no sense. I however like to keep things isolated :)

Thursday, January 6, 2011

Story of one Grails plugin

It's been one of those days where I felt like I need to create a spike app just to prove things are simple in Grails. A friend of mine is preparing a presentation about using NOSQL databases in applications and since it's pretty darn simple to use MongoDB in Grails I've decided to put together a sample that will utilize this datastore.

It all started soooo simple...

grails create-app example
grails uninstall-plugin hibernate
grails install-plugin mongodb
grails create-domain-class Person
grails create-controller Person
grails create-controller Home

Then I've edited the domain class to have 2 properties firstName and lastName, added scaffolding to the PersonController... Basically the Grails 101 stuff...
The HomeController was also pretty simple. Just the index action that returned the [ people: Person.list() ] map and the view that rendered a table to present this data...

Then the geek kicked in...


I thought: now wouldn't it be nice to have the table sortable? Well, sortable but the sorting itself should be done using Ajax - now that'd be cool :)

Right tool for the job - jQuery


When I think "Ajax" it's as if I'd be thiking jQuery at the same time. For me there's nothing else. Period :)

To do that I first needed to extract the part that rendered <tbody> to a separate template _rows.gsp. Then another action, rows was needed to return sorted list of rows instead. That was again dead simple with GORM:
render template: 'rows', model: [ 
people: Person.list(sort: params.key, order: params.order)
]

Then the actual client-side jQuery magic happened:
$("th").click(function() {
var key = $(this).attr("column");
var order = $(this).attr("order");
$(this).siblings()
.removeClass("order-desc")
.removeClass("order-asc")
.removeAttr("order");
if (order == "desc") {
$(this).attr("order", "asc")
.removeClass("order-desc")
.addClass("order-asc");
} else {
$(this).attr("order", "desc")
.removeClass("order-asc")
.addClass("order-desc");
}
$(this).parents("table").find("tbody")
.load("${createLink(controller: 'home', action: 'rows')}?order=" +
order + "&key=" + key);
});

It's all basic stuff... Nothing to be proud of...

Geeky thinking part 2


A simple idea to turn the code above into a jQuery plugin turned out to be the hardest part. First of all, authoring jQuery plugin is not as simple as I'd like it to be. I'm more of a "File - new" person than "Google, google, google..." one. So after I've finally found out how to create such a plugin I was pretty much exhausted.
But what if it'd be really simple? Something along the line of "File - new" or (in the case of Grails) "grails create-jquery-plugin my-plugin"???

And so I've created the jquery-plugin-authoring Grails plugin. From now on whenever I see that part of my application just fits like a jQuery plugin I simply create one. The template leads me every step of the way (the initialization, default values for options, a method to override them, a list of possible messages my plugin can respond to).

And life is simple again :)

End result


I love to see my results be better than someone else's. In this case comparing what it takes to do all that in pure Java having to glue together Hibernate in Entity Manager mode to use Hades, wiring it all with spring beans and adding the web application part with Wicket seems like tons of work by comparison. In fact It took me about 4 hours from the very beginning to the point where I've had the plugin released, used and all was nice and dandy. Grails rule!

I wonder how much time it'd take to do the same in Ruby on Rails? If any of you could perform such an experiment and share the result it'd be really great!

Monday, January 3, 2011

Grails,CXF plugin and .NET

As promised here's a follow up on the WebService interoperability between Grails using Apache CXF plugin and .NET.

First let's create a simple example project and install the grails-cxf plugin:
grails create-app example
grails install-plugin cxf

Next let's create a service:
grails create-service example

This has created 2 files:
grails-app/services/example/ExampleService.groovy
and
test/unit/example/ExampleServiceTests.groovy

The first one is the one we need to modify so let's get to it:
package example

import javax.jws.*

@WebService(serviceName="ExampleService", name="Example")
class ExampleService {

static expose = [ 'cxfjax' ]

@WebMethod(operationName="sayHello")
String sayHello(@WebParam(name="name") String name) {
"Hello ${name}!"
}
}

As you can see I've added every possible bit of information to customize the generated WSDL. With all that in place we can get the description of this service (WSDL) at http://localhost:8080/example/services/example?wsdl. Let's use that to create a .NET client. This time from command-line:
SET PATH=%PATH%;"C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin"
wsdl.exe http://localhost:8080/example/services/example?wsdl

In the first line we're expanding the system path so that all the tools are available from anywhere. In the second line we're generating a client for the web service we've created in Grails. Simple enough, right?

Let's create a simple console application and use this newly generated client:
Program.cs:
using System;

public class Progra {
public static void Main(String[] args) {
Console.WriteLine(new ExampleService().sayHello("John"));
}
}

Let's compile everything from command-line:
set PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
csc *.cs

Again, no magic here. Once you've compiled everything run the application and bum! Everything works as expected :)
My guess is that CXF plugin didn't work just right out of the box because of some problems with default naming that CXF is using when auto-generating WSDL. With all the annotations in place everything seems to be working just fine.

I hope this will help someone. In case you'd like to fiddle with it yourself here's the example. in src/csharp you'll find the sources for the client along with a batch file to compile them.

Unfortunately when it comes to a more advanced scenario, like for example returning an array of domain objects from a service method the CXF plugin still fails to produce good enough WSDL to work with .NET. This is the case where XFire plugin shines best!

FYI: the same trick does not work with Axis2 plugin. The naming is all whacko. Things like this$dist$set$2 and urn:this$dist$get$2 hurt the WSDL code generator so much it spits nothing out but a load of warnings.

Have fun!

Sunday, January 2, 2011

JavaEE 6 + Metro WebServices

Following the post about WebServices with Grails here's a short summary of my attempt to make Java's EE 6 WebServices work with .NET. It's quite a basic example but as history shows even the simplest examples might not work (as it is the case with Axis2).

Setup

So here's a definition of a Hello WebService:
package com.aplaline.integration.webservices;

import javax.jws.*;

@WebService(name="Hello", serviceName="HelloService")
public class HelloService {
@WebMethod
public String sayHello(@WebParam(name="name") String name) {
return "Hello " + name + "!";
}
}

As you can see there's no magic here. Let's see how NetBeans' configuration option look like for this guy:



Surprisingly enough there's mentioning of a .NET version! Let's see how this works. I'll be using VWD 2008 SP1 to test it:



Let's add a web reference to this web site:



Now let's create a blank Default.aspx page and add some code to call that web service:
Default.aspx:
[...]
<form id="form1" runat="server">
<div>
<table>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="Enter name" /></td>
<td><asp:TextBox ID="input" runat="server" /></td>
</tr>
<tr>
<td align="right" colspan="2"><asp:Button ID="Button1" runat="server"
onclick="Button1_Click" Text="Click me" /></td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="output" runat="server" /></td>
</tr>
</table>
</div>
</form>
[...]
Default.aspx.cs:
[...]
protected void Button1_Click(object sender, EventArgs e)
{
output.Text = new localhost.HelloService().sayHello(input.Text);
}
[...]

Results

As it turns out it works out of the box properly.



For the kicks of it let's try and consume the same web service from PHP 5.3:
<?php
$client = new SoapClient("http://localhost:8080/WebServices/HelloService?wsdl");
var_dump($client->sayHello(array("name" => "John")));
>
There's a little inconvenience when calling web services from PHP in that the parameters need to be passed as an array instead of simply specifying their value. In the long run it's kind of healthy because different webservice stacks tend to handle things differently and putting the proper context into the whole thing.



This is the first pleasant surprise since I've started to work on the web-service interoperability between .NET and Java. It looks like things are definitely going in the right direction :)

Here's the Java project for reference.
Here's the .NET website for reference.

Have fun!