KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > Defaults


1 /**
2  * $Id: Defaults.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 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://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx;
30
31 import org.apache.tools.ant.Project;
32
33 import com.idaremedia.antx.apis.Requester;
34 import com.idaremedia.antx.helpers.Tk;
35 import com.idaremedia.antx.parameters.MergeBehavior;
36
37 /**
38  * Defaults for current application and/or iteration. Each default request is processed
39  * as if it were the first; no response information is cached. Note that some defaults
40  * can only exist at the global scope (no project-specific overrrides are permitted). To
41  * alter the default values for global defaults, your application must set <em>either</em>
42  * a fixture property (see {@linkplain AntXFixture}) or a global
43  * <span class="src">System</span> property.
44  * <p>
45  * Most defaults are associated with a property. These properties are named based on
46  * the current Iteration's {@linkplain Iteration#configId configuration prefix} so
47  * Iteration subclasses do not need to subclass Defaults to alter the property's
48  * prefix. If a default is documented as looking for "<span class="src">defaults.foo</span>",
49  * this class will actually look for a property named
50  * "<span class="src"><i>configId</i>.defaults.foo</span>" where <i>configId</i> is the
51  * current Iteration's configuration prefix (this value is
52  * <span class="src">jware.antx</span> for the standalone AntX distribution).
53  *
54  * @since JWare/AntX 0.5
55  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
56  * @version 0.5
57  * @.safety multiple
58  * @.group impl,helper
59  * @see com.idaremedia.antx.valueuri.info.DefaultsValueURIHandler
60  **/

61
62 public class Defaults implements FixtureCore
63 {
64     /**
65      * Returns the name of the default properties prefix property:
66      * "<i>configid</i>&#46;<span class="src">defaults&#46;property&#46;prefix</span>".
67      **/

68     public final String JavaDoc defaultPropertiesPrefixName()
69     {
70         return Iteration.configId()+".defaults.property.prefix";
71     }
72
73
74     /**
75      * Returns the prefix prepended to default properties value URIs.
76      * Looks at the "<span class="src">defaults.property.prefix</span>"
77      * property. Returns <i>defaults&#46;</i> if not defined explicitly.
78      * This is a global default.
79      **/

80     public String JavaDoc defaultPropertiesPrefix()
81     {
82         return AntXFixture.getProperty(defaultPropertiesPrefixName(),
83                                        "defaults.");
84     }
85
86
87
88     /**
89      * Returns the prefix prepended to default properties value URIs
90      * looking into the given project for local overrides.
91      * Looks at the "<span class="src">defaults.property.prefix</span>"
92      * property. Looks to {@linkplain #defaultPropertiesPrefix()}
93      * for default setting.
94      **/

95     public String JavaDoc defaultPropertiesPrefix(Project project)
96     {
97         String JavaDoc s = project.getProperty(defaultPropertiesPrefixName());
98         return Tk.isWhitespace(s) ? defaultPropertiesPrefix() : s;
99     }
100
101
102
103     /**
104      * Returns the name of the main haltiferror property:
105      * "<i>configid</i>&#46;<span class="src">defaults&#46;haltiferror&#46;flag</span>.
106      **/

107     public final String JavaDoc isHaltIfErrorName()
108     {
109         return Iteration.configId()+".defaults.haltiferror.flag";
110     }
111
112
113
114     /**
115      * Returns the name of a feature-specific haltiferror property:
116      * "<i>configid</i>&#46;<span class="src">defaults&#46;haltiferror&#46;<i>feature</i>&#46;flag</span>.
117      **/

118     public final String JavaDoc isHaltIfErrorName(String JavaDoc feature)
119     {
120         return Iteration.configId()+".defaults.haltiferror."+feature+".flag";
121     }
122
123
124     /**
125      * Returns whether the application prefers a severe response to
126      * problems when task-specific handling has not been defined.
127      * Looks at the "<span class="src">defaults.haltiferror.flag</span>"
128      * property. Returns <i>false</i> if default not set. This is a
129      * global default.
130      **/

131     public boolean isHaltIfError()
132     {
133         String JavaDoc s = AntXFixture.getProperty(isHaltIfErrorName(),"no");
134         return Tk.booleanFrom(s);
135     }
136
137
138
139     /**
140      * Returns whether the application prefers a severe response to a
141      * specific feature-bound problem when context-specific handling
142      * has not been defined. Looks at the
143      * "<span class="src">defaults.haltiferror.<i>feature</i>.flag</span>"
144      * property where <i>feature</i> is the name of the application
145      * feature (like "checksums"). Looks to {@linkplain #isHaltIfError()}
146      * for default setting. This is a global default.
147      * @param feature the feature's name (non-null)
148      **/

149     public boolean isHaltIfError(String JavaDoc feature)
150     {
151         String JavaDoc defaultYN= isHaltIfError() ? "yes" : "no";
152         String JavaDoc s = AntXFixture.getProperty(isHaltIfErrorName(feature),
153                                            defaultYN);
154         return Tk.booleanFrom(s);
155     }
156
157
158     /**
159      * Returns the name of the main assertions disabled property:
160      * "<i>configid</i>&#46;<span class="src">defaults&#46;asserts&#46;flag</span>.
161      **/

162     public final String JavaDoc isAssertionsDisabledName()
163     {
164         return Iteration.configId()+".defaults.asserts.flag";
165     }
166
167
168     /**
169      * Returns whether general assertions should be ignored in the
170      * current execution iteration. Looks at the
171      * "<span class="src">defaults.asserts.flag</span>" property.
172      * Returns <i>true</i> if default not set. This is a global default.
173      */

174     public boolean isAssertionsDisabled()
175     {
176         String JavaDoc s = AntXFixture.getProperty(isAssertionsDisabledName(),
177                                            "yes");
178         return !Tk.booleanFrom(s);
179     }
180
181
182
183     /**
184      * Returns the name of the delay file deletion property:
185      * "<i>configid</i>&#46;<span class="src">defaults&#46;delaydelete&#46;flag</span>.
186      **/

187     public final String JavaDoc isTempFileDeletionDelayedName()
188     {
189         return Iteration.configId()+".defaults.delaydelete.flag";
190     }
191
192
193
194     /**
195      * Returns whether the application wants transient scratch files
196      * immediately deleted or not. (Delayed deletion allows diagnostics
197      * on transient files.) Looks at the
198      * "<span class="src">defaults.delaydelete.flag</span>" property.
199      * Returns <i>false</i> if not set. This is a global default.
200      **/

201     public boolean isTempFileDeletionDelayed()
202     {
203         String JavaDoc s = AntXFixture.getProperty(isTempFileDeletionDelayedName(),
204                                            "no");
205         return Tk.booleanFrom(s);
206
207     }
208
209
210
211     /**
212      * Like {@linkplain #isTempFileDeletionDelayed() isTempFileDeletionDelayed()}
213      * but looks into the given project for local overrides.
214      * @param project project that is first checked for property.
215      **/

216     public boolean isTempFileDeletionDelayed(Project project)
217     {
218         String JavaDoc s = project.getProperty(isTempFileDeletionDelayedName());
219         return !Tk.isWhitespace(s) ? Tk.booleanFrom(s)
220                     : isTempFileDeletionDelayed();
221     }
222
223
224
225     /**
226      * Returns the name of the short locations string property:
227      * "<i>configid</i>&#46;<span class="src">defaults&#46;shortlocations&#46;flag</span>.
228      **/

229     public final String JavaDoc isShortLocationsEnabledName()
230     {
231         return Iteration.configId()+".defaults.shortlocations.flag";
232     }
233
234
235     /**
236      * Returns whether AntX can (pre)truncate really long location
237      * strings in certain feedback messages. Looks at the
238      * "<span class="src">defaults.shortlocations.flag</span>" property.
239      * Returns <i>true</i> if default not set. This is a global default.
240      */

241     public boolean isShortLocationsEnabled()
242     {
243         String JavaDoc s = AntXFixture.getProperty(isShortLocationsEnabledName(),
244                                            "yes");
245         return Tk.booleanFrom(s);
246     }
247
248
249
250     /**
251      * Returns the name of the fixture exclusion enabled property:
252      * "<i>configid</i>&#46;<span class="src">defaults&#46;fixtureexcludes&#46;flag</span>.
253      **/

254     public final String JavaDoc isFixtureExcludesEnabledName()
255     {
256         return Iteration.configId()+".defaults.fixtureexcludes.flag";
257     }
258
259
260
261     /**
262      * Returns whether fixture exclusions should be handled by default.
263      * Looks at the
264      * "<span class="src">defaults.fixtureexcludes.flag</span>" property.
265      * Returns <i>false</i> if default not set. This is a global default.
266      */

267     public boolean isFixtureExcludesEnabled()
268     {
269         String JavaDoc s = AntXFixture.getProperty(isFixtureExcludesEnabledName(),
270                                             "no");
271         return Tk.booleanFrom(s);
272     }
273
274
275     /**
276      * Returns the name of the passwords file property:
277      * "<i>configid</i>&#46;<span class="src">defaults&#46;passwordfile</span>.
278      **/

279     public final String JavaDoc passwordFileLocationName()
280     {
281         return Iteration.configId()+".defaults.passwordfile";
282     }
283
284
285
286     /**
287      * Returns the default (file) location of the current iteration's
288      * services password file. The format of this file's contents is
289      * iteration and/or application dependent. However, it is usually
290      * a simple cleartext properties file (stored in a secured location
291      * on file system). Looks at the
292      * "<span class="src">defaults.passwordfile</span>" property in
293      * the given project (first) and the global system properties.
294      * Returns <i>null</i> if property not defined explicitly.
295      * @param project [optional] project from which property should be
296      * first checked (will still look at system if not set)
297      */

298     public String JavaDoc passwordFileLocation(Project project)
299     {
300         return Tk.getTheProperty(project, passwordFileLocationName());
301     }
302
303
304
305     /**
306      * Returns the name of the mergecontrol type property:
307      * "<i>configid</i>&#46;<span class="src">defaults&#46;mergecontrol</span>.
308      **/

309     public final String JavaDoc mergeBehaviorName()
310     {
311         return Iteration.configId()+".defaults.mergecontrol";
312     }
313
314
315     /**
316      * Returns the name of the feature-specific mergecontrol type property:
317      * "<i>configid</i>&#46;<span class="src">defaults&#46;mergecontrol&#46;</span><i>feature</i>.
318      **/

319     public final String JavaDoc mergeBehaviorName(String JavaDoc feature)
320     {
321         return Iteration.configId()+".defaults.mergecontrol."+feature;
322     }
323
324
325     /**
326      * Returns the default iteration configuration settings merge
327      * behavior for the runtime. Defaults to {@linkplain MergeBehavior#FIRST}
328      * if not set explicitly. Note that merge first means information
329      * loaded later takes precedence. Looks at the
330      * "<span class="src">defaults.mergecontrol</span>" property. This
331      * is a global default.
332      **/

333     public MergeBehavior mergeBehavior()
334     {
335         return mergeBehavior(MergeBehavior.FIRST);
336     }
337
338
339
340     /**
341      * Returns the default iteration configuration settings merge
342      * behavior for the runtime using a caller-supplied
343      * preference if not set explicitly. Looks at the
344      * "<span class="src">defaults.mergecontrol</span>" property.
345      * @param pref caller's merge preference (non-null)
346      **/

347     public MergeBehavior mergeBehavior(MergeBehavior pref)
348     {
349         String JavaDoc s = AntXFixture.getProperty(mergeBehaviorName(),
350                                            pref.getValue());
351         return MergeBehavior.from(s, pref);
352     }
353
354
355
356     /**
357      * Returns the default iteration configuration settings merge
358      * behavior for a specific feature. Defaults to a caller-supplied
359      * preference if not set explicitly. Looks at the
360      * "<span class="src">defaults.mergecontrol</span>" property for the
361      * feature. This is a global default.
362      * Example: <pre>
363      * Defaults defaults = Iteration.defaultdefaults();
364      * handling = defaults.mergeBehavior("aliases", defaults.mergeBehavior());
365      * switch(handling.getIndex()) {
366      * ...
367      * }
368      * </pre>
369      * @param feature the feature's name (non-null)
370      * @param pref caller's merge preference (non-null)
371      **/

372     public MergeBehavior mergeBehavior(String JavaDoc feature, MergeBehavior pref)
373     {
374         String JavaDoc s = AntXFixture.getProperty(mergeBehaviorName(feature),
375                                            pref.getValue());
376         return MergeBehavior.from(s, pref);
377     }
378
379
380
381     /**
382      * Initializes a new defaults instance. Iteration subclasses can
383      * subclass our implementation to alter the default defaults or use
384      * different properties.
385      * @see Iteration#getDefaults
386      **/

387     protected Defaults()
388     {
389     }
390
391
392
393     /**
394      * Generic query interface for defaults by name. Used by utilities
395      * like value URIs to extract defaults from Iteration subclasses.
396      * @param field the field being queried (non-null, US-locale lowercased)
397      * @param clnt call-controls (non-null)
398      * @return field's current value as a string or <i>null</i> if no such field.
399      * @since JWare/AntX 0.5
400      **/

401     public String JavaDoc valueFrom(String JavaDoc field, Requester clnt)
402     {
403         if ("assertions".equals(field)) {
404             return String.valueOf(!isAssertionsDisabled());
405         }
406         if ("haltiferror".equals(field)) {
407             return String.valueOf(isHaltIfError());
408         }
409         if (field!=null && field.startsWith("haltiferror.")) {
410             String JavaDoc feature = field.substring("haltiferror.".length());
411             return String.valueOf(isHaltIfError(feature));
412         }
413         if ("mergecontrol".equals(field)) {
414             return String.valueOf(mergeBehavior());
415         }
416         if ("configid".equals(field)) {
417             return Iteration.configId();
418         }
419         if ("noiselevel".equals(field)) {
420             return NoiseLevel.getDefault(clnt.getProject()).toString();
421         }
422         if ("passwordfile".equals(field)) {
423             return passwordFileLocation(clnt.getProject());
424         }
425         if ("delaydelete".equals(field)) {
426             return String.valueOf(isTempFileDeletionDelayed(clnt.getProject()));
427         }
428         if ("defaults.prefix".equals(field)) {
429             return defaultPropertiesPrefix(clnt.getProject());
430         }
431         if ("shortlocations".equals(field)) {
432             return String.valueOf(isShortLocationsEnabled());
433         }
434         if ("fixtureexcludes".equals(field)) {
435             return String.valueOf(isFixtureExcludesEnabled());
436         }
437         return null;
438     }
439
440
441
442     /**
443      * Generic query interface for the name of a default attribute.
444      * Used by utilities like value URIs to extract the name of the property
445      * to set from Iteration subclasses.
446      * @param field the field being queried (non-null, US-locale lowercased)
447      * @param clnt call-controls (non-null)
448      * @return field's property name or <i>null</i> if no such field.
449      * @since JWare/AntX 0.5
450      **/

451     public String JavaDoc propertyFrom(String JavaDoc field, Requester clnt)
452     {
453         if (field==null) {
454             return null;
455         }
456         if ("assertions".equals(field)) {
457             return isAssertionsDisabledName();
458         }
459         if ("haltiferror".equals(field)) {
460             return isHaltIfErrorName();
461         }
462         if (field.startsWith("haltiferror.")) {
463             String JavaDoc feature = field.substring("haltiferror.".length());
464             return isHaltIfErrorName(feature);
465         }
466         if ("mergecontrol".equals(field)) {
467             return mergeBehaviorName();
468         }
469         if (field.startsWith("mergecontrol.")) {
470             String JavaDoc feature = field.substring("mergecontrol.".length());
471             return mergeBehaviorName(feature);
472         }
473         if ("noiselevel".equals(field)) {
474             return AntX.DEFAULT_NOISELEVEL_PROP;
475         }
476         if ("passwordfile".equals(field)) {
477             return passwordFileLocationName();
478         }
479         if ("delaydelete".equals(field)) {
480             return isTempFileDeletionDelayedName();
481         }
482         if ("prefix".equals(field)) {
483             return defaultPropertiesPrefixName();
484         }
485         if ("shortlocations".equals(field)) {
486             return isShortLocationsEnabledName();
487         }
488         if ("fixtureexcludes".equals(field)) {
489             return isFixtureExcludesEnabledName();
490         }
491         return null;
492     }
493 }
494
495 /* end-of-Defaults.java */
Popular Tags