This project has retired. For details please refer to its Attic page.
ClassUtil xref
View Javadoc
1   /*
2    * Copyright 2001-2010 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package org.apache.juddi.v3.client;
18  
19  import java.net.URL;
20  
21  /**
22   * @author <a href="mailto:tcunning@apache.org">Tom Cunningham</a>
23   */
24  
25  public class ClassUtil {
26      
27  	public static Class<?> forName(String name, Class<?> caller)
28      	throws ClassNotFoundException
29      {
30  	    ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
31  	    if (threadClassLoader != null) {
32  	        try {
33  	            return Class.forName(name, true, threadClassLoader) ;
34  	        } catch (ClassNotFoundException cnfe) {
35  	        	if (cnfe.getException() != null) {
36  	        		throw cnfe;
37  	        	}
38  	        }
39  	    }
40      
41  	    ClassLoader callerClassLoader = caller.getClassLoader();
42  	    if (callerClassLoader != null) {
43  	        try {
44  	            return Class.forName(name, true, callerClassLoader) ;
45  	        } catch (final ClassNotFoundException cnfe) {
46  	            if (cnfe.getException() != null) {
47  	                throw cnfe ;
48  	            }
49  	        }
50  	    }
51  	    
52  	    return Class.forName(name, true, ClassLoader.getSystemClassLoader()) ;
53      }
54  	
55  	public static URL getResource(String name, Class<?> caller)
56  	{
57  		ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
58  		if (threadClassLoader != null) {
59  
60  			URL url = threadClassLoader.getResource(name);
61  
62  			if (url != null)
63  				return url;
64  
65  		}
66  
67  		ClassLoader callerClassLoader = caller.getClassLoader();
68  
69  		return callerClassLoader.getResource(name);
70  	}
71  
72  }