Monday, June 30, 2008

Skip Tests in Maven [Tip]

If your tests are failing and you can't be bothered to fix them, this handy tip will show you how you can skip tests and build your Maven project straightaway!

Simply add the property -Dmaven.test.skip=true to your Maven command. Here is an example:

mvn -Dmaven.test.skip=true install

The compiler will not compile the test sources and will not run your JUnit tests:

[INFO] [compiler:testCompile]
[INFO] Not compiling test sources
[INFO] [surefire:test]
[INFO] Tests are skipped.

But of course, your test classes should always run, right?


Friday, June 27, 2008

Read a File Without Trimming Leading Whitespace [Shell Scripting]

This is how you would normally read a file, line-by-line, in a shell script:
while read line
    echo "$line"
done < $file
The problem with this is that the "read" command automatically removes leading whitespace from each line and also concatenates a line ending with a backslash with the one following. This means that you cannot properly process a file with lots of leading whitespace e.g. xml files.

The trick around this, is to redefine your IFS (Internal Field Separator) variable. By default, IFS is set to the space, tab and newline characters to delimit words for the read command. This is how you can amend your script:

while read -r line
    echo "$line"
done < $file
First save the current value of IFS into a temporary variable called OIFS. Then blank out IFS. When you have finished reading your file, set IFS back to its original value of OIFS.

We also provide the read command with the -r flag, so that it treats each backslash to be part of the input line and does not concatenate it with the next line.

Tuesday, June 24, 2008

Shutting Down Java Apps [Howto]

In most cases, users do not always follow the recommended procedure to exit their java applications. They may not type "quit" or "exit" and may instead hit Ctrl+C to terminate the process immediately. In these cases, the application does not get a chance to clean-up.

Fortunately, there is a way to ask the JVM to execute some clean-up code before exiting. This is done by using ShutdownHooks which are registered with System Runtime. The JVM starts all registered shutdown hooks concurrently when shutting down.

Here are the steps involved when creating shutdown hooks:

1. Create the shutdown hook class:

public class AppShutdownHook extends Thread{
    public void run(){"Running shutdown hook...") ;
        //cleanup e.g. close database connections
2. Register the shutdown hook:
public class App{
    public App(){
          addShutdownHook(new AppShutdownHook()) ;
Note that no guarantee can be made about whether or not any shutdown hooks will be run if the JVM aborts with the SIGKILL signal (kill -9) on Unix or the TerminateProcess call on MS Windows.

However, SIGTERM (kill with no arguments) will work.


Monday, June 23, 2008

Moving House

I'm back at work today after having taken time off since Wednesday to move house. I spent Wednesday packing boxes and got the CHAPS transfer and mail redirection (£35.40 for a year) done. Keys were exchanged at 1pm on Thursday, and it took two Luton vans + three removal men (£270) to carry our stuff to the new house. We were all done by 4pm. We've unpacked our important items but are still mostly surrounded by a sea of boxes! Its good to be back at work though!

Here is a list of useful bookmarks if you're moving house:

Tuesday, June 17, 2008

Firefox 3 Released!

Firefox 3 is here! Download it now and help set a Guinness World Record for the most downloads in 24 hours!

You even get a certificate!

Friday, June 13, 2008

Change Logging Levels using JMX [Howto]

This handy example shows you how you can use Java Management Extensions (JMX) to change your application's logging level.

Step 1: Create your management interface which consists of all the attributes which can be read or set and all the operations that can be invoked. In this case, we want to change the logging level of our application.

public interface MyAppMBean{
    public void setLoggingLevel(String level) ;
    public String getLoggingLevel() ;
Step 2: Create a class which implements the MBean interface
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import com.sun.jdmk.comm.HtmlAdaptorServer;

public class MyApp implements MyAppMBean{

    private static Logger logger = Logger.getLogger(MyApp.class);

    public void go() throws Exception{
            logger.debug("DEBUG") ;
  "INFO") ;

    public void setLoggingLevel(String level){"Setting logging level to: " + level);
        Level newLevel = Level.toLevel(level, Level.INFO);

    public String getLoggingLevel(){
        return Logger.getRootLogger().getLevel().toString() ;
Step 3: Register the MBean with the MBeanServer. Also register an HTMLAdaptorServer which allows us to manage an MBeanServer through a web browser.
public static void main(String[] args) throws Exception{
    MyApp app = new MyApp() ;
    ObjectName objName = new ObjectName("MyApp:name=MyApp");
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    server.registerMBean(app, objName);

    int portNumber=9393;
    ObjectName htmlName = new ObjectName(
       "MyApp:name=MyAppHtmlAdaptor,port="+portNumber) ;
    HtmlAdaptorServer html = new HtmlAdaptorServer(portNumber);
    server.registerMBean(html, htmlName);
Step 4: Compile. Make sure you have log4j, jmxtools and a log4j properties file in your classpath

Step 6: Run MyApp. You need to add the following JVM properties:
Step 5: Open jconsole
  • Click on MyApp and press Connect
  • Go to the MBeans tab and choose MyApp from the tree on the left
  • You can then change your Logging Level while the application is running!
Step 6: Open the web console
  • Go to http://localhost:9393
  • Click on name=MyApp
  • Change the LoggingLevel!

Thursday, June 12, 2008

Firefox 3 - Release Date Tuesday, June 17, 2008

The Mozilla Developer Center is reporting Firefox 3 will be released on Tuesday, June 17, 2008!
After more than 34 months of active development, and with the contributions of thousands, we’re proud to announce that we’re ready. It is our expectation to ship Firefox 3 this upcoming Tuesday, June 17th. Put on your party hats and get ready to download Firefox 3 - the best web browser, period.

I've been using the betas and RCs for some time now and can't wait to get my hands on this release. If you haven't already pledged to download it, and help set a world record for the most software downloaded in 24 hours, do so now: (Total pledges 1,109,453 so far)

Monday, June 09, 2008

Google's New Favicon

You may have noticed that Google has a new favicon, the small icon (16x16 favicon.ico image file) you see in your browser next to the URL or in your bookmarks list. It has changed from the familiar upper-case G to a lower-case one.

According to an official Google post:
The reason is that we wanted to develop a set of icons that would scale better to some new platforms like the iPhone and other mobile devices. So the new favicon is one of those, but we’ve also developed a group of logo-based icons that all hang together as a unified set.

To me, its an obvious play on the infinity sign - Google ad infinitum.

The new icon has yet to grow on me. The old one, whilst old-fashioned, was immediately recognisable as part of the Google brand.

Friday, June 06, 2008

Send Your Name to the Moon

Sign up to send your name to the moon! Names will be collected and placed onboard the Lunar Reconnaissance Orbiter (LRO) spacecraft for its historic mission bringing NASA back to the moon.

The Lunar Reconnaissance Orbiter is the first mission in NASA's Vision for Space Exploration, a plan to return to the moon and then to travel to Mars and beyond. LRO will launch no earlier than November 24, 2008, with the objectives to finding safe landing sites, locate potential resources, characterize the radiation environment, and demonstrate new technology.

You will also receive a certificate showcasing your support of the mission. Here's mine: