Monday, March 12, 2007

March 24, 2007 - Shutdow DayShutdown Day 2007

This is a great video of alternative uses of notebooks :). Will your computer be shut down on March 24?

Wednesday, March 07, 2007

Cloning a CD/DVD Disc on MacOS Using a Console

MacOS has a handful of good command line tools for working with discs, images and burning devices. Unfortunately direct support for disc cloning is missing.

I wrote a little shell script that makes it very easy to create a 1:1 copy of CD/DVD discs.
#!/bin/sh

DEVICE_NAME=`drutil status | grep dev | cut -d / -f 3`

diskutil unmountDisk $DEVICE_NAME
watch ls -lh /tmp/disk2burn.iso&
WATCH_PID=$!
dd if=/dev/$DEVICE_NAME of=/tmp/disk2burn.iso
kill $WATCH_PID
hdiutil burn /tmp/disk2burn.iso
if test $? -eq 0; then
 rm -r /tmp/disk2burn.iso
 echo 'DONE!!!'
fi
This script uses watch command to display the progress of the dd command that creates an iso dump of the original media. You can get the watch from darwinports/macports. If you don't care about the progress of creating the iso dump, you can just use:
#!/bin/sh

DEVICE_NAME=`drutil status | grep dev | cut -d / -f 3`

diskutil unmountDisk $DEVICE_NAME
dd if=/dev/$DEVICE_NAME of=/tmp/disk2burn.iso
hdiutil burn /tmp/disk2burn.iso
if test $? -eq 0; then
 rm -r /tmp/disk2burn.iso
 echo 'DONE!!!'
fi

The script discoveres the name your DVD drive $DEVICE_NAME at runtime, e.g. disk1 for (/dev/disk1). This requires that the disc is present in the drive before you start the script.

Note: This script won't copy DRM protected discs (DVD movies, protected Audio CDs) :-(

Btw I didn't know what was the correct usage of the words "disk" and "disc", this article from apple makes it clear.

Update(07-03-24): I found a bug in my scripts that I fixed and I changed the scripts so that the DEVICE_NAME is discovered at run time.

Sunday, March 04, 2007

One More Way How to Get the Current PID in Java

Yesterday I wrote a post about getting the current PID in Java.

I knew that there had to be at least one more way to do this because I read about using perl and the Runtime.exec() method to get the PID. But I didn't like the perl part of the hack so I didn't mention it in my post.

Today after some time playing with bash and an annoying behavior of Runtime.exec(String) method I came up with a 5th approach of retrieving the current PID. This approach uses $PPID and Runtime.exec(String[]):
import java.io.IOException;

public class Pid {
public static void main(String[] args) throws IOException {
  byte[] bo = new byte[100];
  String[] cmd = {"bash", "-c", "echo $PPID"};
  Process p = Runtime.getRuntime().exec(cmd);
  p.getInputStream().read(bo);
  System.out.println(new String(bo));
}
}
The code is pretty straight forward. Get Runtime object, call exec(String[]) method pass it bash -c "echo $PPID" (after splitting it into a String array) and read the output.

Why not to pass the command as one string? Because of problems with quotes described in this bug report.

As I mentioned before even this approach is not perfect, because it depends on *nix and bash, but it is an improvement compared to depending on perl :)

Private Wiki Dilemma

I've been using GTDTiddlyWiki as my private offline wiki for over a year now and I was pretty happy with it for a long time.

At the time when I picked GTDTiddlyWiki as my private wiki I didn't realize how much and how often I would use it. It's about that time when my needs have outgrown what GTDTiddlyWiki has to offer, so I'm looking around for something with a few more features and ongoing development.

I like the idea of a client-side wiki as a private wiki, and TiddlyWiki and all its flavors seems to dominate this area. I was considering switching to TiddlyWiki, but I found a very interesting feature called TagglyTagging of MonkeyPirateTiddlyWiki (yet another TiddlyWiki flavor). I though that content tagging couldn't get any better, but it seems that I was wrong. :)

I'll give MonkeyPirateTiddlyWiki a shot!

BTW tiddlyspot is hosting TiddlyWiki, MonkeyPirateTiddlyWiki and a few other TiddlyWiki flavors. By using their service you can easily upload your wiki to their servers and make it available to the public (or password protect it). Pretty neat, but after being among security geeks at Sun for some time I'm hesitant to use this cool feature. :-/

Am I getting paranoid enough Martin? ;-)

Saturday, March 03, 2007

How a Java Application Can Discover its Process ID (PID)

Occasionally it is important for an application to know its PID, specially if this application cooperates with other non-java applications. Currently there is no direct support for retrieving an application's process id by using standard Java api (this might change in the future if RFEs like 4250622, 4244896 or 4890847 are resolved).

I found five ways how to get the PID from my Java code:
  1. Using the java management and monitoring API (java.lang.management):
    ManagementFactory.getRuntimeMXBean().getName();
    returns something like:
    28906@localhost
    where 28906 is the PID of JVM's process, which is in fact the PID of my app.

    This hack is JVM dependent and I tested it only with Sun's JVM.

    From Javadocs for getName() method of RuntimeMXBean:
    Returns the name representing the running Java virtual machine. The returned name string can be any arbitrary string and a Java virtual machine implementation can choose to embed platform-specific useful information in the returned name string. Each running virtual machine could have a different name.
    So even though this approach is the most comfortable one, your app can break if the implementation of this method changes.
  2. Using shell script in addition to Java properties Start your app with a shellscript like this:
    exec java -Dpid=$$ -jar /Applications/bsh-2.0b4.jar
    then in java code call:
    System.getProperty("pid");
  3. Using shell script's $! facility as described on this blog - this approach is fine if all you want is to create a pid file.
  4. Using Java Native Interface (JNI) - a very cumbersome and platform dependent solution.
  5. Using $PPID and Runtime.exec(String[]) method - described in detail in this post
    import java.io.IOException;
    
    public class Pid {
    public static void main(String[] args) throws IOException {
      byte[] bo = new byte[100];
      String[] cmd = {"bash", "-c", "echo $PPID"};
      Process p = Runtime.getRuntime().exec(cmd);
      p.getInputStream().read(bo);
      System.out.println(new String(bo));
    }
    }
    
It must be said that none of these approaches is perfect and each of them has some drawbacks.

I know that one of Sun's main priorities for Java is cross-platform compatibility, but I have to agree with the comments on the RFEs above which support the addition of a getPid() method to JDK. I especially like this one:
Come on Sun, this started 4.5 years ago. Give us the
PID. We need it. We want it. We demand it.
And another thing ... you will save a lot of developers a
lot of time currently spent searching through the
documentation trying to find a getPID method that isn't
there!
Posted by Rarb@GB on 05-MAR-2004

UPDATE (06-03-05): added 5th approach

Friday, March 02, 2007

Yeah, I got tagged as well

Someone somewhere far far in the internet galaxy started this “viral” chain letter-ish tagging game . The game spread across the blogsphere like a flood and no banks were left dry. Some of my colleagues were more than happy to spread it by picking 5 more victims for this game and so I got tagged twice. At first my boss Rama tagged me, followed by a friend and ex-team mate Martin.

So here are 5 things you probably didn't know about me:
  • I used to be an MS user, living with Windows OS every day, but fortunately my salvation came almost two years ago when I joined Sun. Ever since then I had no need to fix crashing computers :)
  • I shook hands with Arnold Schwarzenegger. Being a European and actually a neighbor of Austria this is a big deal! It's good to know that foreigners in California can be so successful.
  • I'm a performance junkie. I love to play with profilers, tweaking the code and trying different strategies to squeeze as much performance as possible from apps that deal with a lot of data.
  • I'm a Slovak, not Slovakian, not Slovenian or Slovene, not Czech. And I'm proud to be one.
  • I hate chain letter games of any form. Most of the chain letters games are just scams or a waste of time. So besides me being busy, this is one of the reasons for this post coming with such a delay.
And to spare others I decided not to tag anyone ;-).

Thursday, March 01, 2007

Busy as a bee

I'm sitting in a train on my way back from Vienna to Bratislava and I decided that it's about time to finally post something on to my blog (to prove that I'm still alive). So I am alive, just very busy with my personal life as well as projects like zoom23.com and another one that I don't dare to name yet :).