package org.apache.maven.jelly.tags.werkz;
/* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.XMLOutput;
import org.apache.maven.MavenConstants;
import org.apache.maven.jelly.MavenJellyContext;
import org.apache.maven.plugin.GoalToJellyScriptHousingMapper;
import org.apache.maven.plugin.PluginManager;
import org.apache.maven.werkz.NoActionDefinitionException;
import org.apache.maven.werkz.NoSuchGoalException;
import org.apache.maven.werkz.Session;
import org.apache.maven.werkz.UnattainableGoalException;
import org.apache.maven.werkz.WerkzProject;
import org.apache.maven.werkz.jelly.JellySession;
import org.apache.maven.werkz.jelly.WerkzTagSupport;
/**
* Replacement for werkz's <code>AttainGoalTag</code> which will lazy initialise goals.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id: MavenAttainGoalTag.java 517014 2007-03-11 21:15:50Z ltheussl $
*/
public class MavenAttainGoalTag
extends WerkzTagSupport
{
/**
* The goal name.
*/
private String name;
/**
* Construct.
*/
public MavenAttainGoalTag()
{
// intentionally left blank
}
/**
* Retrieve the <code>Session</code> to use, if set.
*
* @return The session or null.
*/
public Session createSession()
{
/* TODO: this is for compatibility - do we want the user to request a new session instead?
eg force=true on the tag
not great to tie variables to session if we are creating a new one */
Session session = (Session) getContext().getVariable( PluginManager.GLOBAL_SESSION_KEY );
Session newSession = new JellySession( ( (MavenJellyContext) getContext() ).getXMLOutput() );
for ( Iterator i = session.getAttributes().keySet().iterator(); i.hasNext(); )
{
String key = (String) i.next();
newSession.setAttribute( key, session.getAttribute( key ) );
}
return newSession;
}
/**
* Evaluate the body to register all the various goals and pre/post conditions
* then run all the current targets
*/
public void doTag( final XMLOutput output )
throws JellyTagException
{
WerkzProject project = getProject();
if ( project == null )
{
throw new JellyTagException( "No Project available" );
}
invokeBody( output );
try
{
// Lazy goal loading
Session session = createSession();
MavenJellyContext baseContext = (MavenJellyContext) session.getAttribute( PluginManager.BASE_CONTEXT );
GoalToJellyScriptHousingMapper mapper = (GoalToJellyScriptHousingMapper) session
.getAttribute( PluginManager.GOAL_MAPPER );
PluginManager pluginManager = (PluginManager) session.getAttribute( PluginManager.PLUGIN_MANAGER );
Set pluginSet;
try
{
pluginSet = pluginManager.prepAttainGoal( getName(), baseContext, mapper );
}
catch ( Exception e )
{
throw new JellyTagException( e );
}
project.attainGoal( getName(), session );
pluginManager.addDelayedPops( pluginSet );
}
catch ( UnattainableGoalException e )
{
Throwable root = e.getRootCause();
if ( root != null )
{
if ( root instanceof JellyTagException )
{
throw (JellyTagException) root;
}
if ( root instanceof UnattainableGoalException )
{
throw new JellyTagException( e );
}
}
e.fillInStackTrace();
throw new JellyTagException( e );
}
catch ( NoActionDefinitionException e )
{
throw new JellyTagException( e );
}
catch ( NoSuchGoalException e )
{
throw new JellyTagException( e );
}
}
/**
* Define a goal.
*
* @param output The output sink.
* @throws JellyTagException If an error occurs while executing the tag.
*/
public void invokeBody( XMLOutput output )
throws JellyTagException
{
MavenJellyContext baseContext = (MavenJellyContext) createSession().getAttribute( PluginManager.BASE_CONTEXT );
getBody().run( baseContext, output );
}
public void setName( String name )
{
this.name = name;
}
public String getName()
{
return this.name;
}
public WerkzProject getProject()
{
return (WerkzProject) getContext().getVariable( MavenConstants.WERKZ_PROJECT );
}
}