Hang-Man Bug

If your system inexplicably hangs …..

one reason might be you are trying to initialize a java.awt class on a server that does not run X.

Fun fact: I just read about this the other day on the forum for the library I am using and for which I need these awt-classes for!

*Added* and another problem I was having: it the programm is producing load the reason is probably not deadlocked threads. Think infinite loop!

Exception Handling

When I started working one and a half years ago, I knew a lot less about software development than I know now. This does not necessarily imply that I know everything now. But I think in the meantime I have learned a lot.

The latest acquisition in the field I made through my second code review where the reviewer came back and said “You need to look over your exception handling, you should not ….” he was absolutely right. Up to that point I had basically ignored exception handling and had seen exceptions as a necessary evil that came with the language. So I had done all kinds of funny things with exceptions, except one thing: handle them in a consistent way throughout my application and make them visible when they occurred.

The first changes with my attitude came about when I learned about assertions.

ssh-agent

So that I will not have to search for a fix for this again.

The xsession must be started as a client of the ssh-agent. For this a file .xsession (plus ln -s .xsession .xinitrc) is needed with the following contents:

#!/bin/sh
SESSION_PROC_ID=$$
ssh-agent $HOME/.xsession.ssh

Secondly I copied the global /etc/X11/xinit/xinitrc to /home/yashima/.xsession.ssh. Then I added the command ssh-add to my gnome-session with the normal gnome config utility. Works. Done. Don’t need to do it again!

Addendum: some of the problems I was having with ssh seem to come from the “-i” flag … when I created new keys with default names and simply tried without specifying a key all of a sudden I was able to run stuff with “-2” that would not work before. Don’t forget to use “-a” when you test things on a remote computer otherwise your agent will screw up everything.

The “Keywords for Better Code” Initiative

The Do’s of programming. My favorite “code” improving keywords are:

# final: makes sure I don’t do stupid assignments, in combination with the compiler warnings “this variable may not have been assigned” a very valuable tool
# finally: close files, connections and clean up despite errors happening!
# throw (note: not throws!): exception handling, my last code review put me onto improving that
# assert: to keep myself from the most stupid things that could have happened
# else: if there is an if there should also be an else, just as with switch there should be a default.

in no particular order. When I am forcing myself to use those frequently the code becomes a lot safer and these are easily remembered. Any others?

Next: the Don’t’s of Programming 😉 (or whenever I get to that).

Unknown Java

A fact on java that many people do not realize: it is not trivial to actually “stop” a thread. All the methods like “destroy()” and the likes are depcrecated and have been for a long time because these methods are NOT safe to use.

I am currently in the process of finding out how to stop java from doing something like continuing an infinite loop nested deeply within a method call of a library. I’ll post my solution or the reason why it won’t work.

Addition: must correct myself “destroy” is not deprecated: it was never implemented. But “stop, resume and suspend” are deprecated. For those who think that by assigning “null” to the variable that you kept the reference to the thread in: go back to learning basic java principles. No not really: there is always a reference in some ThreadGroup object to all threads running. But everybody should know that assigning “null” to something is code smell or rather stink!

Java of Evil: FileLock

Currently I am in a phase of “lots of work” which means I do not find time for blogging because I have only three states of being: working, being tired and sleeping. But I found out lots of fancy things about java.

Yesterday I implemented file locking using the java.nio package. Do not use java.io.File.createNewFile(...) to create file locks. It is not safe! It says so in the api docs and links to the nio package.

I now have a nice little class the returns vm-wide locks on files or lists of files and releases them on demand. Neat: except it didn’t solve my problem.

public static FileLock lock(final String filename) 
throws IOException {
    final RandomAccessFile file =
          new RandomAccessFile(new File(filename), "rw");
    final FileChannel channel = file.getChannel();    
    return channel.lock();       
}

By the way there is also a method tryLock() which tries to lock but does not block as lock() does. Here’s the release method:

public static void release(final FileLock lock) 
throws IOException {
        if(lock.isValid()) { 
            lock.release(); 
            lock.channel().close(); 
        } else { 
            LOG.warn("Trying to release invalid lock"); 
        } 
}

A colleague requested I post this somewhere when I found out how the java FileLock class worked. I think it works. But it does not necessarily solve any problems. How does the java.nio package interact with the java.io package for example?

Evil Genius

To stay with the theme of the month.

After seeing this on both apophenia and mamamusings who both don’t take silly tests all the time, I thought I might give it a try as well. Oh if I had known 😉

So here is my test result for 20 Questions to a Better Personality:

Wackiness: 14/100
Rationality: 40/100
Constructiveness: 52/100
Leadership: 24/100

That doesn’t say a lot, except I am not so very wacky and not a good leader? See there you go, I knew that. Here comes the rest:

You are an SEDF–Sober Emotional Destructive Follower. This makes you an evil genius. You are extremely focused and difficult to distract from your tasks. With luck, you have learned to channel your energies into improving your intellect, rather than destroying the weak and unsuspecting.

Your friends may find you remote and a hard nut to crack. Few of your peers know you very well — even those you have known a long time — because you have expert control of the face you put forth to the world. You prefer to observe, calculate, discern and decide. Your decisions are final, and your desire to be right is impenetrable.

You are not to be messed with. You may explode.

As with all test results some parts of it just seem to fit sooo well and others of course not. Hehe. I like the “evil genius” part and the “you are not to be messed with” except, those aren’t really desirable traits if you think about it. It’s friday it’s quiz time (hint: it’s the first link in that google search) and tell me what you got. Oh yeah: I am not sure how this is supposed to improve my personality?

More evil

Or less evil: databases stink. A lot. Nearly as much as goats. Just so you know.

PS: I think it’s safe to say “July is the month of evil!”