1 /* 2 * Copyright (c) 2012 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 */ 25 26 package org.openjdk.tests.separate; 27 28 import java.util.HashMap; 29 import java.io.File; 30 import java.io.FileInputStream; 31 import java.io.IOException; 32 33 class DirectedClassLoader extends ClassLoader { 34 35 private HashMap<String,File> loadLocations; 36 private File defaultLocation; 37 private ClassFilePreprocessor[] preprocessors; 38 39 public DirectedClassLoader( 40 HashMap<String,File> locations, File fallback, 41 ClassFilePreprocessor ... preprocessors) { 42 loadLocations = new HashMap<>(locations); 43 defaultLocation = fallback; 44 this.preprocessors = preprocessors; 45 } 46 47 public DirectedClassLoader( 48 File fallback, ClassFilePreprocessor ... preprocessors) { 49 loadLocations = new HashMap<>(); 50 defaultLocation = fallback; 51 this.preprocessors = preprocessors; 52 } 53 54 public DirectedClassLoader(ClassFilePreprocessor ... preprocessors) { 55 this((File)null, preprocessors); 56 } 57 58 public void setDefaultLocation(File dir) { this.defaultLocation = dir; } 59 public void setLocationFor(String name, File dir) { 60 loadLocations.put(name, dir); 61 } 62 63 @Override 64 protected Class<?> findClass(String name) { 65 String path = name.replace(".", File.separator) + ".class"; 66 67 File location = loadLocations.get(name); 68 if (location == null || !(new File(location, path)).exists()) { 69 File def = new File(defaultLocation, path); 70 if (def.exists()) { 71 return defineFrom(name, new File(location, path)); 72 } 73 } else { 74 return defineFrom(name, new File(location, path)); 75 } 76 return null; 77 } 78 79 private Class<?> defineFrom(String name, File file) { 80 FileInputStream fis = null; 81 try { 82 try { 83 fis = new FileInputStream(file); 84 byte[] bytes = new byte[fis.available()]; 85 int read = fis.read(bytes); 86 if (read != bytes.length) { 87 return null; 88 } 89 if (preprocessors != null) { 90 for (ClassFilePreprocessor cfp : preprocessors) { 91 bytes = cfp.preprocess(name, bytes); 92 } 93 } 94 return defineClass(name, bytes, 0, bytes.length); 95 } finally { 96 fis.close(); 97 } 98 } catch (IOException e) {} 99 return null; 100 } 101 }