1 11 package org.eclipse.update.internal.jarprocessor; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Properties ; 19 import java.util.Set ; 20 21 public class PackStep extends CommandStep { 22 23 protected static String packCommand = null; 24 private static Boolean canPack = null; 25 26 private String arguments = null; 27 private Set exclusions = Collections.EMPTY_SET; 28 29 public static boolean canPack() { 30 if (canPack != null) 31 return canPack.booleanValue(); 32 33 String [] locations = Utils.getPack200Commands("pack200"); if (locations == null) { 35 canPack = Boolean.FALSE; 36 packCommand = null; 37 return false; 38 } 39 40 int result; 41 for (int i = 0; i < locations.length; i++) { 42 if (locations[i] == null) 43 continue; 44 result = execute(new String [] {locations[i], "-V"}); if (result == 0) { 46 packCommand = locations[i]; 47 canPack = Boolean.TRUE; 48 return true; 49 } 50 } 51 52 canPack = Boolean.FALSE; 53 return false; 54 } 55 56 public PackStep(Properties options) { 57 super(options, null, null, false); 58 exclusions = Utils.getPackExclusions(options); 59 } 60 61 public PackStep(Properties options, boolean verbose) { 62 super(options, null, null, verbose); 63 exclusions = Utils.getPackExclusions(options); 64 } 65 66 public String recursionEffect(String entryName) { 67 if (canPack() && entryName.endsWith(".jar") && !exclusions.contains(entryName)) { return entryName + Utils.PACKED_SUFFIX; 69 } 70 return null; 71 } 72 73 public File preProcess(File input, File workingDirectory, List containers) { 74 return null; 75 } 76 77 public File postProcess(File input, File workingDirectory, List containers) { 78 if (canPack() && packCommand != null) { 79 Properties inf = Utils.getEclipseInf(input, verbose); 80 if (!shouldPack(input, containers, inf)) 81 return null; 82 File outputFile = new File (workingDirectory, input.getName() + Utils.PACKED_SUFFIX); 83 try { 84 String [] cmd = getCommand(input, outputFile, inf, containers); 85 int result = execute(cmd, verbose); 86 if (result != 0 && verbose) 87 System.out.println("Error: " + result + " was returned from command: " + Utils.concat(cmd)); } catch (IOException e) { 89 if (verbose) 90 e.printStackTrace(); 91 return null; 92 } 93 return outputFile; 94 } 95 return null; 96 } 97 98 protected boolean shouldPack(File input, List containers, Properties inf) { 99 for (Iterator iterator = containers.iterator(); iterator.hasNext();) { 102 Properties container = (Properties ) iterator.next(); 103 if (container.containsKey(Utils.MARK_EXCLUDE_CHILDREN_PACK)) { 104 if (Boolean.valueOf(container.getProperty(Utils.MARK_EXCLUDE_CHILDREN_PACK)).booleanValue()) { 105 if (verbose) 106 System.out.println(input.getName() + " is excluded from pack200 by its containers."); 107 return false; 108 } 109 break; 110 } 111 } 112 113 if (inf != null && inf.containsKey(Utils.MARK_EXCLUDE_PACK) && Boolean.valueOf(inf.getProperty(Utils.MARK_EXCLUDE_PACK)).booleanValue()) { 115 if (verbose) 116 System.out.println("Excluding " + input.getName() + " from " + getStepName()); return false; 118 } 119 120 return true; 121 } 122 123 protected String [] getCommand(File input, File outputFile, Properties inf, List containers) throws IOException { 124 String [] cmd = null; 125 String arguments = getArguments(input, inf, containers); 126 if (arguments != null && arguments.length() > 0) { 127 String [] args = Utils.toStringArray(arguments, ","); cmd = new String [3 + args.length]; 129 cmd[0] = packCommand; 130 System.arraycopy(args, 0, cmd, 1, args.length); 131 cmd[cmd.length - 2] = outputFile.getCanonicalPath(); 132 cmd[cmd.length - 1] = input.getCanonicalPath(); 133 } else { 134 cmd = new String [] {packCommand, outputFile.getCanonicalPath(), input.getCanonicalPath()}; 135 } 136 return cmd; 137 } 138 139 protected String getArguments(File input, Properties inf, List containers) { 140 if (arguments != null) 141 return arguments; 142 if (inf != null && inf.containsKey(Utils.PACK_ARGS)) { 144 arguments = inf.getProperty(Utils.PACK_ARGS); 145 return arguments; 146 } 147 148 for (Iterator iterator = containers.iterator(); iterator.hasNext();) { 150 Properties container = (Properties ) iterator.next(); 151 if (container.containsKey(Utils.DEFAULT_PACK_ARGS)) { 152 arguments = container.getProperty(Utils.DEFAULT_PACK_ARGS); 153 return arguments; 154 } 155 } 156 157 Properties options = getOptions(); 159 String argsKey = input.getName() + Utils.PACK_ARGS_SUFFIX; 160 if (options.containsKey(argsKey)) { 161 arguments = options.getProperty(argsKey); 162 return arguments; 163 } 164 165 if (options.containsKey(Utils.DEFAULT_PACK_ARGS)) { 167 arguments = options.getProperty(Utils.DEFAULT_PACK_ARGS); 168 return arguments; 169 } 170 171 if (arguments == null) 172 arguments = ""; 173 return arguments; 174 } 175 176 public String getStepName() { 177 return "Pack"; } 179 180 public void adjustInf(File input, Properties inf, List containers) { 181 if (input == null || inf == null) 182 return; 183 184 boolean v = verbose; 186 verbose = false; 187 if (!shouldPack(input, containers, inf)) { 188 verbose = v; 189 return; 190 } 191 verbose = v; 192 193 inf.put(Utils.MARK_PROPERTY, "true"); 196 String arguments = inf.getProperty(Utils.PACK_ARGS); 198 if (arguments == null) { 199 arguments = getArguments(input, inf, containers); 200 if (arguments != null && arguments.length() > 0) 201 inf.put(Utils.PACK_ARGS, arguments); 202 } 203 } 204 } 205 | Popular Tags |