KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: StepLauncherCaller.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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 as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your option) any later
9  * 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.Target;
37 import org.apache.tools.ant.Task;
38 import org.apache.tools.ant.taskdefs.Property;
39
40 import com.idaremedia.antx.AntX;
41 import com.idaremedia.antx.Iteration;
42 import com.idaremedia.antx.flowcontrol.FlowConstants;
43 import com.idaremedia.antx.ownhelpers.TaskExaminer;
44
45 /**
46  * Special TargetCaller that calls the special step-launcher target (which in turn calls
47  * back into the source target's list of steps).
48  *
49  * @since JWare/AntX 0.1
50  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
51  * @version 0.5
52  * @.safety single
53  * @.group impl,infra
54  * @see StepLauncher
55  * @see OnceTask
56  **/

57
58 public final class StepLauncherCaller extends AnyTargetCaller
59 {
60     /** Used to cache (per project) whether special target has been
61      * has been located and verified.
62      * @since JWare/AntX 0.4
63      **/

64     private static final String JavaDoc VERIFIED_FLAG=
65         AntX.ANTX_INTERNAL_ID+"steplaunch.specialtargetname";
66
67
68     /**
69      * Creates new step-launcher caller. Will look for the default special
70      * target.
71      * @throws BuildException if unable to locate default special target or
72      * that target is improperly defined (missing StepLauncher task)
73      **/

74     public StepLauncherCaller(OnceTask from)
75         throws BuildException
76     {
77         super(from, findSpecialTarget(from));
78     }
79
80
81     /**
82      * Creates new custom steplauncher caller. Will look for a custom
83      * target (which must be structured like a default special target
84      * with single steplaunch task).
85      * @throws BuildException if unable to locate special target or
86      * that target is improperly defined
87      **/

88     public StepLauncherCaller(OnceTask from, String JavaDoc targetName)
89     {
90         super(from,targetName);
91         verifySpecialTarget(from,targetName);
92     }
93
94
95     /**
96      * Creates pre-verified launcher caller. Used internal to this package.
97      **/

98     StepLauncherCaller(OnceTask from, String JavaDoc targetName, boolean verified)
99     {
100         super(from,targetName);
101         if (!verified) {
102             verifySpecialTarget(from,targetName);
103         }
104     }
105
106
107
108     /**
109      * Factory method to creates a preconfigured standard step-launcher caller.
110      * The new caller's environment is automatically setup with the required
111      * step-launcher variables like step-name, step-class, etc.
112      * @see FlowConstants#DEFAULT_STEP_LAUNCHER_TARGET_PROP
113      * @see FlowConstants#DEFAULT_STEP_LAUNCHER_STEPNAME_PROP
114      **/

115     public static final StepLauncherCaller create
116         (OnceTask from, boolean verified, String JavaDoc specialTargetName,
117          String JavaDoc stepName, String JavaDoc stepClass, int searchDepth)
118     {
119         StepLauncherCaller caller;
120         caller = new StepLauncherCaller(from,specialTargetName,verified);
121         caller.setStepName(stepName);
122
123         Property p;
124
125         p = caller.createProperty();
126         p.setName(FlowConstants.DEFAULT_STEP_LAUNCHER_TARGET_PROP);
127         p.setValue(from.getOwningTarget().getName());
128
129         p = caller.createProperty();
130         p.setName(FlowConstants.DEFAULT_STEP_LAUNCHER_STEPNAME_PROP);
131         p.setValue(stepName);
132
133         p = caller.createProperty();
134         p.setName(FlowConstants.DEFAULT_STEP_LAUNCHER_STEPCLASS_PROP);
135         p.setValue(stepClass);
136
137         p = caller.createProperty();
138         p.setName(FlowConstants.DEFAULT_STEP_LAUNCHER_SEARCHDEPTH_PROP);
139         p.setValue(String.valueOf(searchDepth));
140
141         return caller;
142     }
143
144
145     /**
146      * Verifies the definition of a special step-launcher target.
147      **/

148     final static void verifySpecialTarget(OnceTask from,
149                                           String JavaDoc specialTargetName)
150     {
151         Project P = from.getProject();
152         String JavaDoc doneFlag = P.getProperty(VERIFIED_FLAG);
153         if (specialTargetName.equals(doneFlag)) {
154             return;
155         }
156         Map JavaDoc targets = (Map JavaDoc)P.getTargets().clone();//NB:MT-safer
157
Iterator JavaDoc itr= targets.values().iterator();
158         while (itr.hasNext()) {
159             Target target = (Target)itr.next();
160             if (specialTargetName.equals(target.getName())) {
161                 Task[] tasks = target.getTasks();
162                 if (tasks.length==1) {
163                     Task task = TaskExaminer.trueTask(tasks[0],COI_,from);
164                     if (task instanceof StepLauncher) {
165                         P.setNewProperty(VERIFIED_FLAG,specialTargetName);
166                         return;//=> this painful hack will work!
167
}
168                 }
169                 break;
170             }
171         }
172         throw new BuildException
173             (Iteration.uistrs().get("flow.bad.specialtarget", specialTargetName));
174     }
175
176
177     /**
178      * Looks for the default special target within the caller's project.
179      * The special target is used by step-callers to cope with the existing
180      * file requirement of the 'antcall' and 'ant' tasks.
181      **/

182     final static String JavaDoc findSpecialTarget(OnceTask from)
183     {
184         final Project P= from.getProject();
185
186         String JavaDoc specialTargetName =
187             P.getProperty(FlowConstants.DEFAULT_STEP_CALLER_SPECIAL_TARGETNAME_PROP);
188
189         if (specialTargetName==null) {
190             specialTargetName = FlowConstants.DEFAULT_STEP_CALLER_SPECIAL_TARGETNAME;
191         }
192         verifySpecialTarget(from, specialTargetName);
193
194         return specialTargetName;
195     }
196
197
198     /**
199      * Controls the amount of peek-under for UnknownElement placeholders
200      * nested inside task containers.
201      * @since JWare/AntX 0.4
202      **/

203     private static final Class JavaDoc[] COI_= {
204         StepLauncher.class
205     };
206
207 }
208
209
210 /* end-of-StepLauncherCaller.java */
211
Popular Tags