01: /*
02: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License version
07: * 2 only, as published by the Free Software Foundation.
08: *
09: * This program is distributed in the hope that it will be useful, but
10: * WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * General Public License version 2 for more details (a copy is
13: * included at /legal/license.txt).
14: *
15: * You should have received a copy of the GNU General Public License
16: * version 2 along with this work; if not, write to the Free Software
17: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18: * 02110-1301 USA
19: *
20: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
21: * Clara, CA 95054 or visit www.sun.com if you need additional
22: * information or have any questions.
23: *
24: */
25:
26: /*
27: * A classloader that restricts access to any of the class in
28: * the JSRRestrictedClasses.txt configuration file.
29: */
30:
31: package sun.misc;
32:
33: import java.io.BufferedReader;
34: import java.io.File;
35: import java.io.FileReader;
36: import java.io.IOException;
37: import java.net.URL;
38: import java.net.URLClassLoader;
39: import java.util.HashSet;
40:
41: public class CDCAppClassLoader extends URLClassLoader {
42: // Classes that are not allowed to be accessed by CDC apps
43: private static HashSet restrictedClasses = new HashSet();
44:
45: static {
46: getRestrictedClasses();
47: }
48:
49: private static boolean getRestrictedClasses() {
50: String filename = System.getProperty("java.home")
51: + File.separator + "lib" + File.separator
52: + "JSRRestrictedClasses.txt";
53: BufferedReader in;
54:
55: try {
56: in = new BufferedReader(new FileReader(filename));
57: } catch (IOException e) {
58: System.err.println("Could not open " + filename);
59: return false;
60: }
61:
62: try {
63: while (true) {
64: String s = in.readLine();
65: if (s == null) {
66: break; // eof
67: }
68: if (s.length() == 0) {
69: continue;
70: }
71: if (s.charAt(0) == '#') {
72: continue; // comment
73: }
74: // it's a classname, add it to the restricted set
75: restrictedClasses.add(s);
76: }
77: in.close();
78: } catch (IOException e) {
79: System.err.println("Failed to read " + filename);
80: return false;
81: }
82: return true;
83: }
84:
85: public CDCAppClassLoader(URL[] urls, ClassLoader parent) {
86: super (urls, parent);
87: }
88:
89: public Class loadClass(String name) throws ClassNotFoundException {
90: // First check if the class is restricted
91: if (restrictedClasses.contains(name)) {
92: throw new ClassNotFoundException(name);
93: }
94:
95: return super.loadClass(name);
96: }
97:
98: }
|