donaldp     02/02/08 20:49:47

  Modified:    proposal/myrmidon/src/java/org/apache/antlib/core
                        Resources.properties
  Added:       proposal/myrmidon/src/java/org/apache/antlib/core
                        TryCatchTask.java TaskList.java
  Log:
  Add a try-catch taks to emulate javas try-catch constructs
  
  Revision  Changes    Path
  1.7       +9 -1      
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Resources.properties      25 Jan 2002 11:26:45 -0000      1.6
  +++ Resources.properties      9 Feb 2002 04:49:46 -0000       1.7
  @@ -25,4 +25,12 @@
   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 <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.
  \ No newline at end of file
  
  
  
  1.1                  
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/TryCatchTask.java
  
  Index: TryCatchTask.java
  ===================================================================
  /*
   * 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:[EMAIL PROTECTED]">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/09 04:49:46 $
   * @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";
      }
  }
  
  
  
  1.1                  
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/TaskList.java
  
  Index: TaskList.java
  ===================================================================
  /*
   * 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:[EMAIL PROTECTED]">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/09 04:49:46 $
   */
  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() ] );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to