Sunday, January 24, 2010

Applying scala to solving real world problems: Say bye bye to boring constructors and getters/setters

In Java, it is very common and boring to create constructors and getters/setters like:

public class Foo {
  private String x;
  private String y;

  public Foo(String x, String y) {
    this.x = x;
    this.y = y;
  }
  public String getX() {
    return x;
  }
  public String getY() {
    return y;
  }
  public void setX(String x) {
    this.x=x;
  }
  public void setY(String y) {
    this.y=y;
  }
}

Yes, Eclipse provides the "Generate constructor using fields" and "Generate getters and setters" commands to do that, but it is still very boring and requires work from us. In Scala, all this boring work is no longer required:

class Foo(x: String, y: String) {
}

Due to the closure support, x and y will be available to all methods inside the Foo class just like Java fields:

class Foo(x: String, y: String) {
  def someMethod {
    println(x+y)
  }
}

To create getters, use to the "val" keyword:

class Foo(val x: String, val y: String) {
  ...
}

The Scala compile will create a getter methods named "x" and "y" automatically. To create setters in addition to getters, use "var" instead of "val":

class Foo(var x: String, var y: String) {
  ...
}

Then the compiler will create setter methods named "x =" and "y =" for you (Yes, the method name contains a space and then an equal sign, which are allowed in Scala). To call the getters and setters, you may:

val f = new Foo("a", "b")
f.x            //In Scala you don't need to use () to call a method
f.x = ("c")  //This is OK
f.x = "c"    //Again, don't have to use ()

Pretty neat, isn't it?

Friday, January 1, 2010

Installing MIT Scratch on Kubuntu 9.10

Installing MIT Scratch on Kubuntu 9.10

Below are the steps to get Scratch working, including playing audio (recording quality is still quite poor, but you can always record outside of Scratch).

Install PulseAudio

$ aptitide install pulseaudio pulseaudio-utils

Install the latest squeak

The Squeak package included in Ubuntu 9.10 can't play audio (wav). Fortunately, the latest version (3.11.3.2135) works. So, go to http://www.squeakvm.org/unix to download the binary package. However, if you try to download the .deb package, it will say that you don't have the permission. So, download the rpm install and then convert it:

$ alien Squeak-3.11.3.2135-linux_i386.rpm
$ dpkg -i squeak_3.11.3.2135-2_i386.deb

Install Scratch 1.4

Download the Scratch source from http://info.scratch.mit.edu/Source_Code. Unzip it and you will get the VM file ScratchSourceCode1.4.image. Before you can run it, you need to know that it relies on some "plugins" that are written in C language for each platform. So, download the source code to the plugins from that same page. Then compile them. The package contains three plugins: take one of the plugin called ScratchPlugin as an example:

$ cd ScratchPluginSrc1.4
$ cd ScratchPlugin/ScratchPlugin-linux/
$  ./build.sh
$ sudo cp ScratchPlugin /usr/lib/squeak/3.11.3-2135/so.ScratchPlugin

For the UnicodePlugin, you need to do some extra steps:

$ su aptitude install libpangomm-1.4-dev
$ su aptitude install libcairo2-dev
$ add an option no-stack-protector to the gcc command in the unixBuild.sh file:
       ....
       gcc -fno-stack-protector -fPIC -Wall -c `pkg-config --cflags pangocairo` *.c
       ....
$  ./unixBuild.sh
$ sudo cp UnicodePlugin /usr/lib/squeak/3.11.3-2135/so.UnicodePlugin

Running it

To run Scratch, type:

$ squeak ScratchSourceCode1.4.image

It should work.

Sunday, December 6, 2009

open source web-based centralized console for tripwire

When using tripwire to monitor changes on multiple servers, it is common to have to review and accept changes on a daily basis. Logging into multiple servers to accept the changes is troublesome. So, I've created a  web-based centralized console to review and accept changes. It is GPL licensed and can be downloaded from http://centralwire.sourceforge.net. Hope it is useful to others.

Friday, December 4, 2009

Handling BLOB/CLOB in postgreSQL with JDBC/Hibernate

Handling BLOB/CLOB in postgreSQL with JDBC/Hibernate

What is a BLOB/CLOB? It is a large object (binary for BLOB and char for CLOB). It is commonly used when you're storing large files into the database as column values.

It was really a challenge to work with BLOB/CLOB and in the process I almost pulled my hair out. Below are the hard lessons learned:

  • Avoid using LOB if possible! If say, it is just a few KB in size, just treat it as a string or byte array.
  • You must NOT try to read the stream outside the transaction. Note that using the open session in view is NOT enough. Once the transaction is ended, even if the Hibernate session is still open, you still can't read it (you'll get the "invalid large object descriptor" exception).
  • You must NOT try to read a LOB twice in the transaction. The "pointer" can't seem to be reset so you read past some portion, you can't read it again.
  • I still haven't figured this one out: If you repeatedly read a row containing a LOB in different transactions, it may cause the same "invalid large object descriptor" when you try to read it in a new Hibernate session.

Friday, November 13, 2009

Better approach to unit testing pages in web applications

It's common to see two approaches to unit testing pages in web applications: in-container (real container and browser) and out of container (mocked container and no browser). The former is good in that the tests can check the HTML DOM element and thus works with AJAX, but it is difficult to mock the services as the web application is running "on the other side". The latter is the opposite: Easy to mock the services but usually the tests can only check the internal state of the program, not the end HTML code.

In fact, a better approach is to combine them together: Run the application in the container and run the container in-process so that the test code can mock the services. This way you can control the right thing (user input on the web page and the data your pages get, from the services), and then observe the right thing (HTML DOM elements, possible manipulated by Javascript/AJAX). In order to run the container in-process, we can, eg, run an embedded Jetty. As it is run in the same process, the test code can access the servlet context and thus can get the opportunity to replace the services with mocks.

I've created a proof of concept library for unit testing Wicket pages. The mocking is done by using a chain of component injectors. The first one keeps a map of mock objects. The second one is the normal one to inject Spring beans. Usually the map in the first injector is empty so it has no practical effect in production. But your tests can put mock objects into there to perform mocking.

The library is released as open source software (LGPL). You're welcome to check it out at http://wicketpagetest.sourceforge.net. There is a step-by-step tutorial.

Finally, potentially this approach can be applied to other web frameworks like JSF, Tapestry and etc.

Sunday, October 4, 2009

Javablackbelt a rip off?

I always thought that javablackbelt was a non-profit community, but actually it is not. It is actually selling the questions people submit for profit! I think this is ethnically not right:

  1. It always advertises as a community, but actually it is a for-profit company.
  2. It calls the license copyleft, but actually it is not Gnu copyleft. It explicitly grants itself the right to sell the questions.
  3. It should highlight this term. Currently there is no obvious license displayed when people try to submit a question.
  4. This is unlike RedHat making money from open source software because that software is licensed by the authors to be freely available for anyone, so RedHat has to provide its own added value. If javablackbelt is going to sell the questions, they should share the profits with the authors.

Friday, January 16, 2009

PMP exam

Sat and passed the PMP exam in HK. It is very similar to those free example questions on  www.oliverlehmann.com. All I used for studying was the book by Joseph Philips and dthe free questions mentioned above. The book covers almost everything in the exam, even though it would easier to read if it included more concrete examples.