1 5 package xdoclet.loader; 6 7 import java.io.File ; 8 import java.io.FileFilter ; 9 import java.io.FileInputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 13 import java.net.URL ; 14 import java.util.*; 15 import java.util.jar.JarEntry ; 16 import java.util.jar.JarFile ; 17 import java.util.zip.ZipException ; 18 19 import org.apache.commons.logging.Log; 20 21 import org.apache.tools.ant.AntClassLoader; 22 import org.xml.sax.InputSource ; 23 24 import xdoclet.util.LogUtil; 25 import xdoclet.util.Translator; 26 27 36 public class ModuleFinder 37 { 38 private final static FileFilter jarFilter = 39 new FileFilter () 40 { 41 public boolean accept(File file) 42 { 43 return file.getName().endsWith(".jar"); 44 } 45 }; 46 private static String classpath; 47 private static List modules = null; 48 49 public static String getClasspath() 50 { 51 return classpath; 52 } 53 54 60 public static File getNewestFileOnClassPath() 61 { 62 List moduleFiles = findModuleFiles(); 63 long newest = Long.MIN_VALUE; 64 File newestFile = null; 65 66 Iterator i = moduleFiles.iterator(); 67 68 while (i.hasNext()) { 69 File moduleFile = (File ) i.next(); 70 71 if (moduleFile.lastModified() >= newest) { 72 newestFile = moduleFile; 73 newest = moduleFile.lastModified(); 74 } 75 } 76 return newestFile; 77 } 78 79 public static void setClasspath(String classpath) 80 { 81 ModuleFinder.classpath = classpath; 82 } 83 84 91 public static void initClasspath(Class clazz) 92 { 93 if (System.getProperty("xdoclet.class.path") == null) { 94 try { 95 classpath = ((AntClassLoader) clazz.getClassLoader()).getClasspath(); 96 } 97 catch (ClassCastException e) { 98 classpath = System.getProperty("java.class.path"); 99 } 100 } 101 else { 102 classpath = System.getProperty("xdoclet.class.path"); 103 } 104 } 105 106 111 public static List findModules() 112 { 113 if (modules == null) { 114 modules = new ArrayList(); 115 116 Log log = LogUtil.getLog(ModuleFinder.class, "findModules"); 117 118 log.debug(Translator.getString(LoaderMessages.class, LoaderMessages.REGISTERING_MODULES)); 119 120 XDocletXmlParser parser = new XDocletXmlParser(); 121 122 List moduleFiles = findModuleFiles(); 123 Iterator moduleFileIterator = moduleFiles.iterator(); 124 125 while (moduleFileIterator.hasNext()) { 126 File file = (File ) moduleFileIterator.next(); 127 128 if (file.exists()) { 129 try { 130 InputStream xdocletXmlIs = null; 131 URL xtagsURL = null; 132 133 if (file.isDirectory()) { 134 xdocletXmlIs = new FileInputStream (new File (file, "META-INF" + File.separator + "xdoclet.xml")); 135 xtagsURL = (new File (file, "META-INF" + File.separator + "xtags.xml")).toURL(); 136 } 137 else { 138 JarFile jar = new JarFile (file); 139 JarEntry xdocletXml = jar.getJarEntry("META-INF/xdoclet.xml"); 140 141 xtagsURL = new URL (new URL ("jar:" + file.toURL() + "!/"), "META-INF/xtags.xml"); 142 143 if (xdocletXml != null) { 144 log.debug(Translator.getString(LoaderMessages.class, LoaderMessages.PARSING_XDOCLET_XML, new String []{file.getAbsolutePath()})); 145 146 xdocletXmlIs = jar.getInputStream(xdocletXml); 147 } 148 else { 149 log.debug(Translator.getString(LoaderMessages.class, LoaderMessages.SKIP_NO_XDOCLET_XML, new String []{file.getAbsolutePath()})); 150 } 151 } 152 153 if (xdocletXmlIs != null) { 154 InputSource in = new InputSource (xdocletXmlIs); 155 156 in.setSystemId(xtagsURL.toString()); 157 158 XDocletModule module = parser.parse(in); 159 160 if (module != null) { 161 module.setXTagsDefinitionURL(xtagsURL); 162 modules.add(module); 163 } 164 else { 165 log.warn(Translator.getString(LoaderMessages.class, LoaderMessages.BAD_XDOCLET_XML, new String []{file.getAbsolutePath()})); 166 } 167 } 168 } 169 catch (ZipException ze) { 170 log.warn(Translator.getString(LoaderMessages.class, LoaderMessages.INVALID_ZIP_FILE, new String []{file.getName()})); 173 } 174 catch (IOException e) { 175 throw new IllegalStateException (Translator.getString(LoaderMessages.class, LoaderMessages.LOAD_MODULE_ERROR, new String []{e.getMessage()})); 176 } 177 } 178 else { 179 log.warn(Translator.getString(LoaderMessages.class, LoaderMessages.NONEXISTANT_CLASSPATH_ENTRY, new String []{file.getAbsolutePath()})); 180 } 181 } 182 183 log.debug(Translator.getString(LoaderMessages.class, LoaderMessages.DONE_REGISTERING_MODULES, new String []{String.valueOf(modules.size())})); 184 } 185 return modules; 186 } 187 188 public static void resetFoundModules() 189 { 190 modules = null; 191 } 192 193 private static List findModuleFiles() 194 { 195 if (classpath == null) { 196 throw new IllegalStateException (Translator.getString(LoaderMessages.class, LoaderMessages.INIT_CLASSPATH_NOT_CALLED)); 197 } 198 199 ArrayList result = new ArrayList(); 200 201 StringTokenizer pathTokenizer = new StringTokenizer(classpath, System.getProperty("path.separator")); 202 203 while (pathTokenizer.hasMoreTokens()) { 204 File file = new File (pathTokenizer.nextToken()); 205 206 if (file.isDirectory()) { 207 if (new File (file, "META-INF" + File.separator + "xdoclet.xml").exists()) { 210 result.add(file); 211 } 212 } 213 else if (jarFilter.accept(file)) { 214 result.add(file); 215 } 216 } 217 return result; 218 } 219 } 220 | Popular Tags |