Package de.danet.an.workflow.tools.timing

Source Code of de.danet.an.workflow.tools.timing.WaitTool

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: WaitTool.java 1995 2006-12-04 15:08:17Z drmlipp $
*
* $Log$
* Revision 1.4  2006/09/29 12:32:10  drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.3  2004/10/20 20:40:32  drmlipp
* Made simple WaitTool terminatable.
*
* Revision 1.2  2004/10/19 21:06:32  drmlipp
* Fixed indentation.
*
* Revision 1.1.1.1  2004/08/18 15:17:39  drmlipp
* Update to 1.2
*
* Revision 1.5  2004/04/13 14:33:38  lipp
* Cleanup.
*
* Revision 1.4  2004/04/12 19:33:52  lipp
* Clarified application invocation interface.
*
* Revision 1.3  2004/02/27 18:09:31  lipp
* Removed overlooked hard-coding of result parameter.
*
* Revision 1.2  2004/02/23 13:59:31  lipp
* Added simple waittool invocation.
*
* Revision 1.1  2004/02/20 18:56:35  lipp
* Renamed package waittool to timing (much better ;-)).
*
* Revision 1.2  2004/02/20 15:58:22  lipp
* Several WaitTool fixes.
*
* Revision 1.1  2004/02/19 17:55:55  lipp
* Initial version of waittool.
*
*/
package de.danet.an.workflow.tools.timing;

import java.util.Date;
import java.util.Map;

import java.rmi.RemoteException;

import de.danet.an.workflow.omgcore.CannotCompleteException;
import de.danet.an.workflow.omgcore.InvalidDataException;
import de.danet.an.workflow.omgcore.ProcessData;

import de.danet.an.workflow.api.Activity;
import de.danet.an.workflow.api.ActivityUniqueKey;
import de.danet.an.workflow.api.DefaultProcessData;
import de.danet.an.workflow.api.FormalParameter;
import de.danet.an.workflow.api.InvalidKeyException;

import de.danet.an.workflow.spis.aii.ApplicationNotStoppedException;
import de.danet.an.workflow.spis.aii.CannotExecuteException;
import de.danet.an.workflow.tools.util.SimpleApplicationInfo;

/**
* This class provides a tool agent that cancels a timer.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 1995 $
*/
public class WaitTool extends ToolAgentBase {

    private static final org.apache.commons.logging.Log logger
  = org.apache.commons.logging.LogFactory.getLog(WaitTool.class);

    /**
     * Creates an instance of <code>WaitTool</code>
     * with all attributes initialized to default values.
     */
    public WaitTool () {
    }
   
    /**
     * Executes the tool.
     *
     * @param activity an <code>Activity</code> value
     * @param fps the formal parameters
     * @param map a <code>Map</code> value
     * @exception CannotExecuteException if an error occurs
     * @exception RemoteException if an error occurs
     */
    public void invoke
  (Activity activity, FormalParameter[] fps, Map map)
  throws CannotExecuteException, RemoteException {
        Object arg0 = map.get(fps[0].id());
  if ((arg0 instanceof Date)
            || (arg0 instanceof Number) && fps.length == 1) {
      try {
    // uncancalable wait, simply create timer and wait
    long applId = applicationDirectory().registerInstance
        (WaitTool.class.getName(), activity, null, false);
    if (logger.isDebugEnabled()) {
        logger.debug ("Application " + applId
          + " created for " + activity);
    }
    Object timer = timerHandler().createTimer
        (applId, (arg0 instanceof Date) ? (Date)arg0
                     : new Date (Math.round(System.currentTimeMillis()
                                 + (((Number)arg0).doubleValue() * 1000))));
    applicationDirectory().updateState
        (applId, new Object[] { timer, null });
      } catch (InvalidKeyException invKey) {
    if (logger.isDebugEnabled()) {
        logger.debug ("Timer expired immediately");
    }
      }
      return;
  }
  // cancalable wait, update associates timer application
  long applId = ((Long)arg0).longValue ();
  try {
      if (logger.isDebugEnabled()) {
    logger.debug ("Waiting for timer application " + applId);
      }
      applicationDirectory()
    .updateInvokingActivity (applId, activity.uniqueKey());
      if (logger.isDebugEnabled()) {
    logger.debug ("Application " + applId
            + " updated to " + activity);
      }
      SimpleApplicationInfo info
    = applicationDirectory().instanceInfo (applId);
      Object[] data = (Object[])info.state();
      data[1] = fps[1].id();
      applicationDirectory().updateState (applId, data);
      return;
  } catch (InvalidKeyException invKey) {
      if (logger.isDebugEnabled()) {
    logger.debug ("Timer application " + applId
            + " has already expired, completing");
      }
  }
  ProcessData res = new DefaultProcessData ();
  res.put (fps[1].id(), "EXPIRED");
  try {
      activity.setResult (res);
      activity.complete ();
  } catch (CannotCompleteException e) {
      logger.error
    (activity + " waiting for timer "
     + "but cannot be completed?!: " + e.getMessage (), e);
      throw new CannotExecuteException
    ("Receiving activity cannot be completed");
  } catch (InvalidDataException e) {
      logger.error
    ("Cannot set \"status\" out parameter of "
     + "wait tool. Propably wrong declaration. "
     + activity + " will be terminated.");
      throw new CannotExecuteException (e.getMessage ());
  }
    }

    /**
     * Terminate the tool agent.
     *
     * @param activity the activity to be canceled
     * @throws ApplicationNotStoppedException if execution cannot be
     * terminated (see {@link ApplicationNotStoppedException
     * <code>ApplicationNotStoppedException</code>}).
     * @throws RemoteException if a temporary problem occurs and the
     * workflow engine should retry the tool invocation
     */
    public void terminate (Activity activity)
  throws ApplicationNotStoppedException, RemoteException {
  ActivityUniqueKey auk = null;
  try {
      auk = activity.uniqueKey();
      SimpleApplicationInfo info
    = applicationDirectory().infoByActivity (auk);
      if (logger.isDebugEnabled()) {
    logger.debug ("Terminating application " + info.id()
             + " invoked by " + auk);
      }
      applicationDirectory().removeInstance (info.id());
      timerHandler().removeTimer (((Object[])info.state())[0]);
  } catch (InvalidKeyException e) {
      // terminate is called by engine "to make sure"
      logger.debug ("Terminate called for " + auk + " but application "
        + "already removed (is OK): " + e.getMessage ());
  }
    }
   
}
TOP

Related Classes of de.danet.an.workflow.tools.timing.WaitTool

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.