1 package org.apache.maven.jelly.tags.werkz;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.commons.jelly.JellyTagException;
22 import org.apache.commons.jelly.XMLOutput;
23 import org.apache.maven.MavenConstants;
24 import org.apache.maven.jelly.MavenJellyContext;
25 import org.apache.maven.plugin.JellyScriptHousing;
26 import org.apache.maven.plugin.PluginManager;
27 import org.apache.maven.werkz.Action;
28 import org.apache.maven.werkz.Goal;
29 import org.apache.maven.werkz.Session;
30 import org.apache.maven.werkz.WerkzProject;
31 import org.apache.maven.werkz.jelly.GoalTag;
32
33 /**
34 * Replacement for werkz's <code>GoalTag</code> which does not allow a goal
35 * to be redefined once defined.
36 * For Jelly scripts that are run which contain this tag it is assumed that
37 * they are being run in the order whereby the first definition wins.
38 *
39 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40 * @version $Id: MavenGoalTag.java 517014 2007-03-11 21:15:50Z ltheussl $
41 */
42 public class MavenGoalTag
43 extends GoalTag
44 {
45
46
47
48
49 /**
50 * Construct.
51 */
52 public MavenGoalTag()
53 {
54
55 }
56
57 /**
58 * Define a goal.
59 *
60 * @param output The output sink.
61 * @throws JellyTagException If an error occurs while executing the tag.
62 */
63 public void doTag( XMLOutput output )
64 throws JellyTagException
65 {
66 Goal goal = getProject().getGoal( getName() );
67
68 if ( ( goal == null ) || ( goal.getAction() == null ) )
69 {
70 super.doTag( output );
71 goal = getProject().getGoal( getName() );
72 JellyScriptHousing currentHousing = (JellyScriptHousing) getContext()
73 .getVariable( PluginManager.PLUGIN_HOUSING );
74 goal.setAction( new MavenGoalAction( currentHousing ) );
75 }
76 }
77
78 void runBodyTag( Session session )
79 throws JellyTagException
80 {
81 MavenJellyContext context = (MavenJellyContext) getContext();
82
83 getBody().run( context, context.getXMLOutput() );
84 }
85
86 /**
87 * @todo refactor into a separate class that gets the necessary variables delegated to it.
88 */
89 public class MavenGoalAction
90 implements Action
91 {
92 private final JellyScriptHousing housing;
93
94 public MavenGoalAction( JellyScriptHousing housing )
95 {
96 super();
97 this.housing = housing;
98 if ( housing == null )
99 {
100 throw new NullPointerException( "Plugin Housing can not be null" );
101 }
102 }
103
104 public void performAction( Session session )
105 throws Exception
106 {
107 MavenJellyContext oldContext = (MavenJellyContext) getContext();
108
109
110
111
112 MavenJellyContext context = housing.getProject().getContext();
113
114 setContext( context );
115
116 runBodyTag( session );
117
118 setContext( oldContext );
119 }
120
121 public boolean requiresAction()
122 {
123 return true;
124 }
125 }
126
127 public WerkzProject getProject()
128 {
129 return (WerkzProject) getContext().getVariable( MavenConstants.WERKZ_PROJECT );
130 }
131 }