KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > scheduler > TCLActions


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.varia.scheduler;
23
24 import java.security.PrivilegedAction JavaDoc;
25 import java.security.AccessController JavaDoc;
26
27 /** An encapsulation of thread context class loader PrivilegedAction for
28  * getting and setting the TCL.
29  *
30  * @author Scott.Stark@jboss.org
31  * @version $Revision: 37459 $
32  */

33 class TCLActions
34 {
35    private static class GetTCLAction implements PrivilegedAction JavaDoc
36    {
37       static PrivilegedAction JavaDoc ACTION = new GetTCLAction();
38       public Object JavaDoc run()
39       {
40          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
41          return loader;
42       }
43    }
44    private static class GetClassLoaderAction implements PrivilegedAction JavaDoc
45    {
46       Class JavaDoc c;
47       GetClassLoaderAction(Class JavaDoc c)
48       {
49          this.c = c;
50       }
51       public Object JavaDoc run()
52       {
53          ClassLoader JavaDoc loader = c.getClassLoader();
54          c = null;
55          return loader;
56       }
57    }
58    private static class SetTCLAction implements PrivilegedAction JavaDoc
59    {
60       ClassLoader JavaDoc loader;
61       SetTCLAction(ClassLoader JavaDoc loader)
62       {
63          this.loader = loader;
64       }
65       public Object JavaDoc run()
66       {
67          Thread.currentThread().setContextClassLoader(loader);
68          loader = null;
69          return null;
70       }
71    }
72
73    static ClassLoader JavaDoc getContextClassLoader()
74    {
75       ClassLoader JavaDoc loader = (ClassLoader JavaDoc) AccessController.doPrivileged(GetTCLAction.ACTION);
76       return loader;
77    }
78    static ClassLoader JavaDoc getClassLoader(Class JavaDoc c)
79    {
80       GetClassLoaderAction action = new GetClassLoaderAction(c);
81       ClassLoader JavaDoc loader = (ClassLoader JavaDoc) AccessController.doPrivileged(action);
82       return loader;
83    }
84    static void setContextClassLoader(ClassLoader JavaDoc loader)
85    {
86       PrivilegedAction JavaDoc action = new SetTCLAction(loader);
87       AccessController.doPrivileged(action);
88    }
89 }
90
Popular Tags