官术网_书友最值得收藏!

ClassLoader

Java Runtime Environment executes platform-independent bytecodes for classes. Using a Java API, you can instantiate and load a class from its bytecode and integrate it with the runtime environment. When we compile Java files and when import statements are encountered, referenced classes or packages are loaded from bytecode files available in the classpath located in the local filesystem.

The ClassLoader class allows programmers to run the class load from the location. The ClassLoader subclasses should implement the loadClass() method to be able to locate and load the bytecodes for that class definition and, sometimes, find all the other classes on which this class is dependent (the resolving class) only after that class is constructed.

In distributed computing, ClassLoader plays an important role when data is exchanged in binary form. An example of this is an applet running on the browser. Unlike CORBA/RMI, you can use the ClassLoader client to convert incoming bytecode into an object containing data. Thus, using java.io.InputStream, you can transport instantiated class objects over the network and ClassLoader will convert them back into the data-containing object at the client side.

In the following example, the abstract StreamClassLoader class allows you to load a class from a given location using the simple InputStream by altering the ClassLoader.loadClass() method argument's semantics:

//StreamClassLoader

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

public abstract class StreamClassLoader extends ClassLoader {
Hashtable<String, Class<?>> componentCache =
new Hashtable<String, Class<?>>();
InputStream source = null;
public StreamClassLoader() {
}
protected abstract String parseComponentNaming(
String componentLocation) throws ClassNotFoundException;
protected abstract void initializeStream(
String componentLocation) throws IOException;
protected abstract Class<?> readComponent(
String componentLocation, String componentName)
throws IOException, ClassNotFoundException;
public Class<?> loadComponent(String componentLocation,
boolean resolve) throws ClassNotFoundException {
String componentName = parseComponentNaming(componentLocation);
Class<?> component = (Class<?>) componentCache.get(componentName);
if (component == null) {
try {
initializeStream(componentLocation);
} catch (IOException e) {
throw new ClassNotFoundException(
"Failed opening stream to URL.");
}
try {
component = readComponent(componentLocation, componentName);
} catch (IOException e) {
throw new ClassNotFoundException(
"Failed reading class component from the stream: " + e);
}
}
componentCache.put(componentName, component);
if (resolve)
resolveClass(component);
return component;
}
}

The URLClassLoader class that extends StreamClassLoader parses the URL through stream loading:

//URLClassLoader

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLClassLoader extends StreamClassLoader {
URL urlClass = null;
InputStream streamClass = null;

@Override
protected String parseComponentNaming(String componentLocation)
throws ClassNotFoundException {

String componentName = null;
try {
urlClass = new URL(componentLocation);
} catch (MalformedURLException e) {
throw new ClassNotFoundException("Bad URL " +
componentLocation + " given: " + e);
}
System.out.println("File = " + urlClass.getFile());
System.out.println("Host = " + urlClass.getHost());
String filenameFromURL = urlClass.getFile();
if (!filenameFromURL.endsWith(".class"))
throw new ClassNotFoundException("Non-class URL given.");
else
componentName = filenameFromURL.substring(
0, filenameFromURL.lastIndexOf(".class"));
System.out.println("Classname = " + componentName);
return componentName;

}

@Override
protected void initializeStream(String componentLocation)
throws IOException {
streamClass = urlClass.openStream();
}

@Override
protected Class<?> readComponent(String componentLocation,
String componentName)
throws IOException, ClassNotFoundException {
URLConnection urlConn = urlClass.openConnection();
int componentSize = urlConn.getContentLength();
System.out.println("Class file is " + componentSize + " bytes.");
DataInputStream dataInClass = new DataInputStream(streamClass);
int isAvailable = dataInClass.available();
System.out.println("Available = " + isAvailable);
System.out.println("URLClassLoader: Reading class from stream...");
byte[] clsData = new byte[componentSize];
dataInClass.readFully(clsData);
Class<?> component = null;
System.out.println("URLClassLoader: Defining class...");
try {
component = defineClass(null,clsData, 0, clsData.length);
} catch (ClassFormatError e) {
throw new ClassNotFoundException(
"Format error found in class data.");
}
return component;
}
}
主站蜘蛛池模板: 专栏| 当阳市| 安泽县| 宁津县| 桐梓县| 灌阳县| 潼南县| 巴楚县| 通榆县| 广宗县| 台南市| 辽宁省| 凤城市| 岗巴县| 修文县| 常宁市| 肥东县| 抚顺县| 玛纳斯县| 西乌珠穆沁旗| 波密县| 麦盖提县| 巴青县| 盘锦市| 罗山县| 离岛区| 襄樊市| 体育| 四会市| 馆陶县| 潼南县| 香河县| 喜德县| 且末县| 雷波县| 金堂县| 喀什市| 莆田市| 屏山县| 平阴县| 涟水县|