Monday, 26 September 2011

Networking Enhancements in Java SE 7


Networking Enhancements in Java SE 7

The URLClassLoader.close method has been added. This method effectively eliminates the problem of how to support updated implementations of the classes and resources loaded from a particular codebase, and in particular from JAR files. 
Details of URLClassLoader.close():-->

Constructor Summary

URLClassLoader(URL[] urls)
Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader.
URLClassLoader(URL[] urls, ClassLoader parent)
Constructs a new URLClassLoader for the given URLs.
URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory)
Constructs a new URLClassLoader for the specified URLs, parent class loader, and URLStreamHandlerFactory.

Method Summary

Modifier and TypeMethod and Description
protected voidaddURL(URL url)
Appends the specified URL to the list of URLs to search for classes and resources.
voidclose()
Closes this URLClassLoader, so that it can no longer be used to load new classes or resources that are defined by this loader.
protected PackagedefinePackage(String name, Manifest man, URL url)
Defines a new package by name in this ClassLoader.
protected Class<?>findClass(String name)
Finds and loads the class with the specified name from the URL search path.
URLfindResource(String name)
Finds the resource with the specified name on the URL search path.
Enumeration<URL>findResources(String name)
Returns an Enumeration of URLs representing all of the resources on the URL search path having the specified name.
protected PermissionCollectiongetPermissions(CodeSource codesource)
Returns the permissions for the given codesource object.
InputStreamgetResourceAsStream(String name)
Returns an input stream for reading the specified resource.
URL[]getURLs()
Returns the search path of URLs for loading classes and resources.
static URLClassLoadernewInstance(URL[] urls)
Creates a new instance of URLClassLoader for the specified URLs and default parent class loader.
static URLClassLoadernewInstance(URL[] urls, ClassLoader parent)
Creates a new instance of URLClassLoader for the specified URLs and parent class loader.

URLClassLoader

public URLClassLoader(URL[] urls)
Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader. The URLs will be searched in the order specified for classes and resources after first searching in the parent class loader. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.If there is a security manager, this method first calls the security manager's checkCreateClassLoader method to ensure creation of a class loader is allowed.
Parameters:
urls - the URLs from which to load classes and resources
Throws:
SecurityException - if a security manager exists and its checkCreateClassLoader method doesn't allow creation of a class loader.
See Also:
SecurityManager.checkCreateClassLoader()

Closing a URLClassLoader

How to Close a URLClassLoader?

The URLClassLoader close() method effectively eliminates the problem of how to support updated implementations of the classes and resources loaded from a particular codebase, and in particular from JAR files. In principle, once the application clears all references to a loader object, the garbage collector and finalization mechanisms will eventually ensure that all resources (such as the JarFile objects) are released and closed.
The application can then replace the JAR file, and create a new URLClassLoader instance to load from the same location, but this time using the new implementation of the classes/resources. However, since it can't be predicted exactly when finalization and garbage collection will occur, problems are caused for applications which need to be able to do this in a predictable and timely fashion. It is a problem on Windows, because open files cannot be deleted or replaced.
In Java SE 7, the URLClassLoader close() method effectively invalidates the loader, so that no new classes can be loaded from it. It also closes any JAR files that were opened by the loader. This allows the application to delete or replace these files and, if necessary, create new loaders using new implementations.
The close() method follows the familiar "Closeable" pattern, and URLClassLoader now implements the Closeable interface, which defines URLClassLoader.close(). The following sample code shows how one might use the method.
       //
       // create a class loader loading from "foo.jar"
       //
       URL url = new URL("file:foo.jar");
       URLClassLoader loader = new URLClassLoader (new URL[] {url});
       Class cl = Class.forName ("Foo", true, loader);
       Runnable foo = (Runnable) cl.newInstance();
       foo.run();
       loader.close ();

       // foo.jar gets updated somehow

       loader = new URLClassLoader (new URL[] {url});
       cl = Class.forName ("Foo", true, loader);
       foo = (Runnable) cl.newInstance();
       // run the new implementation of Foo
       foo.run();

No comments:

Post a Comment