KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > classloader > PolicyClassLoader


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.classloader;
88
89 import java.io.IOException JavaDoc;
90 import java.net.URL JavaDoc;
91 import java.net.URLClassLoader JavaDoc;
92 import java.security.CodeSource JavaDoc;
93 import java.security.PermissionCollection JavaDoc;
94 import java.security.Policy JavaDoc;
95 import java.util.Enumeration JavaDoc;
96 import org.codehaus.dna.LogEnabled;
97 import org.codehaus.dna.Logger;
98
99 /**
100  * Classloader that uses a specified {@link Policy} object rather than system
101  * {@link Policy} object.
102  *
103  * <p>Note that parts of this were cloned from other projects</p>
104  *
105  * @author Peter Donald
106  */

107 class PolicyClassLoader
108     extends URLClassLoader JavaDoc
109     implements LogEnabled
110 {
111     ///Policy to use to define permissions for classes loaded in classloader
112
private final Policy JavaDoc m_policy;
113
114     ///Logger to use when reporting information
115
private Logger m_logger;
116
117     /**
118      * Construct a ClassLoader using specified URLs, parent ClassLoader and
119      * Policy object.
120      *
121      * @param urls the URLs to load resources from
122      * @param parent the parent ClassLoader
123      * @param policy the Policy object
124      */

125     PolicyClassLoader( final URL JavaDoc[] urls,
126                        final ClassLoader JavaDoc parent,
127                        final Policy JavaDoc policy )
128     {
129         super( urls, parent );
130         if( null == policy )
131         {
132             throw new NullPointerException JavaDoc( "policy" );
133         }
134         m_policy = policy;
135     }
136
137     public void enableLogging( final Logger logger )
138     {
139         m_logger = logger;
140     }
141
142     /**
143      * Overide findClass to log debugging information indicating that a class is
144      * being loaded from application ClassLoader.
145      *
146      * @param name the name of class ot load
147      * @return the Class loaded
148      * @throws ClassNotFoundException if can not find class
149      */

150     protected Class JavaDoc findClass( final String JavaDoc name )
151         throws ClassNotFoundException JavaDoc
152     {
153         if( m_logger.isDebugEnabled() )
154         {
155             m_logger.debug( "findClass(" + name + ")" );
156         }
157         return super.findClass( name );
158     }
159
160     /**
161      * Overide so we can have a per-application security policy with no
162      * side-effects to other applications.
163      *
164      * @param codeSource the codeSource to get permissions for
165      * @return the PermissionCollection
166      */

167     protected PermissionCollection JavaDoc getPermissions( final CodeSource JavaDoc codeSource )
168     {
169         if( m_logger.isDebugEnabled() )
170         {
171             m_logger.debug( "getPermissions(" + codeSource + ")" );
172         }
173         return m_policy.getPermissions( codeSource );
174     }
175
176     /**
177      * Return an enumeration of {@link URL}s representing all of the resources
178      * with the given name. If no resources with this name are found, return an
179      * empty enumeration.
180      *
181      * <p>Note that this method is overidden to provide debugging
182      * information.</p>
183      *
184      * @param name the name of resource to look for
185      * @return the Enumeration of resources
186      * @throws IOException if an input/output error occurs
187      */

188     public Enumeration JavaDoc findResources( final String JavaDoc name )
189         throws IOException JavaDoc
190     {
191         if( m_logger.isDebugEnabled() )
192         {
193             m_logger.debug( "findResources(" + name + ")" );
194         }
195
196         return super.findResources( name );
197     }
198
199     /**
200      * Find the resource in the ClassLoader. Return a {@link URL} object if
201      * found, otherwise return null if this resource cannot be found.
202      *
203      * <p>Note that this method is overidden to provide debugging
204      * information.</p>
205      *
206      * @param name the name of resource to look for
207      * @return the URL if found, else null
208      */

209     public URL JavaDoc findResource( final String JavaDoc name )
210     {
211         if( m_logger.isDebugEnabled() )
212         {
213             m_logger.debug( "findResource(" + name + ")" );
214         }
215
216         final URL JavaDoc url = super.findResource( name );
217
218         if( m_logger.isDebugEnabled() )
219         {
220             if( null != url )
221             {
222                 m_logger.debug( "Resource " + name + " located (" + url + ")" );
223             }
224             else
225             {
226                 m_logger.debug( "Resource " + name + " not located" );
227             }
228         }
229
230         return url;
231     }
232 }
233
Popular Tags