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

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;
}
}
主站蜘蛛池模板: 临沧市| 肃宁县| 盘山县| 三江| 新宁县| 英德市| 汨罗市| 阿拉善左旗| 肇东市| 吴江市| 兴城市| 石首市| 华亭县| 高州市| 镇坪县| 甘肃省| 嘉善县| 洛宁县| 永兴县| 罗江县| 奉化市| 九江县| 禹城市| 九江县| 襄垣县| 湟源县| 金堂县| 上饶县| 怀宁县| 格尔木市| 清远市| 张家港市| 台北市| 宁强县| 准格尔旗| 康定县| 武川县| 金川县| 石泉县| 墨江| 滨海县|