Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f2547178c4530f163311028dd0645b004aeb95f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*******************************************************************************
 * Copyright (c) 2006, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.internal.provisional.equinox.p2.jarprocessor;

import java.io.*;
import java.util.*;
import java.util.jar.*;
import org.eclipse.equinox.internal.p2.jarprocessor.*;

public class JarProcessor {
	public static final String PACKED_SUFFIX = "pack.gz"; //$NON-NLS-1$

	private List steps = new ArrayList();
	private String workingDirectory = ""; //$NON-NLS-1$
	private int depth = -1;
	private boolean verbose = false;
	private boolean processAll = false;
	private LinkedList containingInfs = new LinkedList();

	static public JarProcessor getUnpackProcessor(Properties properties) {
		if (!canPerformUnpack())
			throw new UnsupportedOperationException();
		JarProcessor processor = new JarProcessor();
		processor.addProcessStep(new UnpackStep(properties));
		return processor;
	}

	static public JarProcessor getPackProcessor(Properties properties) {
		if (!canPerformPack())
			throw new UnsupportedOperationException();
		JarProcessor processor = new JarProcessor();
		processor.addProcessStep(new PackStep(properties));
		return processor;
	}

	static public boolean canPerformPack() {
		return PackStep.canPack();
	}

	static public boolean canPerformUnpack() {
		return UnpackStep.canUnpack();
	}

	public String getWorkingDirectory() {
		return workingDirectory;
	}

	public void setWorkingDirectory(String dir) {
		if (dir != null)
			workingDirectory = dir;
	}

	public void setVerbose(boolean verbose) {
		this.verbose = verbose;
	}

	public void setProcessAll(boolean all) {
		this.processAll = all;
	}

	public void addProcessStep(IProcessStep step) {
		steps.add(step);
	}

	public void clearProcessSteps() {
		steps.clear();
	}

	public Iterator getStepIterator() {
		return steps.iterator();
	}

	/**
	 * Recreate a jar file.  The replacements map specifies entry names to be replaced, the replacements are
	 * expected to be found in directory.
	 * 
	 * @param jar - The input jar
	 * @param outputJar - the output
	 * @param replacements - map of entryName -> new entryName
	 * @param directory - location to find file for new entryName
	 * @throws IOException
	 */
	private void recreateJar(JarFile jar, JarOutputStream outputJar, Map replacements, File directory, Properties inf) throws IOException {
		InputStream in = null;
		boolean marked = false;
		try {
			Enumeration entries = jar.entries();
			for (JarEntry entry = (JarEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (JarEntry) entries.nextElement() : null) {
				File replacement = null;
				JarEntry newEntry = null;
				if (replacements.containsKey(entry.getName())) {
					String name = (String) replacements.get(entry.getName());
					replacement = new File(directory, name);
					if (name != null) {
						if (replacement.exists()) {
							try {
								in = new BufferedInputStream(new FileInputStream(replacement));
								newEntry = new JarEntry(name);
							} catch (Exception e) {
								if (verbose) {
									e.printStackTrace();
									System.out.println("Warning: Problem reading " + replacement.getPath() + ", using " + jar.getName() + File.separator + entry.getName() + " instead."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
								}
							}
						} else if (verbose) {
							System.out.println("Warning: " + replacement.getPath() + " not found, using " + jar.getName() + File.separator + entry.getName() + " instead."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
						}
					}
				}
				if (newEntry == null) {
					try {
						in = new BufferedInputStream(jar.getInputStream(entry));
						newEntry = new JarEntry(entry.getName());
					} catch (Exception e) {
						if (verbose) {
							e.printStackTrace();
							System.out.println("ERROR: problem reading " + entry.getName() + " from " + jar.getName()); //$NON-NLS-1$ //$NON-NLS-2$
						}
						continue;
					}
				}
				newEntry.setTime(entry.getTime());
				outputJar.putNextEntry(newEntry);
				if (entry.getName().equals(Utils.MARK_FILE_NAME)) {
					//The eclipse.inf file was read in earlier, don't need to reread it, just write it out now
					Utils.storeProperties(inf, outputJar);
					marked = true;
				} else {
					Utils.transferStreams(in, outputJar, false);
				}
				outputJar.closeEntry();
				in.close();

				//delete the nested jar file
				if (replacement != null) {
					replacement.delete();
				}
			}
			if (!marked) {
				JarEntry entry = new JarEntry(Utils.MARK_FILE_NAME);
				outputJar.putNextEntry(entry);
				Utils.storeProperties(inf, outputJar);
				outputJar.closeEntry();
			}
		} finally {
			Utils.close(outputJar);
			Utils.close(jar);
			Utils.close(in);
		}
	}

	private String recursionEffect(String entryName) {
		String result = null;
		for (Iterator iter = steps.iterator(); iter.hasNext();) {
			IProcessStep step = (IProcessStep) iter.next();

			result = step.recursionEffect(entryName);
			if (result != null)
				entryName = result;
		}
		return result;
	}

	private void extractEntries(JarFile jar, File tempDir, Map data, Properties inf) throws IOException {
		if (inf != null) {
			//skip if excluding children
			if (inf.containsKey(Utils.MARK_EXCLUDE_CHILDREN)) {
				String excludeChildren = inf.getProperty(Utils.MARK_EXCLUDE_CHILDREN);
				if (Boolean.valueOf(excludeChildren).booleanValue())
					if (verbose) {
						for (int i = 0; i <= depth; i++)
							System.out.print("  "); //$NON-NLS-1$
						System.out.println("Children of " + jar.getName() + "are excluded from processing."); //$NON-NLS-1$ //$NON-NLS-2$
					}
				return;
			}
		}

		Enumeration entries = jar.entries();
		if (entries.hasMoreElements()) {
			for (JarEntry entry = (JarEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (JarEntry) entries.nextElement() : null) {
				String name = entry.getName();
				String newName = recursionEffect(name);
				if (newName != null) {
					if (verbose) {
						for (int i = 0; i <= depth; i++)
							System.out.print("  "); //$NON-NLS-1$
						System.out.println("Processing nested file: " + name); //$NON-NLS-1$
					}
					//extract entry to temp directory
					File extracted = new File(tempDir, name);
					File parentDir = extracted.getParentFile();
					if (!parentDir.exists())
						parentDir.mkdirs();

					InputStream in = null;
					OutputStream out = null;
					try {
						in = jar.getInputStream(entry);
						out = new BufferedOutputStream(new FileOutputStream(extracted));
						Utils.transferStreams(in, out, true); //this will close both streams
					} finally {
						Utils.close(in);
						Utils.close(out);
					}
					extracted.setLastModified(entry.getTime());

					//recurse
					String dir = getWorkingDirectory();
					try {
						containingInfs.addFirst(inf);
						setWorkingDirectory(parentDir.getCanonicalPath());
						File result = processJar(extracted);
						newName = name.substring(0, name.length() - extracted.getName().length()) + result.getName();
						data.put(name, newName);
					} finally {
						setWorkingDirectory(dir);
						containingInfs.removeFirst();
					}

					//delete the extracted item leaving the recursion result
					if (!name.equals(newName))
						extracted.delete();
				}
			}
		}
	}

	private File preProcess(File input, File tempDir) {
		File result = null;
		for (Iterator iter = steps.iterator(); iter.hasNext();) {
			IProcessStep step = (IProcessStep) iter.next();
			result = step.preProcess(input, tempDir, containingInfs);
			if (result != null)
				input = result;
		}
		return input;
	}

	private File postProcess(File input, File tempDir) {
		File result = null;
		for (Iterator iter = steps.iterator(); iter.hasNext();) {
			IProcessStep step = (IProcessStep) iter.next();
			result = step.postProcess(input, tempDir, containingInfs);
			if (result != null)
				input = result;
		}
		return input;
	}

	private boolean adjustInf(File input, Properties inf) {
		boolean adjusted = false;
		for (Iterator iter = steps.iterator(); iter.hasNext();) {
			IProcessStep step = (IProcessStep) iter.next();
			adjusted |= step.adjustInf(input, inf, containingInfs);
		}
		return adjusted;
	}

	public File processJar(File input) throws IOException {
		File tempDir = null;
		++depth;
		try {
			long lastModified = input.lastModified();
			File workingDir = new File(getWorkingDirectory());
			if (!workingDir.exists())
				workingDir.mkdirs();

			boolean skip = Utils.shouldSkipJar(input, processAll, verbose);
			if (depth == 0 && verbose) {
				if (skip)
					System.out.println("Skipping " + input.getPath()); //$NON-NLS-1$
				else {
					System.out.print("Running "); //$NON-NLS-1$ 
					for (Iterator iter = steps.iterator(); iter.hasNext();) {
						IProcessStep step = (IProcessStep) iter.next();
						System.out.print(step.getStepName() + " "); //$NON-NLS-1$
					}
					System.out.println("on " + input.getPath()); //$NON-NLS-1$
				}
			}

			if (skip) {
				//This jar was not marked as conditioned, and we are only processing conditioned jars, so do nothing
				return input;
			}

			//pre
			File workingFile = preProcess(input, workingDir);

			//Extract entries from jar and recurse on them
			if (depth == 0) {
				tempDir = new File(workingDir, "temp." + workingFile.getName()); //$NON-NLS-1$
			} else {
				File parent = workingDir.getParentFile();
				tempDir = new File(parent, "temp_" + depth + '_' + workingFile.getName()); //$NON-NLS-1$
			}

			JarFile jar = null;
			try {
				jar = new JarFile(workingFile, false);
				Map replacements = new HashMap();
				Properties inf = Utils.getEclipseInf(workingFile, verbose);
				extractEntries(jar, tempDir, replacements, inf);

				boolean infAdjusted = false;
				if (inf != null)
					infAdjusted = adjustInf(workingFile, inf);

				//Recreate the jar with replacements. 
				//This is not strictly necessary if we didn't change the inf file and didn't change any content
				if (!replacements.isEmpty() || infAdjusted) {
					File tempJar = null;
					tempJar = new File(tempDir, workingFile.getName());
					File parent = tempJar.getParentFile();
					if (!parent.exists())
						parent.mkdirs();
					JarOutputStream jarOut = null;
					try {
						jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)));
						recreateJar(jar, jarOut, replacements, tempDir, inf);
					} finally {
						Utils.close(jarOut);
					}
					if (tempJar != null) {
						if (!workingFile.equals(input)) {
							workingFile.delete();
						}
						workingFile = tempJar;
					}
				}
			} finally {
				Utils.close(jar);
			}

			//post
			File result = postProcess(workingFile, workingDir);

			//have to normalize after the post steps
			normalize(result, workingDir);

			// If the original input is where we ended up, just return it
			if (input.equals(result))
				return result;

			if (!result.equals(workingFile) && !workingFile.equals(input))
				workingFile.delete();
			if (!result.getParentFile().equals(workingDir)) {
				File finalFile = new File(workingDir, result.getName());
				if (finalFile.exists())
					finalFile.delete();
				result.renameTo(finalFile);
				result = finalFile;
			}

			result.setLastModified(lastModified);
			return result;
		} finally {
			--depth;
			if (tempDir != null && tempDir.exists())
				Utils.clear(tempDir);
		}
	}

	private void normalize(File input, File directory) {
		if (input.getName().endsWith(JarProcessor.PACKED_SUFFIX)) {
			//not a jar
			return;
		}
		try {
			File tempJar = new File(directory, "temp_" + input.getName()); //$NON-NLS-1$
			JarFile jar = null;
			JarOutputStream jarOut = null;
			InputStream jarIn = null;
			try {
				jar = new JarFile(input, false);
				jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)));
				Enumeration entries = jar.entries();
				for (JarEntry entry = (JarEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (JarEntry) entries.nextElement() : null) {
					JarEntry newEntry = new JarEntry(entry.getName());
					newEntry.setTime(entry.getTime());
					jarIn = new BufferedInputStream(jar.getInputStream(entry));
					jarOut.putNextEntry(newEntry);
					Utils.transferStreams(jarIn, jarOut, false);
					jarOut.closeEntry();
					jarIn.close();
				}
			} catch (JarException e) {
				//not a jar
				return;
			} finally {
				Utils.close(jarOut);
				Utils.close(jarIn);
				Utils.close(jar);
			}
			tempJar.setLastModified(input.lastModified());
			input.delete();
			tempJar.renameTo(input);
		} catch (IOException e) {
			if (verbose) {
				System.out.println("Error normalizing jar " + input.getName()); //$NON-NLS-1$
				e.printStackTrace();
			}
		}
	}
}

Back to the top