01: /*****************************************************************************
02: * Copyright (C) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: *****************************************************************************/package org.picocontainer.security;
09:
10: import java.net.URL;
11: import java.net.URLClassLoader;
12: import java.security.AccessController;
13: import java.security.CodeSource;
14: import java.security.PermissionCollection;
15: import java.security.Permissions;
16: import java.security.PrivilegedAction;
17: import java.util.Map;
18:
19: /**
20: * CustomPermissionsURLClassLoader extends URLClassLoader, adding the abilty to programatically add permissions easily.
21: * To be effective for permission management, it should be run in conjunction with a policy that restricts
22: * some of the classloaders, but not all.
23: * It's not ordinarily used by PicoContainer, but is here because PicoContainer is common
24: * to most classloader trees.
25: *
26: * @author Paul Hammant
27: */
28: public class CustomPermissionsURLClassLoader extends URLClassLoader {
29: private final Map<URL, Permissions> permissionsMap;
30:
31: public CustomPermissionsURLClassLoader(URL[] urls,
32: Map<URL, Permissions> permissionsMap, ClassLoader parent) {
33: super (urls, parent);
34: this .permissionsMap = permissionsMap;
35: }
36:
37: public Class<?> loadClass(String name)
38: throws ClassNotFoundException {
39: try {
40: return super .loadClass(name);
41: } catch (ClassNotFoundException e) {
42: throw decorateException(name, e);
43: }
44: }
45:
46: protected Class<?> findClass(String name)
47: throws ClassNotFoundException {
48: try {
49: return super .findClass(name);
50: } catch (ClassNotFoundException e) {
51: throw decorateException(name, e);
52: }
53: }
54:
55: private ClassNotFoundException decorateException(String name,
56: ClassNotFoundException e) {
57: if (name.startsWith("class ")) {
58: return new ClassNotFoundException(
59: "Class '"
60: + name
61: + "' is not a classInstance.getName(). "
62: + "It's a classInstance.toString(). The clue is that it starts with 'class ', no classname contains a space.");
63: }
64: ClassLoader classLoader = this ;
65: StringBuffer sb = new StringBuffer("'").append(name).append(
66: "' classloader stack [");
67: while (classLoader != null) {
68: sb.append(classLoader.toString()).append("\n");
69: final ClassLoader cl = classLoader;
70: classLoader = (ClassLoader) AccessController
71: .doPrivileged(new PrivilegedAction() {
72: public Object run() {
73: return cl.getParent();
74: }
75: });
76:
77: }
78: return new ClassNotFoundException(sb.append("]").toString(), e);
79: }
80:
81: public String toString() {
82: String result = CustomPermissionsURLClassLoader.class.getName()
83: + " " + System.identityHashCode(this ) + ":";
84: URL[] urls = getURLs();
85: for (URL url : urls) {
86: result += "\n\t" + url.toString();
87: }
88:
89: return result;
90: }
91:
92: public PermissionCollection getPermissions(CodeSource codeSource) {
93: return (Permissions) permissionsMap.get(codeSource
94: .getLocation());
95: }
96:
97: }
|