1 package org.apache.maven.werkz.jelly;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 import java.util.StringTokenizer;
68
69 import org.apache.commons.jelly.JellyTagException;
70 import org.apache.commons.jelly.XMLOutput;
71 import org.apache.commons.logging.Log;
72 import org.apache.commons.logging.LogFactory;
73 import org.apache.maven.werkz.Action;
74 import org.apache.maven.werkz.CyclicGoalChainException;
75 import org.apache.maven.werkz.DefaultAction;
76 import org.apache.maven.werkz.Goal;
77 import org.apache.maven.werkz.Session;
78
79 /**
80 * Implements a <target> tag which is similar to the Ant equivalent tag
81 * but is based on the Werkz goal engine.
82 *
83 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
84 * @version $Revision: 1.3 $
85 */
86 public class GoalTag
87 extends WerkzTagSupport
88 {
89
90 /** The Log to which logging calls will be made. */
91 private static final Log LOGGER = LogFactory.getLog( GoalTag.class );
92
93 /** the name of the target */
94 private String name;
95
96 private String prereqs;
97
98 private String description;
99
100 public GoalTag()
101 {
102 }
103
104
105
106
107 /**
108 * Evaluate the body to register all the various goals and pre/post conditions
109 * then run all the current targets
110 */
111 public void doTag( final XMLOutput output )
112 throws JellyTagException
113 {
114
115 LOGGER.debug( "doTag(..):" + name );
116
117 Goal goal = getProject().getGoal( getName(), true );
118
119 goal.setDescription( this.description );
120 try
121 {
122 addPrereqs( goal );
123 }
124 catch ( CyclicGoalChainException e )
125 {
126 throw new JellyTagException( e );
127 }
128
129 Action action;
130
131 action = getAction();
132
133 if ( action == null )
134 {
135 action = new DefaultAction()
136 {
137 public void performAction( Session session )
138 throws Exception
139 {
140 performAction();
141 }
142
143 public void performAction()
144 throws Exception
145 {
146 LOGGER.debug( "Running action of target: " + getName() );
147 invokeBody( output );
148 }
149 };
150 }
151
152 goal.setAction( action );
153 }
154
155
156
157 /**
158 * @return the name of the target
159 */
160 public String getName()
161 {
162 return name;
163 }
164
165 /**
166 * Sets the name of the target
167 */
168 public void setName( String name )
169 {
170 LOGGER.debug( "setName(" + name + ")" );
171 this.name = name;
172 }
173
174 public void setPrereqs( String prereqs )
175 {
176 this.prereqs = prereqs;
177 }
178
179 public String getPrereqs()
180 {
181 return this.prereqs;
182 }
183
184 public void setDescription( String description )
185 {
186 this.description = description;
187 }
188
189 protected void addPrereqs( Goal goal )
190 throws CyclicGoalChainException
191 {
192 String prereqs = getPrereqs();
193
194 if ( prereqs == null )
195 {
196 return;
197 }
198
199 StringTokenizer tokens = new StringTokenizer( getPrereqs(), "," );
200
201 String eachToken = null;
202 Goal eachPrereq = null;
203
204 while ( tokens.hasMoreTokens() )
205 {
206 eachToken = tokens.nextToken().trim();
207
208 eachPrereq = getProject().getGoal( eachToken, true );
209
210 goal.addPrecursor( eachPrereq );
211 }
212 }
213
214 /**
215 * Subclasses can override this template method to provide a
216 * custom <code>Action</code> implementation. This scenerio will
217 * most likely come into play when you have also created a custom
218 * <code>Session</code> implementation that can be used to provide
219 * parameters during the attainment of a <code>Goal</code>.
220 */
221 protected Action getAction()
222 {
223 return null;
224 }
225 }