Monday, January 14, 2008

Now is the right time to start running

Yesterday, after the pause of 6 months, I've run 5km. This was the first time after my first half marathon.

It was a little bit cold outside but bright sun and dry road felt very good. I enjoyed it so much that I immediately started to think about running Stuttgarter-Zeitung-Lauf half marathon this year again.

My time for 5km was not impressive - 30 minutes 30 seconds. My personal record for this track is 25 minutes. However these numbers do not matter that much. I've really enjoyed running. I've noticed one difference compared to the first trainings last year. This year I can run for 5 minutes before making a 1 minute pause. It was 2 minutes last year.

If you live in the northern hemisphere, now is the right time to start running or at least start thinking about running. Days are getting longer and brighter, nature will start waking up soon (of course it depends on where you live). If you start running now you will enjoy seeing the spring in its all beauty. Last year was the first time in my life I really saw spring. I saw grass and everything else growing and waking up bit by bit. You have to see it.

Another reason to join Stuttgarter-Zeitung-Lauf half marathon this year is a new track. You will enjoy running through the very center of Stuttgart - Schlossplatz.

22nd of June, 2008. See you there.

Wednesday, December 5, 2007

How to use Velocity as a view component in Struts?

Struts provides a proved framework for building web applications in Java. It is most often used with JSP as a view component. However, other view components may be used as well.

Here I will show how to create a "hello world" application using Struts and Velocity as a view component. I assume you have already downloaded and set up necessary Struts parts.

First of all, we need to download VelocityTools package which provides easy way to use "*.vm" (Velocity Macro) templates to serve HTML pages and has some tools for Struts integration.

Extract the archive and copy following jar files to your project:
  • velocity-tools-1.4.jar
  • velocity-1.5.jar
  • commons-beanutils-1.7.0.jar
  • commons-logging-1.1.jar
  • commons-collections-3.2.jar
  • oro-2.0.8.jar
  • commons-digester-1.8.jar
  • commons-lang-2.2.jar

Add following servlet definition and mapping to your web.xml:
  <servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
Now create a hello-world.vm file in the root directory of your web resources, where you would normally put JSP files.
  <html>
<body>
Hello World at $currentTime!
</body>
</html>

If you start application and point your browser to the http://localhost:8080/struts-velocity/hello-world.vm URL, you will see following output:
  Hello World at $currentTime!    

Create HelloWorldAction Struts Action to add necessary "currentTime" object to the request.

In struts-config.xml:
  <action path="/hello-world" type="vilbara.HelloWorldAction">
<forward name="ok" path="/hello-world.vm"/>
</action>

In HelloWorldAction class:
  request.setAttribute("currentTime", new Date());
return actionMapping.findForward("ok");

Start application and access http://localhost:8080/struts-velocity/hello-world.do URL. You will get following output:
  Hello World at Thu Dec 06 12:36:33 CET 2007!

That was it. Now Struts uses Velocity for the view component in your application.

You may download struts-velocity.war file. Sources are included.

Use Velocity for e-mail templates

You are Java developer and need to send an e-mail. Where and how do you store e-mail content?

First, let me clarify what I mean by the "content". There are several parts of it:
  • subject.
  • body
  • "from" field.
Many developers use Java property resource bundles and naming convention to store 2 or 3 parts of content.
  mail.created.subject=Account for {0} has been created.
mail.created.from=support@example.org
mail.created.body=Account with the user ID {0} has been successfully created.\n\n\
Please follow this link to use your account:\n {1}\n\nYour Support Team\nexample.org
Let's look at the alternate solution using Apache's Velocity.

First, create a file in Velocity macro, e.g. mail-created.vm with following content:
  #set ($subject = "Account for $person.name has been created")
#set ($from = 'support@example.org')

Account with the user ID $person.uid has been successfully created.

Please follow this link to use your account:
$link

Your Support Team
example.org
Now, load and evaluate the template:
  Context context = new VelocityContext();
context.put("person", new Person("Vilmantas Baranauskas", "vilbara"));
context.put("link", "http://vilbara.blogspot.com/");

Template template = Velocity.getTemplate("src/mail-created.vm");

StringWriter body = new StringWriter();
template.merge(context, body);

String from = String.valueOf(context.get("from"));
String subject = String.valueOf(context.get("subject"));

Compare abilities of Velocity and MessageFormat and you will see the benefits.

You may download a zip file containing this example.

Tuesday, December 4, 2007

How to upgrade HDD without reinstalling Windows?

Have you run out of space on your hard disk drive? Your HDD is aging and you are afraid of it breaking soon and possibly loosing some data? Are you procrastinating to upgrade it because of time consuming installations?

I do upgrade my hard disk drives every 3 years or so, before they die unexpectedly or I run out of space. And this is not time consuming at all. Here is what I do.
  1. Buy new HDD. I usually buy some of the cheapest models available. If you buy every 3 years then you have a good chance that your new HDD will be twice as big as your current one.
  2. Download and burn Linux live-CD, e.g. Ubuntu
  3. Attach your new HDD to the computer. You may use USB HDD enclosure for notebooks to attach new HDD using USB port.
  4. Boot Linux live-CD.
  5. Start command line shell. Use menu "Applications/Accesories/Terminal" in Ubuntu.
  6. Make sure you see both disks:
    sudo fdisk -l
    It is likely that your old disk is available as "/dev/sda" and your new one is available as "/dev/sdb".
  7. Copy complete content of your old disk, to the new one.
    sudo cp /dev/sda /dev/sdb
  8. Resize partition and file system or create new partition.
    sudo parted /dev/sdb
Now you are ready to shut down the computer, remove old disk and install the new one. Start your system and enjoy your new and bigger (and likely faster) hard disk.

The last step? Erase data on your old HDD and sell it on eBay.

Forget time-consuming reinstalls.