View Javadoc

1   package org.apache.maven.jelly.tags.werkz;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
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      // C O N S T R U C T O R S
47      // ------------------------------------------------------------
48  
49      /**
50       * Construct.
51       */
52      public MavenGoalTag()
53      {
54          // intentionally left blank.
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             //GoalToJellyScriptHousingMapper mapper = ( GoalToJellyScriptHousingMapper ) session.getAttribute(
110             //PluginManager.GOAL_MAPPER );
111             //JellyScriptHousing housing = mapper.getPluginHousing( getName() );
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 }