Browse Source

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
master
Peter Donald 23 years ago
parent
commit
3ffd08e036
3 changed files with 189 additions and 1 deletions
  1. +9
    -1
      proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties
  2. +32
    -0
      proposal/myrmidon/src/java/org/apache/antlib/core/TaskList.java
  3. +148
    -0
      proposal/myrmidon/src/java/org/apache/antlib/core/TryCatchTask.java

+ 9
- 1
proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties View File

@@ -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.
if.no-condition.error=No condition was specified for If task.

trycatch.multiple-trys.error=Multiple <try/> elements can not be placed inside <try-catch/> task.
trycatch.missing-try-before-catch.error=There needs to be a <try/> element before <catch/> element.
trycatch.multiple-catches.error=Multiple <catch/> elements can not be placed inside <try-catch/> task.
trycatch.missing-try-before-finally.error=There needs to be a <try/> element before <finally/> element.
trycatch.multiple-finallys.error=Multiple <finally/> elements can not be placed inside <try-catch/> task.
trycatch.no-try.error=Missing <try/> element from <try-catch/> task.
trycatch.missing-second.error=Missing <catch/> or <finally/> elements from <try-catch/> task.

+ 32
- 0
proposal/myrmidon/src/java/org/apache/antlib/core/TaskList.java View File

@@ -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 <a href="mailto:peter@apache.org">Peter Donald</a>
* @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() ] );
}
}

+ 148
- 0
proposal/myrmidon/src/java/org/apache/antlib/core/TryCatchTask.java View File

@@ -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 <a href="mailto:peter@apache.org">Peter Donald</a>
* @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";
}
}

Loading…
Cancel
Save