KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > ant > IzPackTask


1 /*
2  * $Id: IzPackTask.java 1708 2007-01-13 18:31:26Z jponge $
3  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
4  *
5  * http://www.izforge.com/izpack/
6  * http://developer.berlios.de/projects/izpack/
7  *
8  * Copyright 2002 Paul Wilkinson
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package com.izforge.izpack.ant;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Properties JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.types.EnumeratedAttribute;
34 import org.apache.tools.ant.types.PropertySet;
35
36 import com.izforge.izpack.compiler.CompilerConfig;
37 import com.izforge.izpack.compiler.CompilerException;
38 import com.izforge.izpack.compiler.PackagerListener;
39
40 /**
41  * A IzPack Ant task.
42  *
43  * @author Paul Wilkinson
44  */

45 public class IzPackTask extends Task implements PackagerListener
46 {
47     /** The embedded installation configuration */
48     private ConfigHolder config;
49
50     /** Holds value of property input. */
51     private String JavaDoc input;
52
53     /** Holds value of property basedir. */
54     private String JavaDoc basedir;
55
56     /** Holds value of property output. */
57     private String JavaDoc output;
58
59     /** Holds value of property compression. */
60     private String JavaDoc compression;
61
62     /** Holds value of property compression. */
63     private int compressionLevel;
64
65     /** Holds value of property installerType. */
66     private InstallerType installerType;
67
68     /**
69      * Holds value of property izPackDir. This should point at the IzPack directory
70      */

71     private String JavaDoc izPackDir;
72
73     /** Holds properties used to make substitutions in the install file */
74     private Properties JavaDoc properties;
75
76     /** should we inherit properties from the Ant file? */
77     private boolean inheritAll = false;
78
79     /** Creates new IZPackTask */
80     public IzPackTask()
81     {
82         basedir = null;
83         config = null;
84         input = null;
85         output = null;
86         installerType = null;
87         izPackDir = null;
88         compression = "default";
89         compressionLevel = -1;
90     }
91
92     /**
93      * Called by ant to create the object for the config nested element.
94      * @return a holder object for the config nested element.
95      */

96     public ConfigHolder createConfig()
97     {
98         config = new ConfigHolder(getProject());
99         return config;
100     }
101
102     /**
103      * Logs a message to the Ant log at default priority (MSG_INFO).
104      *
105      * @param str The message to log.
106      */

107     public void packagerMsg(String JavaDoc str)
108     {
109         packagerMsg(str, MSG_INFO);
110     }
111
112     /**
113      * Logs a message to the Ant log at the specified priority.
114      *
115      * @param str The message to log.
116      * @param priority The priority of the message.
117      */

118     public void packagerMsg(String JavaDoc str, int priority)
119     {
120         final int antPriority;
121         switch (priority)
122         // No guarantee of a direct conversion. It's an enum
123
{
124         case MSG_DEBUG:
125             antPriority = Project.MSG_DEBUG;
126             break;
127         case MSG_ERR:
128             antPriority = Project.MSG_ERR;
129             break;
130         case MSG_INFO:
131             antPriority = Project.MSG_INFO;
132             break;
133         case MSG_VERBOSE:
134             antPriority = Project.MSG_VERBOSE;
135             break;
136         case MSG_WARN:
137             antPriority = Project.MSG_WARN;
138             break;
139         default: // rather than die...
140
antPriority = Project.MSG_INFO;
141         }
142         log(str, antPriority);
143     }
144
145     /** Called when the packaging starts. */
146     public void packagerStart()
147     {
148         log(ResourceBundle.getBundle("com/izforge/izpack/ant/langpacks/messages").getString(
149                 "Packager_starting"), Project.MSG_DEBUG);
150     }
151
152     /** Called when the packaging stops. */
153     public void packagerStop()
154     {
155         log(ResourceBundle.getBundle("com/izforge/izpack/ant/langpacks/messages").getString(
156                 "Packager_ended"), Project.MSG_DEBUG);
157     }
158
159     /**
160      * Packages.
161      *
162      * @exception BuildException Description of the Exception
163      */

164     public void execute() throws org.apache.tools.ant.BuildException
165     {
166         // Either the input attribute or config element must be specified
167
if (input == null && config == null)
168             throw new BuildException(ResourceBundle.getBundle(
169                     "com/izforge/izpack/ant/langpacks/messages").getString(
170                     "input_must_be_specified"));
171
172         if (output == null)
173             throw new BuildException(ResourceBundle.getBundle(
174                     "com/izforge/izpack/ant/langpacks/messages").getString(
175                     "output_must_be_specified"));
176
177         // if (installerType == null) now optional
178

179         if (basedir == null)
180             throw new BuildException(ResourceBundle.getBundle(
181                     "com/izforge/izpack/ant/langpacks/messages").getString(
182                     "basedir_must_be_specified"));
183
184         // if (izPackDir == null)
185
// throw new
186
// BuildException(java.util.ResourceBundle.getBundle("com/izforge/izpack/ant/langpacks/messages").getString("izPackDir_must_be_specified"));
187

188         String JavaDoc kind = (installerType == null ? null : installerType.getValue());
189
190         CompilerConfig c = null;
191         String JavaDoc configText = null;
192         if(config != null )
193         {// Pass in the embedded configuration
194
configText = config.getText();
195             input = null;
196         }
197         try
198         {
199             // else use external configuration referenced by the input attribute
200
c = new CompilerConfig(input, basedir, kind, output,
201                     compression, compressionLevel, this, configText);
202         }
203         catch (CompilerException e1)
204         {
205             throw new BuildException(e1);
206         }
207         CompilerConfig.setIzpackHome(izPackDir);
208
209         if (properties != null)
210         {
211             Enumeration JavaDoc e = properties.keys();
212             while (e.hasMoreElements())
213             {
214                 String JavaDoc name = (String JavaDoc) e.nextElement();
215                 String JavaDoc value = properties.getProperty(name);
216                 value = fixPathString(value);
217                 c.addProperty(name, value);
218             }
219         }
220
221         if (inheritAll)
222         {
223             Hashtable JavaDoc projectProps = getProject().getProperties();
224             Enumeration JavaDoc e = projectProps.keys();
225             while (e.hasMoreElements())
226             {
227                 String JavaDoc name = (String JavaDoc) e.nextElement();
228                 String JavaDoc value = (String JavaDoc) projectProps.get(name);
229                 value = fixPathString(value);
230                 c.addProperty(name, value);
231             }
232         }
233
234         try
235         {
236             c.executeCompiler();
237         }
238         catch (Exception JavaDoc e)
239         {
240             throw new BuildException(e);// Throw an exception if compilation
241
// failed
242
}
243     }
244     
245     private static String JavaDoc fixPathString(String JavaDoc path)
246     {
247        /*
248         * The following code fixes a bug in in codehaus classworlds loader,
249         * which can't handle mixed path strings like "c:\test\../lib/mylib.jar".
250         * The bug is in org.codehaus.classworlds.UrlUtils.normalizeUrlPath().
251         */

252        StringBuffer JavaDoc fixpath = new StringBuffer JavaDoc(path);
253        for(int q=0; q<fixpath.length(); q++)
254           if(fixpath.charAt(q) == '\\')
255              fixpath.setCharAt(q, '/');
256        return fixpath.toString();
257     }
258     
259     /**
260      * Setter for property input.
261      *
262      * @param input New value of property input.
263      */

264     public void setInput(String JavaDoc input)
265     {
266         this.input = input;
267     }
268
269     /**
270      * Setter for property basedir.
271      *
272      * @param basedir New value of property basedir.
273      */

274     public void setBasedir(String JavaDoc basedir)
275     {
276         this.basedir = basedir;
277     }
278
279     /**
280      * Setter for property output.
281      *
282      * @param output New value of property output.
283      */

284     public void setOutput(String JavaDoc output)
285     {
286         this.output = output;
287     }
288
289     /**
290      * Setter for property installerType.
291      *
292      * @param installerType New value of property installerType.
293      */

294     public void setInstallerType(InstallerType installerType)
295     {
296         this.installerType = installerType;
297     }
298
299     /**
300      * Setter for property izPackDir.
301      *
302      * @param izPackDir New value of property izPackDir.
303      */

304     public void setIzPackDir(String JavaDoc izPackDir)
305     {
306         if (!(izPackDir.endsWith("/"))) izPackDir += "/";
307         this.izPackDir = izPackDir;
308     }
309
310     /**
311      * If true, pass all Ant properties to IzPack. Defaults to false;
312      */

313     public void setInheritAll(boolean value)
314     {
315         inheritAll = value;
316     }
317
318     /**
319      * Setter for property compression.
320      * @param compression The type compression to set for pack compression.
321      */

322     public void setCompression(String JavaDoc compression)
323     {
324         this.compression = compression;
325     }
326
327     /**
328      * @param compressionLevel The compressionLevel to set.
329      */

330     public void setCompressionLevel(int compressionLevel)
331     {
332         this.compressionLevel = compressionLevel;
333     }
334
335     /**
336      * Ant will call this for each &lt;property&gt; tag to the IzPack task.
337      */

338     public void addConfiguredProperty(Property property)
339     {
340         if (properties == null) properties = new Properties JavaDoc();
341
342         property.execute(); // don't call perform(), so no build events triggered
343

344         Properties JavaDoc props = property.getProperties();
345         Enumeration JavaDoc e = props.keys();
346         while (e.hasMoreElements())
347         {
348             String JavaDoc name = (String JavaDoc) e.nextElement();
349             String JavaDoc value = props.getProperty(name);
350             log("Adding property: " + property.getClass() + name+"=" + value,
351                 Project.MSG_VERBOSE);
352
353             properties.setProperty(name, value);
354         }
355     }
356
357     /**
358      * A set of properties to pass from the build environment to the install compile
359      *
360      * @param ps The propertyset collection of properties
361      */

362     public void addConfiguredPropertyset(PropertySet ps)
363     {
364         if (properties == null) properties = new Properties JavaDoc();
365
366         properties.putAll(ps.getProperties());
367     }
368
369     /**
370      * Enumerated attribute with the values "asis", "add" and "remove".
371      *
372      * @author Paul Wilkinson
373      */

374     public static class InstallerType extends EnumeratedAttribute
375     {
376
377         public String JavaDoc[] getValues()
378         {
379             return new String JavaDoc[] { CompilerConfig.STANDARD, CompilerConfig.WEB};
380         }
381     }
382 }
383
Popular Tags