KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: InitUISMTask.java 186 2007-03-16 13:42:35Z ssmc $
3  * Copyright 2002-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 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
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.taskdefs.AntlibDefinition;
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.AntXFixture;
45 import com.idaremedia.antx.FixtureInitializer;
46 import com.idaremedia.antx.UISMContext;
47 import com.idaremedia.antx.apis.AntLibFriendly;
48 import com.idaremedia.antx.apis.ProblemHandler;
49 import com.idaremedia.antx.apis.Requester;
50 import com.idaremedia.antx.apis.Responses;
51 import com.idaremedia.antx.helpers.Strings;
52 import com.idaremedia.antx.helpers.Tk;
53 import com.idaremedia.antx.ownhelpers.DefinitionLoader;
54 import com.idaremedia.antx.parameters.CustomLoaderEnabled;
55
56 /**
57  * Helper task that initializes a thread's default and root iteration UIStringManagers.
58  * Usually used in a build's initialization task (outside any target) or with test scripts.
59  * If undefined, this tasks will try to install a <em>default</em> string manager.
60  * Below are some examples using InitUISMTask (assume '<i>managebundles</i>' is the
61  * declared task name of the InitUISMTask class):<pre>
62  * &lt;managebundles url="http://buildmach.mycompany.com:9090/builds/antx/strings.properties"/&gt;
63  * &lt;managebundles resource="my/msgs.properties" classpathref="my.resources.cp"/&gt;
64  * &lt;managebundles action="install-default" bundleid="my.msgs"/&gt;
65  * &lt;managebundles resource="mycompany/tools/strings/AntX.strs" classpath="${my.classpath}"/&gt;
66  * &lt;managebundles action="install" file="/builds/conf/antx/subbuild-msgs.properties"/&gt;
67  * &lt;managebundles action="uninstall"/&gt;
68  * &lt;managebundles resource="my/msgs.properties" loaderref="my.resources.cpl"/&gt;
69  * </pre>
70  * <p>
71  * Implementation note: This class is intentionally subclassed from the standard <i>Task</i>
72  * and not AntX's <i>AssertableTask</i> to ensure UISM-based functionality doesn't apply to
73  * instances of this class (publicly). You can also use this task in your own antlib files
74  * to install a <em>default</em> message bundle like:<pre>
75  * &lt;antlib&gt;
76  * &lt;msgsdef resource="my/msgs.properties"/&gt;
77  * &#46;&#46;&#46;
78  * &lt;/antlib&gt;
79  * </pre>
80  *
81  * @since JWare/AntX 0.1
82  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
83  * @version 0.5
84  * @.safety single
85  * @.group api,infra
86  **/

87
88 public final class InitUISMTask extends AntlibDefinition/*intentional*/
89     implements UISMSource, CustomLoaderEnabled, AntLibFriendly, FixtureInitializer
90 {
91     /**
92      * {@linkplain #setAction Action} name to install the
93      * <em>default</em> bundle.
94      * @since JWare/AntX 0.4
95      **/

96     public static final String JavaDoc OP_INSTALL_DEFAULT = "install-default";
97
98     /**
99      * {@linkplain #setAction Action} name to uninstall the
100      * <em>default</em> bundle.
101      * @since JWare/AntX 0.4
102      **/

103     public static final String JavaDoc OP_UNINSTALL_DEFAULT = "uninstall-default";
104
105     /**
106      * {@linkplain #setAction Action} name to install the
107      * <em>root</em> bundle.
108      * @since JWare/AntX 0.4
109      **/

110     public static final String JavaDoc OP_INSTALL_ROOT = "install-root";
111
112     /**
113      * {@linkplain #setAction Action} name to uninstall the
114      * <em>root</em> bundle.
115      * @since JWare/AntX 0.4
116      **/

117     public static final String JavaDoc OP_UNINSTALL_ROOT = "uninstall-root";
118
119
120
121     /**
122      * Initializes a new init UISM configuration task.
123      **/

124     public InitUISMTask()
125     {
126     }
127
128
129     /**
130      * Initializes this task's default problem notify callback and
131      * ties its bundle impl to any antlib bits.
132      * @since JWare/AntX 0.4
133      **/

134     public void init()
135     {
136         super.init();
137         m_boobooResponse = new Requester.ForComponent(this);
138         m_bundle.setController(new DefinitionLoader(this));
139     }
140
141
142     /**
143      * Sets this task's project; updates other underlying
144      * project-components.
145      **/

146     public void setProject(Project P)
147     {
148         super.setProject(P);
149         m_bundle.setProject(P);
150     }
151
152
153
154     /**
155      * Sets whether this task will fail if it cannot locate and
156      * load its strings from the specified location.
157      * @param mustExist <i>true</i> if resource bundle must be loaded
158      * @since JWare/AntX 0.4
159      **/

160     public void setMustExist(boolean mustExist)
161     {
162         m_bundle.setMustExist(mustExist);
163     }
164
165
166
167     /**
168      * Sets this task's bundle source information as a reference
169      * to an existing UISMBundle.
170      **/

171     public void setBundleId(Reference reference)
172     {
173         m_bundle.setRefid(reference);
174     }
175
176
177     /**
178      * Sets this task's resource bundle's location as a URL. This
179      * URL setting is checked after both the file and resource options
180      * are checked.
181      * @param url URL string representation (non-null)
182      **/

183     public void setURL(String JavaDoc url)
184     {
185         m_bundle.setURL(url);
186     }
187
188
189     /**
190      * Returns this task's resource bundle's URL location. Returns
191      * <i>null</i> if never set or was set to an invalid URL string.
192      **/

193
194     public URL JavaDoc getURL()
195     {
196         return m_bundle.getURL();
197     }
198
199
200     /**
201      * Sets this task's resource bundle's location. This file setting
202      * is checked first when this task tries to locate its UISM
203      * bundle strings.
204      **/

205     public void setFile(String JavaDoc filepath)
206     {
207         m_bundle.setFile(filepath);
208     }
209
210
211     /**
212      * Returns this task's resource bundle's file location. Returns
213      * <i>null</i> if never set.
214      **/

215     public File JavaDoc getFile()
216     {
217         return m_bundle.getFile();
218     }
219
220
221     /**
222      * Sets this task's resource bundle's base name. Resources are
223      * checked <em>after</em> the {@linkplain #setFile file} property.
224      **/

225     public void setResource(String JavaDoc rsrc)
226     {
227         m_bundle.setResource(rsrc);
228     }
229
230
231     /**
232      * Returns this task's resource bundle's base name. Returns
233      * <i>null</i> if never set.
234      **/

235     public String JavaDoc getResource()
236     {
237         return m_bundle.getResource();
238     }
239
240
241     /**
242      * Returns this task's custom classpath used when loading a class
243      * or system resource. Returns <i>null</i> if never set.
244      **/

245     public Path getClassPath()
246     {
247         return m_bundle.getClassPath();
248     }
249
250
251     /**
252      * Returns new (added) empty (nested) Path for use when searching
253      * for resources.
254      */

255     public Path createClassPath()
256     {
257         return m_bundle.createClassPath();
258     }
259
260
261     /**
262      * Adds new classpath element for use when searching for resources.
263      * @param classpath the search path (non-null)
264      */

265     public void setClassPath(Path classpath)
266     {
267         m_bundle.setClassPath(classpath);
268     }
269
270
271     /**
272      * Set the classpath by-reference for use when searching for
273      * resources.
274      * @param reference a Reference to a Path instance (non-null)
275      */

276     public void setClassPathRef(Reference reference)
277     {
278         m_bundle.setClassPathRef(reference);
279     }
280
281
282     /**
283      * Tells this task to use an existing classloader to search
284      * and/or load resources.
285      * @param r reference to an existing ClassLoader (non-null)
286      **/

287     public void setLoaderRef(Reference r)
288     {
289         m_bundle.setLoaderRef(r);
290     }
291
292
293     /**
294      * Returns this task's custom class loader identifier. Will
295      * return <i>null</i> if never set.
296      **/

297     public String JavaDoc getLoaderRefId()
298     {
299         return m_bundle.getLoaderRefId();
300     }
301
302
303     /**
304      * Instructs this task how to manage the iteration's UISM
305      * context.
306      * @param op the operation
307      **/

308     public void setAction(String JavaDoc op)
309     {
310         m_Op = Tk.lowercaseFrom(op);
311     }
312
313
314     /**
315      * Factory method for our UIStringManager.
316      **/

317     private UIStringManager getUISM()
318     {
319         return new BundleStringManager
320             (m_bundle.newPropertyBundle(m_boobooResponse),null);
321     }
322
323
324     /**
325      * Installs a root UIStringManager (based on this bundle) as the catch-all
326      * for UISMContexts, AssertableTasks, etc. within this thread . Should be done
327      * at most once from an 'init' type task.
328      **/

329     private void installRootSM() throws BuildException
330     {
331         UIStringManager alreadyInstalled = UISMContext.installStringManager(getUISM(),null);
332         if (alreadyInstalled!=null) {//Whups!
333
UISMContext.unwindStringManager(null);
334             String JavaDoc ermsg = AntX.uistrs().get("task.uism.err.too.many.root.bundles");
335             log(ermsg, Project.MSG_ERR);
336             throw new BuildException(ermsg,getLocation());
337         }
338         log("Installed root UIStringManager from URL="+getURL(),Project.MSG_DEBUG);
339     }
340
341
342     /**
343      * Installs a <em>default</em> UIStringManager (based on this bundle)
344      * as the catch-all for UISMContexts, AssertableTasks, etc.
345      * @since JWare/AntX 0.4
346      **/

347     private void installDefaultSM()
348     {
349         boolean alreadyInstalled = !UISMContext.isDefaultUndefined();
350         UISMContext.setDefaultStringManager(getUISM());
351         if (alreadyInstalled) {
352             String JavaDoc ermsg = AntX.uistrs().get("task.uism.warn.repl.dflt.bundle");
353             log(ermsg, Project.MSG_WARN);
354         } else {
355             log("Installed default UIStringManager from URL="+getURL(),Project.MSG_DEBUG);
356         }
357     }
358
359
360
361     /**
362      * Executes this UISM manager's instruction; by default installs a default
363      * thread-specific UIStringManager.
364      * @throws BuildException if a root UIStringManager already installed for
365      * current thread or unable to load strings
366      **/

367     public void execute()
368         throws BuildException
369     {
370         //If as Antlib definition, only install operations are permitted!
371
if (getAntlibClassLoader()!=null) {//=>in Antlib always?
372
if (m_Op!=null && m_Op.startsWith("uninstall")) {
373                 String JavaDoc ermsg = AntX.uistrs().get
374                     ("task.manager.err.illegal.operation",m_Op);
375                 log(ermsg,Project.MSG_ERR);
376                 throw new BuildException(ermsg,getLocation());
377             }
378         }
379
380         //Install|Uninstall UI string managers
381
synchronized(m_bundle) {
382             if (m_Op==null || OP_INSTALL_DEFAULT.equals(m_Op)) {
383                 installDefaultSM();
384             }
385             else if (OP_INSTALL_ROOT.equals(m_Op) ||
386                      Strings.INSTALL.equals(m_Op)/*backward-compat*/) {
387                 installRootSM();
388             }
389             else if (OP_UNINSTALL_ROOT.equals(m_Op) ||
390                      Strings.UNINSTALL.equals(m_Op)/*backward-compat*/) {
391                 log("Trying to uninstall the active root UIStringManager",
392                     Project.MSG_DEBUG);
393                 UISMContext.unwindStringManager(m_boobooResponse);
394             }
395             else if (OP_UNINSTALL_DEFAULT.equals(m_Op)) {
396                 if (!UISMContext.isDefaultUndefined()) {
397                     log("Trying to uninstall the default UIStringManager",
398                         Project.MSG_DEBUG);
399                     AntXFixture.reset(UISMContext.FXID,Strings.DEFAULT,m_boobooResponse);
400                 }
401             }
402             else {
403                 String JavaDoc ermsg = AntX.uistrs().get
404                     ("task.manager.err.unknown.operation",m_Op);
405                 log(ermsg,Project.MSG_WARN);
406             }
407         }
408     }
409
410     private UISMBundle m_bundle= new UISMBundle(true);//NB:delegatee for configuration
411
private String JavaDoc m_Op;//NB:==> install-default
412
private ProblemHandler m_boobooResponse = Responses.ERROR;
413 }
414
415 /* end-of-InitUISMTask.java */
416
Popular Tags