/*
* 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: TimerCanceler.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.1.1.1 2004/08/18 15:17:39 drmlipp
* Update to 1.2
*
* Revision 1.8 2004/07/07 14:45:49 lipp
* Added missing return statement.
*
* Revision 1.7 2004/04/12 19:33:52 lipp
* Clarified application invocation interface.
*
* Revision 1.6 2004/04/01 09:32:07 lipp
* Improved tool agent context implementtaion.
*
* Revision 1.5 2004/03/31 20:48:14 lipp
* Cleaned up.
*
* Revision 1.4 2004/03/31 19:36:20 lipp
* Completed implementation of Activity.abandon(String).
*
* Revision 1.3 2004/02/27 18:09:31 lipp
* Removed overlooked hard-coding of result parameter.
*
* Revision 1.2 2004/02/24 13:51:21 lipp
* Fixed typo.
*
* 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.Map;
import java.rmi.RemoteException;
import de.danet.an.workflow.omgcore.CannotCompleteException;
import de.danet.an.workflow.omgcore.CannotStopException;
import de.danet.an.workflow.omgcore.InvalidDataException;
import de.danet.an.workflow.omgcore.NotRunningException;
import de.danet.an.workflow.omgcore.ProcessData;
import de.danet.an.workflow.omgcore.WfExecutionObject.State;
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.spis.aii.ResultProvider;
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: 1607 $
*/
public class TimerCanceler extends ToolAgentBase
implements ResultProvider {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory.getLog(TimerCanceler.class);
/** The result container. */
private ThreadLocal result = new ThreadLocal ();
/**
* Creates an instance of <code>TimerCanceler</code>
* with all attributes initialized to default values.
*/
public TimerCanceler () {
}
/**
* 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 {
// Note that a RemoteException will lead to a rollback and an
// automatic re-invocation of this method.
long applId = ((Long)map.get(fps[0].id())).longValue ();
if (logger.isDebugEnabled ()) {
logger.debug ("Cancelling timer application " + applId);
}
SimpleApplicationInfo info = null;
try {
info = applicationDirectory().instanceInfo (applId);
applicationDirectory().removeInstance (applId);
} catch (InvalidKeyException e) {
logger.warn ("Timer application " + applId + " could not be "
+ "canceled because is does not exist.");
return;
}
Object timer = ((Object[])info.state())[0];
timerHandler().removeTimer (timer);
ActivityUniqueKey auk = info.activityUniqueKey();
Activity act = null;
try {
act = toolAgentContext().lookupActivity(auk);
} catch (InvalidKeyException e) {
logger.warn (auk + " is unknown, cannot cancel wait tool.");
return;
}
ProcessData res = new DefaultProcessData ();
String resParam = (String)((Object[])info.state())[1];
res.put (resParam, "CANCELED");
try {
act.setResult (res);
act.complete ();
if (logger.isDebugEnabled ()) {
logger.debug ("Timer application " + applId + " canceled, "
+ auk + " completed");
}
return;
} catch (InvalidDataException e) {
logger.error
("Cannot set \"status\" out parameter of "
+ "wait tool. Propably wrong declaration. "
+ auk + " will be terminated.");
} catch (CannotCompleteException e) {
logger.error ("Cannot complete " + auk
+ " (will be terminated): " + e.getMessage ());
}
if (act.typedState().workflowState() == State.OPEN) {
try {
act.terminate ();
} catch (CannotStopException e) {
logger.error
("Cannot terminate " + auk + ": " + e.getMessage ());
} catch (NotRunningException e) {
logger.debug (auk + " not running although state is open?");
}
}
}
/**
* Application cannot be terminated, throws exception.
*
* @param activity the activity to be canceled
* @throws ApplicationNotStoppedException if execution cannot be
* terminated (see {@link ApplicationNotStoppedException
* <code>ApplicationNotStoppedException</code>}).
*/
public void terminate (Activity activity)
throws ApplicationNotStoppedException {
throw new ApplicationNotStoppedException
("Cannot terminate TimerCanceler");
}
// Implementation of de.danet.an.workflow.spis.aii.ResultProvider
/**
* Returns the result.
*
* @return a <code>Map</code> value
*/
public Object result() {
return null;
}
}