KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > DocumentJarClassLoader


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import java.net.URL JavaDoc;
21 import java.net.URLClassLoader JavaDoc;
22 import java.security.CodeSource JavaDoc;
23 import java.security.cert.Certificate JavaDoc;
24 import java.security.Permission JavaDoc;
25 import java.security.PermissionCollection JavaDoc;
26 import java.security.Policy JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 /**
30  * This <tt>ClassLoader</tt> implementation only grants permission to
31  * connect back to the server from where the document referencing the
32  * jar file was loaded.
33  *
34  * A <tt>URLClassLoader</tt> extension is needed in case the user
35  * allows linked jar files to come from a different origin than
36  * the document referencing them.
37  *
38  * @author <a mailto="vincent.hardy@sun.com">Vincent Hardy</a>
39  * @version $Id: DocumentJarClassLoader.java,v 1.5 2004/10/30 16:54:53 deweese Exp $
40  */

41 public class DocumentJarClassLoader extends URLClassLoader JavaDoc {
42     /**
43      * CodeSource for the Document which referenced the Jar file
44      * @see #getPermissions
45      */

46     protected CodeSource JavaDoc documentCodeSource = null;
47
48     /**
49      * Constructor
50      */

51     public DocumentJarClassLoader(URL JavaDoc jarURL,
52                                   URL JavaDoc documentURL){
53         super(new URL JavaDoc[]{jarURL});
54
55         if (documentURL != null) {
56             documentCodeSource = new CodeSource JavaDoc
57                 (documentURL, (Certificate JavaDoc[])null);
58         }
59     }
60
61     /**
62      * Returns the permissions for the given codesource object.
63      * The implementation of this method first gets the permissions
64      * granted by the policy, and then adds additional permissions
65      * based on the URL of the codesource.
66      * <p>
67      * Then, if the documentURL passed at construction time is
68      * not null, the permissions granted to that URL are added.
69      *
70      * As a result, the jar file code will only be able to
71      * connect to the server which served the document.
72      *
73      * @param codesource the codesource
74      * @return the permissions granted to the codesource
75      */

76     protected PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource)
77     {
78         // First, get the permissions which may be granted
79
// through the policy file(s)
80
Policy JavaDoc p = Policy.getPolicy();
81
82     PermissionCollection JavaDoc pc = null;
83     if (p != null) {
84         pc = p.getPermissions(codesource);
85     }
86
87         // Now, add permissions if the documentCodeSource is not null
88
if (documentCodeSource != null){
89             PermissionCollection JavaDoc urlPC
90                 = super.getPermissions(documentCodeSource);
91
92             if (pc != null) {
93                 Enumeration JavaDoc items = urlPC.elements();
94                 while (items.hasMoreElements()) {
95                     pc.add((Permission JavaDoc)(items.nextElement()));
96                 }
97             } else {
98                 pc = urlPC;
99             }
100         }
101
102     return pc;
103     }
104 }
105
Popular Tags