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