Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 127375 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/update/core/FeatureContentProvider.java (-8 / +10 lines)
Lines 109-115 Link Here
109
109
110
	// hashtable of locks
110
	// hashtable of locks
111
	private static Hashtable locks = new Hashtable();
111
	private static Hashtable locks = new Hashtable();
112
112
	
113
	protected static Object getLock(String key) {
114
		synchronized (lock) {
115
			if (locks.get(key) == null)
116
				locks.put(key, key);
117
			return locks.get(key);
118
		}
119
	}
120
	
113
	/**
121
	/**
114
	 * Feature content provider constructor
122
	 * Feature content provider constructor
115
	 * 
123
	 * 
Lines 179-191 Link Here
179
		// is still copying into it
187
		// is still copying into it
180
		File localFile = null;
188
		File localFile = null;
181
		FileFragment localFileFragment = null;
189
		FileFragment localFileFragment = null;
182
		Object keyLock = null;
190
		Object keyLock = getLock(key);
183
		synchronized (lock) {
184
			if (locks.get(key) == null)
185
				locks.put(key, key);
186
			keyLock = locks.get(key);
187
		}
188
189
		synchronized (keyLock) {
191
		synchronized (keyLock) {
190
			localFile = Utilities.lookupLocalFile(key);
192
			localFile = Utilities.lookupLocalFile(key);
191
			if (localFile != null) {
193
			if (localFile != null) {
(-)src/org/eclipse/update/internal/core/FeaturePackagedContentProvider.java (-6 / +65 lines)
Lines 10-22 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.update.internal.core;
11
package org.eclipse.update.internal.core;
12
import java.io.*;
12
import java.io.*;
13
import java.net.*;
13
import java.net.URL;
14
import java.util.*;
14
import java.util.*;
15
15
import org.eclipse.core.runtime.CoreException;
16
import org.eclipse.core.runtime.*;
17
import org.eclipse.osgi.util.NLS;
16
import org.eclipse.osgi.util.NLS;
18
import org.eclipse.update.core.*;
17
import org.eclipse.update.core.*;
19
import org.eclipse.update.internal.security.*;
18
import org.eclipse.update.core.model.SiteModel;
19
import org.eclipse.update.internal.security.JarVerifier;
20
import org.eclipse.update.jarprocessor.JarProcessor;
20
21
21
/**
22
/**
22
 * Content Provider of a Feature Package
23
 * Content Provider of a Feature Package
Lines 26-32 Link Here
26
	private ContentReference localManifest = null;
27
	private ContentReference localManifest = null;
27
	private ContentReference[] localFeatureFiles = new ContentReference[0];
28
	private ContentReference[] localFeatureFiles = new ContentReference[0];
28
	private IVerifier jarVerifier = null;
29
	private IVerifier jarVerifier = null;
29
30
	private SiteModel siteModel = null;
30
	/*
31
	/*
31
	 * filter for file with .jar
32
	 * filter for file with .jar
32
	 */
33
	 */
Lines 58-63 Link Here
58
		return jarVerifier;
59
		return jarVerifier;
59
	}
60
	}
60
61
62
	public void setFeature(IFeature feature) {
63
		super.setFeature(feature);
64
		ISite featureSite = feature.getSite();
65
		if(featureSite instanceof SiteModel){
66
			siteModel = (SiteModel) featureSite;
67
		}
68
	}
61
	/*
69
	/*
62
	 * @see IFeatureContentProvider#getFeatureManifestReference()
70
	 * @see IFeatureContentProvider#getFeatureManifestReference()
63
	 */
71
	 */
Lines 162-174 Link Here
162
		URL url = (siteContentProvider == null) ? null : siteContentProvider.getArchiveReference(archiveID);
170
		URL url = (siteContentProvider == null) ? null : siteContentProvider.getArchiveReference(archiveID);
163
171
164
		try {
172
		try {
165
			references[0] = asLocalReference(new JarContentReference(archiveID, url), monitor);
173
			references[0] = retrieveLocalJar(new JarContentReference(archiveID, url), monitor);
166
		} catch (IOException e) {
174
		} catch (IOException e) {
167
			throw errorRetrieving(archiveID, references[0], e);
175
			throw errorRetrieving(archiveID, references[0], e);
168
		}
176
		}
169
		return references;
177
		return references;
170
	}
178
	}
171
179
180
	private ContentReference retrieveLocalJar(JarContentReference reference, InstallMonitor monitor) throws IOException, CoreException {
181
		//If the site does not support pack200, just get the jar as normal
182
		if(siteModel == null || !siteModel.supportsPack200() || !JarProcessor.canPerformUnpack()) {
183
			return asLocalReference(reference, monitor);
184
		}
185
		
186
		ContentReference packedRef = null;
187
		String key = reference.toString();
188
		Object jarLock = getLock(key);
189
		synchronized (jarLock) {
190
			//do we have this jar already?
191
			File localFile = Utilities.lookupLocalFile(key);
192
			if (localFile != null) {
193
				// check if the cached file is still valid (no newer version on server)
194
				if (UpdateManagerUtils.isSameTimestamp(reference.asURL(), localFile.lastModified()))
195
					return reference.createContentReference(reference.getIdentifier(), localFile);
196
			}
197
198
			try {
199
				//don't have jar, check for pack.gz
200
				URL packGZURL = new URL(reference.asURL().toExternalForm() + ".pack.gz"); //$NON-NLS-1$
201
				packedRef = asLocalReference(new JarContentReference(reference.getIdentifier(), packGZURL), monitor);
202
			} catch (IOException e) {
203
				//no pack.gz
204
			}
205
		}
206
		if (packedRef == null) {
207
			//no pack.gz on server, get normal jar
208
			return asLocalReference(reference, monitor);
209
		}
210
211
		synchronized (jarLock) {
212
			Object packedLock = getLock(packedRef.toString());
213
			synchronized (packedLock) {
214
				File tempFile = packedRef.asFile();
215
				long timeStamp = tempFile.lastModified();
216
217
				JarProcessor processor = JarProcessor.getUnpackProcessor(null);
218
				processor.setWorkingDirectory(tempFile.getParent());
219
220
				File packedFile = new File(tempFile.toString() + ".pack.gz"); //$NON-NLS-1$
221
				tempFile.renameTo(packedFile);
222
				//unpacking the jar will strip the ".pack.gz" and leave us back with the original filename
223
				processor.processJar(packedFile);
224
225
				tempFile.setLastModified(timeStamp);
226
				Utilities.mapLocalFile(key, tempFile);
227
			}
228
		}
229
		return packedRef;
230
	}
172
	/*
231
	/*
173
	 * @see IFeatureContentProvider#getNonPluginEntryArchiveReferences(INonPluginEntry)
232
	 * @see IFeatureContentProvider#getNonPluginEntryArchiveReferences(INonPluginEntry)
174
	 */
233
	 */
(-)src/org/eclipse/update/core/model/SiteModel.java (+19 lines)
Lines 45-50 Link Here
45
	private URL locationURL;
45
	private URL locationURL;
46
	private String mirrorsURLString;
46
	private String mirrorsURLString;
47
	private ConfiguredSiteModel configuredSiteModel;
47
	private ConfiguredSiteModel configuredSiteModel;
48
	private boolean pack200 = false;
48
49
49
	/**
50
	/**
50
	 * Creates an uninitialized site model object.
51
	 * Creates an uninitialized site model object.
Lines 447-450 Link Here
447
		assertIsWriteable();
448
		assertIsWriteable();
448
		this.mirrorsURLString = mirrorsURL;
449
		this.mirrorsURLString = mirrorsURL;
449
	}
450
	}
451
	
452
	/**
453
	 * Get whether or not this site may contain jars packed with pack200.
454
	 * The packed version of foo.jar, is expected to be foo.jar.pack.gz
455
	 * @return
456
	 */
457
	public boolean supportsPack200() {
458
		return pack200;
459
	}
460
	
461
	/**
462
	 * Set whether or not this site may contain jars packed with pack200
463
	 * The packed version of foo.jar is expected to be foo.jar.pack.gz
464
	 * @param pack
465
	 */
466
	public void setSupportsPack200(boolean pack){
467
		pack200 = pack;
468
	}
450
}
469
}
(-)src/org/eclipse/update/core/model/DefaultSiteParser.java (+5 lines)
Lines 513-518 Link Here
513
				site.setMirrorsURLString(mirrorsURL);
513
				site.setMirrorsURLString(mirrorsURL);
514
		}
514
		}
515
		
515
		
516
		String pack200 = attributes.getValue("pack200"); //$NON-NLS-1$
517
		if(pack200 != null && new Boolean(pack200).booleanValue()){
518
			site.setSupportsPack200(true);
519
		}
520
		
516
		if ( (site instanceof ExtendedSite) && (Boolean.getBoolean(attributes.getValue("digestURL")))) { //$NON-NLS-1$
521
		if ( (site instanceof ExtendedSite) && (Boolean.getBoolean(attributes.getValue("digestURL")))) { //$NON-NLS-1$
517
			ExtendedSite extendedSite = (ExtendedSite) site;
522
			ExtendedSite extendedSite = (ExtendedSite) site;
518
			extendedSite.setDigestExist(true);
523
			extendedSite.setDigestExist(true);
(-)src/org/eclipse/update/jarprocessor/pack-readme.html (+82 lines)
Added Link Here
1
<!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en">
2
<html>
3
<head>
4
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5
  <title>Eclipse update packing tool readme</title>
6
</head>
7
<body>
8
<h1>Eclipse update packing tool</h1>
9
10
<h3>Overview</h3>
11
The update packing tool processes a hierarchy of arbitrarily nested
12
JARs and ZIP files.  It is a generic utility that performs a depth first traversal of 
13
a nested hierarchy of ZIPs and JARs, performs various commands on
14
each of the JARs in the hierarchy, and then rebuilds the same hierarchy
15
of ZIPs and JARs again.  Currently its main functions are:
16
<ul>
17
	<li>Packing JARs using the Java 1.5 <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/pack200.html">pack200</a>
18
	 command.</li>
19
	 <li>Unpacking PACK.GZs using the Java 1.5 <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/unpack200.html">unpack200</a>
20
	 command.</li>
21
	 <li>Normalizing JARs for future compression by pack200. This is accomplished
22
	 by running the pack200 command with the <tt>--repack</tt> command line argument.</li>
23
	 <li>Signing JARs to allow for authentication of the origin of JARs. This is accomplished by
24
	 running a supplied command (typically the command will just be a wrapper around
25
	 the Java <a href="http://java.sun.com/j2se/1.3/docs/tooldocs/win32/jarsigner.html">jarsigner</a> tool).</li>
26
</ul>
27
The packing tool is used in the following contexts:
28
<ul>
29
	<li>During a PDE build, to prepare JARs for uploading to an Eclipse
30
	update site.  In this usage, it is used to both nomalize JAR contents
31
	(pack200 -repack), and sign JARs.</li>
32
	<li>On an update site, to convert traditional JAR content into the 
33
	compressed pack200 format.</li>
34
	<li>From an Eclipse client application during update, to convert
35
	compressed pack200 format content into executable JAR files.</li>
36
</ul>
37
<h3>Tool usage</h3>
38
To run the packing tool, you need a 1.5 JRE installed. The tool is run
39
by invoking Java as follows:
40
41
<pre>
42
	java jarprocessor.jar [options] input
43
</pre>
44
45
Where <tt>input</tt> is either a zip file, a directory, or a JAR (or a pack.gz file). All files ending 
46
in ".jar" or ".pack.gz" in the provided zip or directory hierarchy
47
will be processed. 
48
The following additional command line arguments are supported:
49
<ul>
50
<li>-repack : Normalize the jars using pack200 <tt>--repack</tt></li>
51
<li>-sign &lt;cmd&gt; : signs the jars by executing the provided command.  
52
The command will be provided a single argument that will be the full path of the JAR to process.
53
</li>
54
<li>-pack : for each input in JAR form, produce a corresponding output
55
in packed form.  For an input "a.jar", the output is a.jar.pack.gz.  
56
</li>
57
<li>-unpack : for each input in packed form, produce a corresponding output
58
in unpacked form.  For an input "a.jar.pack.gz", the output is "a.jar". -unpack is mutually exclusive with -repack, -pack and -sign.</li>
59
<li>-outputDir &lt;dir&gt; : The directory to put the tool's output into.  If the input was a zip file, then an output zip file will be
60
created containg all the output files.  If the input was a directory, for each input file there is a corresponding output file in the output directory. By default the current working directory is used.  If the input is in the same
61
directory as the output, the input files may be overwritten.</li>
62
</ul>
63
64
Additionally, when the input is a zip file, it may contain a file called
65
<tt>pack.properties</tt>.  The pack.properties file supports the following values:
66
<ul>
67
<li>pack.excludes =  jarName[, jarName]* : A comma-delimited list of JARs that should not be packed or repacked.
68
</li>
69
<li>sign.excludes =  jarName[, jarName]* : A comma-delimited list of JARs that should not be signed.
70
</li>
71
<li>&lt;jarname&gt;.pack.args = option[, option]* : A comma-delimited list of additional arguments that should
72
be passed to pack200 when packing any jar with name &lt;jarname&gt;.
73
</ul>
74
</p>
75
<p>
76
<font size=-1>
77
Copyright (c) IBM Corporation and others 2006. All rights reserved. This program and the accompanying materials
78
are made available under the terms of the 
79
<a href="http://www.eclipse.org/legal/epl-v10.html">Eclipse Public License v1.0</a>.
80
</font>
81
</body>
82
</html>
(-)src/org/eclipse/update/jarprocessor/internal/CommandStep.java (+49 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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.jarprocessor.internal;
12
13
import java.io.IOException;
14
import java.util.Properties;
15
import org.eclipse.update.jarprocessor.IProcessStep;
16
17
/**
18
 * @author aniefer
19
 *
20
 */
21
public abstract class CommandStep implements IProcessStep {
22
	protected String command = null;
23
	protected String extension = null;
24
	protected Properties options = null;
25
	
26
	public CommandStep(Properties options, String command, String extension) {
27
		this.command = command;
28
		this.extension = extension;
29
		this.options = options;
30
	}
31
32
	protected static int execute(String[] cmd) throws IOException {
33
		Runtime runtime = Runtime.getRuntime();
34
		Process proc = runtime.exec(cmd);
35
		try {
36
			int result = proc.waitFor();
37
			return result;
38
		} catch (InterruptedException e) {
39
			//ignore
40
		}
41
		return -1;
42
	}
43
	
44
	public Properties getOptions() {
45
		if(options == null)
46
			options = new Properties();
47
		return options;
48
	}
49
}
(-)src/org/eclipse/update/jarprocessor/Main.java (+152 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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.jarprocessor;
12
13
import java.io.*;
14
import java.util.zip.ZipException;
15
import org.eclipse.update.jarprocessor.internal.*;
16
17
/**
18
 * @author aniefer
19
 *
20
 */
21
public class Main {
22
23
	public static class Options {
24
		public String outputDir = "."; //$NON-NLS-1$
25
		public String signCommand = null;
26
		public boolean pack = false;
27
		public boolean repack = false;
28
		public boolean unpack = false;
29
		public File input = null;
30
	}
31
32
	private static void printUsage() {
33
		System.out.println("[-option ...]... input"); //$NON-NLS-1$
34
		System.out.println("The following options are supported:"); //$NON-NLS-1$
35
		System.out.println("-repack         normalize jars "); //$NON-NLS-1$
36
		System.out.println("-sign <command> sign jars using <command>"); //$NON-NLS-1$
37
		System.out.println("-pack           pack the jars.  pack and repack are redundant unless"); //$NON-NLS-1$
38
		System.out.println("                sign is also specified."); //$NON-NLS-1$
39
		System.out.println("-unpack         unpack pack.gz files. Unpack is mutually exclusive"); //$NON-NLS-1$
40
		System.out.println("                with repack, sign and pack."); //$NON-NLS-1$
41
		System.out.println();
42
		System.out.println("-outputDir <dir>  the output directory"); //$NON-NLS-1$
43
	}
44
45
	public static Options processArguments(String[] args) {
46
		if (args.length == 0) {
47
			printUsage();
48
			return null;
49
		}
50
51
		Options options = new Options();
52
		int i = 0;
53
		for (; i < args.length - 1; i++) {
54
			if (args[i].equals("-pack")) {//$NON-NLS-1$
55
				options.pack = true;
56
			} else if (args[i].equals("-unpack")) { //$NON-NLS-1$
57
				options.unpack = true;
58
			} else if (args[i].equals("-sign") && i < args.length - 2) { //$NON-NLS-1$
59
				if (args[i + 1].startsWith("-")) { //$NON-NLS-1$
60
					printUsage();
61
					return null;
62
				}
63
				options.signCommand = args[++i];
64
			} else if (args[i].equals("-repack")) { //$NON-NLS-1$
65
				options.repack = true;
66
			} else if (args[i].equals("-outputDir") && i < args.length - 2) { //$NON-NLS-1$
67
				if (args[i + 1].startsWith("-")) { //$NON-NLS-1$
68
					printUsage();
69
					return null;
70
				}
71
				options.outputDir = args[++i];
72
			}
73
		}
74
75
		options.input = new File(args[i]);
76
77
		String problemMessage = null;
78
		if (options.unpack) {
79
			if (!JarProcessor.canPerformUnpack()) {
80
				problemMessage = "The unpack200 command cannot be found."; //$NON-NLS-1$
81
			} else 	if (options.input.isFile() && !args[i].endsWith(".zip") && !args[i].endsWith(".pack.gz")) { //$NON-NLS-1$ //$NON-NLS-2$
82
				problemMessage = "Input file is not a pack.gz file."; //$NON-NLS-1$
83
			} else 	if (options.pack || options.repack || options.signCommand != null) {
84
				problemMessage = "Pack, repack or sign cannot be specified with unpack."; //$NON-NLS-1$
85
			}
86
		} else {
87
			if (options.input.isFile() && !args[i].endsWith(".zip") && !args[i].endsWith(".jar")) { //$NON-NLS-1$ //$NON-NLS-2$
88
				problemMessage = "Input file is not a jar file."; //$NON-NLS-1$
89
			} else	if ((options.pack || options.repack) && !JarProcessor.canPerformUnpack()) {
90
				problemMessage = "The pack200 command can not be found."; //$NON-NLS-1$
91
			}
92
		}
93
		if(problemMessage != null){
94
			System.out.println(problemMessage);
95
			System.out.println();
96
			printUsage();
97
			return null;
98
		}
99
100
		return options;
101
	}
102
103
	/**
104
	 * @param args
105
	 * @throws FileNotFoundException 
106
	 */
107
	public static void main(String[] args) {
108
		Options options = processArguments(args);
109
		if (options == null)
110
			return;
111
112
		if (options.input.getName().endsWith(".zip")) { //$NON-NLS-1$
113
			ZipProcessor processor = new ZipProcessor();
114
			processor.setWorkingDirectory(options.outputDir);
115
			processor.setSignCommand(options.signCommand);
116
			processor.setPack(options.pack);
117
			processor.setRepack(options.repack || (options.pack && options.signCommand != null));
118
			processor.setUnpack(options.unpack);
119
			try {
120
				processor.processZip(options.input);
121
			} catch (ZipException e) {
122
				// TODO Auto-generated catch block
123
				e.printStackTrace();
124
			} catch (IOException e) {
125
				// TODO Auto-generated catch block
126
				e.printStackTrace();
127
			}
128
		} else {
129
			JarProcessor processor = new JarProcessor();
130
			processor.setWorkingDirectory(options.outputDir);
131
132
			if (options.repack || (options.pack && options.signCommand != null))
133
				processor.addProcessStep(new PackUnpackStep(null));
134
135
			if (options.signCommand != null)
136
				processor.addProcessStep(new SignCommandStep(null, options.signCommand));
137
138
			if (options.pack)
139
				processor.addProcessStep(new PackStep(null));
140
			else if (options.unpack)
141
				processor.addProcessStep(new UnpackStep(null));
142
143
			try {
144
				processor.process(options.input, options.unpack ? Utils.PACK_GZ_FILTER : Utils.JAR_FILTER);
145
			} catch (FileNotFoundException e) {
146
				// TODO Auto-generated catch block
147
				e.printStackTrace();
148
			}
149
		}
150
	}
151
152
}
(-)src/org/eclipse/update/jarprocessor/JarProcessor.java (+269 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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.jarprocessor;
12
13
import java.io.*;
14
import java.util.*;
15
import java.util.jar.*;
16
import org.eclipse.update.jarprocessor.internal.*;
17
18
/**
19
 * @author aniefer
20
 *
21
 */
22
public class JarProcessor {
23
	private List steps = new ArrayList();
24
	private String workingDirectory = ""; //$NON-NLS-1$
25
	private int depth = -1;
26
27
	static public JarProcessor getUnpackProcessor(Properties properties) {
28
		if (!canPerformUnpack())
29
			throw new UnsupportedOperationException();
30
		JarProcessor processor = new JarProcessor();
31
		processor.addProcessStep(new UnpackStep(properties));
32
		return processor;
33
	}
34
35
	static public JarProcessor getPackProcessor(Properties properties) {
36
		if (!canPerformPack())
37
			throw new UnsupportedOperationException();
38
		JarProcessor processor = new JarProcessor();
39
		processor.addProcessStep(new PackStep(properties));
40
		return processor;
41
	}
42
43
	static public boolean canPerformPack() {
44
		return PackStep.canPack();
45
	}
46
47
	static public boolean canPerformUnpack() {
48
		return UnpackStep.canUnpack();
49
	}
50
51
	public String getWorkingDirectory() {
52
		return workingDirectory;
53
	}
54
55
	public void setWorkingDirectory(String dir) {
56
		workingDirectory = dir;
57
	}
58
59
	public void addProcessStep(IProcessStep step) {
60
		steps.add(step);
61
	}
62
63
	public void clearProcessSteps() {
64
		steps.clear();
65
	}
66
67
	public void process(File input, FileFilter filter) throws FileNotFoundException {
68
		if (!input.exists())
69
			throw new FileNotFoundException();
70
71
		File[] files = null;
72
		if (input.isDirectory()) {
73
			files = input.listFiles();
74
		} else if (filter.accept(input)) {
75
			files = new File[] {input};
76
		}
77
		for (int i = 0; i < files.length; i++) {
78
			System.out.println("Processing " + files[i].getName()); //$NON-NLS-1$
79
			if (files[i].isDirectory()) {
80
				String dir = getWorkingDirectory();
81
				setWorkingDirectory(dir + "/" + files[i].getName()); //$NON-NLS-1$
82
				process(files[i], filter);
83
				setWorkingDirectory(dir);
84
			} else if (filter.accept(files[i])) {
85
				try {
86
					processJar(files[i]);
87
				} catch (IOException e) {
88
					// TODO Auto-generated catch block
89
					e.printStackTrace();
90
				}
91
			}
92
		}
93
	}
94
95
	/**
96
	 * Recreate a jar file.  The replacements map specifies entry names to be replaced, the replacements are
97
	 * expected to be found in directory.
98
	 * 
99
	 * @param jar - The input jar
100
	 * @param outputJar - the output
101
	 * @param replacements - map of entryName -> new entryName
102
	 * @param directory - location to find file for new entryName
103
	 * @throws IOException
104
	 */
105
	private void recreateJar(JarFile jar, JarOutputStream outputJar, Map replacements, File directory) throws IOException {
106
		InputStream in = null;
107
		try {
108
			Enumeration entries = jar.entries();
109
			for (JarEntry entry = (JarEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (JarEntry) entries.nextElement() : null) {
110
				File replacement = null;
111
				JarEntry newEntry = null;
112
				if (replacements.containsKey(entry.getName())) {
113
					String name = (String) replacements.get(entry.getName());
114
					replacement = new File(directory, name);
115
					in = new FileInputStream(replacement);
116
					newEntry = new JarEntry(name);
117
				} else {
118
					in = jar.getInputStream(entry);
119
					newEntry = new JarEntry(entry.getName());
120
				}
121
				newEntry.setTime(entry.getTime());
122
				outputJar.putNextEntry(newEntry);
123
				Utils.transferStreams(in, outputJar, false);
124
				outputJar.closeEntry();
125
				in.close();
126
127
				//delete the nested jar file
128
				if (replacement != null) {
129
					replacement.delete();
130
				}
131
			}
132
		} finally {
133
			Utils.close(outputJar);
134
			Utils.close(jar);
135
			Utils.close(in);
136
		}
137
	}
138
139
	private String recursionEffect(String entryName) {
140
		String result = null;
141
		for (Iterator iter = steps.iterator(); iter.hasNext();) {
142
			IProcessStep step = (IProcessStep) iter.next();
143
144
			result = step.recursionEffect(entryName);
145
			if (result != null)
146
				entryName = result;
147
		}
148
		return result;
149
	}
150
151
	private void extractEntries(JarFile jar, File tempDir, Map data) throws IOException {
152
		Enumeration entries = jar.entries();
153
		if (entries.hasMoreElements()) {
154
			for (JarEntry entry = (JarEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (JarEntry) entries.nextElement() : null) {
155
				String name = entry.getName();
156
				String newName = recursionEffect(name);
157
				if (newName != null) {
158
					//extract entry to temp directory
159
					File extracted = new File(tempDir, name);
160
					File parentDir = extracted.getParentFile();
161
					if (!parentDir.exists())
162
						parentDir.mkdirs();
163
164
					InputStream in = null;
165
					FileOutputStream out = null;
166
					try {
167
						in = jar.getInputStream(entry);
168
						out = new FileOutputStream(extracted);
169
						Utils.transferStreams(in, out, true); //this will close both streams
170
					} finally {
171
						Utils.close(in);
172
						Utils.close(out);
173
					}
174
					data.put(name, newName);
175
176
					//recurse
177
					String dir = getWorkingDirectory();
178
					setWorkingDirectory(parentDir.getCanonicalPath());
179
					processJar(extracted);
180
					setWorkingDirectory(dir);
181
182
					//delete the extracted item leaving the recursion result
183
					if (!name.equals(newName))
184
						extracted.delete();
185
				}
186
			}
187
		}
188
	}
189
190
	private File preProcess(File input, File tempDir) {
191
		File result = null;
192
		for (Iterator iter = steps.iterator(); iter.hasNext();) {
193
			IProcessStep step = (IProcessStep) iter.next();
194
			result = step.preProcess(input, tempDir);
195
			if (result != null)
196
				input = result;
197
		}
198
		return input;
199
	}
200
201
	private File postProcess(File input, File tempDir) {
202
		File result = null;
203
		for (Iterator iter = steps.iterator(); iter.hasNext();) {
204
			IProcessStep step = (IProcessStep) iter.next();
205
			result = step.postProcess(input, tempDir);
206
			if (result != null)
207
				input = result;
208
		}
209
		return input;
210
	}
211
212
	public void processJar(File input) throws IOException {
213
		++depth;
214
		File workingDir = new File(getWorkingDirectory());
215
		if (!workingDir.exists())
216
			workingDir.mkdirs();
217
218
		//pre
219
		File workingFile = preProcess(input, workingDir);
220
221
		//Extract entries from jar and recurse on them
222
		File tempDir = null;
223
		if (depth == 0) {
224
			tempDir = new File(workingDir, "temp." + workingFile.getName()); //$NON-NLS-1$
225
		} else {
226
			File parent = workingDir.getParentFile();
227
			tempDir = new File(parent, "temp_" + depth + '_' + workingFile.getName()); //$NON-NLS-1$
228
		}
229
		//		if(depth == 0){
230
		//			JarFile jar = new JarFile(workingFile, false);
231
		//			Map replacements = new HashMap();
232
		//			extractEntries(jar, tempDir, replacements);
233
		//			
234
		//			//Recreate the jar with replacements
235
		//			File tempJar = null;
236
		//			if(replacements.size() > 0) {
237
		//				tempJar = new File(tempDir, workingFile.getName());
238
		//				File parent = tempJar.getParentFile();
239
		//				if(!parent.exists())
240
		//					parent.mkdirs();
241
		//				JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(tempJar));
242
		//				recreateJar(jar, jarOut, replacements, tempDir);
243
		//			}
244
		//			
245
		//			jar.close();
246
		//			if( tempJar != null) {
247
		//				if(!workingFile.equals(input)){
248
		//					workingFile.delete();
249
		//				} 
250
		//				workingFile = tempJar;
251
		//			}
252
		//		}
253
		//post
254
		File result = postProcess(workingFile, workingDir);
255
		if (!result.equals(workingFile) && !workingFile.equals(input))
256
			workingFile.delete();
257
		if (!result.getParentFile().equals(workingDir)) {
258
			File finalFile = new File(workingDir, result.getName());
259
			if (finalFile.exists())
260
				finalFile.delete();
261
			result.renameTo(finalFile);
262
		}
263
264
		if (tempDir.exists())
265
			Utils.clear(tempDir);
266
267
		--depth;
268
	}
269
}
(-)src/org/eclipse/update/jarprocessor/internal/ZipProcessor.java (+171 lines)
Added Link Here
1
/**
2
 * 
3
 */
4
package org.eclipse.update.jarprocessor.internal;
5
6
import java.io.*;
7
import java.util.*;
8
import java.util.zip.*;
9
import org.eclipse.update.jarprocessor.IProcessStep;
10
import org.eclipse.update.jarprocessor.JarProcessor;
11
12
/**
13
 * @author aniefer
14
 *
15
 */
16
public class ZipProcessor {
17
18
	private IProcessStep signStep = null;
19
	private IProcessStep packStep = null;
20
	private IProcessStep packUnpackStep = null;
21
	private IProcessStep unpackStep = null;
22
23
	private String workingDirectory = null;
24
	private Properties properties = null;
25
	private Set packExclusions = null;
26
	private Set signExclusions = null;
27
	private String command = null;
28
	private boolean packing = false;
29
	private boolean signing = false;
30
	private boolean repacking = false;
31
	private boolean unpacking = false;
32
33
	public void setWorkingDirectory(String dir) {
34
		workingDirectory = dir;
35
	}
36
37
	public String getWorkingDirectory() {
38
		if (workingDirectory == null)
39
			workingDirectory = "."; //$NON-NLS-1$
40
		return workingDirectory;
41
	}
42
43
	public void setSignCommand(String command) {
44
		this.command = command;
45
		this.signing = (command != null);
46
	}
47
48
	public void setPack(boolean pack) {
49
		this.packing = pack;
50
	}
51
52
	public void setRepack(boolean repack) {
53
		this.repacking = repack;
54
	}
55
56
	public void setUnpack(boolean unpack) {
57
		this.unpacking = unpack;
58
	}
59
60
	public void processZip(File zipFile) throws ZipException, IOException {
61
		ZipFile zip = new ZipFile(zipFile);
62
		initialize(zip);
63
64
		String extension = unpacking ? "pack.gz" : ".jar"; //$NON-NLS-1$ //$NON-NLS-2$
65
		File tempDir = new File(getWorkingDirectory(), "temp_" + zipFile.getName()); //$NON-NLS-1$
66
		JarProcessor processor = new JarProcessor();
67
		processor.setWorkingDirectory(tempDir.getCanonicalPath());
68
		if (unpacking) {
69
			processor.addProcessStep(unpackStep);
70
		}
71
72
		File outputFile = new File(getWorkingDirectory(), zipFile.getName() + ".temp"); //$NON-NLS-1$
73
		File parent = outputFile.getParentFile();
74
		if (!parent.exists())
75
			parent.mkdirs();
76
		ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outputFile));
77
		Enumeration entries = zip.entries();
78
		if (entries.hasMoreElements()) {
79
			for (ZipEntry entry = (ZipEntry) entries.nextElement(); entry != null; entry = entries.hasMoreElements() ? (ZipEntry) entries.nextElement() : null) {
80
				String name = entry.getName();
81
82
				InputStream entryStream = zip.getInputStream(entry);
83
84
				boolean pack = packing && !packExclusions.contains(name);
85
				boolean sign = signing && !signExclusions.contains(name);
86
				boolean repack = repacking && !packExclusions.contains(name);
87
88
				File extractedFile = null;
89
90
				if (entry.getName().endsWith(extension) && (pack || sign || repack || unpacking)) {
91
					extractedFile = new File(tempDir, name);
92
					parent = extractedFile.getParentFile();
93
					if (!parent.exists())
94
						parent.mkdirs();
95
					FileOutputStream extracted = new FileOutputStream(extractedFile);
96
					Utils.transferStreams(entryStream, extracted, true);
97
98
					if (unpacking) {
99
						processor.processJar(extractedFile);
100
						name = name.substring(0, name.length() - Utils.PACKED_SUFFIX.length());
101
						extractedFile = new File(tempDir, name);
102
					} else {
103
						processor.clearProcessSteps();
104
						if (repack)
105
							processor.addProcessStep(packUnpackStep);
106
						if (sign)
107
							processor.addProcessStep(signStep);
108
						processor.processJar(extractedFile);
109
						extractedFile = new File(tempDir, extractedFile.getName());
110
						if (pack) {
111
							processor.clearProcessSteps();
112
							processor.addProcessStep(packStep);
113
							processor.processJar(extractedFile);
114
115
							File modifiedFile = new File(tempDir, extractedFile.getName() + Utils.PACKED_SUFFIX);
116
							ZipEntry zipEntry = new ZipEntry(name + Utils.PACKED_SUFFIX);
117
							entryStream = new FileInputStream(modifiedFile);
118
							zipOut.putNextEntry(zipEntry);
119
							Utils.transferStreams(entryStream, zipOut, false);
120
							entryStream.close();
121
							Utils.clear(modifiedFile);
122
						}
123
					}
124
					entryStream = new FileInputStream(extractedFile);
125
				}
126
				ZipEntry newEntry = new ZipEntry(name);
127
				zipOut.putNextEntry(newEntry);
128
				Utils.transferStreams(entryStream, zipOut, false);
129
				zipOut.closeEntry();
130
				entryStream.close();
131
132
				if (extractedFile != null)
133
					Utils.clear(extractedFile);
134
			}
135
136
		}
137
		zipOut.close();
138
		zip.close();
139
140
		File finalFile = new File(getWorkingDirectory(), zipFile.getName());
141
		if (finalFile.exists())
142
			finalFile.delete();
143
		outputFile.renameTo(finalFile);
144
		Utils.clear(tempDir);
145
	}
146
147
	private void initialize(ZipFile zip) {
148
		ZipEntry entry = zip.getEntry("pack.properties"); //$NON-NLS-1$
149
		properties = new Properties();
150
		if (entry != null) {
151
			InputStream stream = null;
152
			try {
153
				stream = zip.getInputStream(entry);
154
				properties.load(stream);
155
			} catch (IOException e) {
156
				// TODO Auto-generated catch block
157
				e.printStackTrace();
158
			} finally {
159
				Utils.close(stream);
160
			}
161
		}
162
163
		packExclusions = Utils.getPackExclusions(properties);
164
		signExclusions = Utils.getSignExclusions(properties);
165
166
		packUnpackStep = new PackUnpackStep(properties);
167
		packStep = new PackStep(properties);
168
		signStep = new SignCommandStep(properties, command);
169
		unpackStep = new UnpackStep(properties);
170
	}
171
}
(-)src/org/eclipse/update/jarprocessor/internal/Utils.java (+171 lines)
Added Link Here
1
/**
2
 * 
3
 */
4
package org.eclipse.update.jarprocessor.internal;
5
6
import java.io.*;
7
import java.util.*;
8
import java.util.jar.JarFile;
9
10
/**
11
 * @author aniefer
12
 *
13
 */
14
public class Utils {
15
	public static final String PACK200_PROPERTY = "org.eclipse.update.jarprocessor.pack200"; //$NON-NLS-1$
16
	public static final String JRE = "@jre"; //$NON-NLS-1$
17
	public static final String PATH = "@path"; //$NON-NLS-1$
18
	public static final String NONE = "@none"; //$NON-NLS-1$
19
20
	public static final String PACKED_SUFFIX = ".pack.gz"; //$NON-NLS-1$
21
	public static final String JAR_SUFFIX = ".jar"; //$NON-NLS-1$
22
23
	public static final FileFilter JAR_FILTER = new FileFilter() {
24
		public boolean accept(File pathname) {
25
			return pathname.isFile() && pathname.getName().endsWith(".jar"); //$NON-NLS-1$
26
		}
27
	};
28
29
	public static final FileFilter PACK_GZ_FILTER = new FileFilter() {
30
		public boolean accept(File pathname) {
31
			return pathname.isFile() && pathname.getName().endsWith(PACKED_SUFFIX);
32
		}
33
	};
34
35
	public static void close(Object stream) {
36
		if (stream != null) {
37
			try {
38
				if (stream instanceof InputStream)
39
					((InputStream) stream).close();
40
				else if (stream instanceof OutputStream)
41
					((OutputStream) stream).close();
42
				else if (stream instanceof JarFile)
43
					((JarFile) stream).close();
44
			} catch (IOException e) {
45
				//ignore
46
			}
47
		}
48
	}
49
50
	/**
51
	 * get the set of commands to try to execute pack/unpack 
52
	 * @param cmd, the command, either "pack200" or "unpack200"
53
	 * @return String [] or null
54
	 */
55
	public static String[] getPack200Commands(String cmd) {
56
		String[] locations = null;
57
		String prop = System.getProperty(PACK200_PROPERTY);
58
		String javaHome = System.getProperty("java.home"); //$NON-NLS-1$
59
		if (NONE.equals(prop)) {
60
			return null;
61
		} else if (JRE.equals(prop)) {
62
			locations = new String[] {javaHome + "/bin/" + cmd}; //$NON-NLS-1$
63
		} else if (PATH.equals(prop)) {
64
			locations = new String[] {cmd};
65
		} else if (prop == null) {
66
			locations = new String[] {javaHome + "/bin/" + cmd, cmd}; //$NON-NLS-1$ 
67
		} else {
68
			locations = new String[] {prop + "/" + cmd}; //$NON-NLS-1$
69
		}
70
		return locations;
71
	}
72
73
	/**
74
	 * Transfers all available bytes from the given input stream to the given
75
	 * output stream. Closes both streams if close == true, regardless of failure. 
76
	 * Flushes the destination stream if close == false
77
	 * 
78
	 * @param source
79
	 * @param destination
80
	 * @param close 
81
	 * @throws IOException
82
	 */
83
	public static void transferStreams(InputStream source, OutputStream destination, boolean close) throws IOException {
84
		source = new BufferedInputStream(source);
85
		destination = new BufferedOutputStream(destination);
86
		try {
87
			byte[] buffer = new byte[8192];
88
			while (true) {
89
				int bytesRead = -1;
90
				if ((bytesRead = source.read(buffer)) == -1)
91
					break;
92
				destination.write(buffer, 0, bytesRead);
93
			}
94
		} finally {
95
			if (close) {
96
				close(source);
97
				close(destination);
98
			} else {
99
				destination.flush();
100
			}
101
		}
102
	}
103
104
	/**
105
	 * Deletes all the files and directories from the given root down (inclusive).
106
	 * Returns false if we could not delete some file or an exception occurred
107
	 * at any point in the deletion.
108
	 * Even if an exception occurs, a best effort is made to continue deleting.
109
	 */
110
	public static boolean clear(java.io.File root) {
111
		boolean result = clearChildren(root);
112
		try {
113
			if (root.exists())
114
				result &= root.delete();
115
		} catch (Exception e) {
116
			result = false;
117
		}
118
		return result;
119
	}
120
121
	/**
122
	 * Deletes all the files and directories from the given root down, except for 
123
	 * the root itself.
124
	 * Returns false if we could not delete some file or an exception occurred
125
	 * at any point in the deletion.
126
	 * Even if an exception occurs, a best effort is made to continue deleting.
127
	 */
128
	public static boolean clearChildren(java.io.File root) {
129
		boolean result = true;
130
		if (root.isDirectory()) {
131
			String[] list = root.list();
132
			// for some unknown reason, list() can return null.  
133
			// Just skip the children If it does.
134
			if (list != null)
135
				for (int i = 0; i < list.length; i++)
136
					result &= clear(new java.io.File(root, list[i]));
137
		}
138
		return result;
139
	}
140
141
	public static Set getPackExclusions(Properties properties) {
142
		if (properties == null)
143
			return Collections.EMPTY_SET;
144
145
		String packExcludes = properties.getProperty("pack.excludes"); //$NON-NLS-1$
146
		if (packExcludes != null) {
147
			String[] excludes = packExcludes.split(",\\s*"); //$NON-NLS-1$
148
			Set packExclusions = new HashSet();
149
			for (int i = 0; i < excludes.length; i++) {
150
				packExclusions.add(excludes[i]);
151
			}
152
			return packExclusions;
153
		}
154
		return Collections.EMPTY_SET;
155
	}
156
157
	public static Set getSignExclusions(Properties properties) {
158
		if (properties == null)
159
			return Collections.EMPTY_SET;
160
		String signExcludes = properties.getProperty("sign.excludes"); //$NON-NLS-1$
161
		if (signExcludes != null) {
162
			String[] excludes = signExcludes.split(",\\s*"); //$NON-NLS-1$
163
			Set signExclusions = new HashSet();
164
			for (int i = 0; i < excludes.length; i++) {
165
				signExclusions.add(excludes[i]);
166
			}
167
			return signExclusions;
168
		}
169
		return Collections.EMPTY_SET;
170
	}
171
}
(-)src/org/eclipse/update/jarprocessor/jarprocessor.jardesc (+22 lines)
Added Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
2
<jardesc>
3
    <jar path="org.eclipse.update.core/jarprocessor.jar"/>
4
    <options overwrite="false" compress="true" exportErrors="true" exportWarnings="true" saveDescription="false" descriptionLocation="/org.eclipse.update.core/src/org/eclipse/update/jarprocessor/jarprocessor.jardesc" useSourceFolders="false" buildIfNeeded="true" includeDirectoryEntries="false" storeRefactorings="false"/>
5
    <refactoring structuralOnly="false" deprecationInfo="true"/>
6
    <selectedProjects>
7
        <project name="org.eclipse.update.core"/>
8
    </selectedProjects>
9
    <manifest manifestVersion="1.0" usesManifest="true" reuseManifest="false" saveManifest="false" generateManifest="true" manifestLocation="" mainClassHandleIdentifier="=org.eclipse.update.core/src&lt;org.eclipse.update.jarprocessor{Main.java[Main">
10
        <sealing sealJar="false">
11
            <packagesToSeal/>
12
            <packagesToUnSeal/>
13
        </sealing>
14
    </manifest>
15
    <selectedElements exportClassFiles="true" exportOutputFolder="false" exportJavaFiles="true">
16
        <javaElement handleIdentifier="=org.eclipse.update.core/src&lt;org.eclipse.update.jarprocessor{JarProcessor.java"/>
17
        <javaElement handleIdentifier="=org.eclipse.update.core/src&lt;org.eclipse.update.jarprocessor.internal"/>
18
        <javaElement handleIdentifier="=org.eclipse.update.core/src&lt;org.eclipse.update.jarprocessor{IProcessStep.java"/>
19
        <file path="/org.eclipse.update.core/src/org/eclipse/update/jarprocessor/pack-readme.html"/>
20
        <javaElement handleIdentifier="=org.eclipse.update.core/src&lt;org.eclipse.update.jarprocessor{Main.java"/>
21
    </selectedElements>
22
</jardesc>
(-)src/org/eclipse/update/jarprocessor/IProcessStep.java (+47 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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.jarprocessor;
12
13
import java.io.File;
14
15
/**
16
 * @author aniefer
17
 *
18
 */
19
public interface IProcessStep {
20
	
21
	/**
22
	 * The effect of this processing step if the JarProcessor was to recurse on this entry.
23
	 * Return null if this step will not do anything with this entry.
24
	 * Return the new entryName if this step will modify this entry on recursion.
25
	 * @param entryName
26
	 * @return
27
	 */
28
	String recursionEffect(String entryName);
29
	
30
	/**
31
	 * Perform some processing on the input file before the JarProcessor considers the entries for recursion.
32
	 *  return the file containing the result of the processing
33
	 * @param input
34
	 * @param workingDirectory
35
	 * @return
36
	 */
37
	File preProcess(File input, File workingDirectory);
38
	
39
	/**
40
	 * Perform some processing on the input file after the JarProcessor returns from recursion
41
	 * return the file containing the result of the processing
42
	 * @param input
43
	 * @param workingDirectory
44
	 * @return
45
	 */
46
	File postProcess(File input, File workingDirectory);
47
}
(-)src/org/eclipse/update/jarprocessor/internal/PackUnpackStep.java (+84 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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.jarprocessor.internal;
12
13
import java.io.File;
14
import java.io.IOException;
15
import java.util.Properties;
16
import java.util.Set;
17
18
/**
19
 * @author aniefer
20
 *
21
 */
22
public class PackUnpackStep extends PackStep {
23
	private Set exclusions = null;
24
//	private PackStep pack = null;
25
//	private UnpackStep unpack = null;
26
	
27
	public PackUnpackStep(Properties options) {
28
		super(options);
29
		exclusions = Utils.getPackExclusions(options);
30
//		pack = new PackStep(options);
31
//		unpack = new UnpackStep(options);
32
	}
33
34
	public String recursionEffect(String entryName) {
35
		if (canPack() && entryName.endsWith(".jar") &&  !exclusions.contains(entryName)) { //$NON-NLS-1$
36
			return entryName;
37
		}
38
		return null;
39
	}
40
41
	/* (non-Javadoc)
42
	 * @see org.eclipse.update.jarprocessor.IProcessStep#preProcess(java.io.File, java.io.File)
43
	 */
44
	public File postProcess(File input, File workingDirectory) {
45
		if (canPack() && command != null) {
46
			File tempFile = new File(workingDirectory, "temp_" + input.getName()); //$NON-NLS-1$
47
			try {
48
				String[] tmp = getCommand(input, tempFile);
49
				String[] cmd = new String[tmp.length + 1];
50
				cmd[0] = tmp[0];
51
				cmd[1] = "-r"; //$NON-NLS-1$
52
				System.arraycopy(tmp, 1, cmd, 2, tmp.length - 1);
53
54
				int result = execute(cmd);
55
				if (result == 0 && tempFile.exists()) {
56
					File finalFile = new File(workingDirectory, input.getName());
57
					if(finalFile.exists())
58
						finalFile.delete();
59
					tempFile.renameTo(finalFile);
60
					return finalFile;
61
				}
62
			} catch (IOException e) {
63
				//didn't work
64
				return null;
65
			}
66
		}
67
		return null;
68
	}
69
//	public File postProcess(File input, File workingDirectory){
70
//		if(PackStep.canPack() && UnpackStep.canUnpack() ){
71
//			File packed = pack.postProcess(input, workingDirectory);
72
//			return unpack.preProcess(packed, workingDirectory);
73
//		}
74
//		return null;
75
//	}
76
77
	/* (non-Javadoc)
78
	 * @see org.eclipse.update.jarprocessor.IProcessStep#postProcess(java.io.File, java.io.File)
79
	 */
80
	public File preProcess(File input, File workingDirectory) {
81
		return null;
82
	}
83
84
}
(-)src/org/eclipse/update/jarprocessor/internal/SignCommandStep.java (+56 lines)
Added Link Here
1
/**
2
 * 
3
 */
4
package org.eclipse.update.jarprocessor.internal;
5
6
import java.io.File;
7
import java.io.IOException;
8
import java.util.Properties;
9
import java.util.Set;
10
11
/**
12
 * @author aniefer
13
 *
14
 */
15
public class SignCommandStep extends CommandStep {
16
	private Set exclusions = null;
17
18
	public SignCommandStep(Properties options, String command) {
19
		super(options, command, ".jar"); //$NON-NLS-1$
20
		exclusions = Utils.getSignExclusions(options);
21
	}
22
23
	/* (non-Javadoc)
24
	 * @see org.eclipse.update.jarprocessor.IProcessStep#recursionEffect(java.lang.String)
25
	 */
26
	public String recursionEffect(String entryName) {
27
		if (entryName.endsWith(extension) && !exclusions.contains(entryName))
28
			return entryName;
29
		return null;
30
	}
31
32
	/* (non-Javadoc)
33
	 * @see org.eclipse.update.jarprocessor.IProcessStep#preProcess(java.io.File, java.io.File)
34
	 */
35
	public File preProcess(File input, File workingDirectory) {
36
		return null;
37
	}
38
39
	/* (non-Javadoc)
40
	 * @see org.eclipse.update.jarprocessor.IProcessStep#postProcess(java.io.File, java.io.File)
41
	 */
42
	public File postProcess(File input, File workingDirectory) {
43
		if(command != null) {
44
			try {
45
				String[] cmd = new String[] {command, input.getCanonicalPath()};
46
				int result = execute(cmd);
47
				if(result == 0)
48
					return input;
49
			} catch (IOException e) {
50
				//boo
51
				e.printStackTrace();
52
			}
53
		}
54
		return null;
55
	}
56
}
(-)src/org/eclipse/update/jarprocessor/internal/UnpackStep.java (+109 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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.jarprocessor.internal;
12
13
import java.io.File;
14
import java.io.IOException;
15
import java.util.Properties;
16
17
/**
18
 * @author aniefer
19
 *
20
 */
21
public class UnpackStep extends CommandStep {
22
	public static final String UNPACKER_PROPERTY = "org.eclipse.update.jarprocessor.Unpacker"; //$NON-NLS-1$
23
	private static Boolean canUnpack = null;
24
	private static String command = null;
25
26
	public static boolean canUnpack() {
27
		if (canUnpack != null)
28
			return canUnpack.booleanValue();
29
30
		String[] locations = Utils.getPack200Commands("unpack200"); //$NON-NLS-1$
31
		if (locations == null) {
32
			canUnpack = Boolean.FALSE;
33
			command = null;
34
			return false;
35
		}
36
37
		int result;
38
		for (int i = 0; i < locations.length; i++) {
39
			if (locations[i] == null)
40
				continue;
41
			try {
42
				result = execute(new String[] {locations[i], "-V"}); //$NON-NLS-1$
43
				if (result == 0) {
44
					command = locations[i];
45
					canUnpack = Boolean.TRUE;
46
					return true;
47
				}
48
			} catch (IOException e) {
49
				//no good
50
			}
51
		}
52
53
		canUnpack = Boolean.FALSE;
54
		return false;
55
	}
56
57
	public UnpackStep(Properties options) {
58
		super(options, null, null);
59
	}
60
61
	/* (non-Javadoc)
62
	 * @see org.eclipse.update.jarprocessor.IProcessStep#recursionEffect(java.lang.String)
63
	 */
64
	public String recursionEffect(String entryName) {
65
		if (canUnpack() && entryName.endsWith(Utils.PACKED_SUFFIX)) {
66
			return entryName.substring(0, entryName.length() - Utils.PACKED_SUFFIX.length());
67
		}
68
		return null;
69
	}
70
71
	/* (non-Javadoc)
72
	 * @see org.eclipse.update.jarprocessor.IProcessStep#preProcess(java.io.File, java.io.File)
73
	 */
74
	public File preProcess(File input, File workingDirectory) {
75
		if (canUnpack() && command != null) {
76
			String name = input.getName();
77
			if (name.endsWith(Utils.PACKED_SUFFIX)) {
78
				name = name.substring(0, name.length() - Utils.PACKED_SUFFIX.length());
79
80
				File unpacked = new File(workingDirectory, name);
81
				File parent = unpacked.getParentFile();
82
				if (!parent.exists())
83
					parent.mkdirs();
84
				try {
85
					String options = getOptions().getProperty(input.getName() + ".unpack.args"); //$NON-NLS-1$
86
					String[] cmd = null;
87
					if (options != null) {
88
						cmd = new String[] {command, options, input.getCanonicalPath(), unpacked.getCanonicalPath()};
89
					} else {
90
						cmd = new String[] {command, input.getCanonicalPath(), unpacked.getCanonicalPath()};
91
					}
92
					execute(cmd);
93
				} catch (IOException e) {
94
					//didn't work
95
					return null;
96
				}
97
				return unpacked;
98
			}
99
		}
100
		return null;
101
	}
102
103
	/* (non-Javadoc)
104
	 * @see org.eclipse.update.jarprocessor.IProcessStep#postProcess(java.io.File, java.io.File)
105
	 */
106
	public File postProcess(File input, File workingDirectory) {
107
		return null;
108
	}
109
}
(-)src/org/eclipse/update/jarprocessor/internal/PackStep.java (+101 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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.jarprocessor.internal;
12
13
import java.io.File;
14
import java.io.IOException;
15
import java.util.*;
16
17
public class PackStep extends CommandStep {
18
19
	protected static String command = null;
20
	private static Boolean canPack = null;
21
22
	private Set exclusions = Collections.EMPTY_SET;
23
24
	public static boolean canPack() {
25
		if (canPack != null)
26
			return canPack.booleanValue();
27
28
		String[] locations = Utils.getPack200Commands("pack200"); //$NON-NLS-1$
29
		if (locations == null) {
30
			canPack = Boolean.FALSE;
31
			command = null;
32
			return false;
33
		}
34
35
		int result;
36
		for (int i = 0; i < locations.length; i++) {
37
			if (locations[i] == null)
38
				continue;
39
			try {
40
				result = execute(new String[] {locations[i], "-V"}); //$NON-NLS-1$
41
				if (result == 0) {
42
					command = locations[i];
43
					canPack = Boolean.TRUE;
44
					return true;
45
				}
46
			} catch (IOException e) {
47
				//no good
48
			}
49
		}
50
51
		canPack = Boolean.FALSE;
52
		return false;
53
	}
54
55
	public PackStep(Properties options) {
56
		super(options, null, null);
57
		exclusions = Utils.getPackExclusions(options);
58
	}
59
60
	public String recursionEffect(String entryName) {
61
		if (canPack() && entryName.endsWith(".jar") && !exclusions.contains(entryName)) { //$NON-NLS-1$
62
			return entryName + Utils.PACKED_SUFFIX;
63
		}
64
		return null;
65
	}
66
67
	public File preProcess(File input, File workingDirectory) {
68
		return null;
69
	}
70
71
	public File postProcess(File input, File workingDirectory) {
72
		if (canPack() && command != null) {
73
			File outputFile = new File(workingDirectory, input.getName() + Utils.PACKED_SUFFIX);
74
			try {
75
				String[] cmd = getCommand(input, outputFile);
76
				execute(cmd);
77
			} catch (IOException e) {
78
				//didn't work
79
				return null;
80
			}
81
			return outputFile;
82
		}
83
		return null;
84
	}
85
86
	protected String[] getCommand(File input, File outputFile) throws IOException {
87
		String[] cmd = null;
88
		String options = getOptions().getProperty(input.getName() + ".pack.args"); //$NON-NLS-1$
89
		if (options != null) {
90
			String[] args = options.split(",\\s*"); //$NON-NLS-1$
91
			cmd = new String[3 + args.length];
92
			cmd[0] = command;
93
			System.arraycopy(args, 0, cmd, 1, args.length);
94
			cmd[cmd.length - 2] = outputFile.getCanonicalPath();
95
			cmd[cmd.length - 1] = input.getCanonicalPath();
96
		} else {
97
			cmd = new String[] {command, outputFile.getCanonicalPath(), input.getCanonicalPath()};
98
		}
99
		return cmd;
100
	}
101
}

Return to bug 127375