01: /*BEGIN_COPYRIGHT_BLOCK
02: *
03: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
04: * All rights reserved.
05: *
06: * Redistribution and use in source and binary forms, with or without
07: * modification, are permitted provided that the following conditions are met:
08: * * Redistributions of source code must retain the above copyright
09: * notice, this list of conditions and the following disclaimer.
10: * * Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
14: * names of its contributors may be used to endorse or promote products
15: * derived from this software without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: *
29: * This software is Open Source Initiative approved Open Source Software.
30: * Open Source Initative Approved is a trademark of the Open Source Initiative.
31: *
32: * This file is part of DrJava. Download the current version of this project
33: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
34: *
35: * END_COPYRIGHT_BLOCK*/
36:
37: package edu.rice.cs.util.classloader;
38:
39: import java.util.*;
40:
41: /** A class loader that does nothing but allow, at runtime, classes to be put on a list of "do not load"
42: * classes, which will be rejected from loading, even if they are available.
43: * @version $Id: LimitingClassLoader.java 4255 2007-08-28 19:17:37Z mgricken $
44: */
45: public class LimitingClassLoader extends ClassLoader {
46: private List<String> _restrictedList = new LinkedList<String>();
47:
48: /** Creates a LimitingClassLoader.
49: * @param parent Parent class loader, which is used to load all classes not restricted from loading.
50: */
51: public LimitingClassLoader(ClassLoader parent) {
52: super (parent);
53: }
54:
55: public void addToRestrictedList(String name) {
56: _restrictedList.add(name);
57: }
58:
59: public void clearRestrictedList() {
60: _restrictedList.clear();
61: }
62:
63: /** Overrides {@link ClassLoader#loadClass(String,boolean)} to reject classes whose names are on the
64: * restricted list.
65: * @param name Name of class to load
66: * @param resolve If true then resolve the class
67: * @return {@link Class} object for the loaded class
68: * @throws ClassNotFoundException if name is on the restricted list, or if the parent class loader couldn't
69: * find the class.
70: */
71: protected Class<?> loadClass(String name, boolean resolve)
72: throws ClassNotFoundException {
73: ListIterator itor = _restrictedList.listIterator();
74:
75: while (itor.hasNext()) {
76: String current = (String) itor.next();
77: if (current.equals(name)) {
78: throw new ClassNotFoundException("Class " + name
79: + " is on the restricted list.");
80: }
81: }
82:
83: // If we got here, the class was not restricted.
84: Class<?> clazz = getParent().loadClass(name);
85:
86: // Because we couldn't call the protected loadClass(String,boolean)
87: // on the parent, here we handle resolution if needed.
88: if (resolve) {
89: resolveClass(clazz);
90: }
91:
92: return clazz;
93: }
94: }
|