~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/serialization/ClassLoaderUtility.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.remoting.serialization;
23
 
 
24
 
import java.security.AccessController;
25
 
import java.security.PrivilegedAction;
26
 
 
27
 
/**
28
 
 * @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
29
 
 */
30
 
public class ClassLoaderUtility
31
 
{
32
 
 
33
 
   /**
34
 
    * Tries to load the class from the current thread's context class loader. If
35
 
    * not successful, tries to load the class from the current instance.
36
 
    *
37
 
    * @param classname Desired class.
38
 
    * @param clazz     Class object used to obtain a class loader
39
 
    *                  if no context class loader is available.
40
 
    * @return Class, or null on failure.
41
 
    */
42
 
   public static Class loadClass(String classname, final Class clazz) throws ClassNotFoundException
43
 
   {
44
 
      ClassLoader loader;
45
 
 
46
 
      try
47
 
      {
48
 
         loader = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
49
 
         {
50
 
            public Object run()
51
 
            {
52
 
               return Thread.currentThread().getContextClassLoader();
53
 
            }
54
 
         });
55
 
         if (loader != null)
56
 
         {
57
 
            return Class.forName(classname, false, loader);
58
 
         }
59
 
      }
60
 
      catch (Throwable t)
61
 
      {
62
 
      }
63
 
 
64
 
      if (clazz != null)
65
 
      {
66
 
         try
67
 
         {
68
 
            loader = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
69
 
            {
70
 
               public Object run()
71
 
               {
72
 
                  return clazz.getClassLoader();
73
 
               }
74
 
            });
75
 
            if (loader != null)
76
 
            {
77
 
               return Class.forName(classname, false, loader);
78
 
            }
79
 
         }
80
 
         catch (Throwable t)
81
 
         {
82
 
         }
83
 
      }
84
 
 
85
 
      try
86
 
      {
87
 
         loader = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
88
 
         {
89
 
            public Object run()
90
 
            {
91
 
               return ClassLoader.getSystemClassLoader();
92
 
            }
93
 
         });
94
 
         if (loader != null)
95
 
         {
96
 
            return Class.forName(classname, false, loader);
97
 
         }
98
 
      }
99
 
      catch (Throwable t)
100
 
      {
101
 
      }
102
 
 
103
 
      throw new ClassNotFoundException(classname);
104
 
   }
105
 
 
106
 
 
107
 
   /**
108
 
    * Tries to load the class from the passed class' classloader, then current thread's context class loader.
109
 
    *
110
 
    * @param clazz     Class object used to obtain a class loader
111
 
    *                  if no context class loader is available.
112
 
    * @param classname Desired class.
113
 
    * @return Class, or null on failure.
114
 
    */
115
 
   public static Class loadClass(final Class clazz, String classname) throws ClassNotFoundException
116
 
   {
117
 
      ClassLoader loader;
118
 
 
119
 
      if (clazz != null)
120
 
      {
121
 
         try
122
 
         {
123
 
            loader = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
124
 
            {
125
 
               public Object run()
126
 
               {
127
 
                  return clazz.getClassLoader();
128
 
               }
129
 
            });
130
 
            
131
 
            if (loader != null)
132
 
            {
133
 
               return Class.forName(classname, false, loader);
134
 
            }
135
 
         }
136
 
         catch (Throwable t)
137
 
         {
138
 
         }
139
 
      }
140
 
 
141
 
      try
142
 
      {
143
 
         loader = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
144
 
         {
145
 
            public Object run()
146
 
            {
147
 
               return Thread.currentThread().getContextClassLoader();
148
 
            }
149
 
         });
150
 
         if (loader != null)
151
 
         {
152
 
            return Class.forName(classname, false, loader);
153
 
         }
154
 
      }
155
 
      catch (Throwable t)
156
 
      {
157
 
      }
158
 
 
159
 
 
160
 
      try
161
 
      {
162
 
         loader = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
163
 
         {
164
 
            public Object run()
165
 
            {
166
 
               return ClassLoader.getSystemClassLoader();
167
 
            }
168
 
         });
169
 
         if (loader != null)
170
 
         {
171
 
            return Class.forName(classname, false, loader);
172
 
         }
173
 
      }
174
 
      catch (Throwable t)
175
 
      {
176
 
      }
177
 
 
178
 
      throw new ClassNotFoundException(classname);
179
 
   }
180
 
 
181
 
 
182
 
}