src/share/classes/sun/rmi/rmic/iiop/DirectoryLoader.java

Wed, 28 Mar 2012 02:50:50 -0700

author
mbankal
date
Wed, 28 Mar 2012 02:50:50 -0700
changeset 371
e324dfb90c9e
parent 158
91006f157c46
child 748
6845b95cba6b
permissions
-rw-r--r--

7079902: Refine CORBA data models
Reviewed-by: coffeys

     1 /*
     2  * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 /*
    27  * Licensed Materials - Property of IBM
    28  * RMI-IIOP v1.0
    29  * Copyright IBM Corp. 1998 1999  All Rights Reserved
    30  *
    31  */
    33 package sun.rmi.rmic.iiop;
    35 import java.util.Hashtable;
    36 import java.io.File;
    37 import java.io.FileInputStream;
    39 /**
    40  * DirectoryLoader is a simple ClassLoader which loads from a specified
    41  * file system directory.
    42  * @author Bryan Atsatt
    43  */
    45 public class DirectoryLoader extends ClassLoader {
    47     private Hashtable cache;
    48     private File root;
    50     /**
    51      * Constructor.
    52      */
    53     public DirectoryLoader (File rootDir) {
    54         cache = new Hashtable();
    55         if (rootDir == null || !rootDir.isDirectory()) {
    56             throw new IllegalArgumentException();
    57         }
    58         root = rootDir;
    59     }
    61     private DirectoryLoader () {}
    63     /**
    64      * Convenience version of loadClass which sets 'resolve' == true.
    65      */
    66     public Class loadClass(String className) throws ClassNotFoundException {
    67         return loadClass(className, true);
    68     }
    70     /**
    71      * This is the required version of loadClass which is called
    72      * both from loadClass above and from the internal function
    73      * FindClassFromClass.
    74      */
    75     public synchronized Class loadClass(String className, boolean resolve)
    76         throws ClassNotFoundException {
    77         Class result;
    78         byte  classData[];
    80         // Do we already have it in the cache?
    82         result = (Class) cache.get(className);
    84         if (result == null) {
    86             // Nope, can we get if from the system class loader?
    88             try {
    90                 result = super.findSystemClass(className);
    92             } catch (ClassNotFoundException e) {
    94                 // No, so try loading it...
    96                 classData = getClassFileData(className);
    98                 if (classData == null) {
    99                     throw new ClassNotFoundException();
   100                 }
   102                 // Parse the class file data...
   104                 result = defineClass(classData, 0, classData.length);
   106                 if (result == null) {
   107                     throw new ClassFormatError();
   108                 }
   110                 // Resolve it...
   112                 if (resolve) resolveClass(result);
   114                 // Add to cache...
   116                 cache.put(className, result);
   117             }
   118         }
   120         return result;
   121     }
   123     /**
   124      * Reurn a byte array containing the contents of the class file.  Returns null
   125      * if an exception occurs.
   126      */
   127     private byte[] getClassFileData (String className) {
   129         byte result[] = null;
   130         FileInputStream stream = null;
   132         // Get the file...
   134         File classFile = new File(root,className.replace('.',File.separatorChar) + ".class");
   136         // Now get the bits...
   138         try {
   139             stream = new FileInputStream(classFile);
   140             result = new byte[stream.available()];
   141             stream.read(result);
   142         } catch(ThreadDeath death) {
   143             throw death;
   144         } catch (Throwable e) {
   145         }
   147         finally {
   148             if (stream != null) {
   149                 try {
   150                     stream.close();
   151                 } catch(ThreadDeath death) {
   152                     throw death;
   153                 } catch (Throwable e) {
   154                 }
   155             }
   156         }
   158         return result;
   159     }
   160 }

mercurial