2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
23 import java.net.URLClassLoader;
24 import java.util.Locale;
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
28 import org.eclipse.jetty.util.resource.Resource;
30 /* ------------------------------------------------------------ */
31 /** ClassLoader Helper.
32 * This helper class allows classes to be loaded either from the
33 * Thread's ContextClassLoader, the classloader of the derived class
34 * or the system ClassLoader.
37 * public class MyClass {
40 * Class c=Loader.loadClass(this.getClass(),classname);
48 /* ------------------------------------------------------------ */
49 public static URL getResource(Class<?> loadClass,String name)
52 ClassLoader context_loader=Thread.currentThread().getContextClassLoader();
53 if (context_loader!=null)
54 url=context_loader.getResource(name);
56 if (url==null && loadClass!=null)
58 ClassLoader load_loader=loadClass.getClassLoader();
59 if (load_loader!=null && load_loader!=context_loader)
60 url=load_loader.getResource(name);
64 url=ClassLoader.getSystemResource(name);
69 /* ------------------------------------------------------------ */
75 * @throws ClassNotFoundException
77 @SuppressWarnings("rawtypes")
78 public static Class loadClass(Class loadClass,String name)
79 throws ClassNotFoundException
81 ClassNotFoundException ex=null;
83 ClassLoader context_loader=Thread.currentThread().getContextClassLoader();
84 if (context_loader!=null )
86 try { c=context_loader.loadClass(name); }
87 catch (ClassNotFoundException e) {ex=e;}
90 if (c==null && loadClass!=null)
92 ClassLoader load_loader=loadClass.getClassLoader();
93 if (load_loader!=null && load_loader!=context_loader)
95 try { c=load_loader.loadClass(name); }
96 catch (ClassNotFoundException e) {if(ex==null)ex=e;}
102 try { c=Class.forName(name); }
103 catch (ClassNotFoundException e)
116 /* ------------------------------------------------------------ */
117 public static ResourceBundle getResourceBundle(Class<?> loadClass,String name,boolean checkParents, Locale locale)
118 throws MissingResourceException
120 MissingResourceException ex=null;
121 ResourceBundle bundle =null;
122 ClassLoader loader=Thread.currentThread().getContextClassLoader();
123 while (bundle==null && loader!=null )
125 try { bundle=ResourceBundle.getBundle(name, locale, loader); }
126 catch (MissingResourceException e) {if(ex==null)ex=e;}
127 loader=(bundle==null&&checkParents)?loader.getParent():null;
130 loader=loadClass==null?null:loadClass.getClassLoader();
131 while (bundle==null && loader!=null )
133 try { bundle=ResourceBundle.getBundle(name, locale, loader); }
134 catch (MissingResourceException e) {if(ex==null)ex=e;}
135 loader=(bundle==null&&checkParents)?loader.getParent():null;
140 try { bundle=ResourceBundle.getBundle(name, locale); }
141 catch (MissingResourceException e) {if(ex==null)ex=e;}
150 /* ------------------------------------------------------------ */
152 * Generate the classpath (as a string) of all classloaders
153 * above the given classloader.
155 * This is primarily used for jasper.
156 * @return the system class path
158 public static String getClassPath(ClassLoader loader) throws Exception
160 StringBuilder classpath=new StringBuilder();
161 while (loader != null && (loader instanceof URLClassLoader))
163 URL[] urls = ((URLClassLoader)loader).getURLs();
166 for (int i=0;i<urls.length;i++)
168 Resource resource = Resource.newResource(urls[i]);
169 File file=resource.getFile();
170 if (file!=null && file.exists())
172 if (classpath.length()>0)
173 classpath.append(File.pathSeparatorChar);
174 classpath.append(file.getAbsolutePath());
178 loader = loader.getParent();
180 return classpath.toString();