1 /** 2 * The utillib library. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2003 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * The utillib library is free software; you can redistribute 8 * it and/or modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * The utillib library is distributed in the hope that it will 13 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with utillib library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 package free.util; 23 24 import java.net.URL; 25 import java.net.MalformedURLException; 26 import java.io.InputStream; 27 import java.io.IOException; 28 29 30 /** 31 * A class loader which loads classes and resources from a specified URL. 32 */ 33 34 public class URLClassLoader extends ChildClassLoader{ 35 36 37 38 /** 39 * The URL at which we look for classes and resources. 40 */ 41 42 private final URL url; 43 44 45 46 /** 47 * Creates a new <code>URLClassLoader</code> which loads classes and resources 48 * from the specified URL and the specified parent 49 * <code>ChildClassLoader</code> to which loading is delegated if a class or a 50 * resource can't be found. 51 */ 52 URLClassLoader(URL url, ChildClassLoader parent)53 public URLClassLoader(URL url, ChildClassLoader parent){ 54 super(parent); 55 56 if (url == null) 57 throw new IllegalArgumentException("The specified URL may not be null"); 58 59 this.url = url; 60 } 61 62 63 /** 64 * Creates a new <code>URLClassLoader</code> which loads classes and resources 65 * from the specified URL. 66 */ 67 URLClassLoader(URL url)68 public URLClassLoader(URL url){ 69 if (url == null) 70 throw new IllegalArgumentException("The specified URL may not be null"); 71 72 this.url = url; 73 } 74 75 76 77 /** 78 * Returns an <code>InputStream</code> for reading the resource with the 79 * specified name. 80 */ 81 getResourceAsStreamImpl(String name)82 protected InputStream getResourceAsStreamImpl(String name){ 83 try{ 84 URL resourceURL = new URL(url, name); 85 return resourceURL.openStream(); 86 } catch (IOException e){ 87 return null; 88 } 89 } 90 91 92 93 /** 94 * Returns a <code>URL</code> pointing to the resource with the specified 95 * name. 96 */ 97 getResourceImpl(String name)98 protected URL getResourceImpl(String name){ 99 try{ 100 return new URL(url, name); 101 } catch (MalformedURLException e){return null;} 102 } 103 104 105 106 } 107