KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: AnyTargetCaller.java 180 2007-03-15 12:56:38Z 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.flowcontrol.call;
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.Task;
37 import org.apache.tools.ant.taskdefs.Ant;
38 import org.apache.tools.ant.taskdefs.CallTarget;
39 import org.apache.tools.ant.taskdefs.Property;
40 import org.apache.tools.ant.types.PropertySet;
41
42 import com.idaremedia.antx.parameters.FixturePassthru;
43
44 /**
45  * TargetCaller implementation that calls any existing top-level project target.
46  * Based on the standard ant <span class="src">antcall</span> task so target is
47  * executed in a partitioned child project. Modifications to the child project
48  * are not transferred back to the (caller) parent.
49  *
50  * @since JWare/AntX 0.1
51  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
52  * @version 0.5
53  * @.safety single
54  * @.group impl,infra
55  * @see OnceTask
56  * @.impl Ant 1.6.3 CallTarget has some new initialization dependencies
57  * with setters and its underlying "callee". Calling setTarget twice
58  * before execution ignores the second target-set if an init has
59  * been done between.
60  **/

61
62 public class AnyTargetCaller extends CallTarget implements TargetCaller
63 {
64     /**
65      * Initializes a new target caller from a call task.
66      * @param from the new target caller's controlling task
67      * @param targetName the target to be called
68      **/

69     public AnyTargetCaller(OnceTask from, String JavaDoc targetName)
70     {
71         setTaskName("call");
72         setProject(from.getProject());
73         setOwningTarget(from.getOwningTarget());
74         setLocation(from.getLocation());
75         init(from,from.getPassthruOption());
76         setTarget(targetName);
77     }
78
79
80
81     /**
82      * Initializes a new AntX-based target caller from any
83      * source. Caller is responsible for setting this task's
84      * project, owning target, inherit flags, etc.
85      * @param targetName the target to be called
86      * @since JWare/AntX 0.5
87      **/

88     public AnyTargetCaller(Project project, String JavaDoc targetName)
89     {
90         setProject(project);
91         setTaskName("call");
92         setTarget(targetName);
93     }
94
95
96
97     /**
98      * Ensures any presetting of target name is redone with new
99      * "ant" callee (inherited).
100      * @since JWare/AntX 0.5
101      * @.impl Since Ant 1.6.3, force any new callee (inherited) to
102      * be resynced with any presetting of the target's name.
103      **/

104     public void init()
105     {
106         super.init();
107         if (m_targetName!=null) {
108             super.setTarget(m_targetName);
109         }
110     }
111
112
113
114     /**
115      * Initializes this caller from the controlling task.
116      * @param from controlling task whose context should be
117      * used (non-null)
118      * @param fixturecontrol bits of controller's fixture
119      * to pass along (non-null)
120      * @since JWare/AntX 0.4
121      **/

122     public void init(Task from, FixturePassthru fixturecontrol)
123     {
124         setProject(from.getProject());
125         setOwningTarget(from.getOwningTarget());
126         setLocation(from.getLocation());
127
128         switch (fixturecontrol.getIndex()) {
129             case FixturePassthru.ALL_INDEX: {
130                 setInheritRefs(true);
131                 setInheritAll(true);
132                 break;
133             }
134             case FixturePassthru.PROPERTIES_INDEX: {
135                 setInheritAll(true);
136                 break;
137             }
138             case FixturePassthru.REFERENCES_INDEX: {
139                 setInheritRefs(true);
140                 break;
141             }
142             case FixturePassthru.NONE_INDEX: {
143                 setInheritRefs(false);
144                 setInheritAll(false);
145                 break;
146             }
147         }
148     }
149
150
151     /**
152      * (Re)Sets the target name of this caller.
153      **/

154     public void setTarget(String JavaDoc targetName)
155     {
156         super.setTarget(targetName);
157         m_targetName = targetName;//no-getter on inherited
158
}
159
160
161     /**
162      * Returns this caller's target's name.
163      **/

164     public String JavaDoc getTargetName()
165     {
166         return m_targetName;
167     }
168
169
170     /**
171      * Sets the step on whose behave this caller exists.
172      **/

173     public final void setStepName(String JavaDoc stepName)
174     {
175         m_stepName = stepName;
176     }
177
178
179     /**
180      * Returns the step on whose behave this caller exists.
181      **/

182     public final String JavaDoc getStepName()
183     {
184         return m_stepName;
185     }
186
187
188     /**
189      * Create a new property to passthru to called target.
190      **/

191     public Property createProperty()
192     {
193         return createParam();
194     }
195
196
197     /**
198      * Create a new property set to passthru to called
199      * target.
200      * @since JWare/AntX 0.4
201      **/

202     public PropertySet createPropertySet()
203     {
204         PropertySet properties = new PropertySet();
205         properties.setProject(getProject());
206         addPropertyset(properties);
207         return properties;
208     }
209
210
211     /**
212      * Create a new reference to passthru to called target.
213      * @since JWare/AntX 0.3
214      **/

215     public Ant.Reference createReference()
216     {
217         Ant.Reference r = new Ant.Reference();
218         addReference(r);
219         return r;
220     }
221
222
223     /**
224      * Executes the target this caller references.
225      **/

226     public void run()
227         throws BuildException
228     {
229         maybeConfigure();
230         execute();
231     }
232
233
234     /**
235      * Convenient {@linkplain #run() run} alternative that auto-installs
236      * a required property into the to-be-called target's runtime
237      * environmentbefore it's executed.
238      * @param property property name (non-null)
239      * @param value property value-- used as-is no additional
240      * replacements are done (non-null)
241      **/

242     public final void run(String JavaDoc property, String JavaDoc value)
243         throws BuildException
244     {
245         Property p= this.createProperty();
246         p.setName(property);
247         p.setValue(value);
248         run();
249     }
250
251
252     /**
253      * Convenient {@linkplain #run() run} alternative that auto-installs
254      * a set of properties into the to-be-called target's runtime
255      * environment before it's executed.
256      * @param properties properties file (non-null)
257      * @since JWare/AntX 0.4
258      **/

259     public final void run(Map JavaDoc properties)
260         throws BuildException
261     {
262         if (!properties.isEmpty()) {
263             Iterator JavaDoc itr = properties.entrySet().iterator();
264             while (itr.hasNext()) {
265                 Map.Entry JavaDoc item = (Map.Entry JavaDoc)itr.next();
266                 Property p= this.createProperty();
267                 p.setName(item.getKey().toString());
268                 p.setValue(item.getValue().toString());
269             }
270         }
271         run();
272     }
273
274
275     /**
276      * Convenient {@linkplain #run() run} alternative that auto-installs
277      * arbitrary kinds of fixture data into the to-be-called target's
278      * runtime environment before it's executed.
279      * @param prep client preparation snippet (non-null)
280      **/

281     public final void run(TargetCaller.Prep prep)
282         throws BuildException
283     {
284         prep.prepare(this);
285         run();
286     }
287
288
289     private String JavaDoc m_targetName, m_stepName="";
290 }
291
292 /* end-of-AnyTargetCaller.java */
293
Popular Tags