How-to install KeePassX on Ubuntu

Do you have to remember an ever increasing amount of passwords? If you do, then KeePassX might be of interest to you. It’s a password manager which has high security standards and which is additionally available on Microsoft Windows and Mac OS X. If you are running Ubuntu (this probably also applies to recent Debian versions), the following snippet might be of interest to you as it shows how to install version 0.4.3 of this program. After installation, you can find it in your desktop manager’s application menu (under GNOME 2 it’s located in Applications -> Accessories -> KeePassX).

NAME='keepassx-0.4.3'
FILENAME=$NAME'.tar.gz'

echo ':: Installing prerequisites...'
sudo apt-get install libxtst-dev libqt4-dev qt4-qmake

echo ':: Downloading and extracting file...'
wget 'http://downloads.sourceforge.net/keepassx/'$FILENAME
tar xvf $FILENAME

echo ':: Installing...'
cd $NAME
qmake
make
sudo make install

echo ':: Cleaning up...'
cd ..
rm -rf $NAME $FILENAME

How-to install nginx

Since there is so much buzz around nginx, I wanted to give it a try on my small home server. The server is running a desktop edition of Ubuntu 10.04 (I keep it as backup machine in case my laptop breaks), so keep this in mind. The following bash script will download nginx version 1.1.12, its dependencies and install it. Also, a user nginx will be created.

# Start by creating an appropriate user
useradd -r nginx
mkdir /home/nginx
chown nginx:nginx /home/nginx/

# Retrieve nginx dependencies
cd /home/nginx
mkdir library
cd library
wget http://zlib.net/zlib-1.2.5.tar.gz
tar xfz zlib-1.2.5.tar.gz
rm zlib-1.2.5.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar xfz pcre-8.21.tar.gz
rm pcre-8.21.tar.gz
cd ..

# Installing nginx
wget http://nginx.org/download/nginx-1.1.12.tar.gz
tar xfz nginx-1.1.12.tar.gz
rm nginx-1.1.12.tar.gz
cd nginx-1.1.12
./configure --prefix=/home/nginx \
    --user=nginx \
    --group=nginx \
    --with-pcre=../library/pcre-8.21/ \
    --with-zlib=../library/zlib-1.2.5/
make
make install
cd ..
rm -rf nginx-1.1.12/
chown -R nginx:nginx *
sed -i 's/listen\s*80;/listen 8080;/g' conf/nginx.conf
export NGINX_HOME='/home/nginx/'
export NGINX_PID_FILE=$NGINX_HOME'logs/nginx.pid'

In case you encounter any problems, please refer to the short, but sufficient, official documentation. If it worked out for you, you can now use the following commands to start and stop the server as well as to reload the config.

# Start nginx (runs on port 8080)
su nginx -c $NGINX_HOME'sbin/nginx'

# Reload nginx config
kill -HUP $(cat $NGINX_PID_FILE)

# Stop nginx
kill -QUIT $(cat $NGINX_PID_FILE)

Check out the following blog if you need an init script for nginx.

Installing Rails 3 on a fresh Ubuntu installation

Since I find myself researching the same information a second time, I put it on my blog for further reference. The following steps were everything that is needed to get started with Rails 3 on a fresh Ubuntu 10.10 installation.

# Installing rails, rubygems and its dependencies
sudo apt-get install build-essential ruby1.8-dev libsqlite3-dev
wget http://rubyforge.org/frs/download.php/75574/rubygems-1.8.12.zip
unzip rubygems-1.8.12.zip
cd rubygems-1.8.12.zip
sudo ruby setup.rb
sudo gem install rdoc-data; rdoc-data --install
sudo gem install rails

# create project
rails new .

# only necessary when no JavaScript runtime environment exists
echo "gem 'therubyracer'" >> Gemfile
sudo bundle

# starting the server
rails s

Fixing the unable to initialize TldScanner problem when deploying

With the change to Glassfish 3.3 I encountered the TldScanner problem when deploying a simple EAR file. Since the problem did not occur with Glassfish 3.0.1 and because some of my colleagues were also having problems I investigated the problem today.

The problem is reported by many people in various mailing lists and issue trackers but in all cases the solution wasn’t fixing the problem. After replacing several dependencies (the server log stated sometimes that the problem occurred in a third party library..) and trying various solutions which I had found on the internet, I changed the directory structure and removed all whitespaces from the directory and file names. Even though you might figure that an application server, as mature as Glassfish, might be able to cope with whitespaces in directory names, it is not.

Long story short, I created a symlink in my home directory (/home/ben/odr) which points to the existing checkout at /home/ben/fontys/some semester identifier with whitespace/odr. This solved the TldScanner problem and everything was running fine again. Sometimes the simple solutions are the right ones, even in the Java EE world :-) .

Define JNDI data source for Apache Tomcat

Defining data sources via JNDI has various benefits, e.g. pooling and completely external configuration. The latter is especially important as this avoids putting database credentials into your application. Since I’m currently studying the Spring framework, I also wanted to use another application server. In fact, I wanted to try Apache Tomcat 7, a servlet container.

I have to say that I found the JNDI how-to for Apache Tomcat insufficient and therefore want to share some information that I collected in addition to the how-to.

At first, they show what a context.xml file should look like but they don’t tell you where to put it (ok they lead you to another documentation page). I found the simplest place to be the $CATALINA_BASE/conf/context.xml as this is probably sufficient for most use cases. Please note though that all deployed applications will have access to this data source. The next thing you shouldn’t forget is to put the driver into $CATALINA_BASE/lib/. In combination with the aforementioned how-to this should be all you need to use JNDI data sources with Apache Tomcat.

Gravatar for Java applications

Gravatar for Java (actually Gravatar4Java) is a small library for the generation of Gravatar URLs. For those readers who don’t know Gravatar – Gravatar allows you to have one avatar everywhere by uploading it to the Gravatar website. Other websites can then automatically retrieve an avatar for a user by using the email address as an identifier.

There is already a Java library around Gravatar by Ralf Ebert but it lacks some of the newer functionality like avatar over HTTPS or custom default avatars. Additionally it’s not a Maven project nor hosted on Maven Central, thus enough reasons to write my own.

The Gravatar4Java project is hosted on GitHub and already available in Maven Central (actually my first project in Maven Central)! Following is the typical Maven dependency snippet, a plain Java example and a Spring beans definition for the library.

<dependency>
    <groupId>de.bripkens</groupId>
    <artifactId>gravatar4java</artifactId>
    <version>1.1</version>
 </dependency>

 

import de.bripkens.gravatar.DefaultImage;
import de.bripkens.gravatar.Gravatar;
import de.bripkens.gravatar.Rating;

// ...

String gravatarImageURL = new Gravatar()
    .setSize(50)
    .setHttps(true)
    .setRating(Rating.PARENTAL_GUIDANCE_SUGGESTED)
    .setStandardDefaultImage(DefaultImage.MONSTER)
    .getUrl("foobar@example.com");

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p">

    <!-- Gravatar -->
    <bean id="gravatar"
          class="de.bripkens.gravatar.Gravatar"
          p:size="75"
          p:rating="GENERAL_AUDIENCE"
          p:standardDefaultImage="MONSTER" />

</beans>

SVG path arc curve command

The basic SVG rect shape provides functionality for rounded corners. Unfortunately, you can only change it for all four corners. I needed a rectangle which supports something like the CSS3 top- and bottom-border-radius instructions.

#foo {
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    border-bottom-right-radius: 20px;
    border-bottom-left-radius: 20px;
}

I needed this because of my GSoC project. In this project, there are different nodes types which have to be visualized. You can see the two node types (which are the reason for this blog post) in the following figure.

Two node types of the SBGN

The two node types which are the reason for this blog post.

Sticking to the Don’t-Repeat-Yourself paradigm, I abstracted the drawing of rectangular shapes into a base class RectangularNode from which the classes Macromolecule and NucleicAcidFeature inherit. Since the two node types are the same (with regard to implementation), except for the border radius, I put all the funtionality into the base class RectangularNode. Now the Macromolecule and NucleicAcidFeature classes are merely subclasses which set the border radius on the RectangularNode class.

Now to the drawing of rectangles with different border-radii, there is no basic SVG shape which supports such behavior. As a result, you need the SVG path element. The path element enables you to draw such things as curves and arcs and can be used to draw every possible shape. For this specific issue, the arc is needed. Please refer to the specification for an explanation of the various commands and the path element in general. In this post I’ll only cover the generation of rectangles with different border radii and the arc command. The arc command is defined the following way (expressed as a regular expression in JavaScript with an example parsing). Continue reading

HTML / JavaScript calculate text dimensions

As part of my Google Summer of Code 2011 I have to write a client side visualization library in JavaScript and of course textual elements are part of this visualizations. To calculate the dimensions of any text you can make use of an HTML DIV element. With the help of some specific styles it will then auto adapt its size to the text. That’s how you do it.

Add the following to your stylesheet (CSS).

.textDimensionCalculation {
    position: absolute;
    visibility: hidden;
    height: auto;
    width: auto;
    white-space: nowrap;
}

This snippet adds a CSS class named textDimensionCalculation which we will apply to the DIV for the calculation of the text dimensions. The next listing contains the interesting part. I’m using jQuery to simplify things in this example. Continue reading

Maintaining and testing scope in JavaScript

One could say that the behaviour of “this” in JavaScript is rather unconventional, at least when you have a strong object-oriented background like I do. For example, libraries like jQuery change the scope of event listeners which does not allow you to refer to a surrounding object (think an object’s method is used as an event listener and in this method you try to access the object’s properties through “this”).

To maintain the scope we can create closures which make sure that the appropriate scope is set when the function / listener is called.

/**
 * @description
 * <p>We extend the prototype of all functions with the function
 * createDelegate. This method allows us to change the scope of a
 * function.</p>
 *
 * <p>This is useful when attaching listeners to jQuery events like click
 * or mousemove as jQuery normally uses this to reference the source
 * of the event. When using the createDelegate method, this will point to
 * the object that you want to reference with this.</p>
 *
 * <p>Source:
 * <a href="http://stackoverflow.com/questions/520019/controlling-the-value-of-this-in-a-jquery-event">
 *     Stackoverflow
 * </a></p>
 *
 * @param {Object} scope The scope which you want to apply.
 * @return {Function} function with maintained scope
 */
Function.prototype.createDelegate = function(scope) {
    var fn = this;
    return function() {
        // Forward to the original function using 'scope' as 'this'.
        return fn.apply(scope, arguments);
    };
};

I ran into this issue when I wrote my first bigger (> 7000 lines of code) object-oriented JavaScript application. Additionally, you can find a QUnit test in the following listing. It shows how you can tell whether the correct scope was applied.

test('function.createDelegate', function() {
    expect(1);

    var scope = { answer : 42 };

    var verify = function() {
        equal(this.answer, 42, 'Wrong scope applied.');
    }

    var delegate = verify.createDelegate(scope);

    delegate.call({ answer : 41})
});