Following the instructions given in the Tapestry 5 – Building Web Applications books gave me an error while running the Maven command to create a Tapestry 5 project skeleton. The project creation failed with the following message displayed on the console:

[INFO] Error creating from archetype
Embedded error: The META-INF/maven/archetype.xml descriptor cannot be found.

This might be caused by some changes made in the repository between the book was published and the time I am reading the book.

So here is the Maven command that will successfully give you a quick start project for creating a web application using the Tapestry 5 framework.

mvn archetype:create -DarchetypeCatalog=http://tapestry.formos.com/maven-repository -DarchetypeGroupId=org.apache.tapestry -DarchetypeArtifactId=quickstart -DgroupId=org.kodejava -DartifactId=t5first -DpackageName=org.kodejava.t5first -Dversion=1.0.0-SNAPSHOT

To run the quick start go to the t5first folder and execute mvn jetty:run and access the application in http://localhost:8080/t5first.

websphereRecently we’ve installed a 6.1.0.0 version of WebSphere and patched it with fix pack 25 which is the current version at that time. We have developed an application using the AppFuse jump-start project to generate our project skeleton.

Previously when using WebSphere without the fix pack I mentioned above (a plain 6.1.0.0) everything work just as expected. But after a fix pack upgrade to 25 the spring security login service is broken, it returned a 404 page not found error.

A quick Google give a hint that this is happening due to some implementation changes on some of the servlet filter behavior in the WebSphere. To make the j_security_check recognized we have to add a custom properties the the Web Container. The custom properties is:

com.ibm.ws.webcontainer.invokefilterscompatibility = true

To set this custom properties do the following steps:

  1. In the WebSphere administration console click: Servers > Application Servers > server_name > Web Container Settings > Web Container.
  2. On the Additional Properties section click Custom Properties.
  3. On the Custom Properties page click the New button.
  4. On the setting page enter the property name com.ibm.ws.webcontainer.invokefilterscompatibility in the Name field and assign the value to true in the Value field.
  5. Click the Apply or OK button.
  6. Save the configuration by clicking the Save link on the console task bar.
  7. Restart the WebSphere server.

After the server is up and running we found that the j_security_check login filter functional as it should be and now handling the login service of our application.

Oracle MagazineOracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world’s largest enterprise software company.

For more information about Oracle, please visit their Web site at http://www.oracle.com. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

The java.sun.com website have published a lot of interview with top Java developers. In the recently article it summaries some interviews that gives advice for students on how to become a top Java developer. Here are some quoted advices from the articles:

“Write lots of code. Have fun with it!” — Joshua Bloch

“Learn to use your tools. And I don’t mean just enough to get by. I mean really learn how to use your tools.” — Tor Norbye

“Don’t use line numbers. Don’t put your entire application in one method.” — Chet Haase

“Don’t be overwhelmed by the language or the platform.” — Raghavan Srinivas

For the complete articles you can read it here: Top Java Developers Offer Advice to Students.

Today I was trying to create a Tapestry 4.1 application using the Maven archetype. But unfortunately the archetype was not found on the repository. After searching for sometime I’ve managed to find a working repository  -DremoteRepositories=http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/tapestry/tapestry-archetype/

Here is the complete command:

mvn -U archetype:generate -DarchetypeGroupId=org.apache.tapestry -DarchetypeArtifactId=tapestry-archetype -DgroupId=org.kodejava -DartifactId=tapestryapp -DpackageName=org.kodejava -Dversion=1.0.0-SNAPSHOT -DarchetypeVersion=4.1.6 -DremoteRepositories=http://mirrors.ibiblio.org/pub/mirrors/maven2/org/apache/tapestry/tapestry-archetype/

A good practice or even a must when creating a Serializable classes is to provide a stream unique indentifiers, commonly called as serial version UID. This unique private static long number can be created using the serialver command. But the question is, can we generate it directly from IDEA? Eclipse has this feature!

Actually the IntelliJ IDEA has the feature but it was turned off by default. Now, to make the feature available you need to enable it first, go to:

Settings -> Errors -> IDE Profiles -> Default -> Serialization Issues -> Serializable class without ’serialVersionUID’

After configuring it the IDEA code inspection will warn you everytime you create a Serializable class without a serialVersionUID. By pressing the Alt + Enter a unique number will be generated for you, so you don’t need to switch to the console anymore. Below is the example screen from the IDEA editor:

Serial Version UID

The Rock, Paper and Scissors is a traditional classic game that maybe can be found in almost every part of the world including in my homeland, Bali. This classic game played by two people, they can pick either the rock, the paper or the scissors as their weapon. The rule is simple: rock wins to scissors, paper wins to rock, scissors wins to paper.

In this blog post we will create this simple game. We’ll create the Rock, Paper and Scissors object and will make them comparable to each other and they also have their own name. To make this feature we create a GameObject interface that extends the java.lang.Comparable interface, this is where it will get the ability to do the comparison. Let’s see what we got so far in our code.

The GameObject interface.

package org.kodejava.example.app;

public interface GameObject extends Comparable {
    public String getName();
}

The Rock class.

package org.kodejava.example.app;

public class Rock implements GameObject {
    private String name;

    public Rock() {
        this.name = "Rock";
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Paper) {
            return -1;
        } else if (o instanceof Scissor) {
           return 1;
        } else if (o instanceof Rock) {
            return 0;
        }

        throw new UnsupportedOperationException();
    }
}

The Paper class.

package org.kodejava.example.app;

public class Paper implements GameObject {
    private String name;

    public Paper() {
        this.name = "Paper";
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Scissor) {
            return -1;
        } else if (o instanceof Rock) {
            return 1;
        } else if (o instanceof Paper) {
            return 0;
        }    

        throw new UnsupportedOperationException();
    }
}

The Scissors class.

package org.kodejava.example.app;

public class Scissors implements GameObject {
    private String name;

    public Scissors() {
        this.name = "Scissors";
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Rock) {
            return -1;
        } else if (o instanceof Paper) {
            return 1;
        } else if (o instanceof Scissor) {
            return 0;
        }

        throw new UnsupportedOperationException();
    }
}

As you can see from the code above, all classes; Rock, Paper and Scissors; are implementing the GameObject interface. This will make each class to implements the getName() and compareTo() methods.

The compareTo() method basically compare the object with the passed in object. We first check it using the instanceof operator to know if the passed object is type of Rock, Paper or Scissors. Based on the win, lose or draw rules the method return the corresponding 1, -1 or 0 value.

Now let see the main class that run our game. This class is a standard Java application class with the main(String[] args) method. The class also have a factory method; createObject(int type) method; for creating the correct GameObject from user input or a random pick by the computer player.

package org.kodejava.example.app;

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissorsGame {
    public static final int ROCK     = 0;
    public static final int PAPER    = 1;
    public static final int SCISSORS = 2;

    public static void main(String[] args) {
        RockPaperScissorsGame game = new RockPaperScissorsGame();
        game.runGame();
    }

    protected void runGame() {
        System.out.println("ROCK     = 0");
        System.out.println("PAPER    = 1");
        System.out.println("SCISSORS = 2");

        Scanner scanner = new Scanner(System.in);
        int input;
        do {
            System.out.print("Pick your selection (0, 1, 2): ");
            input = scanner.nextInt();
        } while (input < 0 || input > 2);

        GameObject objectOne = createObject(input);

        //
        // Computer pick his game object.
        //
        Random random = new Random();
        int type = random.nextInt(3);
        GameObject objectTwo = createObject(type);

        System.out.println("Your pick    : " + objectOne.getName());
        System.out.println("Computer pick: " + objectTwo.getName());
        getWinner(objectOne, objectTwo);
    }

    private void getWinner(GameObject objectOne, GameObject objectTwo) {
        if (objectOne.compareTo(objectTwo) > 0) {
            System.out.println(objectOne.getName() + " vs "
                    + objectTwo.getName() + ": YOU WIN!!!");
        } else if (objectOne.compareTo(objectTwo) < 0) {
            System.out.println(objectOne.getName() + " vs "
                    + objectTwo.getName() + ": YOU LOSE!!!");
        } else if (objectOne.compareTo(objectTwo) == 0) {
            System.out.println(objectOne.getName() + " vs "
                    + objectTwo.getName() + ": DRAW!!!");
        }
    }

    private GameObject createObject(int type) {
        if (type == ROCK) {
            return new Rock();
        } if (type == PAPER) {
            return new Paper();
        } if (type == SCISSORS) {
            return new Scissors();
        }
        return null;
    }
}

We have all the code, compile it with your JDK or IDE. To play it, run the application and pick your target weapon to fight the computer player by entering the number displayed in the console, 0 for rock, 1 for paper or 2 for scissors.

I was just reinstall my PC operating system so I need to reinstall everything once again and this include the Subversion version control system. I download Subversion 1.4.5 extract it to my Program Files folder, create a new repository and then start the svnserve so I can start to use it to keep my projects.

But there is one more thing that I need to do, how do I make the svnserve run as a Windows service instead of just run it from the command prompt from time to time I work with Subversion. Previously I know that there is a library that can make it run as a Windows service.

I start to Google it and found some other new way to configure the svnserve as service. I then tried this post and combined it with other people suggestion and it just works.

I am using Windows XP with SP2, to register a service we can use sc command. SC is a command line program used for communicating with the NT Service Controller and Services. So below is the command line:

sc create svn.local binpath= “%ProgramFiles%\Subversion\bin\svnserve.exe –service –root D:\Repository” displayname= “Subversion Repository” depend= Tcpip start= auto

Note: I found that copy pasting breaks the the command above, so I suggest you to just retype what was shown above.

And please note that a space between the equal sign above is required (key= value), so just type it as it is. When it success you can see the success message like “[SC] CreateService SUCCESS” following your command.

Now you should have the svnserve installed as Windows service and you can configure it to run automatically every time you startup your Windows box.

By default the keywords expansion in Subversion is disabled. To enable this feature you’ll have to update the Subversion configuration file. In Windows operating system this file is located under the C:\Documents and Settings\[username]\Application Data\Subversion\config folder, where [username] is your login username.

Ok, now to enable it, find a property called enable-auto-prop in the [miscellany] section and remove the remark, or if it doesn’t exist you can create one as the example below:

[miscellany]
enabe-auto-prop = yes

In the [auto-props] section you can declare the file to which the keywords expansion will be enabled. To create it see the following example for .java and .html files.

[auto-props]
*.java = svn:keywords=Id Author Date Revision HeadURL
*.html = svn:keywords=Id Author Date Revision HeadURL

Here is an example of a Java class with the SVN keywords in it.

/*
 * $Id:$
 * --------------------------------------------------------
 * Copyright (c) 2007 Kode Java Org.
 */
package org.kodejava.example.intro;

import java.util.Date;

/**
 * @author  I Wayan Saryada
 * @version $Revision:$
 */
public class HelloWorld {
    public static void main(String[] args) {
    }
}

From this point, everytime you add a Java or HTML document into your Subversion repository it will automatically have the keywords specified above expanded.

In this post I will show you how we can use Apache Commons-IO FileUtils class to help you doing file manipulation and getting some information about files. To start you need to have the latest commons-io library which you can download from http://jakarta.apache.org/commons project website.

You are now have a handfull classes of Apache Commons-IO. Let’s begin, first I’ll show you how we can read a file content straight to an instance of string using FileUtils class methods, either FileUtils.readFileToString(File file) or FileUtils.readFileToString(File file, String encoding).

I’m using the first method to create an example to show you how to read the content of a file. Let say that we are going to read the content of sample.txt file. So here is the code.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class FileUtilsSample
{
  public static void main(String[] args)
  {
    File file = new File("sample.txt");
    try
    {
      String content = FileUtils.readFileToString(file);
      System.out.println(content);
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

As the readFileToString(File file) is a static method you need to called it by prefixing the method call with its class name. It’s a very basic rule in Java, you should already know about it. We create an instance of File which will be passed into the method. This method also thrown an IOException exception when the operation failed, so we need to add a try-catch block for the method.

The next method is also quite usefull, say that you want to read the file line by line just like the BufferedReader does for you, but one again no while-loop routine. You can do this by using FileUtils.readLines(File file) method. This method read the file content line by line and returns it as a List of string.

Let me give you a simple example to use this method, the example is exactly the same with the previous one but we are using the readLine(File file) method in turn.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;\r\n\r\npublic class FileUtilsSample
{
  public static void main(String[] args)
  {
    File file = new File("sample.txt");
    try
    {
      List contents = FileUtils.readLines(file);
      for (String line : contents)
      {
        System.out.println(line);
      }
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

I’ve shown you the file reading utilities above, now what about writing to a file, can I use this class to? Yes, our course you can. Maybe you can guess the name of a method that you can used to write to a file. I think I heard you, did you say writeStringToFile? Yes, that’s it ;)

This method has the following signature, writeStringToFile(File file, String data). Let’s try the following example.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class WriteToFileExample
{
  public static void main(String[] args)
  {
    try
    {
      File file = new File("sample.txt");
      String data = "Learning Java Programming";
      FileUtils.writeStringToFile(file, data);
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

The example will create sample.txt file an insert some text in it. When this file is not exist it will automatically created. This example can tell you how easy to read from and write to a file using this library.

Beside of read and write utility you can also use this class to calculate directory size including its sub directory, find files in directory, checking file timestamp, copying file, checking if to file have the same content, etc.

top