00001 package ca.mcgill.sable.util; 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * SableUtil, a clean room implementation of the Collection API. * 00005 * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca). * 00006 * All rights reserved. * 00007 * * 00008 * Modifications by Etienne Gagnon (gagnon@sable.mcgill.ca) are * 00009 * Copyright (C) 1998 Etienne Gagnon (gagnon@sable.mcgill.ca). All * 00010 * rights reserved. * 00011 * * 00012 * This work was done as a project of the Sable Research Group, * 00013 * School of Computer Science, McGill University, Canada * 00014 * (http://www.sable.mcgill.ca/). It is understood that any * 00015 * modification not identified as such is not covered by the * 00016 * preceding statement. * 00017 * * 00018 * This work is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Library General Public * 00020 * License as published by the Free Software Foundation; either * 00021 * version 2 of the License, or (at your option) any later version. * 00022 * * 00023 * This work is distributed in the hope that it will be useful, * 00024 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00026 * Library General Public License for more details. * 00027 * * 00028 * You should have received a copy of the GNU Library General Public * 00029 * License along with this library; if not, write to the * 00030 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * 00031 * Boston, MA 02111-1307, USA. * 00032 * * 00033 * To submit a bug report, send a comment, or get the latest news on * 00034 * this project and other Sable Research Group projects, please * 00035 * visit the web site: http://www.sable.mcgill.ca/ * 00036 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00037 00038 /* 00039 Reference Version 00040 ----------------- 00041 This is the latest official version on which this file is based. 00042 The reference version is: $SableUtilVersion: 1.11 $ 00043 00044 Change History 00045 -------------- 00046 A) Notes: 00047 00048 Please use the following template. Most recent changes should 00049 appear at the top of the list. 00050 00051 - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate] 00052 [description of modification]. 00053 00054 Any Modification flagged with "(*)" was done as a project of the 00055 Sable Research Group, School of Computer Science, 00056 McGill University, Canada (http://www.sable.mcgill.ca/). 00057 00058 You should add your copyright, using the following template, at 00059 the top of this file, along with other copyrights. 00060 00061 * * 00062 * Modifications by [name] are * 00063 * Copyright (C) [year(s)] [your name (or company)]. All rights * 00064 * reserved. * 00065 * * 00066 00067 B) Changes: 00068 00069 - Modified on July 5, 1998 by Etienne Gagnon (gagnon@sable.mcgill.ca). (*) 00070 Added getInputStreamOf(String classpath, String className). 00071 Fixed a platform dependence bug while searching zip files. 00072 Added support for jar files. 00073 00074 - Modified on June 15, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00075 First release of this file. 00076 */ 00077 00078 import java.io.*; 00079 import java.util.zip.*; 00080 import java.util.Enumeration; 00081 00082 /** 00083 * Class which provides a static method to locate any class in your CLASSPATH and returns 00084 * an InputStream to it. Does handle .jar files as well. 00085 */ 00086 00087 public class ClassLocator 00088 { 00089 private static char pathSeparator = System.getProperty("path.separator").charAt(0); 00090 private static char fileSeparator = System.getProperty("file.separator").charAt(0); 00091 00092 private ClassLocator() // No instances. 00093 { 00094 } 00095 public static InputStream getInputStreamOf(String className) throws ClassNotFoundException 00096 { 00097 return getInputStreamOf(System.getProperty("java.class.path"), className); 00098 } 00099 public static InputStream getInputStreamOf(String classPath, String className) throws ClassNotFoundException 00100 { 00101 List locations = new VectorList(); 00102 00103 // Split up the class path into locations 00104 { 00105 int sepIndex; 00106 00107 for(;;) 00108 { 00109 sepIndex = classPath.indexOf(pathSeparator); 00110 00111 if(sepIndex == -1) 00112 { 00113 locations.add(classPath); 00114 break; 00115 } 00116 00117 locations.add(classPath.substring(0, sepIndex)); 00118 00119 classPath = classPath.substring(sepIndex + 1); 00120 } 00121 } 00122 00123 // Go through each location, looking for this class 00124 { 00125 00126 for(int i = 0; i < locations.size(); i++) 00127 { 00128 String location = (String) locations.get(i); 00129 00130 if(location.endsWith(".zip") || location.endsWith(".jar")) 00131 { 00132 String fileName = className.replace('.', '/') + ".class"; 00133 try { 00134 ZipFile zipFile = new ZipFile(location); 00135 ZipEntry entry = zipFile.getEntry(fileName); 00136 00137 if(entry == null) 00138 continue; 00139 else 00140 return zipFile.getInputStream(entry); 00141 } catch(IOException e) 00142 { 00143 continue; 00144 } 00145 } 00146 else { 00147 // Default: try loading class directly 00148 00149 String fileName = className.replace('.', fileSeparator) + ".class"; 00150 String fullPath; 00151 00152 if(location.endsWith(new Character(fileSeparator).toString())) 00153 fullPath = location + fileName; 00154 else 00155 fullPath = location + fileSeparator + fileName; 00156 00157 try { 00158 File f = new File(fullPath); 00159 00160 return new FileInputStream(f); 00161 } catch(IOException e) 00162 { 00163 continue; 00164 } 00165 } 00166 00167 } 00168 } 00169 00170 throw new ClassNotFoundException(className); 00171 } 00172 }