aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * Licensed Materials - Property of IBM aoqi@0: * RMI-IIOP v1.0 aoqi@0: * Copyright IBM Corp. 1998 1999 All Rights Reserved aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: package sun.rmi.rmic.iiop; aoqi@0: aoqi@0: import java.util.Hashtable; aoqi@0: import java.io.File; aoqi@0: import java.io.FileInputStream; aoqi@0: aoqi@0: /** aoqi@0: * DirectoryLoader is a simple ClassLoader which loads from a specified aoqi@0: * file system directory. aoqi@0: * @author Bryan Atsatt aoqi@0: */ aoqi@0: aoqi@0: public class DirectoryLoader extends ClassLoader { aoqi@0: aoqi@0: private Hashtable cache; aoqi@0: private File root; aoqi@0: aoqi@0: /** aoqi@0: * Constructor. aoqi@0: */ aoqi@0: public DirectoryLoader (File rootDir) { aoqi@0: cache = new Hashtable(); aoqi@0: if (rootDir == null || !rootDir.isDirectory()) { aoqi@0: throw new IllegalArgumentException(); aoqi@0: } aoqi@0: root = rootDir; aoqi@0: } aoqi@0: aoqi@0: private DirectoryLoader () {} aoqi@0: aoqi@0: /** aoqi@0: * Convenience version of loadClass which sets 'resolve' == true. aoqi@0: */ aoqi@0: public Class loadClass(String className) throws ClassNotFoundException { aoqi@0: return loadClass(className, true); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This is the required version of loadClass which is called aoqi@0: * both from loadClass above and from the internal function aoqi@0: * FindClassFromClass. aoqi@0: */ aoqi@0: public synchronized Class loadClass(String className, boolean resolve) aoqi@0: throws ClassNotFoundException { aoqi@0: Class result; aoqi@0: byte classData[]; aoqi@0: aoqi@0: // Do we already have it in the cache? aoqi@0: aoqi@0: result = (Class) cache.get(className); aoqi@0: aoqi@0: if (result == null) { aoqi@0: aoqi@0: // Nope, can we get if from the system class loader? aoqi@0: aoqi@0: try { aoqi@0: aoqi@0: result = super.findSystemClass(className); aoqi@0: aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: aoqi@0: // No, so try loading it... aoqi@0: aoqi@0: classData = getClassFileData(className); aoqi@0: aoqi@0: if (classData == null) { aoqi@0: throw new ClassNotFoundException(); aoqi@0: } aoqi@0: aoqi@0: // Parse the class file data... aoqi@0: aoqi@0: result = defineClass(classData, 0, classData.length); aoqi@0: aoqi@0: if (result == null) { aoqi@0: throw new ClassFormatError(); aoqi@0: } aoqi@0: aoqi@0: // Resolve it... aoqi@0: aoqi@0: if (resolve) resolveClass(result); aoqi@0: aoqi@0: // Add to cache... aoqi@0: aoqi@0: cache.put(className, result); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reurn a byte array containing the contents of the class file. Returns null aoqi@0: * if an exception occurs. aoqi@0: */ aoqi@0: private byte[] getClassFileData (String className) { aoqi@0: aoqi@0: byte result[] = null; aoqi@0: FileInputStream stream = null; aoqi@0: aoqi@0: // Get the file... aoqi@0: aoqi@0: File classFile = new File(root,className.replace('.',File.separatorChar) + ".class"); aoqi@0: aoqi@0: // Now get the bits... aoqi@0: aoqi@0: try { aoqi@0: stream = new FileInputStream(classFile); aoqi@0: result = new byte[stream.available()]; aoqi@0: stream.read(result); aoqi@0: } catch(ThreadDeath death) { aoqi@0: throw death; aoqi@0: } catch (Throwable e) { aoqi@0: } aoqi@0: aoqi@0: finally { aoqi@0: if (stream != null) { aoqi@0: try { aoqi@0: stream.close(); aoqi@0: } catch(ThreadDeath death) { aoqi@0: throw death; aoqi@0: } catch (Throwable e) { aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return result; aoqi@0: } aoqi@0: }