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