001: /*
002: * $Id: IzPackTask.java 2036 2008-02-09 11:14:05Z jponge $
003: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
004: *
005: * http://izpack.org/
006: * http://izpack.codehaus.org/
007: *
008: * Copyright 2002 Paul Wilkinson
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: */
022:
023: package com.izforge.izpack.ant;
024:
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027: import java.util.Properties;
028: import java.util.ResourceBundle;
029:
030: import org.apache.tools.ant.BuildException;
031: import org.apache.tools.ant.Project;
032: import org.apache.tools.ant.Task;
033: import org.apache.tools.ant.types.EnumeratedAttribute;
034: import org.apache.tools.ant.types.PropertySet;
035:
036: import com.izforge.izpack.compiler.CompilerConfig;
037: import com.izforge.izpack.compiler.CompilerException;
038: import com.izforge.izpack.compiler.PackagerListener;
039:
040: /**
041: * A IzPack Ant task.
042: *
043: * @author Paul Wilkinson
044: */
045: public class IzPackTask extends Task implements PackagerListener {
046: /** The embedded installation configuration */
047: private ConfigHolder config;
048:
049: /** Holds value of property input. */
050: private String input;
051:
052: /** Holds value of property basedir. */
053: private String basedir;
054:
055: /** Holds value of property output. */
056: private String output;
057:
058: /** Holds value of property compression. */
059: private String compression;
060:
061: /** Holds value of property compression. */
062: private int compressionLevel;
063:
064: /** Holds value of property installerType. */
065: private InstallerType installerType;
066:
067: /**
068: * Holds value of property izPackDir. This should point at the IzPack directory
069: */
070: private String izPackDir;
071:
072: /** Holds properties used to make substitutions in the install file */
073: private Properties properties;
074:
075: /** should we inherit properties from the Ant file? */
076: private boolean inheritAll = false;
077:
078: /** Creates new IZPackTask */
079: public IzPackTask() {
080: basedir = null;
081: config = null;
082: input = null;
083: output = null;
084: installerType = null;
085: izPackDir = null;
086: compression = "default";
087: compressionLevel = -1;
088: }
089:
090: /**
091: * Called by ant to create the object for the config nested element.
092: * @return a holder object for the config nested element.
093: */
094: public ConfigHolder createConfig() {
095: config = new ConfigHolder(getProject());
096: return config;
097: }
098:
099: /**
100: * Logs a message to the Ant log at default priority (MSG_INFO).
101: *
102: * @param str The message to log.
103: */
104: public void packagerMsg(String str) {
105: packagerMsg(str, MSG_INFO);
106: }
107:
108: /**
109: * Logs a message to the Ant log at the specified priority.
110: *
111: * @param str The message to log.
112: * @param priority The priority of the message.
113: */
114: public void packagerMsg(String str, int priority) {
115: final int antPriority;
116: switch (priority)
117: // No guarantee of a direct conversion. It's an enum
118: {
119: case MSG_DEBUG:
120: antPriority = Project.MSG_DEBUG;
121: break;
122: case MSG_ERR:
123: antPriority = Project.MSG_ERR;
124: break;
125: case MSG_INFO:
126: antPriority = Project.MSG_INFO;
127: break;
128: case MSG_VERBOSE:
129: antPriority = Project.MSG_VERBOSE;
130: break;
131: case MSG_WARN:
132: antPriority = Project.MSG_WARN;
133: break;
134: default: // rather than die...
135: antPriority = Project.MSG_INFO;
136: }
137: log(str, antPriority);
138: }
139:
140: /** Called when the packaging starts. */
141: public void packagerStart() {
142: log(ResourceBundle.getBundle(
143: "com/izforge/izpack/ant/langpacks/messages").getString(
144: "Packager_starting"), Project.MSG_DEBUG);
145: }
146:
147: /** Called when the packaging stops. */
148: public void packagerStop() {
149: log(ResourceBundle.getBundle(
150: "com/izforge/izpack/ant/langpacks/messages").getString(
151: "Packager_ended"), Project.MSG_DEBUG);
152: }
153:
154: /**
155: * Packages.
156: *
157: * @exception BuildException Description of the Exception
158: */
159: public void execute() throws org.apache.tools.ant.BuildException {
160: // Either the input attribute or config element must be specified
161: if (input == null && config == null)
162: throw new BuildException(ResourceBundle.getBundle(
163: "com/izforge/izpack/ant/langpacks/messages")
164: .getString("input_must_be_specified"));
165:
166: if (output == null)
167: throw new BuildException(ResourceBundle.getBundle(
168: "com/izforge/izpack/ant/langpacks/messages")
169: .getString("output_must_be_specified"));
170:
171: // if (installerType == null) now optional
172:
173: if (basedir == null)
174: throw new BuildException(ResourceBundle.getBundle(
175: "com/izforge/izpack/ant/langpacks/messages")
176: .getString("basedir_must_be_specified"));
177:
178: // if (izPackDir == null)
179: // throw new
180: // BuildException(java.util.ResourceBundle.getBundle("com/izforge/izpack/ant/langpacks/messages").getString("izPackDir_must_be_specified"));
181:
182: String kind = (installerType == null ? null : installerType
183: .getValue());
184:
185: CompilerConfig c = null;
186: String configText = null;
187: if (config != null) {// Pass in the embedded configuration
188: configText = config.getText();
189: input = null;
190: }
191: try {
192: // else use external configuration referenced by the input attribute
193: c = new CompilerConfig(input, basedir, kind, output,
194: compression, compressionLevel, this , configText);
195: } catch (CompilerException e1) {
196: throw new BuildException(e1);
197: }
198: CompilerConfig.setIzpackHome(izPackDir);
199:
200: if (properties != null) {
201: Enumeration e = properties.keys();
202: while (e.hasMoreElements()) {
203: String name = (String) e.nextElement();
204: String value = properties.getProperty(name);
205: value = fixPathString(value);
206: c.addProperty(name, value);
207: }
208: }
209:
210: if (inheritAll) {
211: Hashtable projectProps = getProject().getProperties();
212: Enumeration e = projectProps.keys();
213: while (e.hasMoreElements()) {
214: String name = (String) e.nextElement();
215: String value = (String) projectProps.get(name);
216: value = fixPathString(value);
217: c.addProperty(name, value);
218: }
219: }
220:
221: try {
222: c.executeCompiler();
223: } catch (Exception e) {
224: throw new BuildException(e);// Throw an exception if compilation
225: // failed
226: }
227: }
228:
229: private static String fixPathString(String path) {
230: /*
231: * The following code fixes a bug in in codehaus classworlds loader,
232: * which can't handle mixed path strings like "c:\test\../lib/mylib.jar".
233: * The bug is in org.codehaus.classworlds.UrlUtils.normalizeUrlPath().
234: */
235: StringBuffer fixpath = new StringBuffer(path);
236: for (int q = 0; q < fixpath.length(); q++)
237: if (fixpath.charAt(q) == '\\')
238: fixpath.setCharAt(q, '/');
239: return fixpath.toString();
240: }
241:
242: /**
243: * Setter for property input.
244: *
245: * @param input New value of property input.
246: */
247: public void setInput(String input) {
248: this .input = input;
249: }
250:
251: /**
252: * Setter for property basedir.
253: *
254: * @param basedir New value of property basedir.
255: */
256: public void setBasedir(String basedir) {
257: this .basedir = basedir;
258: }
259:
260: /**
261: * Setter for property output.
262: *
263: * @param output New value of property output.
264: */
265: public void setOutput(String output) {
266: this .output = output;
267: }
268:
269: /**
270: * Setter for property installerType.
271: *
272: * @param installerType New value of property installerType.
273: */
274: public void setInstallerType(InstallerType installerType) {
275: this .installerType = installerType;
276: }
277:
278: /**
279: * Setter for property izPackDir.
280: *
281: * @param izPackDir New value of property izPackDir.
282: */
283: public void setIzPackDir(String izPackDir) {
284: if (!(izPackDir.endsWith("/")))
285: izPackDir += "/";
286: this .izPackDir = izPackDir;
287: }
288:
289: /**
290: * If true, pass all Ant properties to IzPack. Defaults to false;
291: */
292: public void setInheritAll(boolean value) {
293: inheritAll = value;
294: }
295:
296: /**
297: * Setter for property compression.
298: * @param compression The type compression to set for pack compression.
299: */
300: public void setCompression(String compression) {
301: this .compression = compression;
302: }
303:
304: /**
305: * @param compressionLevel The compressionLevel to set.
306: */
307: public void setCompressionLevel(int compressionLevel) {
308: this .compressionLevel = compressionLevel;
309: }
310:
311: /**
312: * Ant will call this for each <property> tag to the IzPack task.
313: */
314: public void addConfiguredProperty(Property property) {
315: if (properties == null)
316: properties = new Properties();
317:
318: property.execute(); // don't call perform(), so no build events triggered
319:
320: Properties props = property.getProperties();
321: Enumeration e = props.keys();
322: while (e.hasMoreElements()) {
323: String name = (String) e.nextElement();
324: String value = props.getProperty(name);
325: log("Adding property: " + property.getClass() + name + "="
326: + value, Project.MSG_VERBOSE);
327:
328: properties.setProperty(name, value);
329: }
330: }
331:
332: /**
333: * A set of properties to pass from the build environment to the install compile
334: *
335: * @param ps The propertyset collection of properties
336: */
337: public void addConfiguredPropertyset(PropertySet ps) {
338: if (properties == null)
339: properties = new Properties();
340:
341: properties.putAll(ps.getProperties());
342: }
343:
344: /**
345: * Enumerated attribute with the values "asis", "add" and "remove".
346: *
347: * @author Paul Wilkinson
348: */
349: public static class InstallerType extends EnumeratedAttribute {
350:
351: public String[] getValues() {
352: return new String[] { CompilerConfig.STANDARD,
353: CompilerConfig.WEB };
354: }
355: }
356: }
|