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