001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.lang.reflect.Constructor;
018    
019    /**
020     * @author Brian Wing Shun Chan
021     */
022    public class InstanceFactory {
023    
024            public static Object newInstance(ClassLoader classLoader, String className)
025                    throws Exception {
026    
027                    return newInstance(
028                            classLoader, className, (Class<?>[])null, (Object[])null);
029            }
030    
031            public static Object newInstance(
032                            ClassLoader classLoader, String className, Class<?> parameterType,
033                            Object argument)
034                    throws Exception {
035    
036                    return newInstance(
037                            classLoader, className, new Class<?>[] {parameterType},
038                            new Object[] {argument});
039            }
040    
041            public static Object newInstance(
042                            ClassLoader classLoader, String className,
043                            Class<?>[] parameterTypes, Object[] arguments)
044                    throws Exception {
045    
046                    if (classLoader == null) {
047                            Thread currentThread = Thread.currentThread();
048    
049                            classLoader = currentThread.getContextClassLoader();
050                    }
051    
052                    Class<?> classObject = classLoader.loadClass(className);
053    
054                    if ((parameterTypes != null) && (arguments != null) &&
055                            (parameterTypes.length > 0) && (arguments.length > 0) &&
056                            (parameterTypes.length == arguments.length)) {
057    
058                            Constructor<?> constructor = classObject.getConstructor(
059                                    parameterTypes);
060    
061                            return constructor.newInstance(arguments);
062                    }
063                    else {
064                            return classObject.newInstance();
065                    }
066            }
067    
068            public static Object newInstance(String className) throws Exception {
069                    return newInstance(null, className);
070            }
071    
072            public static Object newInstance(
073                            String className, Class<?> parameterType, Object argument)
074                    throws Exception {
075    
076                    return newInstance(
077                            null, className, new Class<?>[] {parameterType},
078                            new Object[] {argument});
079            }
080    
081            public static Object newInstance(
082                            String className, Class<?>[] parameterTypes, Object[] arguments)
083                    throws Exception {
084    
085                    return newInstance(null, className, parameterTypes, arguments);
086            }
087    
088    }