KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.Set JavaDoc;
20
21 public class PackStep extends CommandStep {
22
23     protected static String JavaDoc packCommand = null;
24     private static Boolean JavaDoc canPack = null;
25
26     private String JavaDoc arguments = null;
27     private Set JavaDoc exclusions = Collections.EMPTY_SET;
28
29     public static boolean canPack() {
30         if (canPack != null)
31             return canPack.booleanValue();
32
33         String JavaDoc[] locations = Utils.getPack200Commands("pack200"); //$NON-NLS-1$
34
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 JavaDoc[] {locations[i], "-V"}); //$NON-NLS-1$
45
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 JavaDoc options) {
57         super(options, null, null, false);
58         exclusions = Utils.getPackExclusions(options);
59     }
60
61     public PackStep(Properties JavaDoc options, boolean verbose) {
62         super(options, null, null, verbose);
63         exclusions = Utils.getPackExclusions(options);
64     }
65
66     public String JavaDoc recursionEffect(String JavaDoc entryName) {
67         if (canPack() && entryName.endsWith(".jar") && !exclusions.contains(entryName)) { //$NON-NLS-1$
68
return entryName + Utils.PACKED_SUFFIX;
69         }
70         return null;
71     }
72
73     public File JavaDoc preProcess(File JavaDoc input, File JavaDoc workingDirectory, List JavaDoc containers) {
74         return null;
75     }
76
77     public File JavaDoc postProcess(File JavaDoc input, File JavaDoc workingDirectory, List JavaDoc containers) {
78         if (canPack() && packCommand != null) {
79             Properties JavaDoc inf = Utils.getEclipseInf(input, verbose);
80             if (!shouldPack(input, containers, inf))
81                 return null;
82             File JavaDoc outputFile = new File JavaDoc(workingDirectory, input.getName() + Utils.PACKED_SUFFIX);
83             try {
84                 String JavaDoc[] 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)); //$NON-NLS-1$ //$NON-NLS-2$
88
} catch (IOException JavaDoc 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 JavaDoc input, List JavaDoc containers, Properties JavaDoc inf) {
99         //1: exclude by containers
100
// innermost jar is first on the list, it can override outer jars
101
for (Iterator JavaDoc iterator = containers.iterator(); iterator.hasNext();) {
102             Properties JavaDoc container = (Properties JavaDoc) 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         //2: excluded by self
114
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()); //$NON-NLS-1$ //$NON-NLS-2$
117
return false;
118         }
119
120         return true;
121     }
122
123     protected String JavaDoc[] getCommand(File JavaDoc input, File JavaDoc outputFile, Properties JavaDoc inf, List JavaDoc containers) throws IOException JavaDoc {
124         String JavaDoc[] cmd = null;
125         String JavaDoc arguments = getArguments(input, inf, containers);
126         if (arguments != null && arguments.length() > 0) {
127             String JavaDoc[] args = Utils.toStringArray(arguments, ","); //$NON-NLS-1$
128
cmd = new String JavaDoc[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 JavaDoc[] {packCommand, outputFile.getCanonicalPath(), input.getCanonicalPath()};
135         }
136         return cmd;
137     }
138
139     protected String JavaDoc getArguments(File JavaDoc input, Properties JavaDoc inf, List JavaDoc containers) {
140         if (arguments != null)
141             return arguments;
142         //1: Explicitly marked in our .inf file
143
if (inf != null && inf.containsKey(Utils.PACK_ARGS)) {
144             arguments = inf.getProperty(Utils.PACK_ARGS);
145             return arguments;
146         }
147
148         //2: Defaults set in one of our containing jars
149
for (Iterator JavaDoc iterator = containers.iterator(); iterator.hasNext();) {
150             Properties JavaDoc container = (Properties JavaDoc) 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         //3: Set by name in outside pack.properties file
158
Properties JavaDoc options = getOptions();
159         String JavaDoc argsKey = input.getName() + Utils.PACK_ARGS_SUFFIX;
160         if (options.containsKey(argsKey)) {
161             arguments = options.getProperty(argsKey);
162             return arguments;
163         }
164
165         //4: Set by default in outside pack.properties file
166
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 JavaDoc getStepName() {
177         return "Pack"; //$NON-NLS-1$
178
}
179
180     public void adjustInf(File JavaDoc input, Properties JavaDoc inf, List JavaDoc containers) {
181         if (input == null || inf == null)
182             return;
183
184         //don't be verbose to check if we should mark the inf
185
boolean v = verbose;
186         verbose = false;
187         if (!shouldPack(input, containers, inf)) {
188             verbose = v;
189             return;
190         }
191         verbose = v;
192
193         //mark as conditioned
194
inf.put(Utils.MARK_PROPERTY, "true"); //$NON-NLS-1$
195

196         //record arguments used
197
String JavaDoc 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