From 6485113b53ee020b548675bc3695c9df39fc3256 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Mon, 17 Dec 2001 10:49:06 +0000 Subject: [PATCH] Add a fail task similar to ant1.xs except that it also has a condition. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270246 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/java/org/apache/antlib/core/Fail.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/Fail.java diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/Fail.java b/proposal/myrmidon/src/java/org/apache/antlib/core/Fail.java new file mode 100644 index 000000000..8499375bb --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/Fail.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ +package org.apache.antlib.core; + +import org.apache.avalon.framework.context.ContextException; +import org.apache.myrmidon.api.AbstractTask; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.Condition; + +/** + * This is a task used to throw a TaskException. + * Useful for forcing a build to fail on a certain condition. + * + * @author Peter Donald + */ +public class Fail + extends AbstractTask +{ + private String m_message; + private Condition m_condition; + + public void setMessage( final String message ) + { + checkNullMessage(); + m_message = message; + } + + public void addContent( final String message ) + { + checkNullMessage(); + m_message = message; + } + + public void setIf( final String ifCondition ) + { + checkNullCondition(); + m_condition = new Condition( true, ifCondition ); + } + + public void setUnless( final String unlessCondition ) + { + checkNullCondition(); + m_condition = new Condition( false, unlessCondition ); + } + + public void execute() + throws TaskException + { + if( null == m_condition ) + { + throw new TaskException( "Use did not specify a condition" ); + } + + try + { + final boolean failed = + m_condition.evaluate( getContext() ); + + if( failed ) + { + if( null != m_message ) + { + throw new TaskException( m_message ); + } + else + { + throw new TaskException(); + } + } + } + catch( final ContextException ce ) + { + throw new TaskException( ce.toString(), ce ); + } + } + + private void checkNullMessage() + { + if( null != m_message ) + { + final String message = "Message can only be set once by " + + "either nested content or the message attribute"; + throw new IllegalStateException( message ); + } + } + + private void checkNullCondition() + { + if( null != m_condition ) + { + throw new IllegalStateException( "Condition already set!" ); + } + } +}