KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > solo > LocalFixtureTaskSet


1 /**
2  * $Id: LocalFixtureTaskSet.java 187 2007-03-25 17:59:16Z ssmc $
3  * Copyright 2004,2007 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://antxtras.sf.net/ EMAIL- jware[at]users[dot]sourceforge[dot]net
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.solo;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import org.apache.tools.ant.DynamicAttribute;
40 import org.apache.tools.ant.Project;
41
42 import com.idaremedia.antx.AntX;
43 import com.idaremedia.antx.AntXFixture;
44 import com.idaremedia.antx.FixtureOverlay;
45 import com.idaremedia.antx.apis.Responses;
46 import com.idaremedia.antx.helpers.InputFileLoader;
47 import com.idaremedia.antx.helpers.Tk;
48 import com.idaremedia.antx.ownhelpers.LocalTk;
49 import com.idaremedia.antx.ownhelpers.ScopedProperties;
50 import com.idaremedia.antx.parameters.FlexSourceSupport;
51 import com.idaremedia.antx.starters.TaskSet;
52
53 /**
54  * A taskset that overlays a set of localized properties for its nested elements.
55  * You can also annotate this block of tasks using the <span class="src">meta</span>
56  * attribute so that top-level selectors can examine the meta values w/o having to
57  * fully form the usual Ant placeholder <span class="src">UnknownElement</span>.
58  * You define local properties using custom attributes which are then installed as
59  * supplemental (overriding) project properties for the duration of the taskset's
60  * execution (effective for all nested, same-project, tasks). Note that all tasks
61  * inherit a standard <span class="src">description</span> attribute so you should
62  * not use meta tags as a simple description (just use the standard tag).
63  * <p>
64  * Because of the way <span class="src">UnknownElement</span> and
65  * <span class="src">DynamicConfigurator</span> interact, it is not currently
66  * possible to have data types nested inside a local fixture taskset. The
67  * "DynamicConfigurator"-ism overrides the "TaskContainer"-isms.
68  * <p>
69  * This task will automatically convert variable and reference "URLs" to the
70  * current value of the named item <em>whenever the property is read</em>. For
71  * example using the variable URL "<span class="src">$var:varABC</span>" as a
72  * value for a property <span class="src">ABC</span> will cause the variable
73  * <span class="src">varABC</span> to be read everytime the property
74  * <span class="src">ABC</span> is read.
75  * <p>
76  * <b>Example Usage:</b><pre>
77  * &lt;overlay category="normalization" todo="bug1234"&gt;
78  * ... <i>[Your regular tasks here]</i>
79  * &lt;/overlay&gt;
80  *
81  * &lt;overlay meta="category=documentation,audience=public"&gt;
82  * ... <i>[Your regular tasks here]</i>
83  * &lt;/overlay&gt;
84  *
85  * &lt;overlay file="${user.name}/.builds/etl.properties"&gt;
86  * ... <i>[Your regular tasks here]</i>
87  * &lt;/overlay&gt;
88  * </pre>
89  *
90  * @since JWare/AntX 0.4
91  * @author ssmc, &copy;2004,2007 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
92  * @version 0.5.1
93  * @.safety single
94  * @.group api,helper
95  * @.expects Ant 1.6.5+
96  **/

97
98 public class LocalFixtureTaskSet extends TaskSet
99     implements DynamicAttribute, FixtureOverlay, FlexSourceSupport
100 {
101     /**
102      * Initializes a new local fixture taskset. This taskset behaves
103      * exactly like a standard AntX taskset unless you install
104      * local properties.
105      * @see #setDynamicAttribute setDynamicAttribute(&#8230;)
106      **/

107     public LocalFixtureTaskSet()
108     {
109         super(AntX.fixture+"Overlay:");
110     }
111
112
113
114     /**
115      * Initialized an embedded or enclosed fixture taskset.
116      * @param iam caller's CV-label
117      **/

118     public LocalFixtureTaskSet(String JavaDoc iam)
119     {
120         super(iam);
121     }
122
123
124
125     /**
126      * Sets this taskset's primary annotation note text.
127      * @param notes the notes (non-null)
128      **/

129     public final void setMeta(String JavaDoc notes)
130     {
131        m_metaList = notes==null ? "" : notes;
132     }
133
134
135
136     /**
137      * Returns this taskset's primary annotate note. Never
138      * returns <i>null</i> but can return the empty string.
139      **/

140     public final String JavaDoc getMeta()
141     {
142         return m_metaList;
143     }
144
145
146
147     /**
148      * Returns a map of name-value pairs represented by
149      * this task's <span class="src">meta</span> attribute.
150      * Never returns <i>null</i> but can return an
151      * empty map.
152      **/

153     public final Map JavaDoc getMetaMap()
154     {
155         Map JavaDoc map = AntXFixture.newMap();
156         List JavaDoc l = Tk.splitList(getMeta());
157         if (!l.isEmpty()) {
158             for (Iterator JavaDoc itr=l.iterator();itr.hasNext();) {
159                 String JavaDoc s = itr.next().toString();
160                 int i= s.indexOf('=');
161                 if (i>0) {
162                     map.put(s.substring(0,i),s.substring(i+1));
163                 } else {
164                     map.put(s,"");
165                 }
166             }
167         }
168         l=null;
169         return map;
170     }
171
172
173
174     /**
175      * Adds the given attribute to this task's local fixture
176      * properties. Will be installed when this task is executed.
177      **/

178     public final void setDynamicAttribute(String JavaDoc name, String JavaDoc value)
179     {
180         require_(name!=null,"setAttr- nonzro name");
181         getOverridePropertiesNoNull().put(name,value);
182     }
183
184
185
186     /**
187      * Common extraction code for a <span class="src">Properties</span>
188      * stored at a URL once the URL has been determined.
189      * @since JWare/AntX 0.4
190      * @throws BuildException if unable to load definition from URL
191      **/

192     private void setFromPropertiesURL(URL JavaDoc url, String JavaDoc from)
193     {
194         Map JavaDoc p=null;
195         try {
196             p= InputFileLoader.loadProperties(url,null);
197         } catch(IOException JavaDoc iox) {
198             String JavaDoc warning = uistrs().get("task.bad.configfile",
199                                           from,iox.getMessage());
200             log(warning,Project.MSG_WARN);
201         }
202         if (p!=null) {
203             getOverridePropertiesNoNull().putAll(p);
204             p.clear();
205         }
206     }
207
208
209
210     /* @inherit-doc. Load properties from file.
211      */

212     public void setFile(String JavaDoc filepath)
213     {
214         require_(filepath!=null,"setFile- nonzro path");
215
216         File JavaDoc file = getProject().resolveFile(filepath);
217         try {
218             setFromPropertiesURL(AntXFixture.fileUtils().getFileURL(file),filepath);
219         } catch(MalformedURLException JavaDoc mux) {
220             String JavaDoc warning = uistrs().get("task.bad.configfile",
221                                           filepath,mux.getMessage());
222             log(warning,Project.MSG_WARN);
223         }
224     }
225
226
227
228     /* @inherit-doc. Load properties from resource.
229      */

230     public void setResource(String JavaDoc resource)
231     {
232         require_(resource!=null,"setRez- nonzro resource name");
233
234         URL JavaDoc url = LocalTk.getSystemResource(resource, getProject());
235         if (url!=null) {
236             setFromPropertiesURL(url,resource);
237         } else {
238             String JavaDoc warning = uistrs().get
239                 ("task.bad.configfile", resource,"File Not Found");
240             log(warning,Project.MSG_WARN);
241         }
242     }
243
244
245     /* @inherit-doc. Load properties from URL.
246      */

247     public void setURL(String JavaDoc urlstr)
248     {
249         require_(urlstr!=null,"setURL- nonzro URL");
250         URL JavaDoc url = null;
251         try {
252             url = new URL JavaDoc(urlstr);
253         } catch(MalformedURLException JavaDoc mux) {
254             String JavaDoc warning = uistrs().get("task.bad.configfile",
255                                           urlstr,mux.getMessage());
256             log(warning,Project.MSG_WARN);
257         }
258         if (url!=null) {
259             setFromPropertiesURL(url,urlstr);
260         }
261     }
262
263
264
265     /**
266      * Installs local fixture properties if any exist.
267      **/

268     private void installLocalFixture()
269     {
270         verifyInProject_("install");
271         if (m_localProperties!=null) {
272             m_localProperties.install();
273         }
274     }
275
276
277
278     /**
279      * Uninstalls our local fixture properties (if was installed).
280      **/

281     private void uninstallLocalFixture()
282     {
283         if (m_localProperties!=null) {
284             m_localProperties.uninstall(new Responses.LogUsing(this));
285         }
286     }
287
288
289
290
291     /**
292      * Installs this taskset's custom attributes as project
293      * properties while nested tasks are executed. The local
294      * properties will be removed before taskset exits (failure
295      * or not).
296      **/

297     protected void performNestedTasks()
298     {
299         installLocalFixture();
300         try {
301             super.performNestedTasks();
302         } finally {
303             uninstallLocalFixture();
304         }
305     }
306
307
308     /**
309      * Returns this taskset's dynamic attribute name-value pairs.
310      * This taskset will reflect any changes to the map. Will
311      * return <i>null</i> if no dynamic attributes added to this
312      * taskset.
313      **/

314     protected final Map JavaDoc getOverrideProperties()
315     {
316         return m_localProperties;
317     }
318
319
320
321     /**
322      * Returns this taskset's dynamic attribute name-value pairs
323      * allocating a new map if necessary.
324      **/

325     protected final Map JavaDoc getOverridePropertiesNoNull()
326     {
327         if (m_localProperties==null) {
328             m_localProperties = new ScopedProperties(getProject(),false);
329         }
330         return m_localProperties;
331     }
332
333
334
335     private ScopedProperties m_localProperties;//NB: lazy-inited
336
private String JavaDoc m_metaList="";
337 }
338
339 /* end-of-LocalFixtureTaskSet.java */
340
Popular Tags