/*

 * Asynchronous Engine .

 * 

 * Copyright (C) 2004 Claude Hussenet

 * Contact : chussenet@yahoo.com

 * 

 * Distributable under LGPL license.

 * See terms of license at gnu.org.

 */

package org.jboss.aspects.asynchronous.concurrent;

import EDU.oswego.cs.dl.util.concurrent.Callable;
import org.jboss.aspects.asynchronous.AsynchronousConstants;
import org.jboss.aspects.asynchronous.AsynchronousParameters;
import org.jboss.aspects.asynchronous.AsynchronousResponse;
import org.jboss.aspects.asynchronous.AsynchronousUserTask;
import org.jboss.aspects.asynchronous.ProcessingTime;
import org.jboss.aspects.asynchronous.ThreadManagerResponse;
import org.jboss.aspects.asynchronous.common.ThreadManagerResponseImpl;

/**
 * @author <a href="mailto:chussenet@yahoo.com">{Claude Hussenet Independent Consultant}</a>.
 * @version <tt>$Revision: 1.3.4.3 $</tt>
 */

final class AdapterTask

implements Callable, AsynchronousConstants, ProcessingTime
{

   private AsynchronousParameters _inputParametersImpl = null;

   private AsynchronousUserTask _taskImpl = null;

   private String id = null;

   private long startingTime = -1;

   private long endingTime = -1;

   AdapterTask(String id,

               AsynchronousParameters inputParametersImpl,

               AsynchronousUserTask taskImpl)
   {

      _inputParametersImpl = inputParametersImpl;

      _taskImpl = taskImpl;

      this.id = id;

   }

   public void cleanup()
   {

      _taskImpl.cleanup(_inputParametersImpl);

   }

   public java.lang.Object call()
   {

      try
      {

         startingTime = System.currentTimeMillis();

         AsynchronousResponse taskResult =

         _taskImpl.process(_inputParametersImpl);

         endingTime = System.currentTimeMillis();

         ThreadManagerResponse myResult =

         new ThreadManagerResponseImpl(getId(),

         OK,

         null,

         taskResult,

         startingTime,

         endingTime);

         return myResult;

      }
      catch (Exception e)
      {

         try
         {

            endingTime = System.currentTimeMillis();

            return new ThreadManagerResponseImpl(getId(),

            UNKNOWN,

            e.getMessage(),

            e);

         }
         catch (Exception ee)
         {

            endingTime = System.currentTimeMillis();

            return new ThreadManagerResponseImpl(getId(),

            UNKNOWN,

            e.getMessage(),

            ee);

         }

      }

   }

   public String getId()
   {

      return id;

   }

   public long getStartingTime()
   {

      return startingTime;

   }

   public long getEndingTime()
   {

      return endingTime;

   }

}

