Digital Edition

SYS-CON.TV
URL ClassLoader
URL ClassLoader

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:

  • File Format (.class, .zip, .jar, .personal)
  • Class Source (directories)
  • Protocol (http, ftp, file)
  • Security Conditions

    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).

  • ClassLoader() - This constructor must be instantiated by the calling application. The main purpose of this constructor, besides protocol-specific initialization, is to trigger a security check against the checkCreateClassLoader method in the acting SecurityManager instance. If a security violation exists, a SecurityException is thrown.
  • loadClass() - This method is the only one whose implementation needs to be provided by a non-abstract ClassLoader. loadClass method is responsible for mapping a class name to a class Class instance. The class loading policies are implemented inside of this method. The normal flow of execution of the loadClass method is to retrieve the class information in a byte array form, call the defineClass method to generate a Class object from the byte arrays, and to resolve the class references if the resolve variable is true. If the class name cannot be resolved to a class Class instance, a ClassNotFoundException is thrown.
  • defineClass() - This method is used by the ClassLoader to generate a Class object from a byte array containing the byte codes for a specific class. Also, the defineClass method is responsible for introducing the generated class into the JVM runtime process. The byte array must contain the format of a valid class. If the byte array doesn't contain a valid class definition, a ClassFormatError is thrown.
  • findSystemClass() - This method loads a class using the default system ClassLoader. This method uses the java.class.path system property to find the class that matches the requested class name. If the class name cannot be resolved to a class Class instance, a ClassNotFoundException is thrown.
  • resolveClass() - This method is used by the ClassLoader to incorporate a class into the JVM runtime process. Another responsibility of the resolveClass method is to map all of the class references contained inside of the class being loaded (including super class, methods and fields).

    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:

  • Applications that need to download functionality dynamically based on user request
  • Applications whose download time is very large and can benefit from sub-components download
  • Applications that act as broker or trigger environments for classes without common interfaces
  • Applications that need to interact with classes that do not share a common interfaceThe application environment presented in the next example is a trigger environment that starts Java applications. These applications are graphical in nature.

    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
    u = new URL(URLlocation);

    // Open URL Stream
    input = u.openStream();
    data = new DataInputStream(input);

    The registerJavaClass() method continues by downloading the byte codes from the stream and creating a byte array.

    // 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.

    About 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.

  • In order to post a comment you need to be registered and logged in.

    Register | Sign-in

    Reader Feedback: Page 1 of 1



    ADS BY GOOGLE
    Subscribe to the World's Most Powerful Newsletters

    ADS BY GOOGLE

    Dynatrace is an application performance management software company with products for the informatio...
    All in Mobile is a place where we continually maximize their impact by fostering understanding, empa...
    CloudEXPO New York 2018, colocated with DevOpsSUMMIT and DXWorldEXPO New York 2018 will be held Nove...
    Lori MacVittie is a subject matter expert on emerging technology responsible for outbound evangelism...
    Having been in the web hosting industry since 2002, dhosting has gained a great deal of experience w...
    A traditional way of software development efforts reimbursing is pay by the hour, which in case of r...
    Your job is mostly boring. Many of the IT operations tasks you perform on a day-to-day basis are rep...
    The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and...
    Headquartered in Plainsboro, NJ, Synametrics Technologies has provided IT professionals and computer...
    CI/CD is conceptually straightforward, yet often technically intricate to implement since it require...
    Using new techniques of information modeling, indexing, and processing, new cloud-based systems can ...
    SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22n...
    Adding public cloud resources to an existing application can be a daunting process. The tools that y...
    The now mainstream platform changes stemming from the first Internet boom brought many changes but d...
    Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (No...
    Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As au...
    A valuable conference experience generates new contacts, sales leads, potential strategic partners a...
    Andi Mann, Chief Technology Advocate at Splunk, is an accomplished digital business executive with e...
    Containers and Kubernetes allow for code portability across on-premise VMs, bare metal, or multiple ...
    SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 1...