Archive for October, 2008

BeansBinding ELProperty as simple template engine

I just read an article about using adding EL support on your projects and I was looking for a nice alternative to Velocity anyways.

I know EL basics from BeansBinding, which I use in most of my UI projects. EL itself looks pretty interesting, but, well, sort of complicated. Even finding the proper jar files seems to be a problem, or at least “another of life’s obscure mysteries” and using EL for a simple search and replace example implies implementing some internals. I was looking for something that I can just use.

Luckily, the first comment mentions the ELProperty class from BeansBinding, and that jar is just a few mouse clicks away, so I thought I give it a try…and…well…it works ;)

› Continue reading

Tags: ,

Thursday, October 30th, 2008 Other 1 Comment

combine multiple jar files and remove signatures

I just came across a situation where I had to join multiple jar files and my own classes into one jar bundle. Ant comes in handy:

<jar destfile="${app.id}-${app.version}.jar"
  index="true"
  filesetmanifest="merge">
  <fileset dir="${classes.dir}"/>
  <zipgroupfileset dir="${lib.dir}" includes="*.jar" />
</jar>

This takes all the files in ${classes.dir}, packs them in a jar and add the content of all the jar files in ${lib.dir}. In this case, we merge multiple manifests, but this can be changed according to the jar task documentation.
› Continue reading

Tags: , ,

Wednesday, October 29th, 2008 Other 2 Comments

Add Copyright to Java Sources

Just found a nice Eclipse plugin that adds license information to a all the files (well, you can select them) in a project. You can specify the license, enter your own, customize the output …. nice !

Just reminds me, we had a java class that did that for QAlign 2 Panta Rei source code distribution.

Yeah, there it is, May 21, 2005 written in Michas old apartment in Barcelona while working on QAlign2.

The QAlign2 crew

The QAlign2 crew

› Continue reading

Tags: ,

Wednesday, October 22nd, 2008 Eclipse, Other No Comments

ANT build.xml template

Just that I have it at one place. This is my ant build.xml template. It assumes a simple project structure:

src/ - all java soources in here
lib/
  devel/ - development libraries
  runtime/ - runtime libraries

If your project follows this struicture, you can get the following tasks out of the box:

Buildfile: build.xml

Main targets:

 clean          Clean
 compile        Compile all source files
 compile-tests  Compile all source files
 dist           Build library and pack jar file
 dist-one       Build library and pack jar file
 doc            Create Javadoc
 html-reports   Creates HTML from test reports
 test           Run JUnit Tests
Default target: dist

You can modify the properties to adapt different names or a different project structure. The script creates a target directory that contains the results.

Compiling and packing should work out of the box, but the test sections expects a junit and ant-junit jars in lib/devel. They can also be placed in one of Ants lib directories.

The build file

Tags:

Wednesday, October 22nd, 2008 Other No Comments

WTF per minute

Yes, that is the right code quality measurement

Tuesday, October 21st, 2008 Other No Comments

Packagesorter Sources

Here are the sources to the package sorter Eclipse plugin. The plugin is released under the EPL license and is packed as Eclipse Project. Unzip and import the directory into your Eclipse workspace.

Download

Tuesday, October 14th, 2008 Other No Comments

Install Java 6 Update 10 on Ubuntu 8.04

I had to install Java 6 Update 10 on Ubuntu 8.04. The thing is straight forward if you do not want to integrate the new JVM into Ubuntus alternatives system (and do not need teh new Java Plugin in your browser). Download, install, modify your PATH, JAVA_HOME and JDK_HOME, done. Google helped to find this really good description of the process, including moving the JVM to /usr/lib/jvm, where Ubuntu expects the JVMs.

The tricky part is the integration into Ubuntus alternatives system. It provides an easy way to switch between VMs and is usable even by “non geek” users and update-java-alternatives will also switch your java browser plugin.

We have to have Java 6 installed from the Ubuntu repositories:

sudo apt-get install sun-java6-jdk

Now, follow Caspers first two steps to install the VM. Download Update 10 and execute the script

sh jdk-6u10-rc2-bin-b32-linux-amd64-12_sep_2008.bin

Move the directory to /usr/lib/jvm, where Ubuntu expects JVMs

sudo mv jdk1.6.0_10 java-6-sun-1.6.0.10
sudo mv jdk1.6.0_10/ /usr/lib/jvm

And create a symlink

cd /usr/lib/jvm
sudo ln -s java-6-sun-1.6.0.10 java-6.10-sun

The directory should look more or less like this

lrwxrwxrwx   1 root   root      23 2008-10-07 19:01 java-1.5.0-sun -> java-1.5.0-sun-1.5.0.15
drwxr-xr-x  10 root   root    4096 2008-10-08 11:34 java-1.5.0-sun-1.5.0.15
-rw-r--r--   1 root   root    2165 2008-03-26 02:28 .java-1.5.0-sun.jinfo
lrwxrwxrwx   1 root   root      19 2008-10-08 12:21 java-6.10-sun -> java-6-sun-1.6.0.10
lrwxrwxrwx   1 root   root      19 2008-10-07 19:00 java-6-sun -> java-6-sun-1.6.0.06
drwxr-xr-x   8 root   root    4096 2008-10-08 11:35 java-6-sun-1.6.0.06
drwxr-xr-x  10 root   root    4096 2008-10-08 11:40 java-6-sun-1.6.0.10
-rw-r--r--   1 root   root    2341 2008-10-08 11:47 .java-6-sun.jinfo

To integrate the new VM into the alternatives system, we use update-alternatives --install to get new alternatives and provide a .jinfo file used by update-java-alternatives. For simplicity, I (and the script) assume that you have Java 6 installed. We need the Java 6 .jinfo file as a template. Basically, we h clone and modify the file, such that all the links point to the appropriate directory. Well, that can be done with your favourite text editor. The ugly part is, you have to call udpate-alternatives --install for ALL the commands. That calls for a script
.

#!/usr/bin/perl
use strict;

# use this as template - we assume that you have java 6 installed
# its in the repo !
my $jvm_template="java-6-sun";
my $jvm_template_path="/usr/lib/jvm/java-6-sun";

# this is the new jvm
my $jvm_name="java-6-sun-1.6.0.10";
my $jvm_alias="java-6.10-sun";
my $jvm_path="/usr/lib/jvm/java-6.10-sun";
my $jvm_priority=64;
my $jvm_section="non-free";

open(JINFO_OUT, ">/usr/lib/jvm/.$jvm_alias.jinfo") or die "Can not write jinfo file !";

#write header
print JINFO_OUT "name=$jvm_name\n";
print JINFO_OUT "alias=$jvm_alias\n";
print JINFO_OUT "priority=$jvm_priority\n";
print JINFO_OUT "section=$jvm_section\n\n";

my @lines = ();
open(IN, "/usr/lib/jvm/.$jvm_template.jinfo") or die "Can note read jinfo template!";
@lines = <IN>;
close(IN);

my @new_config = ();
foreach(@lines){
    if($_=~ /$jvm_template_path/){
        chomp($_);
        $_ =~ s/$jvm_template_path/$jvm_path/;
        push(@new_config, $_);
        print JINFO_OUT "$_\n";
    }
}
close(JINFO_OUT);

foreach(@new_config){
    my @split = split(' ', $_);
    system("update-alternatives --install /usr/bin/@split[1]  @split[1]  @split[2] $jvm_priority");
}

Download the script and execute it (with sudo/as root – we have to write to /usr/lib/jvm) after you have moved the update 10 installation to /usr/lib/jvm/java-6-sun-1.6.0.10 and created the symlink to java-6.10-sun as described above. (The script is quick and dirty. The names are defined in the first lines. If you have used other directory/alias names, you have to modify the script).

sudo perl create-jvm-alternative-6.10.pl

This generates a .jinfo file in /usr/lib/jvm and it calls update-alternatives --install for all the things mentioned in the template .jinfo. The update-alternatives takes a generic name as first argument (thanks Jonathan – I should read man pages more often). This is created as a symlink pointing to the actual name in /etc/alternatives. /etc/alternatives then points to the currently chosen alternative. SO, for example, at some point the script calls:

update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-6.10/bin/java

This creates two symlinks

/usr/bin/java -> /etc/alternatives/java
/etc/alternatives/java -> /usr/lib/jvm/java-6.10/bin/java

The latter might already exist and point to some java alternative (i.e. java v1.5). As far as I’ve seen this is not overwritten. The latter symlink is the one that is actually changed when you switch alternatives.

When everything is done, you should get something like:

thasso@antef:~> update-java-alternatives -l
java-1.5.0-sun 53 /usr/lib/jvm/java-1.5.0-sun
java-6.10-sun 64 /usr/lib/jvm/java-6.10-sun
java-6-sun 63 /usr/lib/jvm/java-6-sun
Thursday, October 9th, 2008 Other 10 Comments

A simple AbstractBean

Just in case, here is a simple example of an AbstractBean class that can be used as a base class for JavaBeans if you want to use the Eclipse bouded setter plugin.

public abstract class AbstractBean {
    /**
     * Create a new change support
     */
    protected PropertyChangeSupport change = new EDTSafePropertyChangeSupport(this);

    /**
     * @param listener
     * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        change.addPropertyChangeListener(listener);
    }

    /**
     * @param propertyName
     * @param listener
     * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
     */
    public void addPropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        change.addPropertyChangeListener(propertyName, listener);
    }

    /**
     * @param propertyName
     * @param oldValue
     * @param newValue
     * @see java.beans.PropertyChangeSupport#firePropertyChange(java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void firePropertyChange(String propertyName, Object oldValue,
            Object newValue) {
        change.firePropertyChange(propertyName, oldValue, newValue);
    }

    /**
     * @return
     * @see java.beans.PropertyChangeSupport#getPropertyChangeListeners()
     */
    public PropertyChangeListener[] getPropertyChangeListeners() {
        return change.getPropertyChangeListeners();
    }

    /**
     * @param propertyName
     * @return
     * @see java.beans.PropertyChangeSupport#getPropertyChangeListeners(java.lang.String)
     */
    public PropertyChangeListener[] getPropertyChangeListeners(
            String propertyName) {
        return change.getPropertyChangeListeners(propertyName);
    }

    /**
     * @param propertyName
     * @return
     * @see java.beans.PropertyChangeSupport#hasListeners(java.lang.String)
     */
    public boolean hasListeners(String propertyName) {
        return change.hasListeners(propertyName);
    }

    /**
     * @param listener
     * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        change.removePropertyChangeListener(listener);
    }

    /**
     * @param propertyName
     * @param listener
     * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
     */
    public void removePropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        change.removePropertyChangeListener(propertyName, listener);
    }

    /**
     * Simply ensures that events are delivered through the event dispatching thread
     *
     */
    class EDTSafePropertyChangeSupport extends PropertyChangeSupport{
        public EDTSafePropertyChangeSupport(Object sourceBean) {
            super(sourceBean);
        }
        @Override
        public void firePropertyChange(final PropertyChangeEvent evt) {
            if(SwingUtilities.isEventDispatchThread()){
                super.firePropertyChange(evt);
            }else{
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        firePropertyChange(evt);
                    }
                });
            }
        }
    }
}

This implementation restricts itself to the firePropertyChange(String,Object,Object) method. It also provide a custom extension of PropertyChangeSupport that ensures that events are fired in the EventDispatchingThread.

Tags: , ,

Thursday, October 2nd, 2008 Eclipse, Swing No Comments

generate bounded setters plugin

In many cases you need to have bound properties while working with JavaBeans. It might be easy to add PropertyChangeSupport but this does not solve the hassle of modifying your setter methods to fire change events. Of course you can do that with a search and replace pattern, but this is not a very nice and intuitive solution. A better way to address the problem is changing the Eclipse template for setter methods. This works quit well and you can keep things short, but you will loose the ability to create unbound setter methods. Of course, you can modify the template on a per project bases, but in my case, I always end up in situations where I need both bound and unbound properties.

I searched the net to find an Eclipse plugin that does the job of creating bound properties, but I didn’t find anything. So….I wrote my own plugin. The plugin works with Eclipse Ganymede (>=3.4) and can be installed using this update site:

http://java.randgestalten.de/updatesite

At the moment, the the plugin assumes that your bean provides a firePropertyChange(String, Object, Object) method. Thats one drawback I will try to address in the next version. For the moment, I think a good solution is to provide an AbstractBean class that you extend when you write a JavaBean from scratch ( here is an example of such class).

As long as your class provides a firePropertyChange method, you can use the plugin to generate bounded getter/setter methods. The plugin uses the default template for getter methods and provides a new template for bounded setters. You find the template in the Java->Editor->Templates preferences, where you can change it according to your needs.

Using the plugins mechanism to create getter/setter is straight forward, integrated in the “Source” menu of the context popup in the java editor as well as the package explorer and outline views. Just start writing your bean, right-click in the editor and select Source->Generate bounded getters/setters. This will open the same dialog as the default getter/setter generation except that it uses the boundedsetter template to create the setter method.

Menu integration

Menu integration

Beside the integration into the context menu, the plugin also supports quickfix and quickassist. Move your cursor to a variable definition and hit ctrl-1 (or command-1 if you are on a mac) to open the quickfix menu. The plugin integrates a bounded getter/setter generation quickfix that follows the same rules as the default generation behavior. You will see the same dialog except that you will get a bounded setter when you hit the Ok button.

So, as said before, the plugin integrates nicely into the Eclipse mechanism of generating getter/setter methods. You have a custom template just for the bounded setters and all dialogs are basically the same.

Quickfix Dialog

Quickfix Dialog

The thing that is missing currently is the automatic generation of the property change support or some delegation methods. This involves more work, because we have to check for existing methods…create property change support….insert delegation methods etc. I will try to build that in the next version, but for the moment you have to provide that by yourself. Either let your Bean extend an AbstractBean implementation that already supports property changes or integrate it directly into the object using PropertyChangeSupport. Keep in mind that plugin currently expects a firePropertyChange(String,Object,Object) method.

EDIT
The plugins source code is available here.

UPDATE

I just checked, and it seems that the Plugin is also working with Eclipse 3.5 but you have to uncheck the “Group items by category” to get the plugins listed.

Tags: , , ,

Wednesday, October 1st, 2008 Eclipse 7 Comments