From 3ffd08e03636392a062e81aaee04dca7d40fa047 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 9 Feb 2002 04:49:46 +0000 Subject: [PATCH] Add a try-catch taks to emulate javas try-catch constructs git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271236 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/antlib/core/Resources.properties | 10 +- .../java/org/apache/antlib/core/TaskList.java | 32 ++++ .../org/apache/antlib/core/TryCatchTask.java | 148 ++++++++++++++++++ 3 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/TaskList.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/TryCatchTask.java diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties b/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties index 17479b5fd..c85a1c695 100644 --- a/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties @@ -25,4 +25,12 @@ enum.missing.getNames.error=Enum class "{0}" is missing a public static method n invalid.enum.error=Invalid value "{0}" for enum, expected one of {1}. if.ifelse-duplicate.error=Can only set one of if/else for If task type. -if.no-condition.error=No condition was specified for If task. \ No newline at end of file +if.no-condition.error=No condition was specified for If task. + +trycatch.multiple-trys.error=Multiple elements can not be placed inside task. +trycatch.missing-try-before-catch.error=There needs to be a element before element. +trycatch.multiple-catches.error=Multiple elements can not be placed inside task. +trycatch.missing-try-before-finally.error=There needs to be a element before element. +trycatch.multiple-finallys.error=Multiple elements can not be placed inside task. +trycatch.no-try.error=Missing element from task. +trycatch.missing-second.error=Missing or elements from task. \ No newline at end of file diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/TaskList.java b/proposal/myrmidon/src/java/org/apache/antlib/core/TaskList.java new file mode 100644 index 000000000..6ac9b1eb3 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/TaskList.java @@ -0,0 +1,32 @@ +/* + * 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.txt file. + */ +package org.apache.antlib.core; + +import java.util.ArrayList; +import org.apache.avalon.framework.configuration.Configuration; + +/** + * This object contains an ordered list of tasks. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +public class TaskList +{ + private ArrayList m_tasks = new ArrayList(); + + public void add( final Configuration task ) + { + m_tasks.add( task ); + } + + public Configuration[] getTasks() + { + return (Configuration[])m_tasks.toArray( new Configuration[ m_tasks.size() ] ); + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/TryCatchTask.java b/proposal/myrmidon/src/java/org/apache/antlib/core/TryCatchTask.java new file mode 100644 index 000000000..89cf944b0 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/TryCatchTask.java @@ -0,0 +1,148 @@ +/* + * 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.txt file. + */ +package org.apache.antlib.core; + +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.AbstractContainerTask; +import org.apache.myrmidon.interfaces.executor.ExecutionFrame; +import org.apache.myrmidon.interfaces.executor.Executor; + +/** + * A task that emulates the try-catch-finally construct in a number + * of languages. + * + * @author Peter Donald + * @version $Revision$ $Date$ + * @ant:task name="try-catch" + */ +public final class TryCatchTask + extends AbstractContainerTask +{ + private final static Resources REZ = + ResourceManager.getPackageResources( TryCatchTask.class ); + + private TaskList m_try; + private TaskList m_catch; + private TaskList m_finally; + + public void addTry( final TaskList taskList ) + throws TaskException + { + if( null != m_try ) + { + final String message = REZ.getString( "trycatch.multiple-trys.error" ); + throw new TaskException( message ); + } + m_try = taskList; + } + + public void addCatch( final TaskList taskList ) + throws TaskException + { + if( null == m_try ) + { + final String message = REZ.getString( "trycatch.missing-try-before-catch.error" ); + throw new TaskException( message ); + } + else if( null != m_catch ) + { + final String message = REZ.getString( "trycatch.multiple-catches.error" ); + throw new TaskException( message ); + } + m_catch = taskList; + } + + public void addFinally( final TaskList taskList ) + throws TaskException + { + if( null == m_try ) + { + final String message = REZ.getString( "trycatch.missing-try-before-finally.error" ); + throw new TaskException( message ); + } + else if( null != m_finally ) + { + final String message = REZ.getString( "trycatch.multiple-finallys.error" ); + throw new TaskException( message ); + } + m_finally = taskList; + } + + public void execute() + throws TaskException + { + validate(); + + final ExecutionFrame frame = (ExecutionFrame)getService( ExecutionFrame.class ); + final Executor executor = (Executor)getService( Executor.class ); + + try + { + final Configuration[] tasks = m_try.getTasks(); + executeTasks( executor, frame, tasks ); + } + catch( final TaskException te ) + { + if( null != m_catch ) + { + final Configuration[] tasks = m_catch.getTasks(); + executeTasks( executor, frame, tasks ); + } + else + { + throw te; + } + } + finally + { + if( null != m_finally ) + { + final Configuration[] tasks = m_finally.getTasks(); + executeTasks( executor, frame, tasks ); + } + } + } + + private void validate() + throws TaskException + { + if( null == m_try ) + { + final String message = REZ.getString( "trycatch.no-try.error" ); + throw new TaskException( message ); + } + else if( null == m_catch && null == m_finally ) + { + final String message = REZ.getString( "trycatch.missing-second.error" ); + throw new TaskException( message ); + } + } + + /** + * Utility method to execute the tasks in an appropriate environment. + */ + private void executeTasks( final Executor executor, + final ExecutionFrame frame, + final Configuration[] tasks ) + throws TaskException + { + for( int i = 0; i < tasks.length; i++ ) + { + final Configuration task = tasks[ i ]; + executor.execute( task, frame ); + } + } + + public String toString() + { + return "Try-Catch-Finally"; + } +}