Source Code Cross Referenced for Loader.java in  » Web-Services-AXIS2 » kernal » org » apache » axis2 » 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 Services AXIS2 » kernal » org.apache.axis2.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */
019:
020:        package org.apache.axis2.util;
021:
022:        import org.apache.axis2.java.security.AccessController;
023:        import org.apache.commons.logging.Log;
024:        import org.apache.commons.logging.LogFactory;
025:
026:        import java.lang.reflect.InvocationTargetException;
027:        import java.net.URL;
028:        import java.security.PrivilegedAction;
029:        import java.io.InputStream;
030:
031:        /**
032:         * Loads resources (or images) from various sources.
033:         */
034:        public class Loader {
035:            private static final Log log = LogFactory.getLog(Loader.class);
036:
037:            /**
038:             * Searches for <code>resource</code> in different
039:             * places. The search order is as follows:
040:             * <ol>
041:             * <p><li>Search for <code>resource</code> using the thread context
042:             * class loader under Java2. If that fails, search for
043:             * <code>resource</code> using the class loader that loaded this
044:             * class (<code>Loader</code>).
045:             * <p><li>Try one last time with
046:             * <code>ClassLoader.getSystemResource(resource)</code>, that is is
047:             * using the system class loader in JDK 1.2 and virtual machine's
048:             * built-in class loader in JDK 1.1.
049:             * </ol>
050:             * <p/>
051:             *
052:             * @param resource
053:             * @return Returns URL
054:             */
055:            static public URL getResource(String resource) {
056:                ClassLoader classLoader = null;
057:                URL url = null;
058:                try {
059:                    // We could not find resource. Ler us now try with the
060:                    // classloader that loaded this class.
061:                    classLoader = getTCL();
062:                    if (classLoader != null) {
063:                        log.debug("Trying to find [" + resource + "] using "
064:                                + classLoader + " class loader.");
065:                        url = classLoader.getResource(resource);
066:                        if (url != null) {
067:                            return url;
068:                        }
069:                    }
070:                } catch (Throwable t) {
071:                    log
072:                            .warn(
073:                                    "Caught Exception while in Loader.getResource. This may be innocuous.",
074:                                    t);
075:                }
076:
077:                // Last ditch attempt: get the resource from the class path. It
078:                // may be the case that clazz was loaded by the Extentsion class
079:                // loader which the parent of the system class loader. Hence the
080:                // code below.
081:                log.debug("Trying to find [" + resource
082:                        + "] using ClassLoader.getSystemResource().");
083:                return ClassLoader.getSystemResource(resource);
084:            }
085:
086:            /**
087:             * Gets the resource with the specified class loader.
088:             *
089:             * @param loader
090:             * @param resource
091:             * @return Returns URL.
092:             * @throws ClassNotFoundException
093:             */
094:            static public URL getResource(ClassLoader loader, String resource)
095:                    throws ClassNotFoundException {
096:                URL url = null;
097:                try {
098:                    if (loader != null) {
099:                        log.debug("Trying to find [" + resource + "] using "
100:                                + loader + " class loader.");
101:                        url = loader.getResource(resource);
102:                        if (url != null) {
103:                            return url;
104:                        }
105:                    }
106:                } catch (Throwable t) {
107:                    log
108:                            .warn(
109:                                    "Caught Exception while in Loader.getResource. This may be innocuous.",
110:                                    t);
111:                }
112:                return getResource(resource);
113:            }
114:
115:            /**
116:             * Searches for <code>resource</code> in different
117:             * places. The search order is as follows:
118:             * <ol>
119:             * <p><li>Search for <code>resource</code> using the thread context
120:             * class loader under Java2. If that fails, search for
121:             * <code>resource</code> using the class loader that loaded this
122:             * class (<code>Loader</code>).
123:             * <p><li>Try one last time with
124:             * <code>ClassLoader.getSystemResourceAsStream(resource)</code>, that is is
125:             * using the system class loader in JDK 1.2 and virtual machine's
126:             * built-in class loader in JDK 1.1.
127:             * </ol>
128:             * <p/>
129:             *
130:             * @param resource
131:             * @return Returns URL
132:             */
133:            static public InputStream getResourceAsStream(String resource) {
134:                ClassLoader classLoader = null;
135:                try {
136:                    // Let's try the Thread Context Class Loader
137:                    classLoader = getTCL();
138:                    if (classLoader != null) {
139:                        log.debug("Trying to find [" + resource + "] using "
140:                                + classLoader + " class loader.");
141:                        InputStream is = classLoader
142:                                .getResourceAsStream(resource);
143:                        if (is != null) {
144:                            return is;
145:                        }
146:                    }
147:                } catch (Throwable t) {
148:                    log
149:                            .warn(
150:                                    "Caught Exception while in Loader.getResourceAsStream. This may be innocuous.",
151:                                    t);
152:                }
153:
154:                try {
155:                    // We could not find resource. Ler us now try with the
156:                    // classloader that loaded this class.
157:                    classLoader = Loader.class.getClassLoader();
158:                    if (classLoader != null) {
159:                        log.debug("Trying to find [" + resource + "] using "
160:                                + classLoader + " class loader.");
161:                        InputStream is = classLoader
162:                                .getResourceAsStream(resource);
163:                        if (is != null) {
164:                            return is;
165:                        }
166:                    }
167:                } catch (Throwable t) {
168:                    log
169:                            .warn(
170:                                    "Caught Exception while in Loader.getResourceAsStream. This may be innocuous.",
171:                                    t);
172:                }
173:
174:                // Last ditch attempt: get the resource from the class path. It
175:                // may be the case that clazz was loaded by the Extentsion class
176:                // loader which the parent of the system class loader. Hence the
177:                // code below.
178:                log.debug("Trying to find [" + resource
179:                        + "] using ClassLoader.getSystemResourceAsStream().");
180:                return ClassLoader.getSystemResourceAsStream(resource);
181:            }
182:
183:            /**
184:             * Gets the resource with the specified class loader.
185:             *
186:             * @param loader
187:             * @param resource
188:             * @return Returns URL.
189:             * @throws ClassNotFoundException
190:             */
191:            static public InputStream getResourceAsStream(ClassLoader loader,
192:                    String resource) throws ClassNotFoundException {
193:                try {
194:                    if (loader != null) {
195:                        log.debug("Trying to find [" + resource + "] using "
196:                                + loader + " class loader.");
197:                        InputStream is = loader.getResourceAsStream(resource);
198:                        if (is != null) {
199:                            return is;
200:                        }
201:                    }
202:                } catch (Throwable t) {
203:                    log
204:                            .warn(
205:                                    "Caught Exception while in Loader.getResource. This may be innocuous.",
206:                                    t);
207:                }
208:                return getResourceAsStream(resource);
209:            }
210:
211:            /**
212:             * Gets the thread context class loader.
213:             *
214:             * @return Returns ClassLoader.
215:             * @throws IllegalAccessException
216:             * @throws InvocationTargetException
217:             */
218:            static public ClassLoader getTCL() throws IllegalAccessException,
219:                    InvocationTargetException {
220:                return (ClassLoader) AccessController
221:                        .doPrivileged(new PrivilegedAction() {
222:                            public Object run() {
223:                                return Thread.currentThread()
224:                                        .getContextClassLoader();
225:                            }
226:                        });
227:            }
228:
229:            /**
230:             * Loads the specified classloader and then falls back to the loadClass.
231:             *
232:             * @param loader
233:             * @param clazz
234:             * @return Returns Class.
235:             * @throws ClassNotFoundException
236:             */
237:            static public Class loadClass(ClassLoader loader, String clazz)
238:                    throws ClassNotFoundException {
239:                try {
240:                    if (loader != null) {
241:                        Class c = loader.loadClass(clazz);
242:                        if (c != null) {
243:                            return c;
244:                        }
245:                    }
246:                } catch (UnsupportedClassVersionError e) {
247:                    log.error(e.getMessage(), e);
248:                    throw e;
249:                } catch (Throwable e) {
250:                    log.debug(e);
251:                }
252:                return loadClass(clazz);
253:            }
254:
255:            /**
256:             * If running under JDK 1.2, loads the specified class using the
257:             * <code>Thread</code> <code>contextClassLoader</code> . If that
258:             * fails, try Class.forname.
259:             * <p/>
260:             *
261:             * @param clazz
262:             * @return Returns Class.
263:             * @throws ClassNotFoundException
264:             */
265:            static public Class loadClass(String clazz)
266:                    throws ClassNotFoundException {
267:                try {
268:                    ClassLoader tcl = getTCL();
269:
270:                    if (tcl != null) {
271:                        Class c = tcl.loadClass(clazz);
272:                        if (c != null) {
273:                            return c;
274:                        }
275:                    }
276:                } catch (UnsupportedClassVersionError e) {
277:                    log.error(e.getMessage(), e);
278:                    throw e;
279:                } catch (Throwable e) {
280:                    log.debug(e);
281:                }
282:                // we reached here because tcl was null or because of a
283:                // security exception, or because clazz could not be loaded...
284:                // In any case we now try one more time
285:                return Class.forName(clazz);
286:            }
287:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.