Monday, February 23, 2009

Tomcat Timezone Issue

Noticed that Tomcat sometimes uses different time zone other than that of the system.

Solution is to define a TZ environment variable in the user profile and use the user to start up Tomcat.

e.g. TZ=US/Eastern

Saturday, February 21, 2009

CVS User

I used webmin to initialize the CVS repository and it was owned by root:root.

The default repository path is /var/cvs.

To enable a normal "cvsuser" write into the repository, I chown -R cvsuser.cvsgroup /var/cvs.

Java 6 on CentOS 5.2

Can't shutdown Tomcat because of the memory restriction when initialize VM.

After yum install jpackage-utils, it solved.

PostgreSQL Create a New Database

  1. su - postgres
  2. createdb -O

PostgreSQL Create a New User

  1. su - postgres
  2. createuser -S -D -R -P
  3. enter password

PostgreSQL Authentication

PostgreSQL default authentication control really sucks. I modified it like this,
  1. su - postgres
  2. psql
  3. show hba_file;
  4. vi the hba_file and comment out all default settings
  5. add a line "local all all md5"
  6. now it works from command line and phpPgAdmin web screen
But I want the authentication for user postgres works as default, so I add first line as "local all postgres indent sameuser"

Install Java 6 on CentOS 5.2

  1. run /usr/sbin/alternatives --config java, it gives no response.
  2. run uname -m and it gives i386. it means it is a linux not a linux64.
  3. download jdk rpm from java.sun.com, at the time, sun provides JDK 6 Update 12. select the Linux and the rpm.bin
  4. chmod of the bin file and make it executable, run it
  5. Get error -bash: ./jdk-6u12-linux-i586-rpm.bin: /bin/sh: bad interpreter: Permission denied
  6. after some research, i realized that the /tmp is shared managed. it won't allow me to run any scripts there. so i moved the bin file to my home directory and run it, it installed the java automatically
  7. run rpm -qa | grep java and got jdk-1.6.0_12-fcs, run rpm -qa | grep sun also returns a bunch of packages
  8. run /usr/sbin/alternatives --config java, it still gives no response but run java -version, it gives the 1.6.0_12 back.
  9. done.

Monday, February 09, 2009

Unicode in Web Application

Let's say you store a unicode string in a database. The database encoding is UTF-8. The string is 似水流年.

In your web application, you allow users type 似水流年 and you get it from HTTP request and obtain a string.

When you compare the string obtained from request and the string stored in the database, they will not match.

What you need to do is to convert the non-unicode string to unicode. Here is one of the way, simple and elegant.

str = new String(str.getBytes("ISO-8859-1"), "UTF-8");

You would need to handle the UnsupportedEncodingException, but I doubt if there is any JVM not supporting ISO-8859-1 to UTF-8 conversion.

I am not sure if the ISO-8859-1 shall be different on different servers. I tried on my development workstation which is a Windows XP + Eclipse 3.3 + Tomcat 6 + Java 1.6 and my production server which is a Linux RH + Tomcat 6 + Java 1.6. They all work very well and so far I am OK with it hard coded.

Tuesday, February 03, 2009

For-Each Loop

for(Type type : types) {
... type ...
}

The For-Each loop makes Java code much more beautiful and clean than before. We shall use it any time we can.

Several exceptions,
  1. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-each loop is not usable for filtering.
  2. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it.
  3. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.