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 java.util.Iterator;
22  import java.util.Set;
23  
24  import org.apache.commons.jelly.JellyTagException;
25  import org.apache.commons.jelly.XMLOutput;
26  import org.apache.maven.MavenConstants;
27  import org.apache.maven.jelly.MavenJellyContext;
28  import org.apache.maven.plugin.GoalToJellyScriptHousingMapper;
29  import org.apache.maven.plugin.PluginManager;
30  import org.apache.maven.werkz.NoActionDefinitionException;
31  import org.apache.maven.werkz.NoSuchGoalException;
32  import org.apache.maven.werkz.Session;
33  import org.apache.maven.werkz.UnattainableGoalException;
34  import org.apache.maven.werkz.WerkzProject;
35  import org.apache.maven.werkz.jelly.JellySession;
36  import org.apache.maven.werkz.jelly.WerkzTagSupport;
37  
38  /**
39   * Replacement for werkz's <code>AttainGoalTag</code> which will lazy initialise goals.
40   *
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   * @version $Id: MavenAttainGoalTag.java 517014 2007-03-11 21:15:50Z ltheussl $
43   */
44  public class MavenAttainGoalTag
45      extends WerkzTagSupport
46  {
47      /**
48       * The goal name.
49       */
50      private String name;
51  
52      /**
53       * Construct.
54       */
55      public MavenAttainGoalTag()
56      {
57          // intentionally left blank
58      }
59  
60      /**
61       * Retrieve the <code>Session</code> to use, if set.
62       *
63       * @return The session or null.
64       */
65      public Session createSession()
66      {
67          /* TODO: this is for compatibility - do we want the user to request a new session instead?
68           eg force=true on the tag
69           not great to tie variables to session if we are creating a new one */
70          Session session = (Session) getContext().getVariable( PluginManager.GLOBAL_SESSION_KEY );
71          Session newSession = new JellySession( ( (MavenJellyContext) getContext() ).getXMLOutput() );
72          for ( Iterator i = session.getAttributes().keySet().iterator(); i.hasNext(); )
73          {
74              String key = (String) i.next();
75              newSession.setAttribute( key, session.getAttribute( key ) );
76          }
77  
78          return newSession;
79      }
80  
81      /**
82       * Evaluate the body to register all the various goals and pre/post conditions
83       * then run all the current targets
84       */
85      public void doTag( final XMLOutput output )
86          throws JellyTagException
87      {
88          WerkzProject project = getProject();
89  
90          if ( project == null )
91          {
92              throw new JellyTagException( "No Project available" );
93          }
94  
95          invokeBody( output );
96  
97          try
98          {
99              // Lazy goal loading
100             Session session = createSession();
101             MavenJellyContext baseContext = (MavenJellyContext) session.getAttribute( PluginManager.BASE_CONTEXT );
102             GoalToJellyScriptHousingMapper mapper = (GoalToJellyScriptHousingMapper) session
103                 .getAttribute( PluginManager.GOAL_MAPPER );
104             PluginManager pluginManager = (PluginManager) session.getAttribute( PluginManager.PLUGIN_MANAGER );
105 
106             Set pluginSet;
107             try
108             {
109                 pluginSet = pluginManager.prepAttainGoal( getName(), baseContext, mapper );
110             }
111             catch ( Exception e )
112             {
113                 throw new JellyTagException( e );
114             }
115             project.attainGoal( getName(), session );
116             pluginManager.addDelayedPops( pluginSet );
117         }
118         catch ( UnattainableGoalException e )
119         {
120             Throwable root = e.getRootCause();
121 
122             if ( root != null )
123             {
124                 if ( root instanceof JellyTagException )
125                 {
126                     throw (JellyTagException) root;
127                 }
128                 if ( root instanceof UnattainableGoalException )
129                 {
130                     throw new JellyTagException( e );
131                 }
132             }
133             e.fillInStackTrace();
134             throw new JellyTagException( e );
135         }
136         catch ( NoActionDefinitionException e )
137         {
138             throw new JellyTagException( e );
139         }
140         catch ( NoSuchGoalException e )
141         {
142             throw new JellyTagException( e );
143         }
144     }
145 
146     /**
147      * Define a goal.
148      *
149      * @param output The output sink.
150      * @throws JellyTagException If an error occurs while executing the tag.
151      */
152     public void invokeBody( XMLOutput output )
153         throws JellyTagException
154     {
155         MavenJellyContext baseContext = (MavenJellyContext) createSession().getAttribute( PluginManager.BASE_CONTEXT );
156 
157         getBody().run( baseContext, output );
158     }
159 
160     public void setName( String name )
161     {
162         this.name = name;
163     }
164 
165     public String getName()
166     {
167         return this.name;
168     }
169 
170     public WerkzProject getProject()
171     {
172         return (WerkzProject) getContext().getVariable( MavenConstants.WERKZ_PROJECT );
173     }
174 }