KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > loading > ContextClassLoader


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.loading;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26
27 /**
28  * A helper for context classloading.<p>
29  *
30  * When a security manager is installed, the
31  * constructor checks for the runtime permissions
32  * &quot;getClassLoader&quot;
33  *
34  * @version <tt>$Revision: 1958 $</tt>
35  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
36  */

37 public class ContextClassLoader
38 {
39    /**
40     * Retrieve a classloader permission
41     */

42    public static final RuntimePermission JavaDoc GETCLASSLOADER = new RuntimePermission JavaDoc("getClassLoader");
43
44    /**
45     * Instantiate a new context class loader
46     */

47    public static final NewInstance INSTANTIATOR = new NewInstance();
48
49    /**
50     * Constructor.
51     *
52     * @throws SecurityException when not authroized to get the context classloader
53     */

54    /*package*/ ContextClassLoader()
55    {
56       SecurityManager JavaDoc manager = System.getSecurityManager();
57       if (manager != null)
58       {
59          manager.checkPermission(GETCLASSLOADER);
60       }
61    }
62
63    /**
64     * Retrieve the context classloader
65     *
66     * @return the context classloader
67     */

68    public ClassLoader JavaDoc getContextClassLoader()
69    {
70       return getContextClassLoader(Thread.currentThread());
71    }
72
73    /**
74     * Retrieve the context classloader for the given thread
75     *
76     * @param thread the thread
77     * @return the context classloader
78     */

79    public ClassLoader JavaDoc getContextClassLoader(final Thread JavaDoc thread)
80    {
81       return (ClassLoader JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc()
82       {
83          public Object JavaDoc run()
84          {
85             return thread.getContextClassLoader();
86          }
87       });
88    }
89
90    private static class NewInstance
91       implements PrivilegedAction JavaDoc
92    {
93       public Object JavaDoc run()
94       {
95          return new ContextClassLoader();
96       }
97    }
98 }
99
Popular Tags