001    /*
002     * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: ClassLoaderSupport.java,v 88b901bf20fb 2008/06/07 14:12:27 maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements.support;
020    
021    /**
022     * Support utilities for classloading.
023     * 
024     * @version $Revision: 88b901bf20fb $
025     */
026    public final class ClassLoaderSupport {
027    
028        private static final class SingletonHolder {
029            private static final ClassLoaderSupport INSTANCE = new ClassLoaderSupport();
030    
031            private SingletonHolder() {
032            }
033        }
034    
035        /**
036         * Default Constructor.
037         */
038        protected ClassLoaderSupport() {
039            // Empty on purporse.
040        }
041    
042        /**
043         * accessor for singleton instance.
044         * 
045         * @return an instance of this class.
046         */
047        public static ClassLoaderSupport getInstance() {
048            return ClassLoaderSupport.SingletonHolder.INSTANCE;
049        }
050    
051        /**
052         * Try to load the given lass from the current context.
053         * 
054         * @param className
055         *            name of the class to load
056         * @return a Class object for the given class if possible.
057         * @throws ClassNotFoundException
058         *             if the class could not be found.
059         * @see ClassLoader#loadClass(String)
060         */
061        public Class<?> loadClass(final String className)
062                throws ClassNotFoundException {
063            Class<?> retVal;
064            try {
065                retVal = Thread.currentThread().getContextClassLoader().loadClass(
066                        className);
067            } catch (final ClassNotFoundException e) {
068                retVal = ClassLoaderSupport.class.getClassLoader().loadClass(
069                        className);
070            }
071            return retVal;
072        }
073    
074    }