KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > jarprocessor > Utils


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

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 JavaDoc;
17
18 /**
19  * @author aniefer@ca.ibm.com
20  *
21  */

22 public class Utils {
23     public static final String JavaDoc MARK_FILE_NAME = "META-INF/eclipse.inf"; //$NON-NLS-1$
24

25     /*
26      * Properties found in outer pack.properties file
27      */

28     //comma separated list of jars to exclude from sigining
29
public static final String JavaDoc SIGN_EXCLUDES = "sign.excludes"; //$NON-NLS-1$
30
//comma separated list of jars to exlclude from packing
31
public static final String JavaDoc PACK_EXCLUDES = "pack.excludes"; //$NON-NLS-1$
32
//Suffix used when specifying arguments to use when running pack200 on a jar
33
public static final String JavaDoc PACK_ARGS_SUFFIX = ".pack.args"; //$NON-NLS-1$
34

35     /*
36      * Properties found in both pack.properties and eclipse.inf
37      */

38     // Default arguments to use when running pack200.
39
// Affects all jars when specified in pack.properties, affects children when specified in eclipse.inf
40
public static final String JavaDoc DEFAULT_PACK_ARGS = "pack200.default.args"; //$NON-NLS-1$
41

42     /*
43      * Properties found in eclipse.inf file
44      */

45     //This jar has been conditioned with pack200
46
public static final String JavaDoc MARK_PROPERTY = "pack200.conditioned"; //$NON-NLS-1$
47
//Exclude this jar from processing
48
public static final String JavaDoc MARK_EXCLUDE = "jarprocessor.exclude"; //$NON-NLS-1$
49
//Exclude this jar from pack200
50
public static final String JavaDoc MARK_EXCLUDE_PACK = "jarprocessor.exclude.pack"; //$NON-NLS-1$
51
//Exclude this jar from signing
52
public static final String JavaDoc MARK_EXCLUDE_SIGN = "jarprocessor.exclude.sign"; //$NON-NLS-1$
53
//Exclude this jar's children from processing
54
public static final String JavaDoc MARK_EXCLUDE_CHILDREN = "jarprocessor.exclude.children";
55     //Exclude this jar's children from pack200
56
public static final String JavaDoc MARK_EXCLUDE_CHILDREN_PACK = "jarprocessor.exclude.children.pack";
57     //Exclude this jar's children from signing
58
public static final String JavaDoc MARK_EXCLUDE_CHILDREN_SIGN = "jarprocessor.exclude.children.sign";
59     //Arguments used in pack200 for this jar
60
public static final String JavaDoc PACK_ARGS = "pack200.args"; //$NON-NLS-1$
61

62     public static final String JavaDoc PACK200_PROPERTY = "org.eclipse.update.jarprocessor.pack200"; //$NON-NLS-1$
63
public static final String JavaDoc JRE = "@jre"; //$NON-NLS-1$
64
public static final String JavaDoc PATH = "@path"; //$NON-NLS-1$
65
public static final String JavaDoc NONE = "@none"; //$NON-NLS-1$
66

67     public static final String JavaDoc PACKED_SUFFIX = ".pack.gz"; //$NON-NLS-1$
68
public static final String JavaDoc JAR_SUFFIX = ".jar"; //$NON-NLS-1$
69

70     public static final FileFilter JAR_FILTER = new FileFilter() {
71         public boolean accept(File pathname) {
72             return pathname.isFile() && pathname.getName().endsWith(".jar"); //$NON-NLS-1$
73
}
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 JavaDoc 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                 //ignore
93
}
94         }
95     }
96
97     /**
98      * get the set of commands to try to execute pack/unpack
99      * @param cmd, the command, either "pack200" or "unpack200"
100      * @return String [] or null
101      */

102     public static String JavaDoc[] getPack200Commands(String JavaDoc cmd) {
103         String JavaDoc[] locations = null;
104         String JavaDoc prop = System.getProperty(PACK200_PROPERTY);
105         String JavaDoc javaHome = System.getProperty("java.home"); //$NON-NLS-1$
106
if (NONE.equals(prop)) {
107             return null;
108         } else if (JRE.equals(prop)) {
109             locations = new String JavaDoc[] {javaHome + "/bin/" + cmd}; //$NON-NLS-1$
110
} else if (PATH.equals(prop)) {
111             locations = new String JavaDoc[] {cmd};
112         } else if (prop == null) {
113             locations = new String JavaDoc[] {javaHome + "/bin/" + cmd, cmd}; //$NON-NLS-1$
114
} else {
115             locations = new String JavaDoc[] {prop + "/" + cmd}; //$NON-NLS-1$
116
}
117         return locations;
118     }
119
120     /**
121      * Transfers all available bytes from the given input stream to the given
122      * output stream. Closes both streams if close == true, regardless of failure.
123      * Flushes the destination stream if close == false
124      *
125      * @param source
126      * @param destination
127      * @param close
128      * @throws IOException
129      */

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     /**
152      * Deletes all the files and directories from the given root down (inclusive).
153      * Returns false if we could not delete some file or an exception occurred
154      * at any point in the deletion.
155      * Even if an exception occurs, a best effort is made to continue deleting.
156      */

157     public static boolean clear(java.io.File JavaDoc root) {
158         boolean result = clearChildren(root);
159         try {
160             if (root.exists())
161                 result &= root.delete();
162         } catch (Exception JavaDoc e) {
163             result = false;
164         }
165         return result;
166     }
167
168     /**
169      * Deletes all the files and directories from the given root down, except for
170      * the root itself.
171      * Returns false if we could not delete some file or an exception occurred
172      * at any point in the deletion.
173      * Even if an exception occurs, a best effort is made to continue deleting.
174      */

175     public static boolean clearChildren(java.io.File JavaDoc root) {
176         boolean result = true;
177         if (root.isDirectory()) {
178             String JavaDoc[] list = root.list();
179             // for some unknown reason, list() can return null.
180
// Just skip the children If it does.
181
if (list != null)
182                 for (int i = 0; i < list.length; i++)
183                     result &= clear(new java.io.File JavaDoc(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 JavaDoc packExcludes = properties.getProperty(PACK_EXCLUDES);
193         if (packExcludes != null) {
194             String JavaDoc[] excludes = toStringArray(packExcludes, ","); //$NON-NLS-1$
195
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 JavaDoc signExcludes = properties.getProperty(SIGN_EXCLUDES);
208         if (signExcludes != null) {
209             String JavaDoc[] excludes = toStringArray(signExcludes, ","); //$NON-NLS-1$
210
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 JavaDoc concat(String JavaDoc[] array) {
220         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc[] toStringArray(String JavaDoc input, String JavaDoc separator) {
230         StringTokenizer tokenizer = new StringTokenizer(input, separator);
231         int count = tokenizer.countTokens();
232         String JavaDoc[] result = new String JavaDoc[count];
233         for (int i = 0; i < count; i++) {
234             result[i] = tokenizer.nextToken().trim();
235         }
236         return result;
237     }
238
239     /**
240      * Get the properties from the eclipse.inf file from the given jar. If the file is not a jar, null is returned.
241      * If the file is a jar, but does not contain an eclipse.inf file, an empty Properties object is returned.
242      * @param jarFile
243      * @return The eclipse.inf properties for the given jar file
244      */

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 JavaDoc e) {
255             //not a jar, don't bother logging this.
256
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             //not a jar, could be a pack.gz
289
return false;
290         }
291         String JavaDoc exclude = inf.getProperty(MARK_EXCLUDE);
292
293         //was marked as exclude, we should skip
294
if (exclude != null && Boolean.valueOf(exclude).booleanValue())
295             return true;
296
297         //process all was set, don't skip
298
if (processAll)
299             return false;
300
301         //otherwise, we skip if not marked marked
302
String JavaDoc marked = inf.getProperty(MARK_PROPERTY);
303         return !Boolean.valueOf(marked).booleanValue();
304     }
305
306     /**
307      * Stores the given properties in the output stream. We store the properties
308      * in sorted order so that the signing hash doesn't change if the properties didn't change.
309      * @param props
310      * @param stream
311      */

312     public static void storeProperties(Properties props, OutputStream stream) {
313         PrintStream printStream = new PrintStream(stream);
314         printStream.print("#Processed using Jarprocessor\n"); //$NON-NLS-1$
315
SortedMap sorted = new TreeMap(props);
316         for (Iterator iter = sorted.keySet().iterator(); iter.hasNext();) {
317             String JavaDoc key = (String JavaDoc) iter.next();
318             printStream.print(key);
319             printStream.print(" = "); //$NON-NLS-1$
320
printStream.print(sorted.get(key));
321             printStream.print("\n");
322
323         }
324         printStream.flush();
325     }
326 }
327
Popular Tags