View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.orchestra.lib;
20  
21  /***
22   * <p>
23   * Internal helper class for some class management.
24   * </p>
25   * <p>
26   * <b>Not for use outside of Orchestra</b>
27   * </p>
28   */
29  public final class _ClassUtils
30  {
31  	private _ClassUtils()
32  	{
33  	}
34  
35  	/***
36  	 * create a new instance for a class by its name
37  	 */
38  	public static Object newInstance(String className)
39  	{
40  		try
41  		{
42  			Class clazz = classForName(className);
43  			return clazz.newInstance();
44  		}
45  		catch(NoClassDefFoundError e)
46  		{
47  			throw new OrchestraException(e);
48  		}
49  		catch (InstantiationException e)
50  		{
51  			throw new OrchestraException(e);
52  		}
53  		catch (IllegalAccessException e)
54  		{
55  			throw new OrchestraException(e);
56  		}
57  		catch (ClassNotFoundException e)
58  		{
59  			throw new OrchestraException(e);
60  		}
61  	}
62  
63  	/***
64  	 * Lookup class using the webapp classloader first and the the classloader which loaded
65  	 * this class.
66  	 */
67  	public static Class classForName(String className) throws ClassNotFoundException
68  	{
69  		if (className == null)
70  		{
71  			throw new NullPointerException("className");
72  		}
73  
74  		try
75  		{
76  			// Try WebApp ClassLoader first
77  			return Class.forName(className,
78  				false, // do not initialize for faster startup
79  				Thread.currentThread().getContextClassLoader());
80  		}
81  		catch (ClassNotFoundException ignore)
82  		{
83  			// fallback: Try ClassLoader for this class (i.e. the myfaces.jar lib)
84  			return Class.forName(className,
85  				false, // do not initialize for faster startup
86  				_ClassUtils.class.getClassLoader());
87  		}
88  	}
89  }