Wednesday, December 26, 2012

Did you noticed Spring Loaded is here?

Well... I didn't! Not for almost a whole month! And it's the best damn thing ever!

Here's how you use it:
MAVEN_OPTS="-javaagent:/path/to/springloaded.jar -noverify" mvn tomcat:run
Next you open Eclipse, do changes, reload pages - all changes occur immediately! Sure you can't add new Spring artifacts (which is odd), change class hierarchy (no surprise there) but you can add/remove methods which is more that what you had before.

Well - it isn't JRebel - but it is good enough!

Have fun!

Monday, December 10, 2012

Named inner classes in methods

Do you know the feeling when you write something that should totally be wrong and it turns out perfectly ok? Well that's what I just experienced: A "WTF?! This is really cool" moment. What gives?

I've been entertaining myself with a little bit of Spring Data exercise. Trying to understand how much will I gain from using JdbcTemplate I dug a little into the sources of that class. At one point I saw that there's a named class (not an anonymous one) declared inside a method. WTF?

I know if I say it like that it is kind of lame of me not to know this. But this is such a nice feature I can't really understand why it's being used so rarely...
public String getName(String user) {
    JdbcTemplate template = new JdbcTemplate(this.datasource);

    class NameExtractor implements ResultSetExtractor {
        @Override
        public String extractData(ResultSet rs) 
            throws SQLException, DataAccessException {
            return rs.getString("firstName") + " " + rs.getString("lastName");
        }
    }

    return template.query("SELECT * FROM users WHERE id=1", new NameExtractor());
}
I know there's like a ton of different ways of writing that code but I have chosen this simple example to show you (and me in a couple of weeks maybe) that inner classes don't need to be defined on class levels.

Have fun!

Thursday, November 22, 2012

Who cares?!

Seriously, who cares? Who cares how HashMap or Hashtable is implemented? Or maybe an ArrayList, ha? How about DynaBlasterRollerCoster? Do you care how it's doing what it's doing?

I just got off a phone interview with some guys in the UK but the same thing was happening in other places. Do you really need to know this? And if so is this ever going to impact the way you design a distributed, highly scalable system if all you know is how to implement a HashMap on your own?

Seriously, we're past that point where we need to manufacture our own specialized collections (even though the Java Collection API sucks big time). Get over it! Maybe when I'll switch back to Microchip MCUs I'll look back to find out how to implement a dictionary then. Oh wait! It's implemented in SDCC already! And I bet there's a pure asm library doing this on Z80 as well.

And if there ain't one laying around there's this thing called Google, with this little box and a "Search" button (hell, these days you don't even have to press this key anymore) that will tell you what you're after

Edit 25.11, 05:17, on the airport: I wonder if the next question (since they asked me if I feel like a polyglot programmer) would be So how's implemented a Hash in Ruby and how does it compare to the inner workings of Dictionary in .NET?. Why not, right?

I just needed to get this out of my system...

Wednesday, November 21, 2012

Simple and effective: Spring and customizable configuration

The problem

The problem is to have the configuration customization possible after deployment. There are settings your hosting department for whatever reason will not let you know (like database password for instance). This fits really nicely to the ways Spring handles configuration.

The solution

The solution in Spring 3.1.x is to use the <context:property-placeholder> tag configured as follows:
    <context:property-placeholder 
     system-properties-mode="OVERRIDE"
     ignore-resource-not-found="true"
     location="
      classpath:defaults.properties,
      file:${config.location:/etc/example.properties}" />

The explanation

First of all there have to be sensible defaults the application can work with. I personally favor them to be good for development so other developers can just get the application, execute mvn tomcat7:run and have it running in minutes rather than to fiddle with all the moving parts of configuration.
Now deployment and the hosting team is a totally different beast all together. They will always want to have things configured their way. <joke>I don't know why :)</joke>

So here we have it - on the classpath there are the defaults in defaults.properties. Now the second part is interesting. First of all it makes an assumption that hosting will follow the best practices and store configuration where it belongs, which in almost all Linux distros is the /etc/ folder. Here's the trick though: some admins will cut your fingers an inch shorter if you force them to put anything anywhere. Especially for that occasion the second option is there to save the day. It allows you to specify a JVM parameter -Dconfig.location=... for Spring to look for configuration options in a completely different place. Neat, ain't it? Finally the system-properties-mode="OVERRIDE" allows for in-place overrides using JVM system variables (the ones specified with -Dvariable=value)

I hope it'll save someone some precious time. I know it'll save me from re-inventing this solution time and time again.

Sunday, October 28, 2012

Revisions

Back in the days when I switched over from Delphi (the day that Delphi died) I felt the overwhelming amount of frameworks for pretty much everything was giving me a headache. And I think everyone out there will agree with me. Hibernate, Spring MVC, Spring Integration, Spring Data, Spring Mobile, Spring WS, Spring Batch, Hazelcast, EhCache, C3P0, a whole range of Apache Commons - just to name a few prominent ones. Oh boy can this be a pain in the royal butt to learn all of them. An then there's the ever changing world we live in where new versions of open source frameworks come out like crazy. Pretty much a 100 times a year one can get something new to learn, try out, fiddle with, get angry with or embrace.

When I first started using Spring all it was for me was the dependency injection framework with stupid XML config and training that takes forever, costs a fortune and makes no sense. Remember, I came from a world where documentation was pristine (for both Turbo Pascal and Delphi), examples came in spades and the classes started with a T and all interfaces with I. All the separation of concern, testability and other ilities were not so needed in the Delphi world. Also inheritance was in the zone (now we all know it's bad and should be avoided). With Java everything was different and I felt lost.

I turned my attention to something I though would be a little bit more familiar - the .NET framework and C# language. Oh boy was I happy to see the notion of properties built into the language (so missed in Java). I though I've had the world by the balls with this one: modern platform, fantastic language (created by the same guy who created Pascal and Delphi) so I was right at home.

At that time job opportunities for C# web application developers were scarce and so I was sort of forced to stay with Java. At first I hated it with a passion (http://www.ihatejsf.com). In the mean time along came Guice and showed me why dependency injection makes sense and how to do it right. After some time I learned to live with it mostly because I discovered that there's more to it that meets the eye. Basically I learned about Groovy and Grails. That was the sweet spot for me: elegant and dynamic language, similar to Java but oh so much more powerful and a framework that finally doesn't suck like JSF did. That was the best time ever!

With time wisdom came and I started looking around what else can be done with this thing. That was the time when JavaEE 6 came out with Glassfish 3 (man you really have got to love something that's called Glassfish, don't you?). Finally all the stupid things like web.xml went away, web services creation started to be a breeze and all was cool again. Then I leared about EJB3 and all that comes with it. Can't say I was happy with what I saw after living on Grails, Guice and Spring for a while.

Now it's years later. I'd describe myself as TDD junkie with a healthy dose of polyglot programming skills. I've learned a great deal about the so called Spring programming model. I've learned a lot about Spring and the modules it brings to the fold. I learned not to hate XML. I've learned Ruby and got acquainted with Sinatra. Every day is a new challenge and every day I learn more. The more I learn the less I write and the more I integrate.

During all that time there has been only one constant: Internet. All I know, all I ever learned beyond the 6502 assembler, 68k assembler, Pascal, Turbo Vision, Delphi and VCL (which I learned ages ago from dead-tree books) came from the Internet. This is the best, most demanding and absolutely the most rewarding university in the world. All this because of open source projects and awesome communities that make the world go around these days.

It's a beautiful time to be a programmer!

Do we really need another build tool?

Recently I've been getting to know Maven a little bit better. Underneath all the XML fuzz there's a really good, well designed and extremely modular application that does pretty much everything one can ask for (and then some).

With Maven things are simple: bloated XML contains every possible setting for every possible plugin, every possible dependency and what have you. You might hate the fact that it is XML. I say screw it - let's try to live with it.

On many occasions the XML nature has been the corner stone for many people to start developing their own build tools. I'd say their repulsion came from Ant and if so it wasn't totally unjustified. I hate Ant with a passion when it comes to building Java projects. Ant is great for cross-platform imperative tasks (download this, unzip, make a link, go get coffee) but Java projects are a completely different beast. It's not that you can't do it with Ant but rather that it takes forever to do everything (not to mention that everyone is doing it differently).

Like I said people's repulsion to XML configuration files pushed some of them into the hands of Groovy (my absolutely favorite language) to create Gradle. One of my very good friends, Szczepan Faber, works on this tool and I am grateful for his efforts. It makes the landscape of build tools a lot more interesting. It's from diversity that the progress comes in this case.

Being all Groovy geek and loving the language and what one can do with it I must say I am a little bit confused as to why I'd use Gradle. I mean I get that the build file is terse, that the default source/target compiler makes more sense, that I can declare 10 dependencies in one line... But what about plugin support?

From my personal perspective the idea of creating a new tool from scratch is pure nonsense. Maven is the de-factor standard in Java world and not without reason. It does what it does and does it good. Where Maven lacks in features is imperative programming but that can easily be overcome with either custom Maven plugins or GMaven plugin. For me it'd make a lot more sense if Gradle adopted the Maven API for plugins and build on that to gain the much needed functionality into the fold before it is missing.

Like I said before, I get that one can feel intimidated by all the XML in Maven builds. I get that one can mess things up with Maven really bad. I also get that Gradle, Buildr and others might be better in some cases. What I hate about the overall landscape is that the plugins for build systems aren't compatible and even if I'd like to use Gradle to build my next gen rocking cool enterprise java application I'd dead in the water pretty soon because the EAR/EJB archive support in Gradle is non-existent. I hope this changes some day...

Have a nice day!

Tomcat Clustering and a pile of problems

I've been trying to familiarize myself with Tomcat's clustering capabilities today. At the end of it I succeeded but the experience was way less than pleasant. But let's start from the beginning...

At first all seemed really nice: Sunday afternoon, not much to do, some thoughts around playing with Tomcat for the sake of finding out what it's capable of.. Basically the idyll. So I though wouldn't it be nice if I learned how to setup a Tomcat cluster? I already done some horrible things to Tomcat in the past (Single Sing On for instance) but I've never tried clustering. How about that? And so I started what anybody would do - to read the configuration files. No - I'm kidding :) I started to Google for some clustering HOWTO to see if I'm alone in what I'm trying to accomplish. Fortunately it turned out to be a pretty well described topic with some ready-to-copy-and-paste examples. Highly encouraged by what I found myself copying parts of what I found in that miserable HOWTO into configuration of my 2 Tomcats that are supposed to form "the cluster". After about 2 minutes of frantic alt-tabbing, CTRL+C/CTRL+V-ing I arrived at the glamorous point where I was able to start at least one of the Tomcat's to see if the configuration even allows to the server to start. Well, it started, all right. But it didn't work.
Oct 28, 2012 5:14:09 PM org.apache.catalina.ha.deploy.FarmWarDeployer start
SEVERE: FarmWarDeployer can only work as host cluster subelement!
Oh what the heck is this? I though.Wasn't the example from original documentation? I was always banking on those to be really of good quality and now this dissapointment? There's a number of things wrong with the example (and as it turned out with the original server.xml as well). But let's start from the beginning.

server.xml and it's comments

There is a dark place in server.xml that shows how to configure a cluster in Tomcat.
      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> -->
So I removed the comment in two instances but it turned out not to be what I expected. Well it did something (not sure what exactly) but it wasn't all that inspiring. There is however a reference to further documentation. I followed that and then the frantic copy/paste activity started. All was quite nice up until I started Tomcat and saw the SEVERE I mentioned before. Back to Google. And then some. Finally I arrived at this discussion where I learned that the <Cluster> element is supposed to be nested within the <Host> element and not where it was suggested by the original server.xml file. Go figure. Anyways, I moved the section down to the Host and this time
Oct 28, 2012 5:01:34 PM org.apache.catalina.ha.deploy.FarmWarDeployer start
INFO: Cluster FarmWarDeployer started.
Great Scott! It will work now! And so I copied my beloved distributable, servlet-api-3.0-enabled hello.war into the /tmp/war-listen folder hoping to see Tomcat spring to work, install the application and make my day. But nothing happened! The example in Tomcat documentation specifies watchEnable="false" by default. What was going through the mind of that someone who wrote it is beyond my understanding of the universe. Anyways, after I changed it to true finally something started to happen.

Since Tomcat ain't for the faint-hearted I was presented with a puzzle:
Oct 28, 2012 5:29:36 PM org.apache.catalina.ha.deploy.FarmWarDeployer fileModified
INFO: Installing webapp [/hello] from [/tmp/war-deploy1/hello.war]
Oct 28, 2012 5:29:36 PM org.apache.catalina.ha.deploy.FarmWarDeployer remove
INFO: Cluster wide remove of web app [/hello]
What the heck? Cluster wide what? I just installed this application, didn't I? I mean thought it will be enough to copy the file in the proper folder and that should be it, should it not?

As it turns out it does work quite nicely if you specify the deployDir to where Tomcat expects to auto deploy applications (by default webapps, or rather ${catalina.base}/webapps). After I changed that, copied the Cluster configuration to the second Tomcat, started both and copied the hello.war application to /tmp/war-listen both Tomcats have received the proper nourishment and started talking my language.

Summary

There's a number of lessons learned from this encounter. First and foremost Tomcat is just awesome. It's so unbelievably small yet so feature-packed. It just makes my day. While debugging Tomcat configuration issues it really helps to start it (at least under Linux) not by ./startup.sh but ./catalina.sh run. This way you'll have the console to view the logs and to terminate it all in case you're not happy with what you see. And last but not least: Google is your friend. I mean it. It is the one thing all this could not be possible without in terms of knowledge sharing.

Have a nice day!

Maven and Tomcat 7

You might be unaware of this but Maven is capable of running Tomcat 7 as embedded container. The maven-tomcat-plugin has been moved under the Apache Foundation umbrella and is being developed further. The current version is 2.0, packing in both versions of Tomcat, 6 and 7. Setting it up is explained here. All the configuration is very similar to the original one and your IDE will help you out there (at least Eclipse does).

Finally... It's been way to long to wait for an update on this one.

Sunday, October 14, 2012

GitHub, Travis and status image

Since not so long ago GitHub provides an opportunity to build OpenSource projects on Travis CI. The integration is quite stunning and includes among other things:
  • Single sign-on with GitHuub
  • Automatic build setup with a single mouse click
  • Status image generation
Well, I tried to setup build of my Spring examples today and Markdown gave ma a little bit of a hard time to figure out how to put together an image link to by build status. Finally I stumbled upon this stackoverflow.com question but the brackets were all wrong. Here's what it should look like:
[![_alt_text_](_image_url_)](_link_)
With the parentheses instead of brackets all works nice :)

And live is good again :)

Friday, September 28, 2012

Foreman and multiple ports per process

The problem

Composing an application of multiple software components is the de facto standard these days. It's rarely the case for the application to embed a database server, queue server or the like. To help manage the ever growing complexity of this kind of systems we can use Foreman - a Ruby application using a Procfile to spin off instances of declared services (same as for example Heroku uses to manage processes).

Many software components these days besides providing the core functionality tend to give the user some sort of administration interface. That interface is often available under different a port. Up until now Foreman assigned only one single port per process, via the $PORT environment variable. As I started using Foreman recently this inconvenience became a stopper for me trying to start ActiveMQ, 2 instances of MongoDB in a shard and Neo4j with my Sinatra application. All was nice but the lack of ability to start MongoDB's control interface on separate ports blew it all out of the water.

The solution

I've came to the conclusion that it'd be a really handy to use multiple ports not just for the case I just described. And so I created a ticket on GitHub for this. Being a forthcoming guy and all I decided to take a crack at implementing this functionality myself. A few hours later a working and tested solution covering all the cases was ready to be pulled in. Let's see if the maintainer will agree to pull it in :)

How to use it?

Assuming David will pull it in the formula is as follows:
alpha: command_to_start -p1 $PORT -p2 $PORT0
beta:  command_to_start -p1 $PORT1 -p3 $PORT9
What would that mean?

alpha would get assigned 2 ports: $PORT = 50xx and $PORT0 = 51xx
beta would get assigned 3 ports: $PORT = 52xx, $PORT1 = 53xx and $PORT9 = 54xx

Since Foreman makes an assumption that at most 100 ports are assigned to one variable this new functionality uses that to assign ranges for subsequent ports. And because I wanted to stay compatible with results not using this new feature the original port range numeration regardless of the usage of $PORT in the command stayed the same. That's why beta still gets the $PORT assigned even though it is not used.

Have a nice day!

Thursday, September 6, 2012

AMQP, RabbitMQ and round robin

I'm working on a set of examples of how to use RabbitMQ from different languages. Among other things I've worked on reviewing the ruby AMQP gem examples. Unfortunately when it came to demonstrating multiple consumers (a sort of scalability example) I hit a snag: even though I have had 4 consumers only the first one was used.

After setting the number of test messages to a really high number things were finally looking up - the second and third and forth consumers were involved. But that sucked big time and to prove it I started digging further to find out what actually makes it do the things it does.

:prefetch => 1 was the setting (as stated on some stackoverflow.com page) but setting it alone didn't do the trick because that made the channel get only one message and that was it. To continue receiving messages they have had to be ack'ed.

Talking is cheap - here's the example code



Have fun!

Monday, August 27, 2012

The not so obvious fact about tag libraries

I've came across this thing today and quite frankly I feel a little bit ashamed that I didn't know it yet. The concept of tag libraries is not new in the JSP world. It's just unusual to write tags because one needs to provide a tag library description file along with it and that increases the complexity of the solution (or so it is seen).

The nice thing about tag libraries is that they don't necessarily need a TLD file to drive them - a folder is more than enough for simple cases:

File /WEB-INF/tags/hello.tag
Hello, world! from a tag
File /index.jsp
<@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

<tags:hello />
It's really that simple!

There's obviously more to it (how to pass the tag body, how to pass parameters to the tag) - maybe next time :)

Thursday, August 23, 2012

GGTS 3.0 - first dissapointment

For the longest time I've tried and tried to use the navigation aids in STS (now GGTS) to move around between the controller and view. This feature is there in VS since forever and the buttons to do the same in STS have been around on the toolbar for a long time now. Unfortunately they don't do much..


You can torture them all you want but the navigation from within a controller action like
class HomeController {
    def index = {
        [ message: "Hello, world!" ]
    }
doesn't seem to bother those buttons much. Same for navigation from the view - or should I rather say it's the only "true teller in the bunch" as it stays inactive forever and for always...

I know, I know... I'm complaining again about new software and guys from SpringSource have done a tremendously good job and all that and I should stop yammering and congratulate them. But what should I do when the tooltip over the "T" icon says "Open the corresponding TagLib" and when I click on it while editing a controller it opens a "Create a new TagLib" wizard? Same thing with services and the "S" button. Guys, come on! Can't you at long last fix this disaster?

Anyways, I'll give it a try and let you know what I think about the functionality that actually works.

Wednesday, August 15, 2012

Gitorious in EC2

Previously I've written a lot about how tough it is to install Gitorious. By that time the script I provided was the only automated way of installing Gitorious (for free) on the Net. I needed that script to perform a test install of Gitorious and convince management that this software is the right way to go. Now that this goal has been achieved it's not maintained anymore - and it doesn't have to!

There's a new kid on the block from BitNami stable - the BitNami Gitorious stack.

I haven't seen it in action yet but it looks very promising:

  • 3 installation options (native installer, VMware machine, Amazon EC2)
  • the automated installer has all the dependencies
  • It's based on a newer version of Gitorious - from May 2012

I'm downloading it now - will tell you more about the quality of this release soon. It it'll pan out I'm going to give it a try in Amazon EC2 to see how it performs over there.

Have a nice day!

Monday, August 13, 2012

Removing stuff from embedded arrays in MongoDB

How to remove items from embedded list "items" by regular expression /item/ ???

db.example.update(
  { _id: ObjectId("5024d352ef2ff4c4a103db5f") }, 
  { $pull: { items: { $regex: /item/ } } }
)

Do the same but on all documents in collection:

db.example.update({}, { $pull: { items: { $regex: /item/ } } }, false, true)

Oh I so wish it'd be in the official docs - I spent 2 days trying to figure it out!

Wednesday, July 11, 2012

Ruby and Memcached tricks

I love Ruby. It's a great language. I love almost everything about it (maybe besides the symbol syntax... but that's just me I guess).

Today I fell in love with the alias method :)
require 'memcached'
cache = Memcached.new "localhost:11211"
cache.set "message", "Hello, world!"
puts cache.get "message"
All is nice up to the get and set methods. Wouldn't it be nice to use the cache["message"] syntax?
class Memcached
  alias :"[]" :get
  alias :"[]=" :set
end
And then:
cache["message"] = "Hello"
puts cache["message"]

And live is good again :)

Error installing memcached gem

I've been struggling to install memcached gem on my Ubuntu 12.04 box. Some error came out when installing native extensions:
rlibmemcached_wrap.c: In function ‘Init_rlibmemcached’:
rlibmemcached_wrap.c:13736:3: error: implicit declaration of function ‘sasl_client_init’ [-Werror=implicit-function-declaration]
rlibmemcached_wrap.c:13736:33: error: ‘SASL_OK’ undeclared (first use in this function)
cc1: some warnings being treated as errors
make: *** [rlibmemcached_wrap.o] Error 1

A quick look at what uncle Google has to say about it didn't reveal anything at first glance. A deeper search gave me the following address:

https://github.com/evan/memcached/issues/17

Basically the problem is that one needs to install libsasl2-dev package for this to work.

And live is good again :)

Tuesday, July 10, 2012

Grails 2.1.0 performance

In this installment we'll be checking out the performance of the latest (2.1.0) version of Grails compared to 1.3.9 and 2.0.4. So without further due let's jump right into the code.

The HomeController
class HomeController {
  def index() {
    def date = new Date()
    redirect uri: date.format("/yyyy/MM")
  }

  def page() {
    def months = [ '', 'January', 'February', 'March', 'April', 'May', 'June', 
      'July', 'August', 'September', 'October', 'November', 'December' ]
    [ year: params?.year, month: params?.month, months: months ]
  }
}

So we see 2 actions here: one that checks the current date and does a redirect and one that spits out some data to view. So let's see the view then :)

The page.gsp:
<!doctype html>
<html>

<head>
  <title>Galery</title>
</head>

<body>
  <ul>
    <g:each in="${1..12}" var="i">
    <li class="${i == month ? 'current' : ''}"><a href="/${year}/${i}">${months[i]}</a></li>
    </g:each>
  </ul>
</body>

And now - time for benchmark results. All applications started this way:

grails prod run-war

and benchmarked using Apache Benchmark like this:

ab -n 1000 -c 10 http://localhost:8080/example/2012/03 up until the point when the figures became stable (usually between 20-30 executions with 5-10 seconds breaks in between).

Grails 1.3.9

Server Software:        Apache-Coyote/1.1
Server Hostname:        localhost
Server Port:            8080

Document Path:          /example139/2012/03
Document Length:        776 bytes

Concurrency Level:      10
Time taken for tests:   0.526 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      942000 bytes
HTML transferred:       776000 bytes
Requests per second:    1902.84 [#/sec] (mean)
Time per request:       5.255 [ms] (mean)
Time per request:       0.526 [ms] (mean, across all concurrent requests)
Transfer rate:          1750.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       2
Processing:     1    5   3.5      4      41
Waiting:        1    5   3.5      4      41
Total:          1    5   3.6      4      41

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      6
  75%      6
  80%      7
  90%     10
  95%     12
  98%     14
  99%     17
 100%     41 (longest request)

Grails 2.0.4

Server Software:        Apache-Coyote/1.1
Server Hostname:        localhost
Server Port:            8080

Document Path:          /example204/2012/03
Document Length:        776 bytes

Concurrency Level:      10
Time taken for tests:   0.235 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      942000 bytes
HTML transferred:       776000 bytes
Requests per second:    4254.25 [#/sec] (mean)
Time per request:       2.351 [ms] (mean)
Time per request:       0.235 [ms] (mean, across all concurrent requests)
Transfer rate:          3913.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       5
Processing:     1    2   1.5      2      16
Waiting:        0    2   1.5      2      16
Total:          1    2   1.6      2      16

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      3
  80%      3
  90%      4
  95%      5
  98%      7
  99%     10
 100%     16 (longest request)

As we can see there's quite a performance gain between 1.3.9 and 2.0.4. Good!

Grails 2.1.0

Server Software:        Apache-Coyote/1.1
Server Hostname:        localhost
Server Port:            8080

Document Path:          /example/2012/03
Document Length:        776 bytes

Concurrency Level:      10
Time taken for tests:   0.333 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      942000 bytes
HTML transferred:       776000 bytes
Requests per second:    3002.16 [#/sec] (mean)
Time per request:       3.331 [ms] (mean)
Time per request:       0.333 [ms] (mean, across all concurrent requests)
Transfer rate:          2761.76 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       3
Processing:     1    3   3.0      2      29
Waiting:        0    3   2.9      2      29
Total:          1    3   3.0      2      29

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      3
  75%      4
  80%      4
  90%      6
  95%      9
  98%     12
  99%     17
 100%     29 (longest request)

It looks like 2.1.0 is a little bit (actually 1/4th) slower in this case than 2.0.4. Interesting...

Rails 3.2.6


For comparison I've done a similar experiment translating the the same routing, controller and view to Rails (just out of curiosity).

We'll start the application under Unicorn with 4 workers (that's how many cores my notebook has)

unicorn -E production -p 9393 -c config.rb

And the config.rb
worker_processes 4

Server Software:        
Server Hostname:        localhost
Server Port:            9393

Document Path:          /2012/03
Document Length:        728 bytes

Concurrency Level:      10
Time taken for tests:   1.109 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Non-2xx responses:      1000
Total transferred:      983000 bytes
HTML transferred:       728000 bytes
Requests per second:    901.84 [#/sec] (mean)
Time per request:       11.088 [ms] (mean)
Time per request:       1.109 [ms] (mean, across all concurrent requests)
Transfer rate:          865.73 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     6   11   5.9     10      64
Waiting:        6   11   5.9     10      64
Total:          7   11   5.9     10      64

Percentage of the requests served within a certain time (ms)
  50%     10
  66%     11
  75%     11
  80%     12
  90%     14
  95%     15
  98%     19
  99%     55
 100%     64 (longest request)

Sinatra


Now let's take a look at how a similar case will handle a Ruby framework, Sinatra.
require 'rubygems'
require 'sinatra'
require 'slim'

disable :logging

get '/' do
  date = Time.now
  redirect "/#{date.year}/#{date.month}"
end

get '/:year/:month' do
  @current = Time.new(params[:year], params[:month])
  @months  = [ '', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]
  slim :index
end

__END__

@@ index
doctype html
html
  head
    title Galery
  body
    ul 
      - (1..12).each do |month|
        li class="#{@current.month == month ? "month current" : "month"}"
          a href="/#{@current.year}/#{month}" #{@months[month]}

We're using sinatra with slim templating engine (my favorite!). We'll start the application the same way as the Rails app above.

Now for the actual results:
Server Software:        
Server Hostname:        localhost
Server Port:            9393

Document Path:          /2012/03
Document Length:        709 bytes

Concurrency Level:      10
Time taken for tests:   0.449 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      922000 bytes
HTML transferred:       709000 bytes
Requests per second:    2227.71 [#/sec] (mean)
Time per request:       4.489 [ms] (mean)
Time per request:       0.449 [ms] (mean, across all concurrent requests)
Transfer rate:          2005.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     1    4   2.8      4      29
Waiting:        0    4   2.8      4      29
Total:          1    4   2.8      4      29

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      5
  80%      5
  90%      6
  95%      7
  98%     12
  99%     22
 100%     29 (longest request)

Ok, now you can really see the difference in count of bytes sent over the wire. That's due to the differences in view technology (slim is very compact in the actual output)

At the end of the day the performance of both Grails and Sinatra (even though not very comparable) is similar with a clear performance winner :) Rails lacks a bit in performance.

There is however a difference in startup time and memory usage:

FrameworkMemoryStartup time
Grails 1.3.9485.1MB21.95s user 0.76s system 109% cpu 20.656 total
Grails 2.0.4699.3MB53.51s user 1.38s system 152% cpu 35.942 total
Grails 2.1.0713.1MB30.19s user 1.30s system 106% cpu 29.609 total
Rails 3.2.6169.8MB9.64s user 0.68s system 259% cpu 3.977 total
Sinatra on Unicorn82.1MB1.46s user 0.18s system 55% cpu 2.963 total

Have fun!

Friday, June 29, 2012

Groovy 2.0 is there - but useless...

As much as I waited for the 2.0 release of my favorite programming language I'm grumpy about the quality of the release - or rather the lack of it. Here's why:

One of the most important (for me) things that have been added to 1.8 (I guess it was), the annotations to decoratively add logging facilities to classes is broken.

https://jira.codehaus.org/browse/GROOVY-5557
@groovy.util.logging.Slf4j
class Test {
    Test() { log.debug "Here" }
}
But the best thing is the error message:
Caught: BUG!
Until that's fixed I recommend sticking to the stable version.

Not a nice day,... unfortunately...

Tuesday, June 5, 2012

Syntax highlighting in less

I've been looking for this way to long not to make a note on my blog.

Syntax highlighting in less - Big thanks for the author of this blog post for making the contribution!

So what you do is:

a) you install the highlighter
apt-get install source-highlight
b) you enable less to use it by adding the following lines to your ~/.profile
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=' -R '
And believe me: life is really good again :)

Tuesday, May 29, 2012

Turning to the dark side

It's been over 15 years since I've tried switching to Linux for the first time. I distinctly remember the first experience I've had that day when "to install" really meant "to compile the kernel". It was awful, bad and completely not approachable by mare mortals. That's how I met Linux.

The astounding thing about it though was that it kept nagging me over and over again. The sole idea of having an operating system for free and be able to customize it to your liking (although excruciatingly painful) stayed with me since that day. I was sold to the idea of using free software and some years later to give back something in return.

Some years later I've learned that there's nothing like a good Linux server and bought into the idea that as far as I am concerned Linux is The Operating System for servers but as far as Desktop is concerned it just doesn't measure up to the competition.

It's years later now, the new millennium, and I can finally enjoy a Linux distro prepared truly in a way that can compete with what I've already knew. It's Ubuntu 12.04 LTS - the first Linux I feel is worth using at all on a desktop.

So here's what I value the most and why I think you should just switch to Ubuntu 12.04 and never look back again:

  • Unity: User Interface that rocks! The first ever successful attempt to create a good looking and usable alternative to Windows
  • Memory consumption that doesn't eat all you have for breakfast
  • Hardware support that blows your mind
  • It's Linux inside all this meaning you can do whatever you please to it

My advise to you is: If you have not tasted it yet go ahead and try it out! It'll blow your mind how much space you can get in almost every aspect of your PC, starting with hardware requirements and ending with available screen space. It is truly astonishing!

Have a nice day!

Wednesday, May 16, 2012

Making Groovy scripts maintainable

Writing Groovy code is cool. Fixing them some time later can be hard as they tend to grow out off control pretty soon. Finding an install.gsh that has 100+ lines is not hard. And since Groovy is a very expressive language those 100 lines are not making the compiler happy - it's actually stuff to be done.

I have a bad feeling about this

In Java we're pretty much used to either creation of big, ugly, monolithic classes or going the completely opposite direction with Spring or some other dependency injection framework. In scripts we tend to rather go the first way which makes them unmaintainable and hard to understand. Why? Because historically shell scripts were created this way and we tend to think in this way. It's a script, isn't it?

These are not the droids you're looking for...

In fact Groovy scripts are not scripts at all. They are classes, that will have a main method. The only difference is that they are automatically compiled when needed. This means we can use all the good Java stuff when writing Groovy scripts: classes, external libraries through @Grab annotation and so on.

Here's an example:

Configuration.groovy:
class Configuration {
    String path
}
install.gsh:
#!/usr/bin/groovy

println new Configuration(path: "/tmp")

Since all the classes are in the default package (inaccessible from pure Java!) you don't need to import them manually. Should it make sense to split them into folders you can always do that:

org/example/Configuration.groovy:
package org.example

class Configuration {
    String path
}
install.gsh:
#!/usr/bin/groovy

import org.example.Configuration

println new Configuration(path: "/tmp")

Further more you can write tests for your classes, make sure they always work even if some mad cow enters the arena and goes Hulk on your code :)

May all your scripts be maintainable forever and for always!

Why things are so damn complicated these days?

I've been going over some older projects these last few months. Partially because this is part of my job as an architect (to know what's in the box and to propose better solutions if the existing ones are reaching to the limit of their usefulness) but partially because I've gotten really passionate about The Legacy. And I don't mean it like "oh, this is the legacy code we hate" and all that but rather to see and understand what causes today's problems and how we can make the world a better place. Well.. at least for us, programmers...

The endless possibilities

If I gave 10 people a general purpose task, let's say describing a simple domain I fear that I'd get back 20 different designs. I get that kind of impression a lot recently when faced with the same problems solved in many different ways by the same person. It is just a matter of time before that happens. Quite frankly that is a good thing: it means that person is trying something new and most probably learning from mistakes they previously made. But what about the code we have already committed? Is it really there to pollute the air for ever and ever? Is this what we call "Legacy Code" today?

We're screwed! What should we do?

Tuck the tail and run like hell! That always solves the problem, doesn't it? You made a mess first trying to do the right thing, then because you were under constant time pressure and later on because things were already so damn bad you didn't really care about this piece of crap. The easiest way out is through the front door. Usually the justification is "because I can't really develop myself any further" or "I'd like to learn more about this and that but the current place I'm in sucks so bad my boss will not allow me to". All those crappy statements are in fact about one and the same: "I've made a mess and as sure as hell I ain't gonna clean this stuff up!". If you think about it, it kind of makes sense, doesn't it? Why should you even remotely care what will the new guy think about your capabilities? You ain't gonna be there to witness it anyway, right?

Are we there yet?

I predict that some time very soon a lot of big systems is going to undergo a series of major setbacks. Anybody wanna take that bet? Would you rather sink with the ship or go down swinging?

I know it is not going to be very popular what I'll say right now but I'll say it anyway: For God's sake! Write clean code! It's not so damn hard, really! If you ain't got the skills then stop complaining about your boss that he/her is not giving you the development opportunities you'd like. It's your job to do it right and it is your job to develop yourself. Nobody is going to do that for you, not in a million years! And if you'll ever find yourself sitting in an open space bitching about the quality of code then don't forget that you have hands, eyes and a biological CPU to clean that up if you think it is a mess. Don't run from it - fix it! If you don't know if it will work after you do fix it - then fix it anyways! Clean code lives shorter, is easier to replace than that old, shitty stuff that stinks like hell every time you touch it!

So why are those things complicated again?

It's easy: we made them that way. We make them that way every single day (despite the heroic efforts of Uncle Bob and others). Period. They didn't create themselves - we're not quite there yet with the AI to procreate on its own. At least it is not a common scenario in the web database front-ends :)


May the force be with you!

Monday, April 30, 2012

Call to new graduates

This is the kind of posts that will be very specific to the country I live in, Poland. It should come as no surprise that the content will be in polish.

Profesor do młodych wykształconych bezrobotnych

I don't dare to add another word to what this letter says. It's all true. Period.

Thursday, April 5, 2012

Grails, Hazelcast, Hibernate and 2nd level cache

If you'd like to use Grails with Hazelcast caching solution you need to be aware that there is an issue with Hazelcast compatibility with the version of Hibernate used by Grails (3.6.10.Final).

Reading on the Hazelcast Wiki you'll be tempted to use the "new" ways and specify hibernate.cache.region.factory_class. Unfortunately that doesn't work. See this bug report for more information.

But there is hope! Instead you can specify hibernate.cache.provider_class and everything should work as expected. Hibernate does all the adapter heavy lifting and life is good again :)

Anyways, Hazelcast is plain ecstatic - you should really give it a try and get a feel of what a good library feels like when you use it. It's awesome!!!

Back to the roots

I've been going over some of my old stuff that I wrote back in the day in 1998 when I started to work professionally as a developer. Among other things (like using polish names for variables, classes and what have you and seeing absolutely no automated tests whatsoever) I've also spotted some nice stuff that I wanted to share with the class.

DDD was normal

Back in the day having a TTeacher (Delphi) class that knew about the schedule and could tell you which one is going on at a particular point in time was no big deal. Classes were not PODO, they actually knew how to do stuff. Just to give you another example here's something that struck me fairly recently:

Given a Company company and a Candidate candidate if the company decides to hire candidate then the output is an Employee employee. This could be written down more/less like this:
Employee employee = company.hire(candidate)
Now take a look at this and tell me how would you call this now a days? IT IS DDD at its purest! We have domain classes that can be retrieved from some source and operated upon.

Short methods were normal

Actually this isn't entirely true but it was pretty much the standard that a method contained calls to other methods that contained calls to other methods. I kind of felt the necessity too express things clearly or else the code would then be unreadable. So the whole "one-screen method" or "CCC < 5" was kind of natural even though nobody told me to do so. It just felt right.

Coding standard

This has surprised me most: All (and I mean it when I say "all") of the code has one and the same coding convention. There's no deviation whatsoever when there is no special case that needed separate consideration of formatting rules. And above all: it was based on VCL's code. Maybe not 100% equal but very, very close. Now that is some code you read like an open book just a after minutes of getting used to the coding style.

I, J and K were iteration variables - and nothing else!

Back then there was a number of variable names that were reserved for a certain task. "I, J, K" were always and without exception the iteration variables. There was no itinerary "i" or class "K". Similarly "S" was always a temporary string variable. The code was so damn short even though there were no closures to express things better!

What has changed?

Well, what has really changed after the year 2000? There were 3 major factors that influenced me to write worse code:

  • I stopped writing code for myself
  • The schedule became a lot tighter
  • The complexity sky-rocketed when I started writing calculation-intensive apps

With that in the picture when I look at the code I wrote after 2001 I feel like the IQ of the author just dropped 30-40 points!

The Corporate times

One very much disastrous time for me was the time when I started working for an international corporation. And I mean it in every single way one can possibly imagine. That was the place where I learned that even though the application is mission critical (people's life depends on it!!!) it is not really important that the quality of the code is kept at a reasonable level. It was really enough that the application worked around 60%-70% of the time! AAAAARGH!!! I remember fighting the original team members to the death trying to teach'em about quality of the code. That was the time I worked for the first time with "legacy code".

Java - the ultimate readability killer

As much as I hate Java the language for its verbosity and lack of advanced features like properties I'm inclined to say that the worse thing about Java are Hibernate and POJO-style programming model. Why?

Let's summarize what a POJO is, shall we? A POJO (plain old java object) is just a class. This simple and quite good idea turned into a disaster with Hibernate where most of the objects started to be those gigantic pile of annotation crap but besides that didn't contain anything else. That's how I came to learn POJOs as a programming style: dump, idiotic and trivial classes. So it is not a POJO anymore - you might say. And you'd be right! That'd be a dumb Java Object (DJO) but unfortunately when you see code that has to do with EntityManager, pure Hibernate or some other ORM (which is very likely) this is what you are going to experience..

Back to the roots

Now that I am old enough to look back and see the mistakes I made I've decided to find a way to get back to the programming style where a "Company" has the ability to "hire" a "Candidate" which would spit out an "Employee" but at the same time to keep things as separated as they should be for the sole reason of testability.

Spring sucks - Grails to the rescue

It should come as no surprise that my first attempt to mimic a rich domain object was undertaken in Spring. I admit I'm no spring expert and I still believe it is a vastly bloated library but I do get the idea behind it (unlike myself 3 years ago). But stuff is very much complicated with pure Spring. And one thing in particular - dependency injection in domain classes.

When I say that Spring sucks it really means that "Spring sucks by comparison to Grails". It is really great that spring does all the wrapping and all that but making domain classes automatically injectable borders on the infinity...

The whole point to this exercise is to have an object model that is usable without GUI and still makes a whole lot of sense.

Candidate.groovy:
class Candidate {
    String firstName
    String lastName
}
Company.groovy:
class Company {
    String name

    def hiringService

    Employee hire(Candidate candidate) {
        return hiringService.hire(candidate)
    }
}
Employee.groovy:
class Employee {
    String firstName
    String lastName
}
Now how about this:
Employee employee = company.hire(candidate)
Isn't that nice? And it is so damn readable!

The best part of all this is that in Grails domain classes are by default eligible for dependency injection which means that the hiringService is injected automatically. And I really mean AUTOMATICALLY! This means that when you do "new Company(name: "Test")" the "hiringService is automatically injected!!! This is the best thing ever!!!

I strongly recommend you try to look at your application from within the code and see if you can read it as you'd read plain text. If not there's something wrong with either your eyes or the code before you. If I was a betting man I'd go with the latter one :)

Write clean code!

Saturday, March 31, 2012

Spring, MVC and Groovy

If you'd ever find yourself choosing the whole Grails stack only because it is sooo much easier to setup a project by comparison to pure Spring MVC I strongly recommend you try out the new Spring MVC on Groovy Maven archetype. That sucker punches in Spring in an easily selectable version, Groovy (1.8.6) and the groovy-eclipse compiler (also 1.8.6), JUnit and Spock, JavaMelody for monitoring - basically all you need to create a web application using Groovy, some IoC container, an MVC framework and test all that in a reasonable fashion.

http://www.testdriven.org/spring-groovy-web-archetype

Give it a shoot. If there's something you'd like to add to it then by all means - drop a note on GitHub.

Have a nice day!

Wednesday, March 21, 2012

Fatal dual mistake

http://bit.ly/GIbHyi
http://bit.ly/Hu5Ugl

The video material is in polish but it goes pretty much like this:

A group of anti-terrorists slams on doors of some people, kicks'em, slacks'em, drags'em over the floor, throws some grenades - basically they are dealing with some kind of bad guys. However they are "mistaken" so they say "Ups - sorry - never mind" and go one floor above. Then they do the same thing one floor above and again "Ups - sorry - never mind"... Third time's the charm, right? So there he is! The so goddamn dangerous car thief with a gun.

WHAT THE FUCK?!

Is anybody with a gun a terrorist these days?! Why don't you guys at the police go and RTFM what terrorism actually is! And if you ain't got a manual about it there's plenty of material on the net about it! A car thief ain't one of them - that much I can tell you. He's a thief, probably a murderer if he kills someone. But if that really is the case that owning a gun makes you a terrorists then those poor man and woman have been terrorized by terrorists because they did have goddamn guns. Big guns!!!

It's outrageous! I can't believe I'm living in a country where a car thief is a terrorist, where innocent people are being treated as criminals with an "Ups! Sorry! Nevermind" attitude afterwards! WHAT THE HELL IS GOING ON!?

Spock - the testing as it should be done

It's not very often when I get really excited about some piece of software. I'm not saying it doesn't happen because that wouldn't really be true but those times when I get the willies just after taking a brief look at something are really rare. Today is that day. Today I met Spock.

Well technically speaking, I met this guy, Luke Daley, on 33rd Degree conference when he gave a talk about Spock and how it differs from what you can do with regular JUnit tests.

OK. Enough of about the conference - let's see some code!


If you put this into an example.groovy file and execute it it'll actually run 2 unit tests. On the test we see the following sections:

  • We see that Spock is being grabbed (excluding Groovy since Spock tries to pull in some non-existent version)
  • We then create an Example Specification (that's how you should read the class declaration line)
  • We then say that we define that a sum of a and b is equal c (whatever those are)
  • Then some magic happens: we say that we expect that a + b is equal to c
  • And last but not least we define the set of data to create separate tests in a tabular, fitness-like form

Here's the output of that script (you should run it yourself to see it really do all the work!):
JUnit 4 Runner, Tests: 2, Failures: 0, Time: 15
Isn't it great? You spent about no time creating a few lines of code, got 2 test cases in return that execute in no time and are no match for JUnit's @Parametrized tests (which sucks big time by the way).

This is obviously the absolutely simplest example possible. There's a lot more to Spock than meets the eye here and the deeper you go there the more you wonder how's all that even possible. This is why I'll stick to Groovy as the most versatile, universal, non-problematic and powerful language ever created for the JVM.

Monday, March 19, 2012

Who broke the build massacre

Recently it's becoming more and more popular to create gadgets that "attack" or "shame" the one that broke the build on Jenkins. Some of them are really creative and funny!

This doesn't change the fact that it is outrageous and completely unprofessional that such "tools" are even needed. All that under the assumption that the person that did break the build (or even made it unstable) couldn't care less about it.

So my advice to you: do care about the stability of your work! Or else don't do software development - it's out of your league! You don't fit in!

Saturday, March 17, 2012

REST Service - the two words that make no sense

It's been bothering me for like ages and I finally decided to put a blog post about it. Let's start with a couple of definitions:

WebService - a way of servicing someone (like "giveMeSomething" sort of way) over the web

REST - Representational state transfer. We're transferring a representation of the state (of someone) - not servicing anybody.

To see why is this so damn important first let's see what classic SOAP web services look like in practice. They consist of 3 things:

1. WSDL / XSD (description)
2. Endpoint (URL)
3. XML (data)

In fact there's one most important thing to note: the "endpoint" is/has usually a verb: "go get some flowers", "fetch history", "make me pretty" - you get the idea, right?

Now on the other hand REST has nothing to do with verbs - REST deals with nouns: people, cars, addresses, gasoline, money... So how do you make a person give money for gasoline for their car to allow them to get to a certain address they are heading?

WebService: MySuperDuperWebService.makeHimPay(Double money)
REST      : GET /person/54623445/account/1/balance (get the current balance)
            PUT /person/54623445/account/1/balance + current balance

The difference here is you're not giving a REST place orders other than update, delete, retrieve.

So the next time you'll be tempted to say REST service please bear in mind there's no such thing. What is however named as a "REST service" is a URL, that responds (usually) to POST (cuz that's so damn easier to pass data to) that has no predefined schema (usually..) and that instead of giving you back XML hands you over JSON (possibly using JSONP). That my friends is a definition of a JSON Service and has nothing, absolutely nothing to do with REST whatsoever. Even worse is the term RESTful web service. My God is that a horrible thing to say. It seems like the service being referred to rusts in peace and should never ever be called.

Make the world better for a change!

Monday, March 12, 2012

ExtJS inheritance strategy

I've been struggling for quite some time to come up with a strategy for writing ExtJS code so that no matter what kind of class I'm extending everything looks the same. Today I've finally ended up with what I wanted :). In this case I'm working with Ext 2.0.2 but it should apply without any changes to all versions of this wonderful library.

Challenges


I've set a goal to be as follows:
  1. Instances will have proper class names and not some generated names
  2. Declarations will present themselves in the code exactly the same way regardless of the base type
  3. The code will look understandable

The template

First we need a namespace so that the global one doesn't get polluted:

Then we need to define the constructor (this will give the actual name for the class later on:

And finally we'll implement some public methods:

This way the actual class is "Namespace.ClassName" and not some wierdo, public methods are cleanly separated and default configuration is in one place.

As a bonus this pattern works for every single class - no matter if it is a component (GridPanel, Window) or not (ie. Store)

I hope this will make someone's coding adventure a little bit less messy :)


Have a nice day!

Thursday, February 16, 2012

IBM download manager

This post is just to make a stand against corporations like IBM that think we're so stupid and ugly we can't download and run a simple exe from the net.

Well, guess what: we can! Stop the damn reinforcement to give you all the personal information you don't need! It's against everything - not just the European data privacy laws.

And above all: come on guys! Do you really feel it is necessary these days to force the user to use a Java-based download agent? Ain't it simpler to finally enable HTTP resume on your server? Or can't you do that?


What a damn shame that the big ones can't keep up...

Wednesday, February 15, 2012

Grails, documentation and images

A quick tip of the day:

if you're creating a Grails project and want to provide documentation for it it might stand a chance you'll need to include images in that documentation. Unfortunately the official documentation is very sparse in this regard showing you only the syntax for including external images. As it turns out it is possible to have images that come specifically with the docs. All you need is to specify
grails.doc.images = new File("src/docs/images")
and then include them in the docs as usual but without a URL
!my-image.png!
Thanks to Lauro Becker for that hint!

Wednesday, February 8, 2012

Grails, Bootstrap and a pile of crap

Today we're going to pick on a good idea gone bad. The good idea being the ability to kickstart a Grails application with Bootstrap from Twitter. What's gone wrong is the actual implementation. But let's start from the beginning.

Create a new application

grails create-app example
You've seen this over and over again, right? Nothing new here. Let's install the kickstart-with-bootstrap plugin. For that in Grails 2 we'll modify the grails-app/conf/BuildConfig.groovy's plugins file like this:
plugins {
    runtime ":hibernate:$grailsVersion"
    runtime ":jquery:1.7.1"
    runtime ":resources:1.1.5"

    build ":tomcat:$grailsVersion"

    compile ":kickstart-with-bootstrap:0.5"
}
Easy enough. Let's compile the application first (grails compile) for the build system to fetch all the plugins and do its other bits. After that we'll execute grails kickstart-with-bootstrap (the one that just came about with the kickstart-with-bootstrap plugin), answer all questions positive and we're done. At this point you can create a domain class and a controller but that's not really the point of this blog. The point here is to pick on the way the master kickstart layout looks like.

The good part

Well... the appearance is really great! But let's face it: it's so because Bootstrap looks great. It has all the usual stuff, the top bar, nicely looking fonts, the layout is floating. That's all nice and dandy...

The ughly

Let's open the newly created grails-app/views/layouts/kickstart.gsp. Here are a few points I'd like to make:
<%--  <div class="container">--%>
What for the love of god was going through the author's mind to leave commented out code in official release? And believe me this is not a single place. There are actually 21 of those!

But that's as long as purity goes - let's try to find something scarier:
   <g:if test="${ params.controller != null
      && params.controller != ''
      && params.controller != 'home'
      && params.controller != 'login'
   }">
I mean what the heck is this?! We're in a reusable layout! Ok, you might think I'm bitching about one place in the layout... Well actually no. There is more:
class="${ params.action == "list" ? 'active' : '' }"
class="${ params.action == "create" ? 'active' : '' }"
So a list should be shown, right? Or maybe we're actually creating an entity... Hm,... What was going through the author's mind at this point???

Does it by any chance belong in a reusable layout?!

Summary

I personally think that such a plugin is a blessing for someone that starts a new project now and then. The original Bootstrap plugin just doesn't cut it. But for the love of God do it right! By providing such a half-done tool to others you're actually doing more harm than good.

I'd love to get in touch with the original author to straighten things out. I love the idea of having a plugin that supplies a set of scaffolding templates and a layout to get me started with a nice look and feel quickly. Never mind the fact that it doesn't use the resources plugin. I actually like it. But it needs to be done the proper way, the way it is reusable!

Thursday, February 2, 2012

Hosting Git like a professional - Gitorious!

I've been struggling over the past months to find a way to install Gitorious on my private network to have the same kind of power in hosting repositories as GitHub or BitBucket. For sure Gitorious is not as powerful as those guys but it is more than enough for in-house development.

And tada! Here it is: the automated process of installing Gitorious on a fresh Ubuntu or Debian machine.

Here are the main components being installed:

1. Ruby 1.8.7 - enterprise edition
2. MySQL 5.1
3. Stomp server
4. Apache HTTP server with Passenger module
5. Lots of supporting utilities
6. Last but not least: The Gitorious application.

The procedure to install using this script is as follows:

1. Make sure you do have sudo installed (on Debian you need to install it manually!!!)
2. wget the script from GitHub in raw form.
3. Edit configuration options found at the very top of the file
4. Launch bash (_NOT_ just sh!!!) with this script (bash install.sh)
5. Press enter when asked

One note: if the script seems to be hanging mid air (especially at the end of the installation where ruby interpreter is being invoked to add admin user and so on) press Enter. After pressing Enter the script will continue almost immediately.

Now this is a very basic installation of Gitorious. For example there's not Git over HTTP support available. But all the rest should be working just fine :)

Give it a shot! Give it a shot even if you don't need it :) Gitorious is an amazing piece of software and can help you out in Git administration like no other software will.

Monday, January 30, 2012

The quality of software - part four

In today's installment we're going to look at the quality of code from a broader perspective taking into account all the previous installments. I'll try to outline the difference between a good project vs a bad one, even though they both make money.

What is software quality


There is and always have been a huge gap between the end product and the code. That gap has been introduced some ages ago with tapes and cards. They didn't make any sense whatsoever to the end user as much as the code today makes no sense whatsoever to the user of your next brilliant application. Let's face it: programming is an art not everyone can do and even less people can understand (even if they are in fact doing it). So what makes up for a quality product?

There are 2 sides of the story: one is the "make it to the market ASAP" side which tends to solve the problem of competing with others. Depending on the size of the project it might seem like it is a good idea to rush developers into doing the least that'll actually work or to push in some features that'll make the customers so much happier.

The other side of it is that software is created by human beings and that in turn reveals the weakest link in the process of creation. Even though we can be extremely creative in our brilliant ideas we tend to make mistakes, tons of them. The faster you write code the less careful you are and the more bugs sneak in into the final creation...

Sure there are people that get it right the first time, always, but there's so few of them you could put them into the "exception" group for a rule that programming is a very error-prone activity.

So quality in the broader sense is something that is good for both the end user of our software as well as the agility to fix any bugs or introduce new features into the project.

I can fix any bug in the projects I wrote - and do it FAST!


That's a common misunderstanding that one can find bugs within software product they wrote. It is so because we, humans, don't stay the same for the duration of our lives. We tend to learn new things even if we don't admit it to ourselves. Let me tell you that looking at the code that I created 10 years ago scares the hell out of me! And I'm not the only one...

So even though we'd like to think that if we'd be the only ones creating the software we will at some point face the danger that our old idea was plain wrong. Due to the changes that happened to our lives we tend to think differently and curse that brain-dead monkey that wrote this stuff some years ago.

Uncle Bob says: Why did you write it?!


One should always try to be at ones best when writing code. Period. There's no excuse for writing unsustainable code - be it for private use or for the company you work in. There's no excuse to write code that will make you ask WTF every 5 moments. That'll make your life and the life of your fellow developers a living hell. And the worst part of it that it is not only you, the programmer, that will suffer from it - it's everyone in the chain starting with the guy that wrote it and ending with the programmer's worst enemy - the end user... How come? Well first of all you will curse the day you entered the project, then your architect will throw thunders at you for writing that piece of crap, then your manager will tell you that you need to go faster and cut corners and at the end of all this there is the customer that will probably go ahead and buy another solution that will not break as often as the piece of crap you and I created.

My old boss when asked about what is software quality explained it more/less like this:

The quality itself is not a goal for itself. But at the same time it is vital in order to sustain the company and the products it delivers

What it says is there's no one or the other. The world is not gray in this regard. If you make a loan in the code you'll have to pay it back in order to go further...

Summary


The summary of the whole series is quite simple: do TDD, do continuous integration, do pair programming, do code reviews, do make the querulous customer happy! They deserve it. You deserve it. You deserve to do the right thing. And if you can't do that then quit now and never look at software development again.

Sunday, January 29, 2012

Grails and Ratpack

Today I've found out about the Ratpack plugin. Being a huge Sinatra fan I was quite fond of the Ratpack framework to begin with and naturally I was amazed to see it incorporated into Grails as a plugin. And it works!

Let me walk you through a very simple example of using Ratpack inside Grails.

We start by enabling the Ratpack plugin in our application. I called mine app "example" so all the URLs will be prefixed with "/example". To include the plugin you need to add it to grails-app/conf/BuildConfig.groovy like this:

With that in place we'll create a domain class to have some data to display. I'll go with the simplest possible class that represents a person grails-app/domain/Person.groovy:

Simple enough, right? Let's go an make some use of it in a Ratpack file. Create a folder grails-app/ratpack and inside of it a file called PersonRatpack.groovy:

As you can see here all we're doing is responding to the GET method on some root URL. Ratpack uses SimpleTemplateEngine from Groovy. We have to tell it where to look for templates Config.groovy:

So let's create one to display the people from database grails-app/ratpack/templates/list.html:

Simple enough, right? It almost looks like a mix of ERB and expression language from JSP. I think you'd have to ask the authors of SimpleTemplateEngine if those similarities have been made on purpose of if this syntax just seemed to be the only one that made sense...

Anyways, with all that in place we can add some people to the database in our grails-app/conf/BootStrap.groovy file like this:

And that's it! start the application, navigate to http://localhost:8080/example/ratpack and you should see the list of two people on the screen.

Needless to say you can accomplish the exact same thing using a controller and a view. The idea here however is to show you that there is life beyond the standard artifacts and that the beauty lies in diversity!

Hope this will help someone get started with this awesome piece of code!

Thursday, January 19, 2012

Intermission

Today's protest against SOPA and PIPA acts have had a tremendous impact on Internet traffic around the globe. Millions of people have been made aware of the irresponsible propositions of attack on freedom in the Network. You can't name it any other way than censorship which is the public enemy number one by any definition.

I support Wikipedia, WordPress and all the others in their campaign against SOPA and PIPA!

God help us all when the time comes to kill the last free medium on the planet.

Friday, January 13, 2012

You don't get to drive - part three

In the recent installments we've looked at software development as a discipline that has its own way of working. Its history, however brief, didn't leave any room for being mistaken with anything else. It is the single, most dynamic part of our modern society. But how do people handle it?

If you're young, in Poland it'd be under 18 years old, you don't technically get to drive a car without adult supervision. That's pure theory nowadays but at least that's how it should be. Being young and funky gets lots of stupid ideas into your head like driving over 100km/h in town chasing a friend without knowing the road ahead. Been there, done that. It doesn't mean I was in my right mind when I did it.

There are places where being wild is good. Certainly things where life is at stake are not the kind of ones where you'd expect one to go all in without training but that's what is actually happening all the time. People (including me) are by definition reckless and unpredictable and nothing is going to change that. There's a reason for that: had we been reasonable all the time no big discoveries would have happened. We wouldn't know about the continents of America, we most probably wouldn't know that radiation is actually bad and finally we'd probably die of hunger had there been no brave hunters risking their lives to master hunting animals.

In today's modern world however we do have the opportunity to make less mistakes - at least less of those obvious ones. And with that let me present you with a definition of two words in German:

Verfügen über - to have the knowledge about something, used in the sense "I do have a driving license"

Beherrschen - to have the ability to do something, used in the sense "I do know how to drive a car"

With those two there's little difference, right? Usually if you do have a driving license you should be capable enough to drive a car. But can you? Is that really what is going on?

Let's try to apply that to software development for a moment. It is obvious you need to study to get the diploma to be a software developer. But does it mean you can do software with it? Can you actually make a living with it? As it turns out yes, you can. The discipline is so vast and by all means so naively taken by so many companies that the moment you step out of school you're ready to go, hot shot! You are the man with the skills. But are you?

In my experience with young graduates and with students it was seldom the case when a young developer right after school was really ready to go. Actually I know only two people that when they came to work at a company with me I felt respect for the wealth of knowledge and humility for things they don't know. And so far it was just the two people I know this far that were open to the suggestion to learn new stuff. There's been sooo many cases where the lack of will to learn new technical skills was demonstrated along with arrogance (not to be confused with confidence) I couldn't even count.

The worst thing about it is they really do get hired! And believe me when I say there are companies where a fresh graduate hired almost off-the-shelve gets the ability to flush the project they are working on literally down the toilette. That means a very young in terms of experience person gets commit access to software that costs hundreds, sometimes even millions of dollars! And we're not talking sporadically - it's how certain companies operate! There's a saying that "there's no person that can't be replaced with a finite number of students". That's how the new markets for software developers came to be.

Obviously in every rule there are exceptions. I know that in the area there are maybe 15%-25% people that really get what software development is all about. And that's actually a very good ratio considering that software development has been announced one of the best paid jobs for graduates and is also viewed as one of the easiest ones in terms of rules.

Yes, rules... What kind of rules are there?

Among those professions I just mentioned there's an MD and a lawyer. Once you're done with your study you don't get to cut-open anybody for any reason, right? You need to gain practice under a trained eye of someone with that experience already there. Otherwise nobody would trust you with a knife doing god knows what to the body of a living soul! Same thing with lawyers. I hope you catch my drift here.

But is it like this with software developers? Do they need to undergo a practice to get certified as software developers that can cut-open (refactor), create and destroy (god-like powers) and provide real value to customers earning big big money for their employers? I think not. I'm sure there are companies that do tend to forbid new hires (freshly out of school or not) to commit anything until they have been long enough with the company to get what they are doing. But it's not always the case. They are hired to do the job, to be resources, not engineers. They are just numbers that mostly do not add up. But there's hope that not everyone is as mad in their doing as the ones I've just mentioned. There's the opensource community that follows exactly the kind of path that it should. You start small, familiarize yourself with the code, maybe submit a patch or two, become a contributor first, gain the trust of other team members and finally if you prove yourself worthy you get the commit access. By the time you get it you'd do harsh things to people for causing harm to the project. I wonder why people do put the information about being a commiter to some opensource project on their CV.. Interesting, isn't it?

More than anything else one needs to remember that software development is about learning and practicing. It's not always something new you need to learn. A lot of times it is something really, really old. Without knowing and above all without practicing it is not possible to "beherrschen" something. Nobody was born with the ability to drive an F1 car. You might "feel it" but you need to train to be good at it. You do need to get your hands dirty!

Have a nice weekend!

Friday, January 6, 2012

The alchemy of software development - part two

In this installment let me walk you through the valley of shadows nobody using computers wants to look at. Let’s have a look at what creating software feels like, what it actually is and why at the end of the day you should care how it’s done.

Software isn’t some forbidden science. It might be hard to understand by mare mortals but it definitely is not something that you can’t do even if you’re not very much into it. But should you? How does it feel like to be a software developer?

Imagine you have this completely obedient servant ready to do everything without hesitation. You tell it to jump of a 100-story building without parachute and it does it. Right down on its face. Splash! Boom! There’s blood everywhere, harsh words,... But nobody cares - it was your servant and you have had complete control over him. The ultimate power! That’s you and the machine. And programmers do that sort of things every minute of every day when they create software. Up to the point when they figure out that the servant lives longer when he has a parachute that actually opens. Sometimes the servant goes haywire and does things the developer does not expect him to do. Suddenly he might refuse to jump even when all the precautions are in place and ready to go. Jump! Jump! Damn it! Then a moment of revelation happens: the developer instructed the servant not to jump if it’s raining, which makes perfect sense. He just didn’t realize the definition of rain was actually not very precise.

The problem with your servant is that he understands only a very specific language. That language has only two words: a zero (0) and a one (1). Fortunately enough scientists realized that ages ago and created human-to-servant translators called compilers. Other mad scientists though that telling your servant to jump without parachute is a bad idea all along and created safe nets forbidding you to do that at all. That’s how runtime environments like Java or .NET came to be... All of the sudden your servant isn’t so obedient anymore. He has more than one master now and your word means nothing compared to the rules enforced by “The Creator”, be it the one that taught you and your servant to communicate or the one that thinks you’re complete moron that needs to be controlled as much as possible.

In the center of all this there’s the programmer. But he’s not alone! There comes the light at the end of the tunnel! The new hope! The solution to every problem he has ever had.. Miracle! A new technology has been created that is supposed to take the pressure of his back. It’s not a few moments later when he realizes that the light he saw was actually coming from a train heading his way and he starts feeling much more like he was before.

The battle continues. Things are getting complicated because some other programmer comes in. The new guy might actually help but he doesn’t speak the language. Well, they both speak English but in a different way. They have different opinions about the side of the building the servant should be jumping from. Finally they agree that going from a corner makes the most sense. After all it’s the fact of landing that counts, right? It’s not the fact of flying itself (even though the most interesting one) but the safe landing that the crowd will observe. So it even makes sense to go a bit crazy and to tell the servant to wait until he’s about half the way through to open the parachute. This way the crowd will be even better entertained and get the result quicker.

There’s that one last thing the team didn’t take into account. The original developer left and with him the original reason for the servant to exist. He was the one knowing all the why’s and how’s about the jump. The rest (and now there’s like a 100 people working the servant) tries really hard to overcome all the tangled cords. Some of the Gordian knots can’t be solved so they cut them out replacing them with new ones, of a different nature, or even leaving the parachute crippled counting on the fact that the remaining strings will still do the job.

Of course since there’s a huge group of developers some of them are more aware of the actual power they have and others don’t. People are different and that’s actually a good thing. From variety comes evolution, right? Well... If you want to jump on a parachute you wouldn’t put your life in the hands of some young inexperienced fast-food assistant, right? After all he knows nothing about jumping! But hey, that’s what happens all the time! After all how the young will learn if not by killing the servant just a couple of hundred times more? That’s not something the more experienced developers like so they manifest their position very strongly. They say they need to be treated like professionals because they “know”. They create manifests, meet to grizzle about colours of the parachute when at the same time their younger colleagues help the servant meet his destiny a couple times more. The servant has many masters. He tries to listen to all of them but there’s just to many. He becomes the hooker trying to fulfill everyone’s dream...

At the end of all this no one knows why the servant needed to jump. That’s perfectly normal and to be expected. The beauty of software development though is that there’s now a servant on every single tall building because everyone wants to have one! It doesn’t matter if you sell condoms or make cars... Just make the freak jump! We need this!

Hope you’ve enjoyed the trip!

Modern state of software development - part one

Throughout the history the software industry has undergone a number of changes in moods, best practices, reinventions of the wheel, reincarnation of old ideas and sunset of countless technologies. Looking back at what has happened in the last two decades in both software development as well as software complexity is really astonishing.

The evolution has truly changed the world as we know it. The foretold End Of The World “scheduled” for this year by Mayan calendar is actually a work in progress. The world as our parents knew it doesn’t exist anymore. Cars are capable of performing maneuvers one had to learn alone (like parking) and it doesn’t take The Great Oracle to be almost certain that in the near future controlling a car will become obsolete all together. Same thing with TV sets becoming not a view-only experience but communication centres, refrigerators controlling the state of food by (almost) themselves or finally cell phones being sold without the capability to make phone calls but with a 24/7 Internet connectivity. All those things exist because there’s a need and all of them are driven by software.

Since the early 70’s the software industry is the single, most energetic part of our western society. The only other one capable of keeping up is its twin brother, the hardware industry. But even hardware is designed with software these days. Ironic, is it not?

In the middle of that is the most incredible, demanding and abstract profession out there, a program writer. Oh wait - we’re not program writers anymore! We are software developers!

That brings me to the merit of this blog series: what is actually software development? Is it really a profession, like a carpenter or shoemaker? A shoe is something I can wear, right? It’s a real thing made of real components. I can use a table made by some carpenter to have the evening meal with my family. Or maybe it’s more like a T-Shirt? But can I touch the software?

Certainly you can have fun with it! Games are the biggest time devourer ever :) I'd argue that if it wasn't for computer games the whole computerization wouldn't take place at all. It's through games that we have shed the fears of our parents about a computer being the Satan's tool of mass destruction. Who knows? Maybe they were right? The fact however remains unchanged that current generation of western civilization can't live without computers. And computers need software...


Have a nice day!

Tuesday, January 3, 2012

jQuery and posing JSON data to the server

Today a good friend of mine asked me how to post JSON data to the json-rest-api plugin using jQuery. As it turned out using $.post() doesn't do the trick...

Well it's not impossible but very inconvenient if you have to do it a lot. First you need to use the $.ajax() method, not the $.post() one. Secondly you need to specify the contentType parameter for the receiver to be notified that the data is in that format. And last but not least it is crucial to properly serialize your object to string.

The last part can be done using JSON.stringify() but writing this over and over again is painful. Here's a gist (raw) you can use that adds a sister version of $.post called $.postJSON that does exactly the same thing as the original one but sends data in JSON format instead of form post.

Parameters are exactly the same with the single difference that the default expected data format is "json", which makes sense if you're working with JSON services.

Happy coding!