KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ValueURIManagerTask.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.solo;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.PropertyHelper;
37
38 import com.idaremedia.antx.AntX;
39 import com.idaremedia.antx.ExportedProperties;
40 import com.idaremedia.antx.FixtureExaminer;
41 import com.idaremedia.antx.FixtureIds;
42 import com.idaremedia.antx.FixtureInitializer;
43 import com.idaremedia.antx.Iteration;
44 import com.idaremedia.antx.NoiseLevel;
45 import com.idaremedia.antx.ValueURIHandler;
46 import com.idaremedia.antx.apis.BuildError;
47 import com.idaremedia.antx.parameters.FeedbackLevel;
48 import com.idaremedia.antx.starters.SimpleManagerTask;
49
50 /**
51  * A manager task that (un)installs an AntX value URI aware property helper hook. Once
52  * installed this hook lets you use the "<span class="src">${$scheme:[fragment]}</span>"
53  * shorthand string to retrieve arbitrary types of data. This task is a valid
54  * antlib definition task, so you can include it in your antlib scriptlets. Usually
55  * defined as <span class="src">&lt;manageuris&gt;</span>.
56  * <p/>
57  * <b>Example Usage:</b><pre>
58  * -- To Enable --
59  * &lt;manageuris action="enable"/&gt; -or-
60  * &lt;manageuris/&gt;
61  *
62  * -- To Install A Set Of Handlers --
63  * &lt;manageuris action="install"&gt;
64  * &lt;parameter name="foo" value="com.mysoft.FooUriReader"/&gt;
65  * &lt;parameter name="bar" value="com.mysoft.BarUriReader"/&gt;
66  * &lt;/manageuris&gt;
67  *
68  * -- To Disable --
69  * &lt;manageuris action="disable"/&gt;
70  *
71  * -- To Uninstall A Set Of Handlers --
72  * &lt;manageuris action="uninstall"&gt;
73  * &lt;parameter name="foo"/&gt;
74  * &lt;/manageuris&gt;
75  *
76  * -- To Check Whether ValueURI Interpretation Is Enabled --
77  * &lt;manageuris action="check"/&gt; -or-
78  *
79  * &lt;manageuris action="check"&gt;
80  * &lt;parameter name="updateproperty" name="valueuris.enabled"/&gt;
81  * &lt;/manageuris&gt;
82  * </pre>
83  *
84  * @since JWare/AntX 0.5
85  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
86  * @version 0.5
87  * @.safety single
88  * @.group api,helper
89  * @.impl Direct access to project references map is intentional to avoid
90  * the spurious warnings about replaced references.
91  * @see com.idaremedia.antx.ValueURIHandler
92  **/

93
94 public class ValueURIManagerTask extends SimpleManagerTask
95     implements FixtureInitializer
96 {
97     private static final String JavaDoc REFID= FixtureIds.VALUEURI_INTERPRETER;
98     private static final String JavaDoc LABEL = "ValueUriInterpreter";
99
100
101     /**
102      * Initializes a new "enabling" manager task. You must set
103      * the action option of this task to change its default
104      * enable behavior.
105      **/

106     public ValueURIManagerTask()
107     {
108         super(AntX.fixture+"ValueURIManagerTask:","enable");
109     }
110
111
112
113     /**
114      * Verifies that there are no value uri interpreters in the
115      * current property helper hook chain w/o a matching reference
116      * marker.
117      * @param ph the first link in hook chain (non-null)
118      **/

119     private void verifyNoneInChain(PropertyHelper ph)
120     {
121         do {
122             if (ph.getNext() instanceof Interpreter) {
123                 String JavaDoc error = Iteration.uistrs().get
124                     ("fixture.supPh.stack.corrupted", LABEL);
125                 log(error, Project.MSG_ERR);
126                 throw new BuildError(error);
127             }
128             ph = ph.getNext();
129         } while(ph!=null);
130     }
131
132
133
134     /**
135      * Converts a fully qualified class name to a ValueURIHandler
136      * class instance.
137      * @param e the slot o' information
138      * @return the class reference
139      * @throws BuildException if unable to load class or class
140      * is incompatible.
141      **/

142     private Class JavaDoc loadHandlerClass(Map.Entry JavaDoc e)
143     {
144         String JavaDoc classname = (String JavaDoc)e.getValue();
145         try {
146             Class JavaDoc c = Class.forName(classname);
147             if (!ValueURIHandler.class.isAssignableFrom(c)) {
148                 throw new ClassCastException JavaDoc();
149             }
150             return c;
151         } catch (Exception JavaDoc anyX) {
152             String JavaDoc error = Iteration.uistrs().get("task.bad.custimpl.class1",
153                 classname, ValueURIHandler.class.getName());
154             log(error, Project.MSG_ERR);
155             throw new BuildException(error, anyX, getLocation());
156         }
157     }
158
159
160
161     /**
162      * Will install a set of value uri handlers. Each handler is
163      * defined by a generic parameter; the parameter's name is the
164      * value uri prefix, and the value is the fully qualified class
165      * name of the handler.
166      * @param args list of value uri handlers to enable explicitly
167      **/

168     public void doInstall(Map JavaDoc args)
169     {
170         if (!args.isEmpty()) {
171             Iterator JavaDoc itr= args.entrySet().iterator();
172             Class JavaDoc oldc, newc;
173             boolean warnreplace = isVerbose();
174
175             while (itr.hasNext()) {
176                 Map.Entry JavaDoc e = (Map.Entry JavaDoc)itr.next();
177                 String JavaDoc prefix = (String JavaDoc)e.getKey();
178
179                 if (!FixtureExaminer.isBuiltinValueURIScheme(prefix)) {
180                     newc = loadHandlerClass(e);
181                     oldc = Iteration.replaceValueURIHandler(prefix,newc);
182                     if (warnreplace && oldc!=null && oldc!=newc) {
183                         String JavaDoc warning = Iteration.uistrs().get("valueuri.repl.handlr",
184                             prefix, oldc.getName(), newc.getName());
185                         log(warning, Project.MSG_WARN);
186                     } else {
187                         log("For ("+prefix+") installed value URI handler "+newc.getName(),
188                             Project.MSG_VERBOSE);
189                     }
190                 } else {
191                     String JavaDoc warning = Iteration.uistrs().get("valueuri.resrved.handlr",prefix);
192                     log(warning, Project.MSG_WARN);
193                 }
194             }
195         }
196     }
197
198
199
200     /**
201      * Will install a property helper hook to interpret AntX value
202      * uris. Does nothing if an interpreter is already installed for
203      * the enclosing project (issues warning). If value uri handlers
204      * are declared (as parameters), those handlers are added automatically.
205      * @param args list of value uri handlers to also install
206      **/

207     public void doEnable(Map JavaDoc args)
208     {
209         Project p = getProject();
210         Map JavaDoc ht = p.getReferences();
211         boolean warn = false;
212
213         synchronized(ht) {//NB:forces FxExaminer queries to wait too...
214
Object JavaDoc o = ht.get(REFID);
215             if (o==null) {
216                 Interpreter interpreter;
217                 PropertyHelper ph = PropertyHelper.getPropertyHelper(p);
218                 synchronized(ph) {
219                     verifyNoneInChain(ph);
220                     interpreter = new Interpreter(p);
221                     interpreter.setNext(ph.getNext());
222                     ph.setNext(interpreter);
223                 }
224                 ht.put(REFID,interpreter);
225             } else {
226                 warn = true;
227             }
228         }
229         if (!args.isEmpty()) {
230             this.doInstall(args);
231             warn = false;
232         }
233         if (warn && isVerbose()) {
234             String JavaDoc warning = Iteration.uistrs().get("fixture.supPh.install.once",LABEL);
235             log(warning, Project.MSG_WARN);
236         }
237     }
238
239
240
241     /**
242      * Will uninstall a set of named value uri handlers from the
243      * current iteration. Does nothing if the args list is empty.
244      * @param args list of value uri handlers to install
245      **/

246     public void doUninstall(Map JavaDoc args)
247     {
248         if (!args.isEmpty()) {
249             Iterator JavaDoc itr= args.keySet().iterator();
250             while (itr.hasNext()) {
251                 String JavaDoc prefix = (String JavaDoc)itr.next();
252                 if (!FixtureExaminer.isBuiltinValueURIScheme(prefix)) {
253                     Iteration.removeValueURIHandler(prefix);
254                 } else {
255                     String JavaDoc warning = Iteration.uistrs().get("valueuri.resrved.handlr",prefix);
256                     log(warning, Project.MSG_WARN);
257                 }
258             }
259         }
260     }
261
262
263
264     /**
265      * Will uninstall a property helper hook that interprets AntX
266      * value uris. Does nothing if no such interpreter exists in
267      * the current project (issues a warning).
268      * @param ignoredArgs [ignored]
269      **/

270     public void doDisable(Map JavaDoc ignoredArgs)
271     {
272         Project p = getProject();
273         Map JavaDoc ht = p.getReferences();
274         boolean warn=false;
275
276         synchronized(ht) {//NB:forces FxExaminer queries to wait too...
277
Object JavaDoc o = ht.get(REFID);
278             if (o instanceof Interpreter) {
279                 Interpreter interpreter = (Interpreter)o;
280                 ht.remove(REFID);
281
282                 PropertyHelper ph = PropertyHelper.getPropertyHelper(p);
283                 synchronized(ph) {
284                     PropertyHelper prevPh=null;
285                     do {
286                         if (ph.getNext()==interpreter) { break; }
287                         prevPh = ph;
288                         ph = ph.getNext();
289                     } while(ph!=null);
290                     if (ph!=null) {
291                         ph.setNext(interpreter.getNext());
292                         if (prevPh!=null) {
293                             prevPh.setNext(ph);
294                         }
295                     } else {
296                         warn = true;
297                     }
298                     interpreter.setNext(null);
299                 }
300                 interpreter = null;
301             }
302             o = null;
303         }
304
305         if (warn && isVerbose()) {
306             String JavaDoc warning= Iteration.uistrs().get("fixture.supPh.not.instald",LABEL);
307             log(warning,Project.MSG_WARN);
308         }
309     }
310
311
312
313     /**
314      * Tests for the existence of a value uri interpreter in the
315      * enclosing project. The check generates one of three possible
316      * answers: <span class="src">yes</span>, <span class="src">no</span>,
317      * and <span class="src">unknown</span>. An unknown is flagged if
318      * <em>something</em> is declared as an interpreter but is not
319      * known to this manager task.
320      * <p/>
321      * This action can be configured to save its answer string to a
322      * project property or variable; set either the
323      * "<span class="src">updateproperty</span>" or
324      * "<span class="src">updatevariable</span>"
325      * parameters to the name of the fixture elements to be set.
326      * @param args custom update property instructions (non-null)
327      **/

328     public void doCheck(Map JavaDoc args)
329     {
330         Project p = getProject();
331         Map JavaDoc ht = p.getReferences();
332         String JavaDoc result = "no";
333
334         synchronized(ht) {//NB:forces FxExaminer queries to wait too...
335
Object JavaDoc o = ht.get(REFID);
336             if (o instanceof Interpreter) {
337                 result = "yes";
338             }
339             else if (o!=null) {
340                 result = "unknown";
341             }
342         }
343
344         boolean saved=false;
345
346         Object JavaDoc o = args.get("updateproperty");
347         if (o!=null) {
348             String JavaDoc property = o.toString();
349             FixtureExaminer.checkIfProperty(p,m_rqlink,property,true);
350             p.setNewProperty(property,result);
351             saved = true;
352         }
353         o = args.get("updatevariable");
354         if (o!=null) {
355             ExportedProperties.set(o.toString(),result);
356             saved = true;
357         }
358         if (!saved) {
359             int level = NoiseLevel.getDefault(p).getNativeIndex();
360             String JavaDoc msg = Iteration.uistrs().get("valueuri.instaled",result);
361             log(msg, level);
362         }
363     }
364
365
366
367     /**
368      * Our hook into the standard Ant project property framework.
369      * This class must play nice with other installed hooks.
370      *
371      * @since JWare/AntX 0.5
372      * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
373      * @version 0.5
374      * @.safety guarded (inherited)
375      * @.group impl,helper
376      */

377     private static class Interpreter extends PropertyHelper
378     {
379         private final Project m_project;
380
381         Interpreter(Project project) {
382             m_project = project;
383         }
384
385         /**
386          * Filters the incoming value through our fixture examiner.
387          **/

388         public Object JavaDoc getPropertyHook(String JavaDoc ns, String JavaDoc name,
389                                       boolean userOnly) {
390             Object JavaDoc o;
391             if ((ns==null || ns.length()==0) && !userOnly) {
392                 o= FixtureExaminer.findValue(m_project,name,null);
393             } else {
394                 o= null;
395             }
396             if (o==null && getNext()!=null) {
397                 o = getNext().getPropertyHook(ns,name,userOnly);
398             }
399             return o;
400         }
401
402         /**
403          * Always delegates to next in hook chain if available.
404          **/

405         public boolean setPropertyHook(String JavaDoc ns, String JavaDoc name, Object JavaDoc value,
406                                        boolean inherited, boolean userOnly,
407                                        boolean isNew) {
408             if (getNext()!=null) {
409                 return getNext().setPropertyHook(ns,name,value,inherited,
410                                                  userOnly,isNew);
411             }
412             return false;
413         }
414     }
415
416
417     private boolean isVerbose()
418     {
419         //NB:undef => is quiet (don't warn)
420
return !FeedbackLevel.isQuietish(getFeedbackLevel(),true,true);
421     }
422 }
423
424 /* end-of-ValueURIManagerTask.java */
Popular Tags