Cricinfo Live Scores

Tuesday, January 29, 2008

Writing Your Own ClassLoader

What is a ClassLoader?

Among commercially popular programming languages, the Java language distinguishes itself by running on a Java virtual machine (JVM). This means that compiled programs are expressed in a special, platform-independent format, rather than in the format of the machine they are running on. This format differs from traditional executable program formats in a number of important ways. In particular, a Java program, unlike one written in C or C++, isn't a single executable file, but instead is composed of many individual class files, each of which corresponds to a single Java class. Additionally, these class files are not loaded into memory all at once, but rather are loaded on demand, as needed by the program. The ClassLoader is the part of the JVM that loads classes into memory. The Java ClassLoader, furthermore, is written in the Java language itself. This means that it's easy to create your own ClassLoader without having to understand the finer details of the JVM.

Why write a ClassLoader?

If the JVM has a ClassLoader, then why would you want to write another one? Good question. The default ClassLoader only knows how to load class files from the local filesystem. This is fine for regular situations, when you have your Java program fully compiled and waiting on your computer. But one of the most innovative things about the Java language is that it makes it easy for the JVM to get classes from places other than the local hard drive or network. For example, browsers use a custom ClassLoader to load executable content from a Web site. There are many other ways to get class files. Besides simply loading files from the local disk or from a network, you can use a custom ClassLoader to:

* Automatically verify a digital signature before executing untrusted code

* Transparently decrypt code with a user-supplied password

* Create dynamically built classes customized to the user's specific needs

Anything you can think of to write that can generate Java bytecode can be integrated into your application.

A Custom Class Loader:

The following code is an example of a custom class loader. For details you can go through this PDF Article.

Here is the source code for CompilingClassLoader.java

// $Id$

import java.io.*;

/*

A CompilingClassLoader compiles your Java source on-the-fly. It

checks for nonexistent .class files, or .class files that are older

than their corresponding source code.

*/

public class CompilingClassLoader extends ClassLoader

{

// Given a filename, read the entirety of that file from disk

// and return it as a byte array.

private byte[] getBytes( String filename ) throws IOException {

// Find out the length of the file

File file = new File( filename );

long len = file.length();

// Create an array that's just the right size for the file's

// contents

byte raw[] = new byte[(int)len];

// Open the file

FileInputStream fin = new FileInputStream( file );

// Read all of it into the array; if we don't get all,

// then it's an error.

int r = fin.read( raw );

if (r != len)

throw new IOException( "Can't read all, "+r+" != "+len );

// Don't forget to close the file!

fin.close();

// And finally return the file contents as an array

return raw;

}

// Spawn a process to compile the java source code file

// specified in the 'javaFile' parameter. Return a true if

// the compilation worked, false otherwise.

private boolean compile( String javaFile ) throws IOException {

// Let the user know what's going on

System.out.println( "CCL: Compiling "+javaFile+"..." );

// Start up the compiler

Process p = Runtime.getRuntime().exec( "javac "+javaFile );

// Wait for it to finish running

try {

p.waitFor();

} catch( InterruptedException ie ) { System.out.println( ie ); }

// Check the return code, in case of a compilation error

int ret = p.exitValue();

// Tell whether the compilation worked

return ret==0;

}

// The heart of the ClassLoader -- automatically compile

// source as necessary when looking for class files

public Class loadClass( String name, boolean resolve )

throws ClassNotFoundException {

// Our goal is to get a Class object

Class clas = null;

// First, see if we've already dealt with this one

clas = findLoadedClass( name );

//System.out.println( "findLoadedClass: "+clas );

// Create a pathname from the class name

// E.g. java.lang.Object => java/lang/Object

String fileStub = name.replace( '.', '/' );

// Build objects pointing to the source code (.java) and object

// code (.class)

String javaFilename = fileStub+".java";

String classFilename = fileStub+".class";

File javaFile = new File( javaFilename );

File classFile = new File( classFilename );

//System.out.println( "j "+javaFile.lastModified()+" c "+

// classFile.lastModified() );

// First, see if we want to try compiling. We do if (a) there

// is source code, and either (b0) there is no object code,

// or (b1) there is object code, but it's older than the source

if (javaFile.exists() &&

(!classFile.exists() ||

javaFile.lastModified() > classFile.lastModified())) {

try {

// Try to compile it. If this doesn't work, then

// we must declare failure. (It's not good enough to use

// and already-existing, but out-of-date, classfile)

if (!compile( javaFilename ) || !classFile.exists()) {

throw new ClassNotFoundException( "Compile failed: "+javaFilename );

}

} catch( IOException ie ) {

// Another place where we might come to if we fail

// to compile

throw new ClassNotFoundException( ie.toString() );

}

}

// Let's try to load up the raw bytes, assuming they were

// properly compiled, or didn't need to be compiled

try {

// read the bytes

byte raw[] = getBytes( classFilename );

// try to turn them into a class

clas = defineClass( name, raw, 0, raw.length );

} catch( IOException ie ) {

// This is not a failure! If we reach here, it might

// mean that we are dealing with a class in a library,

// such as java.lang.Object

}

//System.out.println( "defineClass: "+clas );

// Maybe the class is in a library -- try loading

// the normal way

if (clas==null) {

clas = findSystemClass( name );

}

//System.out.println( "findSystemClass: "+clas );

// Resolve the class, if any, but only if the "resolve"

// flag is set to true

if (resolve && clas != null)

resolveClass( clas );

// If we still don't have a class, it's an error

if (clas == null)

throw new ClassNotFoundException( name );

// Otherwise, return the class

return clas;

}

}

Author:
-----------------------
Kazi Masudul Alam
Software Engineer

Game Programming Event




In Pyxisnet we had a game programming contest which lasted for 3 days. During this time all the employees of our company gave their full effort to make it a success. Some of us spent the whole
night before pc's.

So, the thing i want to add here, my team which had 5 group members received the
"Most Creative" award. And, Now what i am going to do.....!!!!!!!!!!

I am going to share some pics of those award giving ceremony with all.

Author:
-----------------------
Kazi Masudul Alam
Software Engineer

Java print API for splitted JTable

In this article i am posting a Java API which is capable of printing JTable by splitting
the columns according to the Page Format of the printing file.

The example creates a Table Model from a set of data. The number of columns in this
case is more to fit in one page. So, the solution is provided here to split the table columns
and print them as such this.

Full Page
--------------------------
--------------------------
--------------------------
-----------1--------------
--------------------------

Split Page 1
---------
---------
---------
---1-2---
---------

Split Page 2
---------
---------
---------
---1-2---
---------

Split Page 3
---------
---------
---------
---1-3---
---------

The solution is implemented as applet. The solution goes in this way First getPageInfo()
function derives the page information. That is, the number of splits should be made depending
upon the length of the table column and page format. This function makes a temporary data
array subTableSplit which describes the page left and page right for each split of the page
through out the JTable.

Next that particular part pageWidth = pageRight-pageLeft is printed.

The complete code is available in the following link.


http://www.mediafire.com/?excw0rz0s7c

Author:
-----------------------
Kazi Masudul Alam
Software Engineer