Category Archives: tutorial

Amazing Caching Proxy in Java

Use Case

Imagine you have some Java code that does lots and lots of computation. All the time intensive calculations is performed by the class SlowCalculator which implements the interface Calculator:

public static interface Calculator {
    public String calculate(int a, String b);
}

public static void main(String[] args) {
    Calculator c = new SlowCalculator();
    // call c.calculate() a lot of times here...
}

You notice that calculate() is often called with the same parameters which lead to the exact same result (SlowCalculator is stateless). This means it is possible to cache values so there’s no need to recompute. Using the generic CachingProxy™ described below, you can create a cached proxy for any class with just one single line of code:

// ...

public static void main(String[] args) {
    Calculator c = new SlowCalculator();
    c = CachedProxy.create(Calculator.class, c);
    // call c.calculate() a lot of times here...
}

That’s it, and the application is blazingly fast again.

UPDATE: Support for null values, transparently handles exceptions, better hash, nullpointer-bugfix.

UPDATE: Here is an article “Memoization in Java Using Dynamic Proxy Classes” that does (almost) exactly the same as this code.

Continue reading

How to Make a Compact Gnome Theme

The themes Human Compact and Clearlooks Compact have been quite a success, and I got several requests to make a tutorial on how to create such a compact theme.

UPDATE: Human Compact Theme for Ubuntu 8.10 (Intrepid Ibex) is available!

Well, it is a bit difficult to create a step-by-step tutorial, but I will try anyways. Prepare to fiddle around with your configuration and try it out several times until you get the desired results.

  1. All your Gnome themes are located under /usr/share/themes. Find the theme of which you want to create a compact one, and copy it into your home directory with e.g.
    cp -r /usr/share/themes/Human ~/.themes/Human-Compact
  2. If there is a file index.theme, open it and change all the names (e.g. add “Compact” where appropriate). This file is necessary if you want to directly choose the theme from the Appearance Preferences; if it is not available you have to choose a theme and select “customize” to select the compact controls for it.
  3. Now to the fiddling part. Open gtk-2.0/gtkrc, and change lots of thickness and width settings… When you do this, always check how your changes work visibly, e.g. if the application still have usable borders etc. To help with the fiddling, I have added a diff of the Human vs. the Compact theme, you might be able to reuse some of the settings there.
  4. Once you are satisfied with your theme, you can create a .tar.bz2 distribution for backup or to be used by others, e.g. with this command:
    cd ~/.themes
    tar cjvf ~/Human-Compact.tar.bz2 Human-Compact
  5. That’s it. Have fun with your theme!

Continue reading

Logical Volume Manager Cheatsheet

Today I got a new harddisk, so it was finally time for me to have an in depth look at LVM. I use it to combine two harddisks /dev/sdg1 and /dev/sdb1 into one volume group ext_vg which contains one big logical volume /dev/ext_vg/ext. In short, my 250GB and 500GB harddisks are used in one big 750GB ext3 mount point.

I followed the excellent LVM Tutorial, and was surprised how easy everything goes. Here is a short cheat sheet of the important commands (take care to exchange the partition/volume names if you use this):

Installation

I use Ubuntu, so this is a piece of cake:

sudo apt-get install lvm2

There is also a GUI available, but I found the command line tools are very easy to use so there is no real need. I wouldn’t use EVMS as it is not supported in Ubuntu 7.10 and may cause problems.

Create a New Filesystem

First I create the physical volume on the partition sdg1, create a new volume group ext_vg that contains this physical volume, and create a new logical volume of size 450GB within the volume group. Finally create the filesystem (disabled reservation space, see Get More Space Out of Your ext3 Partition).

sudo pvcreate /dev/sdg1
sudo vgcreate ext_vg /dev/sdg1
sudo lvcreate -L 450G -n ext ext_vg
sudo mkfs.ext3 -m 0 /dev/ext_vg/ext

Show Status

Each LVM layer has its corresponding command to get information about the metadata:

sudo pvdisplay
sudo vgdisplay
sudo lvdisplay
sudo pvs
sudo vgs
sudo lvs

Mount via fstab

I use the filesystem mainly for data, so allowing just rw is enough (no executables allowed). noatime allows quicker access.

sudo mkdir /media/mega
/dev/ext_vg/ext /media/mega     ext3    rw,noatime,user 0       2
mount /media/mega

Resize

It is even possible to do an online resize of the system, wohoo :-) This extends the logical volume by 200MB.

sudo lvextend -L +200 /dev/ext_vg/ext
sudo resize2fs -p /dev/ext_vg/ext

You can watch the resize process going on with df -h.

Add Another Partition to the Logical Volume

To add another partition and use up all the available space in the logical volume, first add the physical volume to the volume group, then use pvdisplay to find out the total available number of free PE (add the numbers from the physical volumes), then use lvextend to use up all this available space.

sudo pvcreate /dev/sdb1
sudo vgextend ext_vg /dev/sdb1
sudo pvdisplay
sudo lvextend -l +63602 /dev/ext_vg/ext
sudo resize2fs /dev/ext_vg/ext

That’s it! Any questions? please post.

Howto Get Enough Sleep Despite StumbleUpon with Ubuntu

What?

I am a long-term StumbleUpon user, which means that I don’t get much sleep. Now, after almost 4 years of stumbling, I have decided to get my life back. Well, at least some sleep!

What does this do?

Every night when I have to work on the next day (Sunday night to Thursday night ), at 23:25 my computer shows me this little warning message:

go to bed warning

After the 5 minutes have passed, the computer shuts itself automatically down.

I use this little trick with Ubuntu, but it should work anywhere where Gnome is installed.

How?

Thanks to the power of Linux, this is not difficult to do for yourself, and configure it however you want it to behave:

  1. Open /etc/crontab with your favourite text editor (no need for crontab -e since this is the system wide crontab), e.g.
    sudo gedit /etc/crontab
  2. Add the following lines (replace manker with your username!)
    25 23 * * 0-4   manker  /usr/bin/zenity --display :0 --warning --text="Shutdown in 5 minutes. Go to bed."
    25 23 * * 0-4   root    shutdown -h +5
  3. The first part of both lines 25 23 * * 0-4 means that the commands are executed at 23:25, but only Sunday (day 0) to Thursday (day 4). Read man 5 crontab for a detailed description of that format.
  4. The first command uses zenity to show a warning message. You have to execute this as the same user that you use for working, or you will not see the message, so change manker to your username (root does not work either).
  5. The second command shutdown -h +5 means that the computer will halt in 5 minutes. This has to be run as root, and it also shows nice warning messages in all your open terminals so you can’t really miss it.
  6. Save the file, and stumble until it is 23:25.

Sweet dreams!

Ajax Dojo Comet Tutorial

EDIT: This tutorial is for an old version of dojo / comet, and it will not work in a recent version!

Markus Holzmann, an intern at Profactor of my fellow colleague Philipp Hartl, had the opportunity to experiment with Ajax during his job. He wrote a tutorial about how to push events from the server to the client. For example, display popup messages on all browsers at the same time (see screencast in full resolution here):






Read on how Markus did this:

Continue reading