KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: LocalTargetCaller.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.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.taskdefs.Ant;
40 import org.apache.tools.ant.taskdefs.Property;
41 import org.apache.tools.ant.types.PropertySet;
42
43 import com.idaremedia.antx.AntX;
44 import com.idaremedia.antx.AntXFixture;
45 import com.idaremedia.antx.ownhelpers.ProjectDependentSkeleton;
46 import com.idaremedia.antx.ownhelpers.ScopedProperties;
47
48 /**
49  * Implementation of {@linkplain TargetCaller} for {@linkplain LocalTargetTask}.
50  * Without any overlayed properties, this caller is an expensive adapter of the
51  * <span class="src">Project</span> object's <span class="src">executeTarget</span>
52  * method (so you really want to use it only if overlaying is likely).
53  * <p>
54  * When you need to overlay properties for multiple targets, it is much more
55  * efficient to run multiple targets from a single target caller (by resetting
56  * the <span class="src">targetName</span> attribute) than it is to create a new
57  * target caller everytime. The caller will cache the property and propertyset
58  * declarations for (re)installation for every target.
59  *
60  * @since JWare/AntX 0.4
61  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
62  * @version 0.5
63  * @.safety single
64  * @.group impl,helper
65  **/

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

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

90     public LocalTargetCaller(CallerTask task)
91     {
92         setCaller(task);
93     }
94
95
96
97     /**
98      * Initializes this caller's controlling task. Inlined
99      * steps will be read from this task's owning target.
100      * @param task controlling task (non-null)
101      * @.sideeffect This caller's target name is initialized
102      * to the task's enclosing target's name (if exists).
103      **/

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

120     public void setTarget(String JavaDoc targetName)
121     {
122         m_targetName = targetName;
123     }
124
125
126     /**
127      * Returns this caller's target's name.
128      **/

129     public String JavaDoc getTargetName()
130     {
131         return m_targetName;
132     }
133
134
135     /**
136      * Sets the step on whose behave this caller exists.
137      **/

138     public final void setStepName(String JavaDoc stepName)
139     {
140         m_stepName = stepName;
141     }
142
143
144     /**
145      * Returns the step on whose behave this caller exists.
146      **/

147     public final String JavaDoc getStepName()
148     {
149         return m_stepName;
150     }
151
152
153     /**
154      * Create a new property to overlay called target.
155      **/

156     public Property createProperty()
157     {
158         Property p = new Property();
159         p.setProject(getProject());
160         if (m_plist==null) {
161             m_plist = AntXFixture.newList();
162         }
163         m_plist.add(p);
164         return p;
165     }
166
167
168     /**
169      * Create a new property set to overlay called target.
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      * Create a new reference to overlay called target.
186      * Currently ignored.
187      **/

188     public Ant.Reference createReference()
189     {
190         return null;
191     }
192
193
194
195     /**
196      * Installs all of our property declarations as a
197      * PropertyHelper hook.
198      **/

199     private void installOverlay()
200     {
201         Project P= getProjectNoNull();
202         if (m_plist!=null) {
203             m_overlay = new ScopedProperties(P,true);
204             Iterator JavaDoc itr= m_plist.iterator();
205             while (itr.hasNext()) {
206                 Object JavaDoc o = itr.next();
207                 if (o instanceof Property) {
208                     m_overlay.put((Property)o);
209                 } else {
210                     m_overlay.put((PropertySet)o);
211                 }
212             }
213             m_overlay.install();
214         }
215     }
216
217
218     /**
219      * Undoes the effect of {@linkplain #installOverlay}.
220      **/

221     private void uninstallOverlay()
222     {
223         if (m_overlay!=null) {
224             m_overlay.uninstall(null);
225             m_overlay=null;
226         }
227     }
228
229
230     private Task[] getFreshTargetTasks(String JavaDoc targetName)
231     {
232         Target t = (Target)getProject().getTargets().get(getTargetName());
233         //@.todo Add call to 'reconfigure' if tasks != UnknownElement
234
return t.getTasks();
235     }
236
237
238     /**
239      * Executes the target this caller references with
240      * overlay properties if necessary.
241      * @.bug Update to try to reconfigure tasks that are Reconfigurable
242      **/

243     public void run()
244         throws BuildException
245     {
246         installOverlay();
247         try {
248             if (getStepName()==null) {
249                 getProject().executeTarget(getTargetName());
250             }
251             else {
252                 StepLauncher steplaunch = new StepLauncher();
253                 steplaunch.setProject(getProject());
254
255                 Task[] fromTasks = getFreshTargetTasks(getTargetName());
256
257                 InlineStep step = (InlineStep)steplaunch.getInlinedTarget
258                     (getStepName(),InlineStep.class,fromTasks,1,1);
259
260                 steplaunch = null;//gc
261

262                 if (step!=null) {
263                     step.maybeConfigure();
264                     step.run();
265                 } else {
266                     String JavaDoc error = AntX.uistrs().
267                         get("flow.steplaunch.missing.step", getStepName(),
268                             getTargetName());
269                     m_owningTask.log(error,Project.MSG_ERR);
270                     throw new BuildException
271                         (error, m_owningTask.getLocation());
272                 }
273             }
274         } finally {
275             uninstallOverlay();
276         }
277     }
278
279
280
281     /**
282      * Convenient {@linkplain #run() run} alternative that auto-installs
283      * a required property into the to-be-called target's runtime
284      * environmentbefore it's executed.
285      * @param property property name (non-null)
286      * @param value property value-- used as-is no additional
287      * replacements are done (non-null)
288      **/

289     public final void run(String JavaDoc property, String JavaDoc value)
290         throws BuildException
291     {
292         Property p= this.createProperty();
293         p.setName(property);
294         p.setValue(value);
295         run();
296     }
297
298
299
300     /**
301      * Convenient {@linkplain #run() run} alternative that auto-installs
302      * a set of properties into the to-be-called target's runtime
303      * environment before it's executed.
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 auto-installs
325      * arbitrary kinds of fixture data into the to-be-called target's
326      * runtime environment before it's executed.
327      * @param prep client preparation snippet (non-null)
328      **/

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