14 December 2010

Cast/convert Object to byte[] array for Dbunit

If you are using Dbunit you might want to verify that the data in a BLOB is correct:

IDataSet dataSet = idatabaseConnection.createDataSet();
ITable myTable = dataSet.getTable("my_table");
Object blob = myTableDbUnit.getValue(0, "myBlob")

But now what? The blob cannot be converted or casted to a byte[] in Java! Here is solution:
    private static byte[] toByteArray(Object object) {
        int length = Array.getLength(object);
        byte[] result = new byte[length];
        for (int i = 0; i < length; i++) {
            result[i] = Array.getByte(object, i);
        return result;

byte[] blob = toByteArray(myTableDbUnit.getValue(0, "myBlob"));

16 October 2010

Sainte-Laguë method or "Uddatalsmetoden"

I created a class in Java to calculate allocation of seats in a paralament with Sainte-Laguë method.

import java.util.*;

 * Method for is allocating seats proportionally for representative assemblies with party list
 * voting systems. Or to calculate an even percentage number given some proportional factor
 *  In Swedish this is called "Uddatalsmetoden"
 * @author Lennart Schedin
 * @param <T> The type of object base
public class SainteLagueMethod<T> {
  private static class SainteLagueObject<T> {
    private T party;
    private double votes;
    private int mandates;
    SainteLagueObject(T party, double votes) {
      this.party = party;
      this.votes = votes;
      this.mandates = 0;
    private double getCompareNumer() {
      return votes / (2 * mandates + 1);
  public Map<T, Integer> calculateMandate(Map<T, Integer> partiesVotes, int mandates) {
    List<SainteLagueObject<T>> objectList = new ArrayList<SainteLagueObject<T>>();
    for (T party : partiesVotes.keySet()) {
      Integer votes = partiesVotes.get(party);
      objectList.add(new SainteLagueObject<T>(party, votes));

    return distributeMandates(objectList, mandates);

  private Map<T, Integer> distributeMandates(List<SainteLagueObject<T>> objectList, int mandates) {
    while (mandates > 0) {
      SainteLagueObject<T> winner = findOneWinner(objectList);
      winner.mandates = winner.mandates + 1;
    Map<T, Integer> resultList = new HashMap<T, Integer>();
    for (SainteLagueObject<T> object : objectList) {
      resultList.put(object.party, object.mandates);
    return resultList;
  private SainteLagueObject<T> findOneWinner(List<SainteLagueObject<T>> objectList) {
    SainteLagueObject<T> winner = null;
    for (SainteLagueObject<T> current : objectList) {
      if (winner == null) {
        winner = current;
      if (current.getCompareNumer() > winner.getCompareNumer()) {
        winner = current;
    return winner;

14 October 2010

How to create an rpm from a deb file

If your are forced to run an OS like Redhat or CentOS your are often in the situation that rpm does not exist for your faviourte program. But it probably be found as a deb file.

To create an rpm file from a deb file for the "makeself" program do this:
Download the deb file to some folder
fakeroot alien -r makeself_2.1.5-1_all.deb

13 June 2010

My slave name server at ZoneEdit.com are down

It looks like the servers ns2.zoneedit.com and ns17.zoneedit.com are down. This mean you cannot access my blog (unless using a Google search).

I have my master DNS at my own computer and uses Zonedit.com for slave name servers. Zoneedit assignes each domain I have with two name servers. Unfortunately both of them are down for "lesc.se".

08 June 2010

Protocol decoder for Internet based protocol

The ASN.1 language is a good language for specifying protocols. It is widely used by the telecommunications industry. However it is seldom good for "Internet based" protocols. For example IP/TCP/UDP/PPP.

For these kinds of protocols you have to have some other approach. One approach is to write your own language specification (grammar) and a compiler for this. From my own experience this is a risky path because you have to think really carefully how you design the grammar and compiler.

Does anyone know of any open or free implementations for a grammar and compiler that is suitable for Internet based protocols?

04 June 2010

Software update for X10 mini

Today I ran a Software update on my X10 mini

My previous version: 1.0.A.1.36
My current version: 1.0.A.1.38

I haven't noticed any difference yet.

30 May 2010

How to ignore calls from specific contacts on X10 mini

If you have a Sony Ericsson X10 mini phone you might have noticed that the contacts application is custom. In other words they have not used the Android default contacts applications, but written their own (probably so they could include the timescape button in the upper right corner).

The problem with this is that they have removed the "Send calls directly to voicemail" feature. This is a good feature if you are bothered by some contacts. It is possible to use this feature anyway:

1.Run RoadSync Mail or RoadSync Calendar application. It is a third party software bundled with your X10 mini. Complete the product registration by entering a name and an e-mail address. I'm not sure if it is necessary to actually configure an Exchange account or if it is enough to just complete the product registration.

2. Exit RoadSync and return to start screen and start the Sony Ericsson contacts application

3. Choose a contact and scroll down and press Edit

4. You will now be presented with an option on how to edit the contact:
Choose the "Edit RoadSync contact"

5. Now you will enter the normal Android default contacts editor:
At the bottom there is an option to send calls directly to voice mail.

6. Test it. Try to call your phone with the changed contact. For me the call was not sent to voicemail, I got a busy tone. This is good enough for me.

Note 1: When you get an incoming call you will not get any notification that someone is calling you. You can however see that someone has tried to call you in the call log.

Note 2: Be a bit careful when clicking around in the Android default contacts editor. It is easy to delete all contact phone numbers by pressing the red minus icon in conjunction by pressing the back key; the contact will automatically be saved.

Note 3:  If you accidentally have pressed the "Use by default for this action" checkbox you can clear this by opening Settings -> Applications -> Manage applications -> find you application that is default, scroll down and press "Clear defaults".

29 May 2010

SonyEricsson X10 mini USB adb debugging/development drivers

If you are an Android developer and want to connect to your X10 mini via USB this is the way to do it. It is assumed that you already know how to develop Android applications and have the Android SDK and Eclipse installed.

1. Enable USB-dev mode: Settings -> Applications-> Development -> USB debugging

2. Connect the USB cable

3. On the phone choose "Install PC Companion"

4. Install USB Driver (updated 2010-05-31)
Install the PC Companion (version 2.00.115). Let PC Companion update (and download a new version, my was 2.00.134).

This is how it will look in Device Manager when the Sony Ericsson ADB Interface driver is installed

5. Reboot the computer

6 . Optional: Uninstall PC Companion (in my opinion the program does not add any real value). The USB drivers will not be uninstalled.

7. Check that is works
C:\Documents and Settings\Lennart>adb devices
List of devices attached
43123531389A394D38F5    device

8. Develop
Set up a Run Configuration on one of your Android projects and choose "Manual" target:

When running choose your device:

18 May 2010

Lockness thread dump analyser

The Lockness thread dump analyzer is a nice plugin in Eclipse you can use to analyze Java thread dumps. The things I have used it for:
  • See if there is a deadlock
  • Find out what thread is blocking what (Lock following)
  • Quickly browse to source code from thread dump
How to install:
  1. Download the jar file from http://lockness.plugin.free.fr/home.php
  2. Place the jar file in Eclipse dropins directory
  3. Restart Eclipse
  4. Save the dump to a text file. For example "dump.txt"
  5. Right click on the dump and choose Open with -> Lockness 
 More information and usage can be found in the link above.

    17 May 2010

    Scrum seminar

    I attended a short Scrum seminar today. Mostly an introduction to Scrum. I have never personally worked with Scrum, but I have colleagues who (almost) work with it.

    One of the problems with Scrum is that it is not so easy to implement. It is not just the development department that needs to accept it, it must also be accepted higher up in the chain: product owner. The product owner must make time to get involved in the development with respect to features and prioritization. And this work is not something you can do one time per 6 month period, it must be done at each "sprint" (usually about 2-4 weeks).

    One thing a learned from the seminar that I did not know was the positive side effect of Scrum: team spirit. It can be used as a motivational tools to get more out of developers!

    Here is what Wikipedia says about Scrum

    Ordered a web camera (Logitech C300)

     I have now placed an order for a web camera. Logitech C300:
    I did a little research and a got to the conclusion that this camera should be a great budget camera.

    14 May 2010

    Chat session with HP

    Today I had a go with HP chat support.

    The HP chat has very limited browser compatibility. Main compatible browsers Internet Explorer 7 and Firefox 2? What? Really? With Internet Explorer 8 it was possible to enter a "compatibility mode". This is something you should really fix HP!

    Lennart Schedin : Where to a I buy an adapter (one 3.5mm male mic and one audio to 3.5 mm special combined audio/mic) as this link describes: http://h10025.www1.hp.com/ewfrf/wc/document?docname=c01633145&lc=en&dlc=en&cc=us&rule=15961&product=3892398
    [An agent will be with you shortly.]
    [You are now chatting with Sam Gale .]
    Sam Gale : Welcome to HP Total Care for Notebooks. My name is Sam Gale. Please give me a few moments while I review your problem description details.

    NOTE: For security reasons, PLEASE DO NOT send credit card information via chat.

    Sam Gale : Hi Lennart.
    Sam Gale : How are you?
    Lennart Schedin : Fine thanks!
    Sam Gale : Great!
    Sam Gale : As I understand that you want to purchase an adapter, am I correct?
    Lennart Schedin : Yes!
    Lennart Schedin : I have a "normal" headset and I want to use it with my Compaq Mini.
    Sam Gale : To assist you better, may I know the Product number and Serial number of your Notebook? You can check these numbers on a non-Microsoft sticker when you flip the notebook over. Please provide the numbers beside s/n and p/n on the sticker?
    Lennart Schedin : s/n: XXXXXXXXXX
    Lennart Schedin : p/n: XXXXXXXXXX
    Sam Gale : Thank you.
    Sam Gale : Lennart, you can purchase the part at any nearest store.
    Sam Gale : I will provide you the contact details of our regional support.
    Sam Gale : They will help you where to purchase the part.
    Lennart Schedin : Probably not... The special connector required for the Mini is not that common.
    Lennart Schedin : I will probably buy an external USB sound card instead, since it is so hard to find a 3.5mm audio adapter.
    Sam Gale : Let me check.
    Sam Gale : Are you looking for the same special connector which is there in the link?
    Lennart Schedin : Yes (one the right in the picutre).
    Lennart Schedin : My scenario is: I want to use skype. And my headset has two 3.5mm plugs (one for audio and one microphone).
    Lennart Schedin : My Compaq mini has only 1 input a combined 3.5 mm audio and mic (with 4 analog wires, instead of the normal 3)
    Sam Gale : Okay.
    Lennart Schedin : (In my opinion it is rather stupid to use a combined input, but I guess it was used to save space on the Compaq mini).
    Sam Gale : Thank you for elaborating the issue.
    Sam Gale : May I have 3 to 5 minutes of your time while I check for information?
    Lennart Schedin : One workaround is for me to buy an external sound card. Something like this: http://www.coolgear.com/images/USBG-X2X11.jpg
    Lennart Schedin : Sure, I have up to 10 min!
    Sam Gale : Thank you.
    Sam Gale : Thank you for being online.
    Sam Gale : I am afraid that this part may not be available with HP.
    Sam Gale : The only option would be to check with your local dealership.
    Lennart Schedin : I understand! That was also my guess. :)
    Lennart Schedin : Well an external USB sound card is easy to find. And it costs less then $20. So that will be the solution I'm going with.
    Lennart Schedin : Thanks for you help!
    Sam Gale : Thank you for your time and patience.
    Sam Gale : My pleasure
    Lennart Schedin : Bye!
    Sam Gale : Is there anything else I may assist you with?
    Lennart Schedin : No that's it!

    So HP chat support could not help me find any adapter (probably because the 3.5mm plug is so custom). Worth noting is that HP support personnel does not know every computer at the top of their head, it took a while under I managed to explain that my Compaq Mini has such a custom plug.

    13 May 2010

    Hibernation times: Ubuntu 10.04 vs Windows XP Home

    I have two OS's installed on my netbook. I wanted to test how long it would take to go to and resume from hibernate. I think this time is an important time, because I always use hibernation on my netbook.

    Computer: Compaq Mini 730eo (Atom N270 1,6 GHz with 4200 rpm harddrive)
    OS 1: Windows XP Home (Service Pack 3)
    OS 2: Ubuntu Netbook Remix 10.04
    Scenario: One Firefox instance open that has loaded Gmail.

    Time to boot computer to bootloader: 8 s. This number can be added to the below times to get the total startup time.

    To hibernate48 s14 s
    From hibernate40 s18 s

    Why is Windows so much faster? I don't know. One guess I have is that Ubuntu saves the entire RAM to disc, including caches and buffers. But that would be rather crazy, so it is probably some other explanation.

    12 May 2010

    How to create a bootable Windows install cd from an i386 folder

    So you have bought a HP computer? Or some other brand that does not supply you with the proper Windows installation discs? If you are lucky you can find a "i386" folder on one of your hard drive. This folder can be used to create a bootable cd. I'm going to describe the Windows XP Home edition version on how to do this.
    1. Create a new folder on your computer (for example c:\XPHOME_SETUP\).
    2. Copy the i386 folder into the folder created above so you have  c:\XPHOME_SETUP\i386\
    3. Create a file in c:\XPHOME_SETUP\ called "WIN51IC". The contents of the file should be "Windows " and a line break (note the blank space after the "s"). Other versions of Windows XP require some other files (see http://www.howtohaven.com/system/createwindowssetupdisk.shtml)
    4. Install nLite (it is an amazing freeware tool that easily can create custom Windows bootable CDs). For Vista there is vLite.
    5. In nLite point out the c:\XPHOME_SETUP\ folder and follow the programs rather simple GUI to create an ISO.
    6. Optional: download a service pack from Microsoft website and integrate ("slipsteam") it into the installation with nLite.
    7. Burn the ISO (I used CDBurnerXP)
    8. Boot!

      How to convince a company to start using a dependency management system

      I have tried two dependency management systems (Ivy and Maven) for personal use. I clearly see the advantage of using this. But it is long way from convincing myself to convincing a company. For a company to start using it, it must have a return of investment analysis: How much time must we spend and what do we gain?

      In my experience the following things happens when using Ivy:
      • It is easier to develop since the source code is often (always?) included in the libraries
      • It is easier to release because there is a convention on how to store and upload artifacts/binaries
      • Checkouts is faster because libraries is cached and disc usage on source repository server is less.
      • It is easier to mange dependencies between own projects with clear definition of what version of project A works with what version of project B.
      • Release notes and other documentation can be published and stored along side with binarys because it all is an artifact.
      • It takes time to educate developers
      • It requires some one with experience with Ivy to setup an initial environment (ivy.xml build.xml and a local corporate server to store and cache artifacts)
      • When using Eclipse to build there will often be build errors due to poorly written plugins (will probably be better with IvyDE 2.1.0).

      10 May 2010

      How to delete password or change user login name for Subclipse

      I got a new user to use when logging in to a Subversion (SVN) repository. I use "Subclipse", a Eclipse SVN plugin. I had added a SVN repository with my old user:

      Unfortunately there is no GUI to change username or password. The usernames and passwords are stored in a SVN client implementation specific way. If you use SVNKit:
      the passwords are stored in the Eclipse keystring (along with many other password-alike things). If you delete this file (configuration/org.eclipse.core.runtime/.keyring) and restart Eclipse then you will be prompted to enter username and password again. Note: while deleting this file you will delete other things stored by other Eclipse plugins!

      05 May 2010

      Giving back to stackoverflow.com

      I have written some questions on stackoverflow.com but never answered another person's question. Well today I think I made a difference in the world:

      A month ago I had experienced the same problem as the question-writer so it feels good to be able to help someone else.

      01 May 2010

      Ubuntu Netbook Remix 10.04 and Compaq Mini wireless drivers

      I tried the new Ubuntu Netbook Remix 10.04 (Lucid Lynx) today on my Compaq Mini netbook.

      I have tried all previous versions since I got my netbook. But everytime there has been a problem with the driver for the wireless network card. Previous versions have shown a GUI saying: you need to install a proprietary driver and when I clicked it the computer just locked. I have gotten some helpful tips on executing a some commands in the terminal, but I haven't bothered to try that. Even though I think I'm capable of executing some commands in the terminal I don't think a OS is worth my attention if there is not a simpler way to get such a basic feature as wireless network.

      But today on the 10.04 the GUI click to install the driver worked! So now I think I will try this OS a bit more.

      27 April 2010

      My first screen cast - Let me Google that for you

      This is a simple screen cast I created in Camtasia Studio. It is a simple tutorial on how to use the Let me Google that for you site.

      Produce screen casts

      I've always wanted to produce a screen cast of some sort. I think I will try to do a simple cast with Camtasia Studio.

      But, as always, it is easier to talk about doing things than actually doing it! :)

      11 February 2010

      Can Google Buzz trigger a new dawn for blogging?

      Yesterday Google released "Buzz". It is a mix between Twitter, facebook and a blog. It is currently tightly integrated into Gmail. This is how it looks like:
      All my postings on my blog will also appear in the Buzz. Could this trigger some kind of spark for my blogging?