Context ClassLoader : ClassLoader « Reflection « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Reflection » ClassLoaderScreenshots 
Context ClassLoader
    

/**************************************************************************************
 * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
 * http://aspectwerkz.codehaus.org                                                    *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the LGPL license      *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/


import java.io.InputStream;
import java.net.URL;

/**
 * Utility methods dealing with the context class loader. Fail-over is provided to the default class loader.
 *
 @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
 */
public final class ContextClassLoader {

    /**
     * Loads a class starting from the given class loader (can be null, then use default class loader)
     *
     @param loader
     @param name   of class to load
     @return
     @throws ClassNotFoundException
     */
    public static Class forName(final ClassLoader loader, final String namethrows ClassNotFoundException {
        Class klass = null;
        if (loader != null) {
            klass = Class.forName(name, false, loader);
        else {
            klass = Class.forName(name, false, ClassLoader.getSystemClassLoader());
        }
        return klass;
    }


    /**
     * Loads a class from the context class loader or, if that fails, from the default class loader.
     *
     @param name is the name of the class to load.
     @return a <code>Class</code> object.
     @throws ClassNotFoundException if the class was not found.
     */
    public static Class forName(final String namethrows ClassNotFoundException {
        Class cls = null;
        try {
            cls = Class.forName(name, false, Thread.currentThread().getContextClassLoader());
        catch (Exception e) {
            cls = Class.forName(name);
        }
        return cls;
    }

    /**
     * Loads a resource from the context class loader or, if that fails, from the default class loader.
     *
     @param name is the name of the resource to load.
     @return a <code>URL</code> object.
     */
    public static URL loadResource(final String name) {
        try {
            return Thread.currentThread().getContextClassLoader().getResource(name);
        catch (Exception e) {
            return ClassLoader.class.getClassLoader().getResource(name);
        }
    }

//    /**
//     * Loads a resource from the context class loader or, if that fails, from the default class loader, as stream
//     *
//     * @param name is the name of the resource to load.
//     * @return a <code>InputStream</code> object.
//     */
//    public static InputStream getResourceAsStream(final String name) {
//        InputStream stream = null;
//        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
//        if (contextClassLoader != null) {
//            stream = contextClassLoader.getResourceAsStream(name);
//        }
//        if (stream == null) {
//            ClassLoader classLoader = ClassLoader.class.getClassLoader();
//            if (classLoader != null) {
//                stream = classLoader.getResourceAsStream(name);
//            }
//        }
//        return stream;
//    }

    /**
     * Returns the context class loader.
     *
     @return the context class loader
     */
    public static ClassLoader getLoader() {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = ClassLoader.class.getClassLoader();
        }
        return loader;
    }
}

   
    
    
    
  
Related examples in the same category
1. Determining from Where a Class Was Loaded
2. Loading a Class That Is Not on the Classpath
3. Dynamically Reloading a Modified Class
4. Get the path from where a class is loaded
5. Using the forName() method
6. Unqualified names
7. Return the context classloader.
8. Jar Class Loader
9. Zip Class Loader
10. Load class from Thread's ContextClassLoader, classloader of derived class or the system ClassLoader
11. Various Class Loader utilities
12. Load Class
13. Load classes
14. Class Finder
15. Class For Name
16. Analyze ClassLoader hierarchy for any given object or class loader
17. A class loader which loads classes using a searchlist of other classloaders
18. Equivalently to invoking Thread.currentThread().getContextClassLoader().loadClass(className); but it supports primitive types and array classes of object types or primitive types.
19. A class to simplify access to resources through the classloader
20. Get a compatible constructor for the given value type
21. Force the given class to be loaded fully.
22. does Class Exist
23. Java interpreter replacement
24. Utility to essentially do Class forName and allow configurable Classloaders.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.