Trail: The Extension Mechanism
|
Lesson: Creating and Using Extensions
|
|
Download Extensions
Download extensions are classes, including classes in JAR files,
that are specified in the Class-Path headers in the manifests
of other JAR files. Assume for example that a.jar and b.jar are
two JAR files in the same directory, and that the manifest of a.jar
contains this header:
Class-Path: b.jar
Then the classes in b.jar serve as extension classes for purposes
of the classes in a.jar. The classes in a.jar can invoke classes in
b.jar without b.jar's classes having to be
named on the class path. a.jar may or may not itself be an extension.
If b.jar weren't in the same directory as a.jar, then
the value of the Class-Path header should be set to the relative
pathname of b.jar.
There's nothing special about the classes that are playing
the role of a download extension. They are treated as extensions solely
because they're referenced by the manifest of some other JAR file.
To get a better understanding of how download extensions work, let's
create one and put it to use.
An Example
Suppose you want to create an applet that makes use of the
RectangleArea class of the previous
section:
public final class RectangleArea {
public static int area(java.awt.Rectangle r) {
return r.width * r.height;
}
}
In the previous section, you made the RectangleArea class
into an installed extension by placing the JAR file containing it into
the lib/ext directory of the JRE. By making it an installed
extension, you enabled any application to use the RectangleArea
class as if it were part of the Java platform.
If you want to be able to use the RectangleArea class from an
applet, the situation is a little different. Suppose, for example,
that you have an applet, AreaApplet, that makes use of class
RectangleArea:
import java.applet.Applet;
import java.awt.*;
public class AreaApplet extends Applet {
Rectangle r;
public void init() {
int width = 10;
int height = 5;
r = new Rectangle(width, height);
}
public void paint(Graphics g) {
g.drawString("The rectangle's area is "
+ RectangleArea.area(r), 10, 10);
}
}
This applet instantiates a 10 x 5 rectangle and then displays
the rectangle's area by using the RectangleArea.area method.
However, you can't assume that everyone
who downloads and uses your applet is going to have the RectangleArea
class available on their system, as an installed extension or
otherwise. One way around that problem is
to make the RectangleArea class available from the server side,
and you can do that by using it as a download extension.
To see how that's done, let's assume that you've bundled
AreaApplet in a
JAR file called AreaApplet.jar and that the class
RectangleArea is bundled in RectangleArea.jar. In order for
RectangleArea.jar to be treated as a download extension,
RectangleArea.jar must be listed in the
Class-Path header in AreaApplet.jar's manifest.
AreaApplet.jar's manifest might look like this, for example:
Manifest-Version: 1.0
Class-Path: RectangleArea.jar
The value of the Class-Path header in this manifest is
RectangleArea.jar with no path specified, indicating that
RectangleArea.jar is located in the same directory as the applet's
JAR file.
More about the Class-Path Header
If an applet or application uses more than one extension, you can list
multiple URLs in a manifest. For example, the following is a valid header:
Class-Path: area.jar servlet.jar images/
In the Class-Path header any URLs listed
that don't end with '/' are assumed to be JAR files. URLs
ending in '/' indicate directories. In the
preceding example, images/ might be a directory containing
resources needed by the applet or the application.
You can also specify multiple extension URLs by using more than one
Class-Path header in the manifest. For example:
Class-Path: area.jar
Class-Path: servlet.jar
Download extensions can be "daisy chained", meaning that the manifest of
one download extension can have a Class-Path header that
refers to a second extension, which can refer to a third extension, and so on.
|