KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > flowcontrol > call > MacroInstanceCaller


1 /**
2  * $Id: MacroInstanceCaller.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.flowcontrol.call;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.Target;
38 import org.apache.tools.ant.Task;
39 import org.apache.tools.ant.UnknownElement;
40 import org.apache.tools.ant.taskdefs.Ant;
41 import org.apache.tools.ant.taskdefs.Property;
42 import org.apache.tools.ant.types.PropertySet;
43
44 import com.idaremedia.antx.AntX;
45 import com.idaremedia.antx.AntXFixture;
46 import com.idaremedia.antx.helpers.InnerNameValuePair;
47 import com.idaremedia.antx.ownhelpers.ProjectDependentSkeleton;
48 import com.idaremedia.antx.ownhelpers.ScopedProperties;
49 import com.idaremedia.antx.ownhelpers.TaskExaminer;
50
51 /**
52  * Implementation of {@linkplain TargetCaller} for local macrodefs. Without any
53  * overlayed properties and/or attributes, this caller is an expensive adapter of the
54  * standard macro invocation mechanism (so you really want to use it only if
55  * overlaying is likely).
56  *
57  * @since JWare/AntX 0.5
58  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
59  * @version 0.5
60  * @.safety single
61  * @.group impl,helper
62  **/

63
64 public final class MacroInstanceCaller extends ProjectDependentSkeleton
65     implements TargetCaller, ConfigurableCaller
66 {
67     private static final String JavaDoc IAM_= AntX.flow+"MacroInstanceCaller";
68
69
70     /**
71      * Initializes a new local macro instance caller. This
72      * caller's target and project must be defined before it
73      * is used.
74      * @see #setCaller setCaller(&#8230;)
75      **/

76     public MacroInstanceCaller()
77     {
78     }
79
80
81     /**
82      * Initializes a new local macro instance caller. This
83      * caller's target must be defined before it is used.
84      * @param task controlling task (non-null)
85      * @see #setCaller setCaller(&#8230;)
86      **/

87     public MacroInstanceCaller(CallerTask task)
88     {
89         setCaller(task);
90     }
91
92
93
94     /**
95      * Initializes this caller's controlling task.
96      * @param task controlling task (non-null)
97      * @.sideeffect This caller's target name is initialized
98      * to the task's enclosing target's name (if exists).
99      **/

100     public void setCaller(Task task)
101     {
102         AntX.require_(task!=null,IAM_,"setCalr- nonzro task");
103         Target parent = task.getOwningTarget();
104         if (parent!=null) {
105             setTarget(parent.getName());
106         }
107         setProject(task.getProject());
108         m_owningTask = task;
109     }
110
111
112
113     /**
114      * (Re)Sets the target macro's name for this caller.
115      **/

116     public void setTarget(String JavaDoc targetName)
117     {
118         m_targetName = targetName;
119     }
120
121
122     /**
123      * Returns this caller's target macro's name.
124      **/

125     public String JavaDoc getTargetName()
126     {
127         return m_targetName;
128     }
129
130
131
132     /**
133      * Unsupported operation; only top-level macros can be called.
134      * @throws UnsupportedOperationException always.
135      **/

136     public final void setStepName(String JavaDoc stepName)
137     {
138         throw new UnsupportedOperationException JavaDoc();
139     }
140
141
142     /**
143      * Returns <i>null</i> always; can only call top-level macros.
144      **/

145     public final String JavaDoc getStepName()
146     {
147         return null;
148     }
149
150
151
152     /**
153      * Create a new property to passthru to called macro.
154      **/

155     public Property createProperty()
156     {
157         Property p = new Property();
158         p.setProject(getProject());
159         if (m_plist==null) {
160             m_plist = AntXFixture.newList();
161         }
162         m_plist.add(p);
163         return p;
164     }
165
166
167
168     /**
169      * Create a new property set to passthru to called macro.
170      **/

171     public PropertySet createPropertySet()
172     {
173         PropertySet p = new PropertySet();//?use just one?
174
p.setProject(getProject());
175         if (m_plist==null) {
176             m_plist = AntXFixture.newList();
177         }
178         m_plist.add(p);
179         return p;
180     }
181
182
183
184     /**
185      * Unsupported operation; only macro parameters can be created.
186      * @throws UnsupportedOperationException always.
187      **/

188     public Ant.Reference createReference()
189     {
190         throw new UnsupportedOperationException JavaDoc();
191     }
192
193
194
195     /**
196      * Create a new macro attribute to passthru to called macro.
197      **/

198     public InnerNameValuePair createAttribute()
199     {
200         InnerNameValuePair a = new InnerNameValuePair();
201         if (m_plist==null) {
202             m_plist = AntXFixture.newList();
203         }
204         m_plist.add(a);
205         return a;
206     }
207
208
209
210     /**
211      * Installs all of our property declarations as a PropertyHelper
212      * hook. Any macro attribute definitions are returned to be
213      * given directly to macro instance factory.
214      **/

215     private Map JavaDoc installOverlay()
216     {
217         Project P= getProjectNoNull();
218         Map JavaDoc attrs = null;
219         if (m_plist!=null) {
220             m_overlay = new ScopedProperties(P,true);
221             Iterator JavaDoc itr= m_plist.iterator();
222             while (itr.hasNext()) {
223                 Object JavaDoc o = itr.next();
224                 if (o instanceof InnerNameValuePair) {
225                     InnerNameValuePair nv = (InnerNameValuePair)o;
226                     nv.verifyNamed();
227                     if (attrs==null) {
228                         attrs = AntXFixture.newMap();
229                     }
230                     attrs.put(nv.getName(), nv.getValue(P,true));
231                 }
232                 else if (o instanceof Property) {
233                     m_overlay.put((Property)o);
234                 }
235                 else {
236                     m_overlay.put((PropertySet)o);
237                 }
238             }
239             m_overlay.install();
240         }
241         return attrs;
242     }
243
244
245
246     /**
247      * Undoes the effect of {@linkplain #installOverlay}.
248      **/

249     private void uninstallOverlay()
250     {
251         if (m_overlay!=null) {
252             m_overlay.uninstall(null);
253             m_overlay=null;
254         }
255     }
256
257
258
259
260     /**
261      * Executes a new instance of this caller's named macro. Any custom
262      * properties are converted into property overlay properties; named
263      * attribute values are passed directly into the macro instance
264      * factory.
265      **/

266     public void run()
267         throws BuildException
268     {
269         Map JavaDoc attrs = installOverlay();
270         try {
271             UnknownElement launch = TaskExaminer.newUEProxy
272                 (getTargetName(),attrs,(String JavaDoc)null,getProject());
273             launch.setOwningTarget(m_owningTask.getOwningTarget());
274             launch.perform();
275         } finally {
276             uninstallOverlay();
277         }
278     }
279
280
281
282     /**
283      * Convenient {@linkplain #run() run} alternative that defines
284      * a required attribute of the macro instance before we execute
285      * it.
286      * @param property property name (non-null)
287      * @param value property value-- used as-is no additional
288      * replacements are done (non-null)
289      **/

290     public final void run(String JavaDoc property, String JavaDoc value)
291         throws BuildException
292     {
293         Property p= this.createProperty();
294         p.setName(property);
295         p.setValue(value);
296         run();
297     }
298
299
300
301     /**
302      * Convenient {@linkplain #run() run} alternative that defines
303      * a set of macro instance attributes before we execute it.
304      * @param properties properties file (non-null)
305      **/

306     public final void run(Map JavaDoc properties)
307         throws BuildException
308     {
309         if (!properties.isEmpty()) {
310             Iterator JavaDoc itr = properties.entrySet().iterator();
311             while (itr.hasNext()) {
312                 Map.Entry JavaDoc item = (Map.Entry JavaDoc)itr.next();
313                 Property p= this.createProperty();
314                 p.setName(item.getKey().toString());
315                 p.setValue(item.getValue().toString());
316             }
317         }
318         run();
319     }
320
321
322
323     /**
324      * Convenient {@linkplain #run() run} alternative that defines
325      * non-trivial macro instance attributes before we execute it.
326      * @param prep client preparation snippet (non-null)
327      **/

328     public final void run(TargetCaller.Prep prep)
329         throws BuildException
330     {
331         prep.prepare(this);
332         run();
333     }
334
335
336     private String JavaDoc m_targetName;
337     private List JavaDoc m_plist;
338     private Task m_owningTask;
339     private ScopedProperties m_overlay;//as-propertyhook
340
}
341
342 /* end-of-MacroInstanceCaller.java */
343
Popular Tags