Wednesday, January 27, 2010

Enabling JSP support in Jetty 7

As Jetty 6 failed to use the port inherited from xinetd, I have to use Jetty 7. Unfortunately Jetty is missing the JSP support by default. Here is how to enable it:

  1. Download the Jetty 6 distribution.
  2. Copy the jsp-2.1 folder to the Jetty 7 lib/ folder.
  3. You can check if it works by running java -jar start.jar --list-options
  4. Edit start.ini to include jsp-2.1 in the OPTIONS line.

Jetty 6 vs. Jetty 7

There are several differences between Jetty 6 and Jetty 7:

  • Most classes have been renamed from org.mortbay to org.eclipse and sometimes moved to a different path. Find the new class names in the Jetty 7 JavaDoc. This applies to most settings in jetty.xml and web.xml.
  • Jetty 7 comes with fewer plugins. Important extras such as JSP support are missing from the standard distribution.
  • Jetty 7 includes several bugfixes. Example: Taking a port from xinetd did not work with Jetty 6 on my machine.

More information is at the Porting to Jetty 7 page.

Jetty 7 gzip Filter

Jetty includes a gzip-filter for dynamic compression of contents, thus saving bandwidth and enabling your site to load faster (especially important if you have a lot of javascript).

To configure it, add the follwing filter definition to your web.xml:


  <filter>
    <filter-name>GzipFilter</filter-name>
    <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
    <init-param>
      <param-name>mimeTypes</param-name>
      <param-value>text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,image/svg+xml</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>GzipFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping> 

Please note: This configuration is for Jetty 7. For Jetty 6 change the filter to org.mortbay.servlet.GzipFilter.

Jettys missing passwd.sh

Jetty comes with a very nice Password encrypting command. Unfortunately, there is no passwd.sh to call it, and you have to manually find out where it is defined. Also, the documentation is for Jetty 6 only, and the names of the classes and libraries have changed in Jetty 7.

Here is the missing passwd.sh for Jetty 7:


#!/bin/bash

# look for JETTY_HOME
if [ -z "$JETTY_HOME" ] 
then
  cd $(dirname "$0")
  JETTY_HOME_1=$(pwd)
  cd - > /dev/null
  JETTY_HOME_1=$(dirname "$JETTY_HOME_1")
  JETTY_HOME=${JETTY_HOME_1} 
fi

java -cp $JETTY_HOME/lib/jetty-http-7.0.1.v20091125.jar:\
$JETTY_HOME/lib/jetty-util-7.0.1.v20091125.jar \
org.eclipse.jetty.http.security.Password $*

Please note that the JETTY_HOME detection is also changed from the original jetty shell scripts to be more sane.

Tuesday, January 26, 2010

Jetty vs. Tomcat

When setting up a new webserver, I decided to evaluate Jetty as an alternative to Tomcat. I have previousely used Tomcat, and was content with it. Here is what I've found:
Both:
  • Implement recent JSP and Servlet standards
  • Can easily run .WAR files
  • Are pretty fast when serving content (speed comparisons on the web show only minor differences)
Pro Jetty:
  • Jetty uses less memory
  • Jetty seams more "lightweight"
Pro Tomcat:
  • Tomcat is very well documented - Jetty Documentation is sparse and targeted towards Java developers. The Jetty config file is (intentionally) like coding
  • I know Tomcat by now
As the machine I am using now is low on memory, I decided to give Jetty a try. However, installing it was not easy. But that's a topic for the next post.

Monday, January 25, 2010

Useful PNG commands

Useful command for handling PNG files.

Prerequisites: ImageMagick, Pngcrush

Resizing

Resize to a given maximum size

for i in *png ; do convert $i -geometry 16x16 x/$i; done

Resize exactly to given size, adding transparent borders

for f in $(for i in x/* ; do file $i | grep -v '16 x 16' | cut -d: -f1; done) ; 
  do convert $f -type TrueColorMatte -compose Src -gravity center -extent 16x16 y/$f ; 
done

Optimizing

Trying different PNG compression settings, removing extra PNG contents (such as comments)

for i in y/*png ; do pngcrush  -rem alla -reduce -brute "$i" z/"$i"; done

Why (not) 64 bit

Why 64 Bit Windows

Most chosen actual reason:

  • Because you payed a lot of money for that new 64 bit processor and feel cheated if you don't use it

Technical Reasons

  • You really have an Applications which needs more than 4 GB of memory. (In most cases: You don't, unless you run a large database or huge self-written scientific applications). Use the system profiler to find out how much memory you actually need.)
  • Your System has more than 3,5 GB of memory (3 GB for Windows XP) and you actually need it.

Why not 64 Bit Windows

  • Because you don't need it (see above)
  • Drivers support is incomplete
  • Some applications don't work
  • Many programs are installed twice: in the 32bit and 64bit versions, and some plugins work in one, but not the other. Notable Examples: Internet Explorer, Firefox, Explorer

Biggest Myth

  • a 64 bit OS is not faster than a 32 bit OS!

Please note: Most of this is also true for 64 bit Linux.

Desktop file format

Confused about all the .desktop files in Gnome / KDE? There is an official spec at: www.freedesktop.org/wiki/Specifications/desktop-entry-spec.

Where are they?

All User files are in /usr/share/applications
Personal files are in $HOME/.local/share/applications

Gnome additions: / Autostart

 
X-GNOME-Autostart-enabled=true

should be set for files in $HOME/.config/autostart. This describes autostart applications. More info on autostart: KDE Bug #170817.

PEM from/to P12

I commonly need to convert between PEM and p12

PEM
Is an ASCII format and can be opened with a text editor. It is used by most SSL-based tools. Key and certificate are two separate files.
p12
Acutally: Pkcs12 is used by most browsers. Key and certificate are in one file.

PEM to P12

You need both the key and the certificate:

 
openssl pkcs12 -export -in usercert.pem -inkey userkey.pem -out bundle.p12

P12 to PEM

 
openssl pkcs12 -in bundle.p12 -out userkey.pem -nodes -clcerts

The resulting file contains both the key and certificate. Use a text editor to split into two files again.

Singleton Pattern in Java

The singleton pattern ensures that there is exactly one instance of a class.

There are multiple ways to implement the Singleton Pattern in Java, here is my personal favorite:


 
public final class SomeClass {
 
  private static final class SingletonHolder {
    private static final SomeClass INSTANCE = new SomeClass();
    private SingletonHolder() {}
  }
 
  private SomeClass() {
    // Add code if needed
  }
 
  /**
    * @return the Singleton Instance.
    */
  public static SomeClass getInstance() {
    return SomeClass.SingletonHolder.INSTANCE;
  }
 
  // add other methods
 
}

Notes:

  • Replace SomeClass with the class name, keep all other names exactly as given.
  • Class is final to ensure its constructor cannot be made public by subclassing.
  • The SingletonHolder nested class enables lazy loading and ensures synchronization without performance loss.
  • Constructor must be made private.
  • getInstance returns the singleton instance of this class.

Find mime-type for a file

Here is how to determine the mime-type for a given file on the command line.

OS X

file --mime '''filename''

Linux

xdg-mime query filetype '''filename'''
gvfs-info -a standard::content-type '''filename''' | grep content | cut -d: -f4 | cut -d\  -f2-
gnomevfs-info '''filename''' | grep MIME | cut -d\: -f2- | cut -d\  -f2-
file --mime '''filename''' | cut -d\: -f2- | cut -d\  -f2

Everywhere

Update: We can modify the conditional alias to support mime types as well:

conditional_alias2 () {
  UNSET=false
  alias $1 > /dev/null 2>&1 || UNSET=true
  [ $UNSET = true ] && which $2 > /dev/null 2>&1 && alias $1=$3
}
mime_gvfs() {
  gvfs-info -a standard::content-type $1 | grep content | cut -d: -f4 | cut -d\  -f2-
}
mime_xdg() {
 xdg-mime query filetype $1
}
mime_gnomevfs() {
 gnomevfs-info file://$PWD/$1 | grep MIME | cut -d\: -f2- | cut -d\  -f2-
}
conditional_alias2 mime gvfs-info mime_gvfs
conditional_alias2 mime xdg-mime mime_xdg
conditional_alias2 mime gnomevfs-info mime_gnomevfs

Command Line SSL

Finally found out how to connect to an SSL server over the command line:

openssl s_client -connect www.example.com:443

What a great netcat replacement!

Command Line Open in UNIX

On OS X I can just "open" any file. On Windows, I can use "start". And on Linux? On modern installations, the "xdg-open" command works just fine. Unfortunately - this is not always the case. And I don't want to remember different commands for different machines. So here is the ultimate open alias setting for bash, for all systems that I use (it can be easily extended for other environments).


 
conditional_alias() {
  UNSET=false
  alias $1 > /dev/null 2>&1 || UNSET=true
  [ x$UNSET = xtrue ] && which $2 > /dev/null 2>&1 && alias $1=$2
}
 
conditional_alias open gvfs-open
conditional_alias open xdg-open
conditional_alias open gnome-open
conditional_alias open exo-open
uname | grep MINGW > /dev/null 2>&1 && alias open=start

Home

Followers