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