View Javadoc

1   /*
2    * Copyright 2006-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.core.impl.resourceloader;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.core.api.resourceloader.ResourceLoader;
20  import org.kuali.rice.core.api.resourceloader.ResourceLoaderException;
21  
22  /**
23   * A class for {@link org.kuali.rice.core.api.resourceloader.ResourceLoader} related utilities.
24   *
25   * @author Kuali Rice Team (rice.collab@kuali.org)
26   */
27  public final class ResourceLoaderUtil {
28      private static final Logger LOG = Logger.getLogger(ResourceLoaderUtil.class);
29      
30  	private ResourceLoaderUtil() {
31  		throw new UnsupportedOperationException("do not call");
32  	}
33  
34      /**
35       * Instantiates className class via no-arg constructor, and returns a proxy
36       * that wraps invocations with the specified classLoader as the context classloader
37       * @param className the class to instantiate
38       * @param classLoader the classLoader to set as the context classloader
39       * @return a proxy that wraps an instance with code that sets and unsets the appropriate context classloader
40       */
41  	public static Object createObject(String className, ClassLoader classLoader) {
42  		try {
43  			Class theClass = Class.forName(className, true, classLoader);
44  			return ContextClassLoaderProxy.wrap(theClass.newInstance());
45  		} catch (ClassNotFoundException e) {
46              LOG.error("Error instantiating class '" + className + "' in classloader " + classLoader, e);
47              return null;
48  		} catch (InstantiationException e) {
49  			throw new ResourceLoaderException(e);
50  		} catch (IllegalAccessException e) {
51  			throw new ResourceLoaderException(e);
52  		}
53  	}
54  
55  }