/*

 * 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.aspects;

import org.jboss.aspects.asynchronous.AsynchronousConstants;
import org.jboss.aspects.asynchronous.AsynchronousResponse;
import org.jboss.aspects.asynchronous.AsynchronousTask;
import org.jboss.aspects.asynchronous.ThreadManagerResponse;

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

public class AsynchronousFacadeImpl

implements AsynchronousFacade, AsynchronousConstants
{

   private AsynchronousFacadeFields asynchronousFacadeFields = null;

   public AsynchronousFacadeImpl()
   {

      asynchronousFacadeFields =

      new AsynchronousFacadeFieldsThreadLocalImpl();

   }

   private AsynchronousFacadeFields getAsynchronousFacadeFields()
   {

      return asynchronousFacadeFields;

   }

   public AsynchronousTask getAsynchronousTask()
   {

      return getAsynchronousFacadeFields().getAsynchronousTask();

   }

   public void setTimeout(long timeout)
   {

      getAsynchronousFacadeFields().setTimeout(timeout);

   }

   public long getTimeout()
   {

      return getAsynchronousFacadeFields().getTimeout();

   }

   public void setAsynchronousTask(AsynchronousTask asynchronousTask)
   {

      getAsynchronousFacadeFields().setAsynchronousTask(asynchronousTask);

   }

   public void setId(String id)
   {

      getAsynchronousFacadeFields().setId(id);

   }

   public String getId()
   {

      return getAsynchronousFacadeFields().getId();

   }

   public ThreadManagerResponse getThreadManagerResponse()
   {

      return getAsynchronousTask().getResponse();

   }

   public AsynchronousResponse waitForResponse()
   {

      return waitForResponse(getAsynchronousTask());

   }

   public Object getReturnValue()
   {

      return getReturnValue(getAsynchronousTask());

   }

   public boolean isDone()
   {

      return isDone(getAsynchronousTask());

   }

   public int getResponseCode()
   {

      return getResponseCode(getAsynchronousTask());

   }

   public AsynchronousResponse waitForResponse(AsynchronousTask asynchronousTask)
   {

      if (asynchronousTask.getResponse().getResponseCode()

      == AsynchronousConstants.OK)
      {

         AsynchronousResponse atask =

         (AsynchronousResponse) asynchronousTask

         .getResponse()

         .getResult();

         return atask;

      }

      return asynchronousTask.getResponse();

   }

   public Object getReturnValue(AsynchronousTask asynchronousTask)
   {

      if (asynchronousTask == null)

         return null;

      AsynchronousResponse asynchronousResponse =

      waitForResponse(asynchronousTask);

      if (asynchronousResponse.getResponseCode() == OK)

         return asynchronousResponse.getResult();

      else

         return null;

   }

   public boolean isDone(AsynchronousTask synchronousTask)
   {

      if (synchronousTask == null)

         return false;

      else

         return synchronousTask.isDone();

   }

   public int getResponseCode(AsynchronousTask synchronousTask)
   {

      return (waitForResponse(synchronousTask).getResponseCode());

   }

}

