Code sample - Implementing a new X-TIME command

In this example I will show how to implement a very simple command that outputs current time. When a user inputs X-TIME a server replies something like this:

Server's time is: 10/10/2006 12:18:02

This is the source of the command:

public class XTimeCommand extends AbstractCommand {

  private String pattern = "dd/MM/yyyy HH:mm";

  public void setPattern(String pattern) {
    this.pattern = pattern;

  public Reply execute() {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    reply.setText("Server's time is: "+sdf.format(new Date()));
    return reply;

Now you have to update your beans.xml so that the command becomes available, as follows:

<bean id="xtimeCommand"
  <property name="pattern" value="dd.MM.yyyy HH:mm:ss"/>

<bean id="commandFactory"
  <constructor-arg index="0">
      <entry key="USER" value="userCommand"/>
      <entry key="PASS" value="passCommand"/>
      <entry key="PWD" value="pwdCommand"/>
      <entry key="X-TIME" value="xtimeCommand"/>

The first XML defines the command object (named xtimeCommand). The second XML piece shows how to add this xtimeCommand to the command factory. Now if a user inputs X-TIME then the xtimeCommand will be executed.