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

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;
}
}
主站蜘蛛池模板: 凤山县| 九寨沟县| 乌兰浩特市| 新密市| 卓资县| 邻水| 遂宁市| 海阳市| 于田县| 铜梁县| 长沙县| 保靖县| 长岭县| 南投市| 烟台市| 焉耆| 城步| 大英县| 清河县| 安阳县| SHOW| 桂林市| 土默特左旗| 青川县| 工布江达县| 周宁县| 囊谦县| 宝清县| 常宁市| 色达县| 新河县| 上思县| 合阳县| 南宫市| 探索| 昌吉市| 皋兰县| 西和县| 富源县| 若尔盖县| 汉源县|