Friday, July 31, 2009

Running Apache Tomcat from inside Eclipse

You can run Apache Tomcat from inside Eclipse by:
* Window--> Show View --> Server

In the Servers panel that opens up:
* R-click --> New --> Server
* Select the Apache Tomcat Server from List
* Add your WebApp Project

You can now Right Click on the Server and Start/Stop it from inside Eclipse.


Here is a Gotcha:
- The application is run directly from your src folder (i.e. the war file is not actually copied to the /webapps directory of the tomcat install!)
- This is great because it speeds up development - you do not have to keep building the project to test it out on the server

- But REMEMBER to make a copy of all the jar files in the src/WEB-INF/lib directory otherwise TomCat will not find them!

How to maven-ize richfaces

For some reason, richfaces jars are no longer available on the maven repository.
To do it yourself:
- Download richfaces-ui-3.3.1.GA-bin.zip from the JBoss richfaces website
- Unzip it to say C:\Program Files\Richfaces


- Open a command prompt and cd to the /lib directory
You will see 3 jars in the /lib directory

- Give the following commands to load the jars to maven
> mvn install:install-file -DgroupId=org.richfaces -DartifactId=richfaces-impl -Dversion=3.3.1.GA -Dpackaging=jar -Dfile=./richfaces-impl-3.3.1.GA.jar

> mvn install:install-file -DgroupId=org.richfaces -DartifactId=richfaces-ui -Dversion=3.3.1.GA -Dpackaging=jar -Dfile=./richfaces-ui-3.3.1.GA.jar

> mvn install:install-file -DgroupId=org.richfaces -DartifactId=richfaces-api -Dversion=3.3.1.GA -Dpackaging=jar -Dfile=./richfaces-api-3.3.1.GA.jar


You can now reference these libraries in your pom.xml:

<dependency>
<groupId>org.richfaces</groupId>
<artifactId>richfaces-ui</artifactId>
<version>3.3.1.GA</version>
</dependency>

<dependency>
<groupId>org.richfaces</groupId>
<artifactId>richfaces-api</artifactId>
<version>3.3.1.GA</version>
</dependency>

<dependency>
<groupId>org.richfaces</groupId>
<artifactId>richfaces-impl</artifactId>
<version>3.3.1.GA</version>
</dependency>

Thursday, July 23, 2009

Example: Generating a SHA hash in Java

import java.security.MessageDigest;
...
public String createHashSHA (String someString){

try {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(someString.getBytes());
byte[] someStringDigest = md.digest();
String someStringHashSHA = new String(someStringDigest);
return someStringHashSHA;
}
catch (Exception e){
...
}
}

Jetty cant find applicationContext.xml

If your web aplication uses Spring, then you probably have an applicationContext.xml. By default, this file is in the /WEB-INF directory. However, for some reason, Jetty looks for it in /WEB-INF/classes. So Jetty gives this "applicationContext.xml file does not exist" error. Putting the file in /WEB-INF/classes seems to resolve this issue.

Note: I had to explode my war to do this. You can use zipitfree (http://www.zipitfast.com/) to explode (uncompress) the war.

How to install web application in Jetty

To install web application in Jetty, simply copy the your_web_app.war into /webapps directory of Jetty. Jetty will automatically pick it up on restart.

How to install and start Jetty Server

Here is what works for me:
- Download Jetty (I downloaded version 7)
- Unzip it in a directory (I used C:\Program Files\jetty_7.0.x)
- Install a JRE (I installed mine in C:\Program Files\JAVA)
- Open a cygwin command prompt (download cygwin from http://www.cygwin.com/)
- Type:
> cd /cygwin/c/Program Files/jetty_7.0.x
> export JETTY_HOME=/cygwin/c/Program Files/jetty_7.0.x
> export JAVA_HOME=/cygwin/c/Program Files/Java/jre
> java -jar start.jar

This should start Jetty.
To stop it, I just use ctrl-C (jetty will stop gracefully and not just abort)

MySQL 5.1 - server does not start on Vista

There seems to be some problem staring the MySQL 5.1 server on Vista. It won't start no matter what you do. I went back to version 5.0 and it installed and runs fine.