Thu, 20 Dec 2012 16:24:18 -0800
8004982: JDK8 source with GPL header errors
Reviewed-by: ohair
rfield@1422 | 1 | /* |
katleman@1448 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
rfield@1422 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
rfield@1422 | 4 | * |
rfield@1422 | 5 | * This code is free software; you can redistribute it and/or modify it |
rfield@1422 | 6 | * under the terms of the GNU General Public License version 2 only, as |
rfield@1422 | 7 | * published by the Free Software Foundation. Oracle designates this |
rfield@1422 | 8 | * particular file as subject to the "Classpath" exception as provided |
rfield@1422 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
rfield@1422 | 10 | * |
rfield@1422 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
rfield@1422 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
rfield@1422 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
rfield@1422 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
rfield@1422 | 15 | * accompanied this code). |
rfield@1422 | 16 | * |
rfield@1422 | 17 | * You should have received a copy of the GNU General Public License version |
rfield@1422 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
rfield@1422 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
rfield@1422 | 20 | * |
rfield@1422 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
rfield@1422 | 22 | * or visit www.oracle.com if you need additional information or have any |
rfield@1422 | 23 | * questions. |
rfield@1422 | 24 | */ |
rfield@1422 | 25 | |
rfield@1422 | 26 | package org.openjdk.tests.separate; |
rfield@1422 | 27 | |
rfield@1422 | 28 | import java.util.HashMap; |
rfield@1422 | 29 | import java.io.File; |
rfield@1422 | 30 | import java.io.FileInputStream; |
rfield@1422 | 31 | import java.io.IOException; |
rfield@1422 | 32 | |
rfield@1422 | 33 | class DirectedClassLoader extends ClassLoader { |
rfield@1422 | 34 | |
rfield@1422 | 35 | private HashMap<String,File> loadLocations; |
rfield@1422 | 36 | private File defaultLocation; |
rfield@1422 | 37 | private ClassFilePreprocessor[] preprocessors; |
rfield@1422 | 38 | |
rfield@1422 | 39 | public DirectedClassLoader( |
rfield@1422 | 40 | HashMap<String,File> locations, File fallback, |
rfield@1422 | 41 | ClassFilePreprocessor ... preprocessors) { |
rfield@1422 | 42 | loadLocations = new HashMap<>(locations); |
rfield@1422 | 43 | defaultLocation = fallback; |
rfield@1422 | 44 | this.preprocessors = preprocessors; |
rfield@1422 | 45 | } |
rfield@1422 | 46 | |
rfield@1422 | 47 | public DirectedClassLoader( |
rfield@1422 | 48 | File fallback, ClassFilePreprocessor ... preprocessors) { |
rfield@1422 | 49 | loadLocations = new HashMap<>(); |
rfield@1422 | 50 | defaultLocation = fallback; |
rfield@1422 | 51 | this.preprocessors = preprocessors; |
rfield@1422 | 52 | } |
rfield@1422 | 53 | |
rfield@1422 | 54 | public DirectedClassLoader(ClassFilePreprocessor ... preprocessors) { |
rfield@1422 | 55 | this((File)null, preprocessors); |
rfield@1422 | 56 | } |
rfield@1422 | 57 | |
rfield@1422 | 58 | public void setDefaultLocation(File dir) { this.defaultLocation = dir; } |
rfield@1422 | 59 | public void setLocationFor(String name, File dir) { |
rfield@1422 | 60 | loadLocations.put(name, dir); |
rfield@1422 | 61 | } |
rfield@1422 | 62 | |
rfield@1422 | 63 | @Override |
rfield@1422 | 64 | protected Class<?> findClass(String name) { |
rfield@1422 | 65 | String path = name.replace(".", File.separator) + ".class"; |
rfield@1422 | 66 | |
rfield@1422 | 67 | File location = loadLocations.get(name); |
rfield@1422 | 68 | if (location == null || !(new File(location, path)).exists()) { |
rfield@1422 | 69 | File def = new File(defaultLocation, path); |
rfield@1422 | 70 | if (def.exists()) { |
rfield@1422 | 71 | return defineFrom(name, new File(location, path)); |
rfield@1422 | 72 | } |
rfield@1422 | 73 | } else { |
rfield@1422 | 74 | return defineFrom(name, new File(location, path)); |
rfield@1422 | 75 | } |
rfield@1422 | 76 | return null; |
rfield@1422 | 77 | } |
rfield@1422 | 78 | |
rfield@1422 | 79 | private Class<?> defineFrom(String name, File file) { |
rfield@1422 | 80 | FileInputStream fis = null; |
rfield@1422 | 81 | try { |
rfield@1422 | 82 | try { |
rfield@1422 | 83 | fis = new FileInputStream(file); |
rfield@1422 | 84 | byte[] bytes = new byte[fis.available()]; |
rfield@1422 | 85 | int read = fis.read(bytes); |
rfield@1422 | 86 | if (read != bytes.length) { |
rfield@1422 | 87 | return null; |
rfield@1422 | 88 | } |
rfield@1422 | 89 | if (preprocessors != null) { |
rfield@1422 | 90 | for (ClassFilePreprocessor cfp : preprocessors) { |
rfield@1422 | 91 | bytes = cfp.preprocess(name, bytes); |
rfield@1422 | 92 | } |
rfield@1422 | 93 | } |
rfield@1422 | 94 | return defineClass(name, bytes, 0, bytes.length); |
rfield@1422 | 95 | } finally { |
rfield@1422 | 96 | fis.close(); |
rfield@1422 | 97 | } |
rfield@1422 | 98 | } catch (IOException e) {} |
rfield@1422 | 99 | return null; |
rfield@1422 | 100 | } |
rfield@1422 | 101 | } |