Source Code Cross Referenced for ClassLoaderUtil.java in  » Web-Framework » SiteMesh » com » opensymphony » module » sitemesh » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
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 Source Code / Java Documentation » Web Framework » SiteMesh » com.opensymphony.module.sitemesh.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.opensymphony.module.sitemesh.util;
002:
003:        import java.net.URL;
004:        import java.io.InputStream;
005:        import java.io.IOException;
006:
007:        /**
008:         * This class is extremely useful for loading resources and classes in a fault tolerant manner
009:         * that works across different applications servers.
010:         *
011:         * It has come out of many months of frustrating use of multiple application servers at Atlassian,
012:         * please don't change things unless you're sure they're not going to break in one server or another!
013:         */
014:        public class ClassLoaderUtil {
015:
016:            /**
017:             * Load a given resource.
018:             *
019:             * This method will try to load the resource using the following methods (in order):
020:             * <ul>
021:             *  <li>From Thread.currentThread().getContextClassLoader()
022:             *  <li>From ClassLoaderUtil.class.getClassLoader()
023:             *  <li>callingClass.getClassLoader()
024:             * </ul>
025:             *
026:             * @param resourceName The name of the resource to load
027:             * @param callingClass The Class object of the calling object
028:             */
029:            public static URL getResource(String resourceName,
030:                    Class callingClass) {
031:                URL url = Thread.currentThread().getContextClassLoader()
032:                        .getResource(resourceName);
033:
034:                if (url == null) {
035:                    url = ClassLoaderUtil.class.getClassLoader().getResource(
036:                            resourceName);
037:                }
038:
039:                if (url == null) {
040:                    ClassLoader cl = callingClass.getClassLoader();
041:
042:                    if (cl != null) {
043:                        url = cl.getResource(resourceName);
044:                    }
045:                }
046:
047:                if ((url == null) && (resourceName != null)
048:                        && (resourceName.charAt(0) != '/')) {
049:                    return getResource('/' + resourceName, callingClass);
050:                }
051:
052:                return url;
053:            }
054:
055:            /**
056:             * This is a convenience method to load a resource as a stream.
057:             *
058:             * The algorithm used to find the resource is given in getResource()
059:             *
060:             * @param resourceName The name of the resource to load
061:             * @param callingClass The Class object of the calling object
062:             */
063:            public static InputStream getResourceAsStream(String resourceName,
064:                    Class callingClass) {
065:                URL url = getResource(resourceName, callingClass);
066:
067:                try {
068:                    return (url != null) ? url.openStream() : null;
069:                } catch (IOException e) {
070:                    return null;
071:                }
072:            }
073:
074:            /**
075:             * Load a class with a given name.
076:             *
077:             * It will try to load the class in the following order:
078:             * <ul>
079:             *  <li>From Thread.currentThread().getContextClassLoader()
080:             *  <li>Using the basic Class.forName()
081:             *  <li>From ClassLoaderUtil.class.getClassLoader()
082:             *  <li>From the callingClass.getClassLoader()
083:             * </ul>
084:             *
085:             * @param className The name of the class to load
086:             * @param callingClass The Class object of the calling object
087:             * @throws ClassNotFoundException If the class cannot be found anywhere.
088:             */
089:            public static Class loadClass(String className, Class callingClass)
090:                    throws ClassNotFoundException {
091:                try {
092:                    return Thread.currentThread().getContextClassLoader()
093:                            .loadClass(className);
094:                } catch (ClassNotFoundException e) {
095:                    try {
096:                        return Class.forName(className);
097:                    } catch (ClassNotFoundException ex) {
098:                        try {
099:                            return ClassLoaderUtil.class.getClassLoader()
100:                                    .loadClass(className);
101:                        } catch (ClassNotFoundException exc) {
102:                            return callingClass.getClassLoader().loadClass(
103:                                    className);
104:                        }
105:                    }
106:                }
107:            }
108:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.