KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > init > UISMConfigureTask


1 /**
2  * $Id: UISMConfigureTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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 as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * 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 (GNU Lesser General Public License) 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 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.init;
30
31 import java.io.File JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.types.Path;
38 import org.apache.tools.ant.types.Reference;
39
40 import com.idaremedia.apis.BundleStringManager;
41 import com.idaremedia.apis.UIStringManager;
42
43 import com.idaremedia.antx.AntX;
44 import com.idaremedia.antx.FixtureOverlay;
45 import com.idaremedia.antx.UISMContext;
46 import com.idaremedia.antx.apis.BuildError;
47 import com.idaremedia.antx.apis.ProblemHandler;
48 import com.idaremedia.antx.apis.Responses;
49 import com.idaremedia.antx.starters.TaskSet;
50
51 /**
52  * Configuration task that adds another UIStringManager for use by nested AntX
53  * resource bundle aware tasks in a shared project. The effect of a UISMConfigureTask
54  * is like a logical 'or'-- if the nearest task's UIStringManager does not contain
55  * the requested msgid, its preceding UIStringManager (another configure task or a
56  * root UIStringManager) is asked to find the msgid. This delegation continues until
57  * the msgid is located or there are no more UIStringManagers to ask. Note that the
58  * default resource bundle is not automatically searched like the root bundle.
59  *
60  * @since JWare/AntX 0.2
61  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
62  * @version 0.5
63  * @.safety guarded (from UIStringManager interface once configured)
64  * @.group api,infra
65  **/

66
67 public class UISMConfigureTask extends TaskSet
68     implements FixtureOverlay, UISMSource, UIStringManager
69 {
70     /**
71      * Initializes a new UISMConfigureTask instance.
72      **/

73     public UISMConfigureTask()
74     {
75         super(AntX.uism);
76     }
77
78
79     /**
80      * Initializes a new CV-tagged UISMConfigureTask instance.
81      * @param iam CV-label (non-null)
82      **/

83     public UISMConfigureTask(String JavaDoc iam)
84     {
85         super(iam);
86     }
87
88
89     /**
90      * Sets this task's project; updates other underlying
91      * project-components.
92      **/

93     public void setProject(Project P)
94     {
95         super.setProject(P);
96         m_bundle.setProject(P);
97     }
98
99
100     /**
101      * Initializes this taskset's problem handler instance.
102      * @since JWare/AntX 0.4
103      **/

104     public void init()
105     {
106         super.init();
107         m_boobooNotify = new Responses.LogUsing(this);
108     }
109
110
111     /**
112      * Sets this tasks bundle as reference to an existing
113      * msgs bundle declaration.
114      * @param reference referenc to bundle (non-null)
115      **/

116     public void setBundleId(Reference reference)
117     {
118         m_bundle.setRefid(reference);
119     }
120
121 // ---------------------------------------------------------------------------------------
122
// UISMSource implementation (inherit javadoc comments):
123
// ---------------------------------------------------------------------------------------
124

125     public void setURL(String JavaDoc url)
126     {
127         m_bundle.setURL(url);
128     }
129
130     public URL JavaDoc getURL()
131     {
132         return m_bundle.getURL();
133     }
134
135     public void setFile(String JavaDoc filepath)
136     {
137         m_bundle.setFile(filepath);
138     }
139
140     public File JavaDoc getFile()
141     {
142         return m_bundle.getFile();
143     }
144
145     public void setResource(String JavaDoc rsrc)
146     {
147         m_bundle.setResource(rsrc);
148     }
149
150     public String JavaDoc getResource()
151     {
152         return m_bundle.getResource();
153     }
154
155
156 // ---------------------------------------------------------------------------------------
157
// Resource loading implementation (subset of CustomLoaderEnabled):
158
// ---------------------------------------------------------------------------------------
159

160     /**
161      * Defines a simple lookup path for this taskset's bundle
162      * resource.
163      * @param classpath the path to be searched (non-null)
164      * @see #setClassPathRef
165      **/

166     public void setClassPath(Path classpath)
167     {
168         m_bundle.setClassPath(classpath);
169     }
170
171
172     /**
173      * Defines a custom lookup path for this taskset's bundle
174      * resource by (re)using an existing path reference.
175      * @param r reference to an existing path (non-null)
176      **/

177     public void setClassPathRef(Reference r)
178     {
179         m_bundle.setClassPathRef(r);
180     }
181
182
183
184     /**
185      * Returns the custom lookup path this taskset will use when
186      * looking for its resource bundle. Will returns <i>null</i>
187      * if lookup path has not been customized.
188      **/

189     public Path getClassPath()
190     {
191         return m_bundle.getClassPath();
192     }
193
194
195     /**
196      * Tells this taskset to use an existing classloader when
197      * searching and/or loading bundle resource.
198      * @param r reference to an existing ClassLoader (non-null)
199      * @since JWare/AntX 0.4
200      **/

201     public void setLoaderRef(Reference r)
202     {
203         m_bundle.setLoaderRef(r);
204     }
205
206
207     /**
208      * Returns the identifier of the custom classloader this taskset
209      * will use to lookup its bundle resource. Returns <i>null</i>
210      * if no custom class loader.
211      * @since JWare/AntX 0.4
212      **/

213     public String JavaDoc getLoaderRefId()
214     {
215         return m_bundle.getLoaderRefId();
216     }
217
218
219 // ---------------------------------------------------------------------------------------
220
// UIStringManager implementation (inherit javadoc comments):
221
// ---------------------------------------------------------------------------------------
222

223     /** Used as marker for strings that aren't in my immediate bundle. **/
224     private static final String JavaDoc NOSTR= new String JavaDoc("==_\\\"");//NB:unlikely legit entry
225

226
227     public String JavaDoc mget(String JavaDoc id, Object JavaDoc[] args, String JavaDoc defm)
228     {
229         BundleStringManager uism = getThisUISM();
230
231         String JavaDoc msg = uism.mget(id,args,NOSTR);
232
233         if (NOSTR.equals(msg)) {
234             if (!ignoreAllInherited() && m_inheritedUISM!=null) {
235                 msg = m_inheritedUISM.mget(id,args,defm);
236             } else if (defm!=null) {
237                 msg = defm;
238             } else {
239                 msg = uism.getDefaultString();
240             }
241         }
242         return msg;
243     }
244
245
246     public String JavaDoc mget(MessageFormat JavaDoc mf, String JavaDoc id, Object JavaDoc[] args, String JavaDoc defm)
247     {
248         BundleStringManager uism = getThisUISM();
249
250         String JavaDoc msg = uism.mget(mf,id,args,NOSTR);
251         if (NOSTR.equals(msg)) {
252             if (!ignoreAllInherited() && m_inheritedUISM!=null) {
253                 msg = m_inheritedUISM.mget(mf,id,args,defm);
254             } else if (defm!=null) {
255                 msg = defm;
256             } else {
257                 msg = uism.getDefaultString();
258             }
259         }
260         return msg;
261     }
262
263
264     public final String JavaDoc mget(String JavaDoc id, Object JavaDoc[] args)
265     {
266         return mget(id,args,null);
267     }
268
269     public final String JavaDoc get(String JavaDoc id)
270     {
271         return mget(id,(Object JavaDoc[])null,(String JavaDoc)null);
272     }
273
274     public final String JavaDoc dget(String JavaDoc id, String JavaDoc defm)
275     {
276         return mget(id,(Object JavaDoc[])null, defm);
277     }
278
279     public final String JavaDoc get(String JavaDoc id, Object JavaDoc arg1)
280     {
281         return mget(id, new Object JavaDoc[]{arg1}, null);
282     }
283
284     public final String JavaDoc dget(String JavaDoc id, Object JavaDoc arg1, String JavaDoc defm)
285     {
286         return mget(id, new Object JavaDoc[]{arg1}, defm);
287     }
288
289     public final String JavaDoc get(String JavaDoc id, Object JavaDoc arg1, Object JavaDoc arg2)
290     {
291         return mget(id, new Object JavaDoc[]{arg1,arg2}, null);
292     }
293
294     public final String JavaDoc dget(String JavaDoc id, Object JavaDoc arg1, Object JavaDoc arg2, String JavaDoc defm)
295     {
296         return mget(id, new Object JavaDoc[]{arg1,arg2}, defm);
297     }
298
299     public final String JavaDoc get(String JavaDoc id, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3)
300     {
301         return mget(id, new Object JavaDoc[]{arg1,arg2,arg3}, null);
302     }
303
304     public final String JavaDoc dget(String JavaDoc id, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3, String JavaDoc defm)
305     {
306         return mget(id, new Object JavaDoc[]{arg1,arg2,arg3}, defm);
307     }
308
309     public final String JavaDoc getDefaultString()
310     {
311         return getThisUISM().getDefaultString();
312     }
313
314 // ---------------------------------------------------------------------------------------
315
// Taskset/controller management
316
// ---------------------------------------------------------------------------------------
317

318     /**
319      * Returns <i>true</i> if this task ignores its inherited UIStringManager
320      * and returns only messages contained in its bundle. Is <i>false</i>
321      * by default.
322      **/

323     protected final boolean ignoreAllInherited()
324     {
325         return m_noInherited;
326     }
327
328
329     /**
330      * Set whether this task will use the UIStringManager hierarchy or
331      * return only messages contained in its bundle.
332      * @param passthru <i>false</i> if this task should ignore the
333      * the UIStringManager hierarchy.
334      **/

335     public void setInheritance(boolean passthru)
336     {
337         m_noInherited = !passthru;
338     }
339
340
341     /**
342      * Returns this task's underlying UIStringManager. Never
343      * returns <i>null</i>.
344      * @.safety single
345      **/

346     private BundleStringManager getThisUISM()
347     {
348         if (m_UISM==null) {
349             m_UISM = new BundleStringManager
350                 (m_bundle.newPropertyBundle(m_boobooNotify),null);
351         }
352         return m_UISM;
353     }
354
355
356     /**
357      * Installs this UIStringManager as the frontmost within the current
358      * thread's iteration environment before running nested tasks. When tasks
359      * have finished (or failed), uninstalls self from UISM context stack.
360      * @throws BuildException if inner tasks do, or we cannot install UIStringManager
361      * @throws BuildError if iteration UIStringManager stack is fatally corrupted
362      * @see UISMContext
363      **/

364     protected void performNestedTasks()
365         throws BuildException
366     {
367         synchronized(m_runlock) {
368             boolean installed=false;
369             try {
370                 m_inheritedUISM = UISMContext.installStringManager(this,m_boobooNotify);
371                 installed= true;
372                 getThisUISM();
373                 super.performNestedTasks();
374
375             } finally {
376                 if (installed) {
377                     m_inheritedUISM = null;
378                     UIStringManager uism = UISMContext.getStringManager();
379                     if (uism!=this) {
380                         String JavaDoc ME= uistrs().dget("task.uism.whoami","UIStringManager");
381                         String JavaDoc ermsg = uistrs().get("context.stack.corrupted",ME);
382                         log(ermsg, Project.MSG_ERR);
383                         throw new BuildError(ermsg,getLocation());
384                     }
385                     UISMContext.unwindStringManager(null);
386                 }//installed
387
}
388         }
389     }
390
391
392     private boolean m_noInherited;//NB:=>use inheritance hierarchy
393
private UIStringManager m_inheritedUISM;//NB:on execution
394
private UISMBundle m_bundle= new UISMBundle();//NB:delegatee for configuration
395
private BundleStringManager m_UISM;//NB:inited on 1st use
396
private Object JavaDoc m_runlock = new int[0];//NB:prevent nested execution
397
private ProblemHandler m_boobooNotify;//NB:inited by init()
398
}
399
400 /* end-of-UISMConfigureTask.java */
401
Popular Tags