| By Israel Hilerio | Article Rating: |
|
| January 1, 1998 12:00 AM EST | Reads: |
15,928 |
One of the primary reasons for the success of Java has been its robust dynamic class loading mechanism. The Java Virtual Machine ClassLoader is a mechanism that Java uses to load classes at runtime. Techniques to take advantage of Java's dynamic nature and the interactive nature of the Web usually create a spark of interest in the Java community.
This article leads the reader through the development of a new Java ClassLoader mechanism that leverages the Web for downloading Java classes. It begins with an overview of the Java ClassLoader mechanism, followed by an examination of how Java's ClassLoader can be extended to download Java classes from multiple Web servers to be used by a single Java application. This will allow users to develop applications that download behavior dynamically and extend their functionality on the fly using the Web.
The Mechanics of the Java ClassLoader
The Java Virtual Machine ClassLoader is a mechanism used to load classes from files in directories or zip files specified in the java.class.path system property. This property is set using the CLASSPATH environment variable. Classes are searched sequentially through the directories and zip files contained in the java.class.path property. The default setting for the java.class.path property is the subdirectory where the JDK has been installed and the zip file containing the Java classes (i.e., classes.zip). The ClassLoader is responsible for taking a class name and loading it into the Java runtime process.
Another important activity of the ClassLoader is to link a Class. Linking takes a binary class and incorporates it into the Java Virtual Machine Runtime. This allows a class to be executed. There are three distinct operations involved when linking:
1. Verification
2. Preparation
3. Resolution of symbolic references
Verification ensures that the binary representation of a class maps to a valid operation, the instructions are type safe and the integrity of the Java Virtual Machine is not violated. Preparation initializes the static fields (both variables and constants) of a class with default values, and checks for abstract method consistency between classes. The resolution of symbolic references is used to verify the correctness of a reference (method signature verification, field signature verification, etc.) and substitute symbols with direct reference.
The default ClassLoader can be extended to define specific properties for loading Java classes. Some of the properties that can be defined are:
Developers can create their own ClassLoaders by extending the ClassLoader abstract class. The ClassLoader abstract class contains the following methods. For a detailed explanation of the ClassLoader abstract class refer to "The Java Class Libraries An Annotated Reference" (Chan and Lee, Addison Wesley, 1997).
The next section provides an example of a new ClassLoader, called the URLClassLoader, that extends the ClassLoader abstract class and leverages the Internet and Web browser to download Java classes into Java applications.
URLClassLoader
The URLClassLoader is a ClassLoader mechanism that leverages Web Servers to download Java classes to client Java applications. Java applets are able to download multiple Java classes from the connecting Web server. The URLClassLoader allows Java applications to similarly download multiple Java classes, but unlike Java applets, from multiple Web servers. Once the classes are downloaded they are incorporated into the running Java application.
Unlike regular ClassLoader mechanisms which are file-based, the URLClassLoader requires a URL string in order to identify the location from where to download the class bytecodes. An example of a URL String is given below:
http://www.motorservice.com/
java_classes/texteditor.class.
This String tells the ClassLoader to download a class call texteditor.class from the machine www.motorservice.com in the directory called java_classes.
The URLClassLoader is responsible for downloading the bytecodes from the URL location, instantiating a Class object from the downloaded bytecodes and caching the downloaded Class object for future use. These operations take place outside of the loadClass() method. The loadClass() method is called whenever an object is instantiated from the cached generated Class object using the newInstance() method on the Class class. We have chosen to download bytecodes and to process all of the class information outside of the loadClass() method because the URL information associated with the class name is not contained in the input parameters of the loadClass method. The caching policy may vary between ClassLoader implementations but the idea is the same, viz., to minimize the amount of time you go the network to access a class.
Applications of the URLClassLoader
The following types of applications can benefit from the capabilities of the URLClassLoader:
URLClass Loader Trigger
Application
The trigger environment implemented in this example takes URL strings of the form
http://www.motorservice.com/
java_classes/texteditor.class
The texteditor.class is a Java application that extends the Frame class. The trigger application downloads this type of application over the Internet and executes them. This trigger environment acts as a runtime shell for Java programs. The advantage of having the trigger environment executing the various Java applications is that it can act as a task manager by suspending or stopping the execution of the spawned programs. The user could extend this program to allow components of one application to communicate and interact with components of a second application.
There are two classes associated with the trigger environment, trigger and URLClassLoader. Figure 1 shows the class diagrams of the two classes. The trigger class is responsible for providing the URLClassLoader with the desired Java application to be downloaded. Also, it is responsible for creating an instance of the downloaded Java application class and for starting its execution by calling the main() method on the class. The URLClassLoader is responsible for downloading the bytecodes associated with the URL string and for linking the downloaded Java application class with the running JVM process.
Figure 2 shows the collaboration diagrams for executing a Java Application downloaded over the Web.
The trigger environment provides a simple application that allows a user to enter a URL with a Java application class. In order for the Java application class to be downloadable it must be contained in a Web server directory. Figure 3 shows the trigger environment application screen.
The method registerJavaClass() creates a URL input stream and downloads the byte codes via this mechanism. This is the first method called whenever the "Execute Java Application" button is pressed. The string typed inside the textfield is passed to this method.
// Create URL Stream
// Open URL Stream
u = new URL(URLlocation);
input = u.openStream();
data = new DataInputStream(input);
// Download byte codes from URL Stream
byte classBytes[] =
downloadByteCodesFromURL(data);
Also, the registerJavaClass() method creates a Class from the downloaded byte array.
// Create class from byte code
ourClass = createClassFromByteCodes
(classBytes, true);
The method createClassFromByteCodes() creates a class by calling the defineClass() method on the default ClassLoader. In addition, this method is responsible for linking the created class object with the JVM runtime process. Once the class object has been linked it caches the object inside of a Hashtable (see Listing 1).
The loadClass() method gets executed whenever the class is being instantiated. This method checks the local java.class.path system property first to load the class locally. It checks the Hashtable cache if it can not find the class locally. If the class being instantiated has not been loaded it proceeds by trying to download the class from the last accessed Web site location (see Listing 2).
If the class being instantiated is not cached, the URLClassLoader attempts to download the requested class from the Web using the last accessed Web site. Fetching the class from the network is done by reconstructing a URL path to the requested class name (see Listing 3).
After the Java application class has been registered by the ClassLoader and instantiated by the trigger class, we use reflection to invoke the static main method inside the Java application. Since we are downloading Java applications over the Web, we expect the applications to contain a main() method. We search the Java class for a main(String[]) method and once we find it we execute the method (see Listing 4).
For a copy of the complete and working trigger program source code, please refer to Listing 5 and Listing 6. An additional enhancement that can be made to the trigger environment application is to persist the Hashtable object. This would allow the permanent caching of downloaded Java applications. However, the drawback to this approach is that as new releases of the applications are stored on Web servers you won't be able to use the new applications.
Conclusion
In this article, we walked through the design of a ClassLoader mechanism that allows you to use the Web servers as extensions of your hard drive. There are security issues involved in implementing your own ClassLoader that we have not discussed here.
Published January 1, 1998 Reads 15,928
Copyright © 1998 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Israel Hilerio
Israel Hilerio is a program manager at Microsoft in the Windows Workflow Foundation team. He has 15+ years of development experience doing business applications and has a PhD in Computer Science.
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?







































