001/*
002 * Copyright (c) 2004-2012, Willem Cazander
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 * 
008 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009 *   following disclaimer.
010 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
011 *   the following disclaimer in the documentation and/or other materials provided with the distribution.
012 * 
013 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
014 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
015 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
016 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
018 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
019 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
020 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
021 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.x4o.xml.core.config;
025
026/**
027 * X4OLanguageClassLoader is short hand for safe class loading.
028 * 
029 * @author Willem Cazander
030 * @version 1.0 6 Aug 2012
031 */
032public class X4OLanguageClassLoader {
033        
034        /**
035         * Gets the thread classloader or the normal classloader.
036         * @return      Returns the ClassLoader.
037         */
038        public static ClassLoader getClassLoader() {
039                ClassLoader cl = Thread.currentThread().getContextClassLoader();
040                if (cl == null) {
041                        cl = X4OLanguageClassLoader.class.getClassLoader();
042                }
043                return cl;
044        }
045        
046        /**
047         * Loads a Class from the ContextClassLoader and if that is not set, then
048         * uses the class of the String className instance.
049         * 
050         * @param className     The class name to load
051         * @return      The loaded class
052         * @throws ClassNotFoundException       if class not loaded.
053         */
054        public static Class<?> loadClass(String className) throws ClassNotFoundException {
055                return getClassLoader().loadClass(className);
056        }
057
058        /**
059         * Creates new instance of clazz.
060         * @param clazz The class to make object from.
061         * @return      The object of the clazz.
062         * @throws InstantiationException       When className has no default constructor.
063         * @throws IllegalAccessException       When class loading has security error.
064         */
065        public static Object newInstance(Class<?> clazz) throws  InstantiationException, IllegalAccessException {
066                return clazz.newInstance();
067        }
068        
069        /**
070         * Creates new instance of className.
071         * @param className     The className to create object from.
072         * @return      The object of the className.
073         * @throws ClassNotFoundException       When className is not found.
074         * @throws InstantiationException       When className has no default constructor.
075         * @throws IllegalAccessException       When class loading has security error.
076         */
077        public static Object newInstance(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
078                return newInstance(loadClass(className));
079        }
080}