1 11 package org.eclipse.update.internal.jarprocessor; 12 13 import java.io.*; 14 import java.util.*; 15 import java.util.jar.*; 16 import java.util.zip.ZipException ; 17 18 22 public class Utils { 23 public static final String MARK_FILE_NAME = "META-INF/eclipse.inf"; 25 28 public static final String SIGN_EXCLUDES = "sign.excludes"; public static final String PACK_EXCLUDES = "pack.excludes"; public static final String PACK_ARGS_SUFFIX = ".pack.args"; 35 38 public static final String DEFAULT_PACK_ARGS = "pack200.default.args"; 42 45 public static final String MARK_PROPERTY = "pack200.conditioned"; public static final String MARK_EXCLUDE = "jarprocessor.exclude"; public static final String MARK_EXCLUDE_PACK = "jarprocessor.exclude.pack"; public static final String MARK_EXCLUDE_SIGN = "jarprocessor.exclude.sign"; public static final String MARK_EXCLUDE_CHILDREN = "jarprocessor.exclude.children"; 55 public static final String MARK_EXCLUDE_CHILDREN_PACK = "jarprocessor.exclude.children.pack"; 57 public static final String MARK_EXCLUDE_CHILDREN_SIGN = "jarprocessor.exclude.children.sign"; 59 public static final String PACK_ARGS = "pack200.args"; 62 public static final String PACK200_PROPERTY = "org.eclipse.update.jarprocessor.pack200"; public static final String JRE = "@jre"; public static final String PATH = "@path"; public static final String NONE = "@none"; 67 public static final String PACKED_SUFFIX = ".pack.gz"; public static final String JAR_SUFFIX = ".jar"; 70 public static final FileFilter JAR_FILTER = new FileFilter() { 71 public boolean accept(File pathname) { 72 return pathname.isFile() && pathname.getName().endsWith(".jar"); } 74 }; 75 76 public static final FileFilter PACK_GZ_FILTER = new FileFilter() { 77 public boolean accept(File pathname) { 78 return pathname.isFile() && pathname.getName().endsWith(PACKED_SUFFIX); 79 } 80 }; 81 82 public static void close(Object stream) { 83 if (stream != null) { 84 try { 85 if (stream instanceof InputStream) 86 ((InputStream) stream).close(); 87 else if (stream instanceof OutputStream) 88 ((OutputStream) stream).close(); 89 else if (stream instanceof JarFile) 90 ((JarFile) stream).close(); 91 } catch (IOException e) { 92 } 94 } 95 } 96 97 102 public static String [] getPack200Commands(String cmd) { 103 String [] locations = null; 104 String prop = System.getProperty(PACK200_PROPERTY); 105 String javaHome = System.getProperty("java.home"); if (NONE.equals(prop)) { 107 return null; 108 } else if (JRE.equals(prop)) { 109 locations = new String [] {javaHome + "/bin/" + cmd}; } else if (PATH.equals(prop)) { 111 locations = new String [] {cmd}; 112 } else if (prop == null) { 113 locations = new String [] {javaHome + "/bin/" + cmd, cmd}; } else { 115 locations = new String [] {prop + "/" + cmd}; } 117 return locations; 118 } 119 120 130 public static void transferStreams(InputStream source, OutputStream destination, boolean close) throws IOException { 131 source = new BufferedInputStream(source); 132 destination = new BufferedOutputStream(destination); 133 try { 134 byte[] buffer = new byte[8192]; 135 while (true) { 136 int bytesRead = -1; 137 if ((bytesRead = source.read(buffer)) == -1) 138 break; 139 destination.write(buffer, 0, bytesRead); 140 } 141 } finally { 142 if (close) { 143 close(source); 144 close(destination); 145 } else { 146 destination.flush(); 147 } 148 } 149 } 150 151 157 public static boolean clear(java.io.File root) { 158 boolean result = clearChildren(root); 159 try { 160 if (root.exists()) 161 result &= root.delete(); 162 } catch (Exception e) { 163 result = false; 164 } 165 return result; 166 } 167 168 175 public static boolean clearChildren(java.io.File root) { 176 boolean result = true; 177 if (root.isDirectory()) { 178 String [] list = root.list(); 179 if (list != null) 182 for (int i = 0; i < list.length; i++) 183 result &= clear(new java.io.File (root, list[i])); 184 } 185 return result; 186 } 187 188 public static Set getPackExclusions(Properties properties) { 189 if (properties == null) 190 return Collections.EMPTY_SET; 191 192 String packExcludes = properties.getProperty(PACK_EXCLUDES); 193 if (packExcludes != null) { 194 String [] excludes = toStringArray(packExcludes, ","); Set packExclusions = new HashSet(); 196 for (int i = 0; i < excludes.length; i++) { 197 packExclusions.add(excludes[i]); 198 } 199 return packExclusions; 200 } 201 return Collections.EMPTY_SET; 202 } 203 204 public static Set getSignExclusions(Properties properties) { 205 if (properties == null) 206 return Collections.EMPTY_SET; 207 String signExcludes = properties.getProperty(SIGN_EXCLUDES); 208 if (signExcludes != null) { 209 String [] excludes = toStringArray(signExcludes, ","); Set signExclusions = new HashSet(); 211 for (int i = 0; i < excludes.length; i++) { 212 signExclusions.add(excludes[i]); 213 } 214 return signExclusions; 215 } 216 return Collections.EMPTY_SET; 217 } 218 219 public static String concat(String [] array) { 220 StringBuffer buffer = new StringBuffer (); 221 for (int i = 0; i < array.length; i++) { 222 if (i > 0) 223 buffer.append(' '); 224 buffer.append(array[i]); 225 } 226 return buffer.toString(); 227 } 228 229 public static String [] toStringArray(String input, String separator) { 230 StringTokenizer tokenizer = new StringTokenizer(input, separator); 231 int count = tokenizer.countTokens(); 232 String [] result = new String [count]; 233 for (int i = 0; i < count; i++) { 234 result[i] = tokenizer.nextToken().trim(); 235 } 236 return result; 237 } 238 239 245 public static Properties getEclipseInf(File jarFile, boolean verbose) { 246 if (jarFile == null || !jarFile.exists()) { 247 if (verbose) 248 System.out.println("Failed to obtain eclipse.inf due to missing jar file: " + jarFile); 249 return null; 250 } 251 JarFile jar = null; 252 try { 253 jar = new JarFile(jarFile, false); 254 } catch (ZipException e) { 255 return null; 257 } catch (IOException e) { 258 if (verbose) { 259 System.out.println("Failed to obtain eclipse.inf due to IOException: " + jarFile); 260 e.printStackTrace(); 261 } 262 return null; 263 } 264 try { 265 JarEntry mark = jar.getJarEntry(MARK_FILE_NAME); 266 if (mark != null) { 267 InputStream in = jar.getInputStream(mark); 268 Properties props = new Properties(); 269 props.load(in); 270 in.close(); 271 return props; 272 } 273 return new Properties(); 274 } catch (IOException e) { 275 if (verbose) { 276 System.out.println("Failed to obtain eclipse.inf due to IOException: " + jarFile); 277 e.printStackTrace(); 278 } 279 return null; 280 } finally { 281 close(jar); 282 } 283 } 284 285 public static boolean shouldSkipJar(File input, boolean processAll, boolean verbose) { 286 Properties inf = getEclipseInf(input, verbose); 287 if (inf == null) { 288 return false; 290 } 291 String exclude = inf.getProperty(MARK_EXCLUDE); 292 293 if (exclude != null && Boolean.valueOf(exclude).booleanValue()) 295 return true; 296 297 if (processAll) 299 return false; 300 301 String marked = inf.getProperty(MARK_PROPERTY); 303 return !Boolean.valueOf(marked).booleanValue(); 304 } 305 306 312 public static void storeProperties(Properties props, OutputStream stream) { 313 PrintStream printStream = new PrintStream(stream); 314 printStream.print("#Processed using Jarprocessor\n"); SortedMap sorted = new TreeMap(props); 316 for (Iterator iter = sorted.keySet().iterator(); iter.hasNext();) { 317 String key = (String ) iter.next(); 318 printStream.print(key); 319 printStream.print(" = "); printStream.print(sorted.get(key)); 321 printStream.print("\n"); 322 323 } 324 printStream.flush(); 325 } 326 } 327 | Popular Tags |