Friday, June 13, 2008

What's JarIndex mechanism? What's it used for?


What's JarIndex mechanism? What's it used for?

Java 1.3 introduced this mechanism for optimizing the search process of the classloaders for searching classes bundled into many JAR Files either for applications or applets. If you want to refresh your understanding of JAR Files then read this artcile first - JAR, WAR, & EAR >>

Why was JarIndex mechanism required?

Applets are normally shipped in form of JAR Files (to cut down the number of HTTP Connections required to download the code to the client machine) and initially the .class files used to be searched using simple linear search mechanism in all of those downloaded JARs unless all of them are exhausted or the .class was found, which ever happened earlier. This used to take significant time in some cases and it used to slow the response time and performance for the obvious reasons. JarIndex mechanism was thought of as a solution to handle this scenario in a better way to ensure a better performance in all possible scenarios.

How JarIndex mechanism works?

This mechanism collects the contents of all the JAR files of the applet and creates an index file named INDEX.LIST to store all the data collected. It puts this index file in the first JAR file (in the META-INF directory) specified on the applet's CLASSPATH. The applet classloader uses this INDEX.LIST file to make the search process of the .class files efficient.

The 'jar' tool, which is used to create JAR files was enhanced to be able to collect the infortaion needded by the JarIndex mechanism. Once the INDEX.LIST file is ready, it's automatically put in the META-INF directory (if the directory doesn't exist, it's created first) of the first JAR file specified in the applet's CLASSPATH OR in the root JAR file in case of a application. The classloaders simply read the INDEX.LIST file (it's besically a simple text file) and create a hash table of mappings using the information stored in the index file. This hash table is used to locate the actual JAR file whenever a .class file is required and the file is automatically picked from particular JAR. This eliminates the process of scanning all the JAR files for searching every .class file, which we used to previously.

It's important to note here that this mechanism is completely backward compatible with the older applications using JAR files created with the older 'jar' utility. If the root JAR file (or the first JAR file of an applet's CLASSPATH) contains a file named INDEX.LIST in the META-INF directory then the classloader will use this mechanism otherwise it'll simply follow the simple linear search algorithm to search the classes.

Contents of the INDEX.LIST file?

This text file contains the details of all the JAR files where the info about each of the JAR files is separated by a single blank line. Info about any JAR file includes the JAR File pathname and the list of packages and filenames contained by that JAR file. Each peice of info is stored on a separate line and as mentioned earlier we'll have a single blank line as a delimeter between the info of different JAR Files. The pathnames saved in this index file will all be relative to the codebase of the root JAR file (in case of an application) or the first JAR file of the applet's CLASSPATH (in case of an applet).



Share/Save/Bookmark


No comments: