1 23 24 package org.objectweb.jorm.util.io.lib; 25 26 import org.objectweb.jorm.util.io.api.PathExplorer; 27 import org.objectweb.jorm.util.api.Loggable; 28 import org.objectweb.util.monolog.api.BasicLevel; 29 import org.objectweb.util.monolog.api.Logger; 30 import org.objectweb.util.monolog.api.LoggerFactory; 31 32 import java.util.Iterator ; 33 import java.util.Collection ; 34 import java.util.ArrayList ; 35 import java.util.Enumeration ; 36 import java.io.InputStream ; 37 import java.io.OutputStream ; 38 import java.io.FileInputStream ; 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.util.jar.JarFile ; 42 import java.util.zip.ZipEntry ; 43 44 49 public class DirJavaExplorer implements PathExplorer, Loggable { 50 53 private Collection paths; 54 private Logger logger; 55 private LoggerFactory loggerFactory; 56 57 static final String LOGGER_NAME = "org.objectweb.jorm.io.pathexplorer"; 58 static String fileSeparator = System.getProperty("file.separator"); 59 static String pathSeparator = System.getProperty("path.separator"); 60 61 64 public DirJavaExplorer() { 65 paths = new ArrayList (); 66 } 67 68 public void setLogger(Logger logger) { 69 this.logger = logger; 70 } 71 72 75 public Logger getLogger() { 76 return logger; 77 } 78 79 82 public LoggerFactory getLoggerFactory() { 83 return loggerFactory; 84 } 85 86 91 public void setLoggerFactory(LoggerFactory loggerfactory) { 92 this.loggerFactory = loggerFactory; 93 if (logger == null && this.loggerFactory != null) 94 logger = loggerFactory.getLogger(LOGGER_NAME); 95 } 96 97 103 public void addPath(String cpath) { 104 int previous, current; 105 previous = 0; 106 current = cpath.indexOf(pathSeparator, previous); 107 while (current != -1) { 108 paths.add(cpath.substring(previous, current)); 109 110 previous = current + 1; 111 current = cpath.indexOf(pathSeparator, previous); 112 } 113 paths.add(cpath.substring(previous)); 114 } 115 116 121 public void addPath(Collection path) { 122 paths.addAll(path); 123 } 124 125 132 public InputStream getInputStream(String file) { 133 if (logger != null) 134 logger.log(BasicLevel.DEBUG, "Search " + file); 135 try { 136 File f = null; 137 for (Iterator it = paths.iterator(); it.hasNext();) { 138 String path = (String ) it.next(); 139 if (logger != null) 140 logger.log(BasicLevel.DEBUG, "Try with " + path); 141 File p = new File (path); 142 if (p.exists()) { 143 if (p.isDirectory()) { 144 f = new File (p, file); 145 if (f.exists()) { 146 if (logger != null) 147 logger.log(BasicLevel.DEBUG, "Found " + f.getAbsolutePath()); 148 return new FileInputStream (f); 149 } 150 } else if (p.isFile()) { 151 JarFile jarfile = null; 152 try { 153 jarfile = new JarFile (path); 154 ZipEntry zipentry = jarfile.getEntry(file); 155 if (zipentry == null) 156 zipentry = jarfile.getEntry(file.replace('\\', '/')); 157 if (zipentry == null) 158 zipentry = jarfile.getEntry(file.replace('/', '\\')); 159 if (zipentry != null) { 160 if (logger != null) 161 logger.log(BasicLevel.DEBUG, "Found " + file + " in " + path); 162 return jarfile.getInputStream(zipentry); 163 } else { 164 if (logger != null) { 165 logger.log(BasicLevel.DEBUG, "Entry not Found " + file + " in jar file " + path); 166 for (Enumeration e = jarfile.entries(); e.hasMoreElements();) { 167 logger.log(BasicLevel.DEBUG, "entry: " + e.nextElement()); 168 } 169 } 170 } 171 } catch (IOException e) { 172 if (logger != null) 173 logger.log(BasicLevel.WARN, "Unmanaged file entry: " + path); 174 } 175 } else { 176 if (logger != null) 177 logger.log(BasicLevel.WARN, "Unmanaged path entry: " + path); 178 } 179 } else { 180 System.out.println("WARN: Unreachable path entry: " + path); 181 } 182 } 183 f = new File (file); 184 if (f.exists()) { 185 if (logger != null) 186 logger.log(BasicLevel.DEBUG, "Found " + f.getAbsolutePath()); 187 return new FileInputStream (f); 188 } 189 190 return null; 191 } catch (Exception e) { 192 if (logger != null) 193 logger.log(BasicLevel.ERROR, "", e); 194 return null; 195 } 196 } 197 198 205 public OutputStream getOutputStream(String file) { 206 return null; 207 } 208 209 public String getClassPath() { 210 String result = ""; 211 boolean first = true; 212 for (Iterator it = paths.iterator(); it.hasNext();) { 213 result += (first ? "" : File.pathSeparator) + it.next(); 214 first = false; 215 } 216 return result; 217 } 218 } 219 | Popular Tags |