Tuesday, July 27, 2004

Self extracting installer in Perl

Self-extracting installation is a great feature of windows applications. Clicking on a Setup.exe extracts all files and starts the configuration of the application. No unzipping, no README. I wanted to use the same approach for packaging a PHP-based web application meant for Apache running on a Linux box.

Perl seemed a good choice as platform for the installer because of its compact syntax, multiple platform support and huge number of libraries. The basic idea is very simple - Append a perl script with the zipped archive. This perl script reads itself and extracts the zipped archive, unzips and calls the configuration script. The problem is how to append the zipped archive and still not confuse the interpreter. The trick of using multiline comments in Perl can do the job.

Still reading on OOPS in Perl starting with this handy introduction but the basic Perl script is as follows :

use Shell;

# Change these values for your desired installation
# Parameter 1 - Name of this file
# Parameter 2 - Name of temporary file
# Parameter 2 - Size of the zip file to be appended to script (in bytes)
# Parameter 3 - OS Command for unzip
# Parameter 4 - OS Command to run the configuration script

install("setup.pl", "temp.zip", 200048, "unzip temp.zip -d /usr/local/", "perl /usr/local/configure.pl");

sub install {
my $SETUP = $_[0];
my $TEMP = $_[1];
my $ZIP_SIZE = $_[2];
my $UNZIP_CMD = $_[3];
my $CONFIGURE_CMD = $_[4];

open(PROG, $SETUP) or die "Could not open file : " + $!;
binmode(PROG);
seek(PROG, -$ZIP_SIZE, 2);

open(BINFILE, ">$TEMP") or die "Could not open file : " + $!;
binmode(BINFILE);
while (read(PROG, $buff, 65536))
{
print BINFILE $buff;
}

close(PROG) or die "Could not close file : ";
close(BINFILE) or die "Could not close file :";

print "Extraction complete\n";

system($UNZIP_CMD);
system($CONFIGURE_CMD);
}

# Force the interpreter to think of the binary file as comment.
=for comment

For using the script, you need to modify the size of the zipped archive, the unzipping command and the configuration command. Concatenate the zipped archive to the modified script and you are ready to run the setup using the command : perl -w setup.pl

Monday, July 12, 2004

IoC Containers

Last week I was working on a .NET application needing a very pluggable architecture. To ensure extensibility, the application was factored into components. It is obvious that most components required some basic services like lifecycle management, configuration, contextualization and composition. Thus began my search for an IoC (Inversion of Control) container in .NET. Martin Fowler explains it very well.

There has been considerable interest in light-weight containers in the Java world for providing these services wich has resulted in projects like Avalon/Merlin, Spring and PicoContainer. Of course, their heavier counterparts, J2EE application servers are only too well-known. These containers differ from the application servers in many respects. One is, they just provide a microkernel for managing the components. The application servers provide a much larger set of services to the EJBs including security, transactions, connection pooling, message queues and schedulers. There are almost no restrictions on the components being managed by the container and no hard contracts to fulfill.

While application servers provide their services to the contained EJBs using descriptors, these containers explicitly handover the service providing component to the user component. There is a difference in the lookup pattern also. While the J2EE containers use the ServiceLocator pattern (JNDI) to find other components, these containers use some variation of IoC or dependency injection - Constructor injection(PicoContainer), Setter injection(Spring) or Interface injection(Avalon).

These containers also provide features which are not part of the J2EE framework. For example, Spring provides a framework for aspect-oriented programming. Avalon provides components on the avalon planet for thread pool, scheduler and datasources thus allowing the use of different implementations of these services. If you are working on the J2EE platform, HiveMind is a good bet. It is an IoC container that can be deployed on a J2EE application server. Thus you have access to a simple container with the full power of J2EE.

As for my search, I found that PicoContainer.NET is the only option at the moment. Spring.NET is in planning stages. Avalon is also being ported as Castle but has not been released.

Friday, July 02, 2004

Return of the template

The good old templates are now available both in Java and C# by the name Generics. C++ programmers have long used templates to provide compile time safety for data structures which are collections of objects. Templates or Generics, as they will be now known, save the burden of casting the object back to the appropriate type for the programmer and avoid the penalty of runtime type checking for the virtual machine.

There is a brief introduction of the Generics implementation in the new features in the J2SE 1.5 release, code named Tiger on java.sun.com. This tutorial offers a more complete explanation of the Generics feature. With features like boxing, metadata, foreach loop and typesafe enumerations, it is now even more closer to C#.

Microsoft has announced the beta release of Visual Studio 2005, code named Whidbey which implements the C# 2.0 specification. This document on C# 2.0 features on informit.com explains the Generics implementation. C# 2.0 now also provides a template like syntax for implementing nullable types which has been a requirement for long and even has an open source project dedicated to implementing nullable types.

Thursday, July 01, 2004

Driving Underground with Lucky Ali

Listening to rap music like Out of control by Rancid, while racing a car at speeds over 250 km/hr can be quite strenous. So last night, I and my friend Sanjeev set out to change the soundtracks bundled with Need For Speed Underground. EA Games stores all the 27 tracks in the directory SNDSTREAMS as an AST format file. After a little Googling and reading lots of posts from NFS fans, we stumbled on this excellent repository of NFS Underground software.

NFS Music Importer by Arushan works really well. The zipped archive comes bundled with a little tool from EA itself which imports MP3 files into ASF format. Then the music importer can flawlessly burn these soundtracks into the AST file. A minor glitch - you cannot change the number of tracks and only overwrite an existing track. But the soothing track of Kitni Haseen Jindagi Hai - Lucky Ali while racing a car is something not to be missed :) Please take a backup of the original AST, if you have not grown too old for rap.

This repository also has an NFS server for windows/linux and a cool patch which replaces the introductory movies, so that you get to the action faster. I have not read the EA license, but I cannot see them going gaga over this :) Please do read it before you face any damages. And finally, remember the EA advice - Leave the racing for the track. Drive safely.