KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > CustomPermissionsURLClassLoader


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9
10 package org.picocontainer.defaults;
11
12 import java.net.URL JavaDoc;
13 import java.net.URLClassLoader JavaDoc;
14 import java.security.AccessController JavaDoc;
15 import java.security.CodeSource JavaDoc;
16 import java.security.PermissionCollection JavaDoc;
17 import java.security.Permissions JavaDoc;
18 import java.security.PrivilegedAction JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * CustomPermissionsURLClassLoader extends URLClassLoader, adding the abilty to programatically add permissions easily.
23  * To be effective for permission management, it should be run in conjunction with a policy that restricts
24  * some of the classloaders, but not all.
25  * It's not ordinarily used by PicoContainer, but is here because PicoContainer is common
26  * to most classloader trees.
27  *
28  * @author Paul Hammant
29  */

30 public class CustomPermissionsURLClassLoader extends URLClassLoader JavaDoc {
31     private final Map JavaDoc permissionsMap;
32
33     public CustomPermissionsURLClassLoader(URL JavaDoc[] urls, Map JavaDoc permissionsMap, ClassLoader JavaDoc parent) {
34         super(urls, parent);
35         this.permissionsMap = permissionsMap;
36     }
37
38     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
39         try {
40             return super.loadClass(name);
41         } catch (ClassNotFoundException JavaDoc e) {
42             throw decorateException(name, e);
43         }
44     }
45
46     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
47         try {
48             return super.findClass(name);
49         } catch (ClassNotFoundException JavaDoc e) {
50             throw decorateException(name, e);
51         }
52     }
53
54     private ClassNotFoundException JavaDoc decorateException(String JavaDoc name, ClassNotFoundException JavaDoc e) {
55         if (name.startsWith("class ")) {
56             return new ClassNotFoundException JavaDoc("Class '" + name + "' is not a classInstance.getName(). " +
57                     "It's a classInstance.toString(). The clue is that it starts with 'class ', no classname contains a space.");
58         }
59         ClassLoader JavaDoc classLoader = this;
60         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("'").append(name).append("' classloader stack [");
61         while (classLoader != null) {
62             sb.append(classLoader.toString()).append("\n");
63             final ClassLoader JavaDoc cl = classLoader;
64             classLoader = (ClassLoader JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
65                 public Object JavaDoc run() {
66                     return cl.getParent();
67                 }
68             });
69
70         }
71         return new ClassNotFoundException JavaDoc(sb.append("]").toString(), e);
72     }
73
74     public String JavaDoc toString() {
75         String JavaDoc result = CustomPermissionsURLClassLoader.class.getName() + " " + System.identityHashCode(this) + ":";
76         URL JavaDoc[] urls = getURLs();
77         for (int i = 0; i < urls.length; i++) {
78             URL JavaDoc url = urls[i];
79             result += "\n\t" + url.toString();
80         }
81
82         return result;
83     }
84
85     public PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codeSource) {
86         return (Permissions JavaDoc) permissionsMap.get(codeSource.getLocation());
87     }
88
89 }
90
91
Popular Tags