|
图9. 定制类加载器关系 |
| public byte[] findClassBytes(String className) { try { String pathName = currentRoot + File.separatorChar + className. replace(’.’, File.separatorChar) + ".class"; FileInputStream inFile = new FileInputStream(pathName); byte[] classBytes = new byte[inFile.available()]; inFile.read(classBytes); return classBytes; } catch (java.io.IOException ioEx) { return null; } } public Class findClass(String name)throws ClassNotFoundException { byte[] classBytes = findClassBytes(name); if (classBytes==null) { throw new ClassNotFoundException(); } else{ return defineClass(name, classBytes, 0, classBytes.length); } } public Class findClass(String name, byte[] classBytes)throws ClassNotFoundException { if (classBytes==null) { throw new ClassNotFoundException( "(classBytes==null)"); } else{ return defineClass(name, classBytes, 0, classBytes.length); } } public void execute(String codeName, byte[] code) { Class klass = null; try { klass = findClass(codeName, code); TaskIntf task = (TaskIntf) klass.newInstance(); task.execute(); } catch(Exception exception){ exception.printStackTrace(); } } |
| public class Client { public static void main (String[] args) { try{ byte[] code = getClassDefinition ("client.TaskImpl"); serverIntf.execute("client.TaskImpl", code); } catch(RemoteException remoteException) { remoteException.printStackTrace(); } } private static byte[] getClassDefinition (String codeName) { String userDir = System.getProperties(). getProperty("BytePath"); FileSystemClassLoader fscl1 = null; try { fscl1 = new FileSystemClassLoader (userDir); } catch(FileNotFoundException fileNotFoundException) { fileNotFoundException.printStackTrace(); } return fscl1.findClassBytes(codeName); } } |
| public void execute(String codeName, byte[] code)throws RemoteException { FileSystemClassLoader fileSystemClassLoader = null; try { fileSystemClassLoader = new FileSystemClassLoader(); fileSystemClassLoader.execute(codeName, code); } catch(Exception exception) { throw new RemoteException(exception.getMessage()); } } |
|
图10. 定制类加载器执行引擎 |
|
图11. 定制类加载器,客户端1 |
|
图12. 定制类加载器,客户端1 |
关注此文的读者还看过: