01: package com.sun.istack.tools;
02:
03: import java.util.Collection;
04:
05: /**
06: * {@link ClassLoader} that masks a specified set of classes
07: * from its parent class loader.
08: *
09: * <p>
10: * This code is used to create an isolated environment.
11: *
12: * @author Kohsuke Kawaguchi
13: */
14: public class MaskingClassLoader extends ClassLoader {
15:
16: private final String[] masks;
17:
18: public MaskingClassLoader(String... masks) {
19: this .masks = masks;
20: }
21:
22: public MaskingClassLoader(Collection<String> masks) {
23: this (masks.toArray(new String[masks.size()]));
24: }
25:
26: public MaskingClassLoader(ClassLoader parent, String... masks) {
27: super (parent);
28: this .masks = masks;
29: }
30:
31: public MaskingClassLoader(ClassLoader parent,
32: Collection<String> masks) {
33: this (parent, masks.toArray(new String[masks.size()]));
34: }
35:
36: @Override
37: protected synchronized Class<?> loadClass(String name,
38: boolean resolve) throws ClassNotFoundException {
39: for (String mask : masks) {
40: if (name.startsWith(mask))
41: throw new ClassNotFoundException();
42: }
43:
44: return super.loadClass(name, resolve);
45: }
46: }
|