Monday, October 31, 2005

Java Generics

J2SE 5.0 have been released for a long time. However, I didn't have time to study it. Today, I spend some time to have a look of it's new features. One new feature introduced in J2SE 5.0 is generics.

In Wikipedia, the term generics is defined as:

generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. The programming style emphasizing use of this technique is called generic.

For example, a List object can contains different elements String, Integer, etc. In generics, we can declare a List object only contains certain types in elements. E.g, List as a List object only contains String objects.

Generics is easily mixing up with the term template. In Java, the syntax of generics is similar to Template in C++. However, generics and Template are two different concept. Template is about code generation, and generics is about type checking. You can refer to a Wikipedia's article: Comparison of generics to templates.

With generics, it can avoid type-safe problem of collection framework of Java. However, the generics in Java is very complicated. It is not elegant. After you read the tutorial of generics, you may feel very confusing, and you may find that there are many exceptional cases need to take care when using generics in Java.

Sunday, October 30, 2005

Fix the RAID

Yesterday, I found that one of my harddisk in my Old PC has bad sectors. I decided not to replace the bad harddisk but to relocate the root partition to other free spaces in the harddisk. Originally, the RAID of root filesystem contains the hda8 and hdc8 partitions. The bad sectors were found in hda8. I try to search whether there exists unused partition on hda. I find hda7 is not used and the size matches the hdc8. To add hda7 to the RAID:
raidhotadd /dev/md0 /dev/hda7
From "/proc/mdstat", it shows hda7 is adding to md0. Wait for a while, mirroring of data from hdc8 to hda7 finished. I restart the PC.

After startup, it fails. The OS still try to use hda8 as the RAID. After some inspection, it is due to the configuration problem. First, it needs to correct the "/etc/mdadm/mdadm.conf" file. Second, it needs to regenerate the initrd image using the "mkinitrd" command. Lastly, it needs to correct the GURB config. Then, I restart te PC and this round, it works!

Saturday, October 29, 2005

GIF vs PNG

Today, I test my homepage with IE. IE cannot display transparent PNG graphics properly, and the background becomes grey in color.

Fortunately, I find a workaround on the Web -- http://homepage.ntlworld.com/bobosola/. It has details about the problem and how to solve it. In short, it uses javascript to patch all <img> tag of PNG graphic's CSS using the "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" of IE.

Actually, PNG has some advantages over GIF.

  1. Max color depth of PNG is 24-bit but that of GIF is 8-bit.
  2. PNG has higher compression rate than GIF.
  3. PNG support alpha channel transparency but GIF only support binary transparency.
However, GIF supports animation but PNG not. This is a weak point of PNG.

Related Links:
AlphaImageLoader Filter
PNG Offical Site

Tuesday, October 25, 2005

Makefile to Build Homepage

After study GNU Make yesterday, I write a generic Makefile for building my homepage today.

The the following is the Generic Makefile:

PHP=php

all: premake all_subdirs all_curdirpages postmake

premake:
if test -x ./premake.sh; then ./premake.sh; fi

postmake:
if test -x ./postmake.sh; then ./postmake.sh; fi

include Makefile.dep

all_subdirs: $(subdirs)
for dir in $(subdirs); do (cd $$dir; make); done

all_curdirpages: $(pages)

$(filter %.html,$(pages)): %.html: %.tpl
$(PHP) $< > $@
touch timestamp

clean:
for dir in $(subdirs); do (cd $$dir; make clean); done
rm -f *.html timestamp *.gen

.PHONY: all clean all_subdirs all_curdirpages premake postmake $(subdirs) $(dep_phony)

This Makefile is put (symbolic-link) in all sub-directories of the homepage. In other words, all sub-directories use the same Makefile. So, it is called generic Makefile. If it needs to add some extra logics before or after make, we can create "premake.sh" and "postmake.sh" shell scripts respectively. The scripts will be executed accordingly. In the Makefile, the marco "pages", "subdirs" and "dep_phony" are provided by the "Makefile.dep" The customization depends on the "Makefile.dep". In each directory, it should contain a "Makefile.dep". The following is a sample "Makefile.dep":

subdirs=blog secImage

pages=index.html tba.html contact.html

index.html: index.tpl header.tpl footer.tpl rightbar.tpl \
lib/search.tpl lib/acknowledgement.tpl blog/recent/index.html

contact.html: contact.tpl header.tpl footer.tpl rightbar.tpl \
lib/search.tpl lib/acknowledgement.tpl

tba.html: tba.tpl header.tpl footer.tpl

The "subdirs" macro defines the the sub-directories requiring to process, and the "pages" macro defines the HTML pages requiring to generated. The "Makefile.dep" file also defines the dependency of HTML files to the template files (*.tpl).

To build the homepage, I only need to issue the "make" command in the root directory of the homepage source.

Monday, October 24, 2005

Designing My Homepage

I want to host my homepage in my NLSU2-Linux box. Today, it's the time to think about it. Since the NLSU2-Linux box is not a powerful machine, it is not appropriate to use Java technology. To put less load on the CPU, it should avoid using too much dynamic pages.

In my past experience, using static HTML, there are many duplication parts among different pages, for example, the page header, page footer, menus.... This make the web-site difficult to maintain. If I need to changes the page header, I need to edit nearly all HTML pages repeatedly. In programming, we avoid code duplication by using functions. How about make web pages? I think template is a solution.

In my homepage, I decided to use PHP as the template language. (To avoid confusion with PHP scripts for dynamic pages, the extension of template files is ".tpl") The following shows a typical page contains header and footer in PHP:

<? include 'header.tpl' ?>

.... the contents here .....

<? include 'footer.tpl' ?>


The "header.tpl" and "footer.tpl" are PHP scripts which generate the header and footer HTML fragments respectively. In PHP, the "include" function embedded the contents of the specific file in current pages, similar to the "include" directive in C/C++. In the homepage, every pages contains header and footer will be like the above example. If we want to change the header, we only need to change the "header.tpl" file once.

However, we need to convert the PHP file to HTML. For example, we have an "index.tpl" file which is a PHP script for generating "index.html". We can issue the following commands:

php index.tpl > index.html

For a complete homepage, there should be many ".tpl" files. I think nobody want to type the above command line again and again. GNU Make is a good utility to do this task.

Since I only have some experience to create a simple "Makefile", it is not enough for handling a complete homepage. I spent some hours to study the GNU Make and hope I can create a generic Makefile tomorrow.

Related links:
PHP
GNU Make

Friday, October 21, 2005

Purchased Linksys NSLU2



I want to setup a 7x24 server at home. The main purpose of this server is for hosting my homepage and files sharing with my friends. After some googling, I found the Linksys NSLU2 is quite suitable for this purposes.

NSLU2 is just a SOHO-grade network attached device with simple functions. However, it can be turned into a more powerful Linux box with the help of NSLU2-Linux project. Then, I can have a lower power consumption, ultra-slim and fanless server :P

Finally, I bought it at Golden Shopping Center. The following is a photo of my running NSLU2 : )



Tuesday, October 18, 2005

Start Hacking the NSLU2

Today, I had annual leave. I had full day to hack my NSLU2 box. After read the wiki of NSLU2-Linux project, I decided to install the Unslung firmware. I followed the user guide and reflashed the firmware of the NSLU2 box. Everything worked fine. :)

Next, I setup the SSH, so I can remotely access the NSLU2 box. Then, PHP, Perl, and other useful utilities have been installed. I also setup a native compilation environment in the NSLU2 box, such that it can compile additional software packages.

Moreover, I tried to setup a cross compilation environment in my old PC which running Debian. This can help me to compile any additional packages faster. However, I fail to compile the cross compilation environment. Then, I read the guide in Wiki carefully, I found that I missed to install some necessary packages (flex, bison, rsync...). After installed the necessary packages, the cross compilation environment was compiled successfully.