KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: VendorInfoTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 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.solo;
30
31 import java.net.URL JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37
38 import com.idaremedia.apis.Buildstrs;
39 import com.idaremedia.apis.EmptyBuildstrs;
40
41 import com.idaremedia.antx.AntX;
42 import com.idaremedia.antx.AssertableTask;
43 import com.idaremedia.antx.apis.AntLibFriendly;
44 import com.idaremedia.antx.helpers.InputFileLoader;
45 import com.idaremedia.antx.helpers.Tk;
46
47
48 /**
49  * Task that converts a standard JWare
50  * <span class="src">{@linkplain Buildstrs Buildstrs}</span> bean into a set of
51  * project properties.
52  * <p>
53  * <b> Example Usage:</b><pre>
54  * &lt;vendorinfo/&gt; //implies AntXtras
55  * &lt;vendorinfo name="antxtras"/&gt;
56  * &lt;vendorinfo infoclass="com.idaremedia.antunit.BuildInfo" prefix="jwau."/&gt;
57  * &lt;vendorinfo name="antx" prefix="abc."/&gt;
58  * &lt;vendorinfo name="antx" prefix="abc." fields="longversion,longdate"/&gt;
59  * &lt;vendorinfo infoclass="mycompany.ProductInfo" fields="label,version"/&gt;
60  * &lt;vendorinfo name="pet" mustexist="no"/&gt;
61  * </pre>
62  *
63  * @since JWare/AntX 0.4
64  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
65  * @version 0.5
66  * @.safety single
67  * @.group api,helper
68  **/

69
70 public class VendorInfoTask extends AssertableTask
71     implements AntLibFriendly
72 {
73     /** AntX's build information (which created/stored in
74         <em>final</em> product archive). **/

75     private static final String JavaDoc ANTX_BI= AntX.ANTX_PACKAGE_PREFIX+".BuildInfo";
76
77
78     /**
79      * Loads the default list of known name to info class mappings.
80      **/

81     private static final Properties JavaDoc DEFAULT_KNOWN_MAPPINGS;
82     static {
83         Properties JavaDoc list = null;
84         URL JavaDoc url = VendorInfoTask.class.getResource("vendors.properties");
85         if (url!=null) {
86             try {
87                 list = InputFileLoader.loadProperties(url,null);
88             } catch(Exception JavaDoc iox) {/*burp*/}
89         }
90         if (list==null) {
91             System.err.println(AntX.uistrs().get("vendorinfo.noini"));
92             list = new Properties JavaDoc();
93             list.setProperty("none",EmptyBuildstrs.class.getName());
94             list.setProperty("antxtras",ANTX_BI);
95             list.setProperty("antx",ANTX_BI);
96         }
97         DEFAULT_KNOWN_MAPPINGS= list;
98         list= null;
99     }
100
101
102
103     /**
104      * Initializes a new vendorinfo task.
105      **/

106     public VendorInfoTask()
107     {
108         super(AntX.nopackage);
109     }
110
111
112     /**
113      * Initializes a new, enclosed vendorinfo task.
114      * @param iam CV-label (non-null)
115      **/

116     public VendorInfoTask(String JavaDoc iam)
117     {
118         super(iam);
119     }
120
121
122     /**
123      * Sets a default short hand name for this task. This name is
124      * used if neither a name nor an info class is specified. This
125      * name defaults to <span class="src">antxtras</span>" if not
126      * set.
127      * @param defaultName default (known) short hand name (non-null)
128      **/

129     protected final void setDefaultName(String JavaDoc defaultName)
130     {
131         require_(defaultName!=null,"setDfltName- nonzro name");
132         m_defaultVendorName = defaultName;
133     }
134
135 // ---------------------------------------------------------------------------------------
136
// Parameters:
137
// ---------------------------------------------------------------------------------------
138

139     /**
140      * Sets the vendor's name using a symbolic short hand name.
141      * @param name the (known) short hand name (non-null)
142      * @throws BuildException if the info class has already been set
143      **/

144     public void setName(String JavaDoc name)
145     {
146         require_(name!=null,"setName- nonzro vendor name");
147         if (m_infoClass!=null) {
148             String JavaDoc error = uistrs().get("task.one.or.other.attr",
149                                         "infoclass","name");
150             log(error, Project.MSG_ERR);
151             throw new BuildException(error,getLocation());
152         }
153         m_vendorName = name;
154     }
155
156
157     /**
158      * Returns the vendor's short hand name if known. Returns
159      * <i>null</i> if never set.
160      **/

161     public String JavaDoc getName()
162     {
163         return m_vendorName;
164     }
165
166
167     /**
168      * Sets the vendor's buildstrs class by FQ name. The class
169      * must implement the standard JWare <span class="src">Buildstrs</span>
170      * interface.
171      * @param infoClass the class to use (non-null)
172      * @throws BuildException if the short hand name has already been set
173      **/

174     public void setInfoClass(Class JavaDoc infoClass)
175     {
176         require_(infoClass!=null,"setInfoClaz- nonzro claz");
177         String JavaDoc error=null;
178         if (!Buildstrs.class.isAssignableFrom(infoClass)) {
179             error = uistrs().get("task.bad.custimpl.class1",infoClass.getName(),
180                                  "Buildstrs");
181         }
182         if (m_vendorName!=null) {
183             error = uistrs().get("task.one.or.other.attr","infoclass","name");
184         }
185         if (error!=null) {
186             log(error, Project.MSG_ERR);
187             throw new BuildException(error,getLocation());
188         }
189         m_infoClass = infoClass;
190     }
191
192
193     /**
194      * Returns the vendor's buildstrs class if known. Will return
195      * <i>null</i> if never set and this task has not been executed
196      * at least once.
197      **/

198     public Class JavaDoc getInfoClass()
199     {
200         return m_infoClass;
201     }
202
203
204     /**
205      * Tells this task that it should signal an error if it is
206      * unable to either locate or create the vendor's build
207      * information.
208      * @param must <i>false</i> to let task ignore missing info
209      **/

210     public void setMustExist(boolean must)
211     {
212         m_mustExist = must;
213     }
214
215
216     /**
217      * Returns <i>true</i> if this task will signal an error if it
218      * cannot locate or create the vendor's information. Defaults
219      * to <i>true</i> if never set explicitly.
220      **/

221     public boolean getMustExist()
222     {
223         return m_mustExist;
224     }
225
226
227     /**
228      * Sets a custom property prefix for all of the interned vendor
229      * info properties. If undefined each property is prefixed with
230      * a standard marker like "<span class="src">vendor.build.</span>".
231      * @param prefix the prefix string (non-null)
232      **/

233     public void setPrefix(String JavaDoc prefix)
234     {
235         require_(prefix!=null,"setPrefix- nonzro prefix");
236         m_customPrefix = prefix;
237     }
238
239
240     /**
241      * Returns the custom string this task will use with all of
242      * the interned info properties. Will return <i>null</i> if
243      * never set explicitly.
244      **/

245     public String JavaDoc getPrefix()
246     {
247         return m_customPrefix;
248     }
249
250
251     /**
252      * Returns the string that <em>will</em> be used to prefix all
253      * of the interned vendor info properties. This method returns
254      * a standard prefix if you have not set the prefix explicitly.
255      **/

256     public String JavaDoc getFinalPrefix()
257     {
258         if (m_customPrefix!=null) {
259             String JavaDoc prefix = m_customPrefix;
260             if (!prefix.endsWith(".")) {
261                 prefix += ".";
262             }
263             return prefix;
264         }
265         if (m_vendorName!=null) {
266             return m_vendorName+".build.";
267         }
268         return "vendor.build.";
269     }
270
271
272     /**
273      * Tells this task to intern only the info fields described by
274      * the comma-delimited list. See this class's static fields
275      * for the set of field name choices.
276      * @param fieldnames comma-delimited list of names (non-null)
277      **/

278     public void setFields(String JavaDoc fieldnames)
279     {
280         require_(fieldnames!=null,"setFields- nonzro list");
281         m_fieldsList = Tk.lowercaseFrom(fieldnames);
282     }
283
284
285     /**
286      * Returns the list of fields this task will intern. Returns
287      * <i>null</i> if this list never set explicitly.
288      **/

289     public String JavaDoc getFieldsList()
290     {
291         return m_fieldsList;
292     }
293
294 // ---------------------------------------------------------------------------------------
295
// Extracting information from (Arbitrary) Buildstrs:
296
// ---------------------------------------------------------------------------------------
297

298     public static final String JavaDoc LABEL="label";
299     public static final String JavaDoc VERSION="version";
300     public static final String JavaDoc LONGVERSION="longversion";
301     public static final String JavaDoc DATE="date";
302     public static final String JavaDoc LONGDATE="longdate";
303     public static final String JavaDoc PLATFORM="platform";
304     public static final String JavaDoc HOST="host";
305
306
307     /**
308      * Returns the collection of known short hand names to FQ
309      * class names. Never returns <i>null</i>; subclasses can add to
310      * this default list.
311      **/

312     protected Properties JavaDoc getKnownVendors()
313     {
314         return m_knownVendors;
315     }
316
317
318     /**
319      * Determines which <span class="src">Buildstrs</em> implementation
320      * class this task will use. The class is determined once; subsequent
321      * calls reuse this class reference.
322      * @.sideeffect Updates this task's underlying info class reference
323      * if a symbolic vendor-name was specified.
324      * @see #getKnownVendors
325      **/

326     private void determineBuildInfoClass()
327     {
328         if (m_infoClass==null) {
329             if (m_vendorName==null && m_infoClass==null) {
330                 m_vendorName = m_defaultVendorName;
331             }
332
333             if (m_vendorName!=null) {
334                 String JavaDoc classname = getKnownVendors().getProperty(m_vendorName);
335                 if (classname==null) {
336                     String JavaDoc error = uistrs().get("vendorinfo.nomatch.name",
337                                                 m_vendorName);
338                     if (getMustExist()) {
339                         log(error, Project.MSG_ERR);
340                         throw new BuildException(error,getLocation());
341                     }
342                     log(error, Project.MSG_WARN);
343                     m_infoClass = EmptyBuildstrs.class;
344                 } else {
345                     try {
346                         m_infoClass = Class.forName(classname);
347                     } catch(Exception JavaDoc anyX) {
348                         String JavaDoc error = uistrs().get("vendorinfo.cant.makeinfo",
349                                                     classname);
350                         if (getMustExist()) {
351                             log(error, Project.MSG_ERR);
352                             throw new BuildException(error,getLocation());
353                         }
354                         log(error, Project.MSG_WARN);
355                         m_infoClass = EmptyBuildstrs.class;
356                     }
357                 }
358             }//nam!=nul
359
}
360     }
361
362
363     /**
364      * Returns the <span class="src">Buildstrs</span> this task uses
365      * to extract vendor information. Never returns <i>null</i> but
366      * can return a placeholder if unable to load/create the named
367      * vendor's information (and {@linkplain #getMustExist mustExist}
368      * option is <i>false</i>).
369      * @.impl Expects this task's infoClass already determined.
370      **/

371     private Buildstrs getBuildInfo()
372     {
373         if (m_buildinfo==null) {
374             determineBuildInfoClass();
375             try {
376                 m_buildinfo = (Buildstrs)m_infoClass.newInstance();
377             } catch(Exception JavaDoc anyX) {
378                 String JavaDoc error = uistrs().get("vendorinfo.cant.makeinfo",
379                                             m_infoClass.getName());
380                 if (getMustExist()) {
381                     log(error, Project.MSG_ERR);
382                     throw new BuildException(error,anyX,getLocation());
383                 }
384                 log(error, Project.MSG_WARN);
385                 m_buildinfo = EmptyBuildstrs.INSTANCE;
386             }
387         }
388         return m_buildinfo;
389     }
390
391
392     /**
393      * Sets the appropriate field property in this task's project.
394      **/

395     private void setInfoProperty(String JavaDoc name, String JavaDoc value)
396     {
397         name = getFinalPrefix()+name;
398         checkIfProperty_(name,true);
399         getProject().setNewProperty(name,value);
400     }
401
402
403     /**
404      * Copies all build information fields to properties.
405      **/

406     private void putAllProperties(Buildstrs info)
407     {
408         setInfoProperty(LABEL,info.getDisplayName());
409         setInfoProperty(VERSION,info.getVersion());
410         setInfoProperty(LONGVERSION,info.getBuildVersion());
411         setInfoProperty(DATE,info.getAbbrDate());
412         setInfoProperty(LONGDATE,info.getLongDate());
413         setInfoProperty(PLATFORM,info.getOS());
414         setInfoProperty(HOST,info.getHostID());
415     }
416
417
418     /**
419      * Extracts the symbolically named field's value. Returns
420      * <i>null</i> if field name not recognized.
421      **/

422     private String JavaDoc getFieldValue(Buildstrs info, String JavaDoc f)
423     {
424         String JavaDoc value = null;
425         switch (f.charAt(0)) {
426             case 'v': {
427                 if (VERSION.equals(f)) {
428                     value = info.getVersion();
429                 } break;
430             }
431             case 'd': {
432                 if (DATE.equals(f)) {
433                     value = info.getAbbrDate();
434                 } break;
435             }
436             case 'l': {
437                 if (LABEL.equals(f)) {
438                     value = info.getDisplayName();
439                 } else if (LONGVERSION.equals(f)) {
440                     value = info.getBuildVersion();
441                 } else if (LONGDATE.equals(f)) {
442                     value = info.getLongDate();
443                 } break;
444             }
445             case 'p': {
446                 if (PLATFORM.equals(f)) {
447                     value = info.getOS();
448                 } break;
449             }
450             case 'h': {
451                 if (HOST.equals(f)) {
452                     value = info.getHostID();
453                 } break;
454             }
455         }
456         return value;
457     }
458
459
460     /**
461      * Copies a subset of build information to properties.
462      **/

463     private void putTheseProperties(Buildstrs info, String JavaDoc filterlist)
464     {
465         List JavaDoc l= Tk.splitList(filterlist);
466         if (!l.isEmpty()) {
467             for (int i=0,N=l.size();i<N;i++) {
468                 String JavaDoc f = l.get(i).toString();
469                 String JavaDoc value = getFieldValue(info,f);
470                 if (value!=null) {
471                     setInfoProperty(f,value);
472                 } else {
473                     String JavaDoc warning = uistrs().get("vendorinfo.bad.field",f);
474                     log(warning,Project.MSG_WARN);
475                 }
476             }
477         }
478         l=null;
479     }
480
481
482     /**
483      * Copies the named vendor's build information into a set of
484      * project properties.
485      **/

486     public void execute()
487     {
488         verifyCanExecute_("exec");
489
490         Buildstrs info = getBuildInfo();
491
492         if (m_fieldsList==null) {
493             putAllProperties(info);
494         } else {
495             putTheseProperties(info,m_fieldsList);
496         }
497     }
498
499
500     private boolean m_mustExist=true;
501     private String JavaDoc m_vendorName;
502     private Class JavaDoc m_infoClass;
503     private String JavaDoc m_customPrefix, m_fieldsList;
504     private Buildstrs m_buildinfo;
505     private String JavaDoc m_defaultVendorName="antxtras";
506     private Properties JavaDoc m_knownVendors= new Properties JavaDoc(DEFAULT_KNOWN_MAPPINGS);
507 }
508
509 /* end-of-VendorInfoTask.java */
510
Popular Tags