|
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 |
} |