KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > classloader > PolicyClassLoader


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.classloader;
9
10 import java.io.IOException JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.net.URLClassLoader JavaDoc;
14 import java.security.CodeSource JavaDoc;
15 import java.security.PermissionCollection JavaDoc;
16 import java.security.Policy JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import org.apache.avalon.framework.logger.LogEnabled;
19 import org.apache.avalon.framework.logger.Logger;
20
21 /**
22  * Classloader that uses a specified {@link Policy} object
23  * rather than system {@link Policy} object.
24  *
25  * <p>Note that parts of this were cloned from other projects</p>
26  *
27  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
28  */

29 class PolicyClassLoader
30     extends URLClassLoader JavaDoc
31     implements LogEnabled
32 {
33     ///Policy to use to define permissions for classes loaded in classloader
34
private final Policy JavaDoc m_policy;
35
36     ///Logger to use when reporting information
37
private Logger m_logger;
38
39     /**
40      * Construct a ClassLoader using specified URLs, parent
41      * ClassLoader and Policy object.
42      *
43      * @param urls the URLs to load resources from
44      * @param parent the parent ClassLoader
45      * @param policy the Policy object
46      */

47     PolicyClassLoader( final String JavaDoc[] urls,
48                        final ClassLoader JavaDoc parent,
49                        final Policy JavaDoc policy )
50         throws MalformedURLException JavaDoc
51     {
52         super( new URL JavaDoc[ 0 ], parent );
53
54         if( null == policy )
55         {
56             throw new NullPointerException JavaDoc( "policy" );
57         }
58         m_policy = policy;
59
60         for( int i = 0; i < urls.length; i++ )
61         {
62             final URL JavaDoc url = new URL JavaDoc( urls[ i ] );
63             addURL( url );
64         }
65     }
66
67     public void enableLogging( final Logger logger )
68     {
69         m_logger = logger;
70     }
71
72     protected void addURL( final URL JavaDoc url )
73     {
74         super.addURL( url );
75     }
76
77     protected final Logger getLogger()
78     {
79         return m_logger;
80     }
81
82     /**
83      * Overide findClass to log debugging information
84      * indicating that a class is being loaded from application
85      * ClassLoader.
86      *
87      * @param name the name of class ot load
88      * @return the Class loaded
89      * @throws ClassNotFoundException if can not find class
90      */

91     protected Class JavaDoc findClass( final String JavaDoc name )
92         throws ClassNotFoundException JavaDoc
93     {
94         if( getLogger().isDebugEnabled() )
95         {
96             getLogger().debug( "findClass(" + name + ")" );
97         }
98         return super.findClass( name );
99     }
100
101     /**
102      * Overide so we can have a per-application security policy with
103      * no side-effects to other applications.
104      *
105      * @param codeSource the codeSource to get permissions for
106      * @return the PermissionCollection
107      */

108     protected PermissionCollection JavaDoc getPermissions( final CodeSource JavaDoc codeSource )
109     {
110         if( getLogger().isDebugEnabled() )
111         {
112             getLogger().debug( "getPermissions(" + codeSource + ")" );
113         }
114         return m_policy.getPermissions( codeSource );
115     }
116
117     /**
118      * Return an enumeration of {@link URL}s representing all of the
119      * resources with the given name. If no resources with this name are found,
120      * return an empty enumeration.
121      *
122      * <p>Note that this method is overidden to provide debugging
123      * information.</p>
124      *
125      * @param name the name of resource to look for
126      * @return the Enumeration of resources
127      * @throws IOException if an input/output error occurs
128      */

129     public Enumeration JavaDoc findResources( final String JavaDoc name )
130         throws IOException JavaDoc
131     {
132         if( getLogger().isDebugEnabled() )
133         {
134             getLogger().debug( "findResources(" + name + ")" );
135         }
136
137         return super.findResources( name );
138     }
139
140     /**
141      * Find the resource in the ClassLoader. Return a {@link URL}
142      * object if found, otherwise return null if this resource cannot
143      * be found.
144      *
145      * <p>Note that this method is overidden to provide debugging
146      * information.</p>
147      *
148      * @param name the name of resource to look for
149      * @return the URL if found, else null
150      */

151     public URL JavaDoc findResource( final String JavaDoc name )
152     {
153         if( getLogger().isDebugEnabled() )
154         {
155             getLogger().debug( "findResource(" + name + ")" );
156         }
157
158         final URL JavaDoc url = super.findResource( name );
159
160         if( getLogger().isDebugEnabled() )
161         {
162             if( null != url )
163             {
164                 getLogger().debug( "Resource " + name + " located (" + url + ")" );
165             }
166             else
167             {
168                 getLogger().debug( "Resource " + name + " not located" );
169             }
170         }
171
172         return url;
173     }
174 }
175
Popular Tags