Java And Final Keyword

I must admit I am addict to using final keyword. Today I came across Renaud Waldura’s The Final Word On the final Keyword which nicely explains why final keyword should not be ignored.


Modifying NetBeans Generated Getters And Setters

For quite a long time I was thinking about automatically adding Javadoc to getters and setters generated by NetBeans, and also automatically adding final modifier to setter parameter. NetBeans does not have this feature yet. So what exactly I wanted?

package com.mycompany.mavenproject1;

public class App {

     * Contains exciting values.
    private String test;

     * Getter for {@link #test}.
     * @return {@link #test}
    public String getTest() {
        return test;

     * Setter for {@link #test}.
     * @param test {@link #test}
    public void setTest(final String test) {
        this.test = test;

With some guidance from Petr Pišl from NetBeans team, I was able to do this patch: netbeans-getter-setter.patch. You can patch your NetBeans with it, adjust the generated Javadocs to your needs, compile NetBeans and your generated getters and setters should contain exactly what you need without pressing any extra key on your keyboard.

If you want to achieve the same for php, look at org.netbeans.modules.php.editor.codegen.CGSGenerator at GETTER_TEMPLATE and SETTER_TEMPLATE declarations. it’s even easier to modify these to get the comments added.

Sure it would be better if this would be done using templates, but atm this is better than nothing.

Also I updated NetBeans ebuild in Gentoo so that you can emerge NetBeans including your patches. You can find more info at NetBeans article at Gentoo Wiki.


Xfce, Keychain And Two ssh-agents

While trying to find out how to make NetBeans work with keychain, I noticed that after I start Xfce and then keychain is run from my .bashrc, I end up with two ssh-agents running instead of one. I did not find out why it works this way, only thing I found out is that SSH_AGENT_PID is empty when running .bashrc, and I was not willing to dig deeper to find the cause. Anyway, I made a small patch that makes Xfce work with keychain:

--- /etc/xdg/xfce4/xinitrc.orig	2010-07-17 18:43:00.025468585 +0200
+++ /etc/xdg/xfce4/xinitrc	2010-07-17 18:54:10.045657796 +0200
@@ -113,7 +113,11 @@
 sshagent=`which ssh-agent`
 if test -z "$SSH_AGENT_PID" -a "$sshagent" -a "x$sshagent" != "xno"; then
-	eval `$sshagent -s`
+	if test ! -e $HOME/.keychain; then
+		mkdir $HOME/.keychain
+	fi
+	$sshagent -s | grep -v "echo Agent pid" > $HOME/.keychain/$HOSTNAME-sh
+	. $HOME/.keychain/$HOSTNAME-sh

The patch saves output of ssh-agent into file that keychain uses so keychain then reuses the current data and does not have to spawn new ssh-agent. just to make the info complete, this is what i have in my .bashrc:

keychain -q
. ~/.keychain/$HOSTNAME-sh
. ~/.keychain/$HOSTNAME-sh-gpg

bug report at xfce: bug 6558

Tags: , ,

NetBeans And ssh-agent

I am using SSH and DSA key for access to my company repositories. But NetBeans always failed to connect to the server because it did not find app which it could use to ask me for password. After I installed x11-ssh-askpass, the situation improved in a way that NetBeans asked me for my DSA key password each time I issued some command that communicates with the repo. As I use keychain, other way that makes the things work flawlessly is running NetBeans from terminal window, but it has one drawback – it’s pretty annoying to run NetBeans from terminal.

Finally I found a way to make it all work in (for me) ideal way. I modified NetBeans script so that it:

  • runs keychain
  • sources keychain file that exports all needed variables
  • adds keys that I want to the ssh-agent and prompts me for keys password if ssh-agent does not handle the keys yet

Here is the patch for NetBeans script:

--- /usr/share/netbeans-6.9/bin/netbeans.orig	2010-07-17 04:35:11.777217529 +0200
+++ /usr/share/netbeans-6.9/bin/netbeans	2010-07-17 05:25:38.895871679 +0200
@@ -169,6 +169,19 @@
+    keychain -q
+    if [ -e "$HOME/.keychain/$HOSTNAME-sh" ]; then
+        . $HOME/.keychain/$HOSTNAME-sh
+    fi
+    if [ -e $userdir/keychain-keys.txt ]; then
+        for key in `cat $userdir/keychain-keys.txt`; do
+            SSH_ASKPASS=`which x11-ssh-askpass`
+            export SSH_ASKPASS
+            if [ -n "$key" ]; then
+                keychain $key
+            fi
+	done
+    fi
     if [ "${founduserdir}" = "yes" ]; then
         exec $sh "$nbexec" "$@"

So if you have password protected SSH keys, all that you need to do to make them work in NetBeans without being annoyed is:

  • install keychain and x11-ssh-askpass
  • apply the patch above to NetBeans executable script
  • if you want to be prompted for SSH key passwords on NetBeans startup, create file $userdir/keychain-keys.txt and put there name of the files with the keys

Just minor update, I added this support to Gentoo ebuild for NetBeans, so since netbeans-6.9-r3 this feature can be activated by enabling ‘keychain’ use flag.

Tags: ,

Upgrading PostgreSQL Database Schemas

If you are developing applications that work with PostgreSQL or database generally, you often do changes in the development database that you need to transfer to production server one day. Tracking these changes is not that trivial and you cannot use tools like diff on schema dumps. That’s why I created tool that I named Another PostgreSQL Diff Tool, hosted at ( claims the project was founded 2006-03-23).

How does it work? You generate schema dump of production and development databases, and then let apgdiff to generate for you DDL SQL statements that you can use to upgrade your production schema to the latest development schema. You can find more detail description at project website.

Here is little bit larger picture how the tool fits in my development process. For the projects I code I created Hibernate like library that generates schema in database when application runs for the first time. If I change some objects after the first release, I direct my app to different database that is clean, let app generate new schema, then dump old and new schema, run apgdiff to get the update commands, check them to see everything looks ok, and put the update file to my app’s automated update procedure. Then I run the app on my development database, see if schema upgrade was performed without issues, and then continue with development. Before I release my code on production server, I can repeat this cycle several times. Then, when I put the app on production server, all updates are performed while the app is being deployed.

There is a chance that apgdiff will work even for other database servers (and “servers”). Recently somebody posted info that he was able to get apgdiff output even from SQLite dumps. Maybe it’s time to move the app to more general design so developers using other database servers could benefit from this free app too.

This app can be directly installed in Gentoo, Debian, Ubuntu, and probably in some other distros too, though the packages in those distros do not have to be necessarilly the latest ones.

Tags: ,

NetBeans And System Tomcat

At this moment, NetBeans and Tomcat does not work together with each other in Gentoo out of the box. You need to do minor tweak to make it work, but it’s really easy:

# adds user to tomcat group
gpasswd -a user tomcat
# NetBeans expects conf directory in Catalina home
ln -s /etc/tomcat-6/ /usr/share/tomcat-6/conf
# if you want NetBeans to create manager account, you need this
chmod g+w /etc/tomcat-6/tomcat-users.xml

You might need to relogin/restart after gpasswd to get the new user group applied to your user in your desktop environment.

Tags: , ,

NetBeans, IcedTea And Fonts

I bought new laptop and while installing Gentoo on it, I decided to try IcedTea instead of Sun-JDK. But when running NetBeans using IcedTea, the fonts were of serif type which on my resolution was not much readable. Anyway, the solution is easy. Just create symlink to in the IcedTea installation:

cd /opt/icedtea6-bin-1.8.0/jre/lib/
ln -s

When you start NetBeans after this change, the used font will be of sans serif type which is much more readable. More info about this issue can be found at Gentoo Bugzilla.

Tags: ,

Speeding Up emerge

When working on NetBeans ebuilds, it’s often about doing some modifications, then emerging the ebuild … and then finding another problem (it’s most often caused by “playing” with dependencies). What speads the emerge up significantly is using tmpfs for /var/tmp/portage, which is the directory where portage compiles application. Using part of your RAM for emerge is also generally useful for any emerge tasks as it makes it work faster. Here is how to use tmpfs for /var/tmp/portage:

mount -t tmpfs -o size=6G,mode=775,uid=portage,gid=portage tmpfs /var/tmp/portage/

Specify size that you want or can afford to share max from your RAM. After this, your emerge commands will be faster because instead of writing files to disk and reading from disk, most of the operations are performed in memory.

If you want to mount it permanently, you can put something like this below to your /etc/fstab file:

tmpfs /var/tmp/portage tmpfs size=6G,mode=775,uid=portage,gid=portage 1 2

This will automatically mount it on each system boot.