001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: TimerCanceler.java,v 1.2 2006/09/29 12:32:10 drmlipp Exp $
021: *
022: * $Log: TimerCanceler.java,v $
023: * Revision 1.2 2006/09/29 12:32:10 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.1.1.1 2004/08/18 15:17:39 drmlipp
027: * Update to 1.2
028: *
029: * Revision 1.8 2004/07/07 14:45:49 lipp
030: * Added missing return statement.
031: *
032: * Revision 1.7 2004/04/12 19:33:52 lipp
033: * Clarified application invocation interface.
034: *
035: * Revision 1.6 2004/04/01 09:32:07 lipp
036: * Improved tool agent context implementtaion.
037: *
038: * Revision 1.5 2004/03/31 20:48:14 lipp
039: * Cleaned up.
040: *
041: * Revision 1.4 2004/03/31 19:36:20 lipp
042: * Completed implementation of Activity.abandon(String).
043: *
044: * Revision 1.3 2004/02/27 18:09:31 lipp
045: * Removed overlooked hard-coding of result parameter.
046: *
047: * Revision 1.2 2004/02/24 13:51:21 lipp
048: * Fixed typo.
049: *
050: * Revision 1.1 2004/02/20 18:56:35 lipp
051: * Renamed package waittool to timing (much better ;-)).
052: *
053: * Revision 1.2 2004/02/20 15:58:22 lipp
054: * Several WaitTool fixes.
055: *
056: * Revision 1.1 2004/02/19 17:55:55 lipp
057: * Initial version of waittool.
058: *
059: */
060: package de.danet.an.workflow.tools.timing;
061:
062: import java.util.Map;
063:
064: import java.rmi.RemoteException;
065:
066: import de.danet.an.workflow.omgcore.CannotCompleteException;
067: import de.danet.an.workflow.omgcore.CannotStopException;
068: import de.danet.an.workflow.omgcore.InvalidDataException;
069: import de.danet.an.workflow.omgcore.NotRunningException;
070: import de.danet.an.workflow.omgcore.ProcessData;
071: import de.danet.an.workflow.omgcore.WfExecutionObject.State;
072:
073: import de.danet.an.workflow.api.Activity;
074: import de.danet.an.workflow.api.ActivityUniqueKey;
075: import de.danet.an.workflow.api.DefaultProcessData;
076: import de.danet.an.workflow.api.FormalParameter;
077: import de.danet.an.workflow.api.InvalidKeyException;
078:
079: import de.danet.an.workflow.spis.aii.ApplicationNotStoppedException;
080: import de.danet.an.workflow.spis.aii.CannotExecuteException;
081: import de.danet.an.workflow.spis.aii.ResultProvider;
082: import de.danet.an.workflow.tools.util.SimpleApplicationInfo;
083:
084: /**
085: * This class provides a tool agent that cancels a timer.
086: *
087: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
088: * @version $Revision: 1.2 $
089: */
090:
091: public class TimerCanceler extends ToolAgentBase implements
092: ResultProvider {
093:
094: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
095: .getLog(TimerCanceler.class);
096:
097: /** The result container. */
098: private ThreadLocal result = new ThreadLocal();
099:
100: /**
101: * Creates an instance of <code>TimerCanceler</code>
102: * with all attributes initialized to default values.
103: */
104: public TimerCanceler() {
105: }
106:
107: /**
108: * Executes the tool.
109: *
110: * @param activity an <code>Activity</code> value
111: * @param fps the formal parameters
112: * @param map a <code>Map</code> value
113: * @exception CannotExecuteException if an error occurs
114: * @exception RemoteException if an error occurs
115: */
116: public void invoke(Activity activity, FormalParameter[] fps, Map map)
117: throws CannotExecuteException, RemoteException {
118: // Note that a RemoteException will lead to a rollback and an
119: // automatic re-invocation of this method.
120: long applId = ((Long) map.get(fps[0].id())).longValue();
121: if (logger.isDebugEnabled()) {
122: logger.debug("Cancelling timer application " + applId);
123: }
124: SimpleApplicationInfo info = null;
125: try {
126: info = applicationDirectory().instanceInfo(applId);
127: applicationDirectory().removeInstance(applId);
128: } catch (InvalidKeyException e) {
129: logger.warn("Timer application " + applId
130: + " could not be "
131: + "canceled because is does not exist.");
132: return;
133: }
134: Object timer = ((Object[]) info.state())[0];
135: timerHandler().removeTimer(timer);
136: ActivityUniqueKey auk = info.activityUniqueKey();
137: Activity act = null;
138: try {
139: act = toolAgentContext().lookupActivity(auk);
140: } catch (InvalidKeyException e) {
141: logger.warn(auk + " is unknown, cannot cancel wait tool.");
142: return;
143: }
144: ProcessData res = new DefaultProcessData();
145: String resParam = (String) ((Object[]) info.state())[1];
146: res.put(resParam, "CANCELED");
147: try {
148: act.setResult(res);
149: act.complete();
150: if (logger.isDebugEnabled()) {
151: logger.debug("Timer application " + applId
152: + " canceled, " + auk + " completed");
153: }
154: return;
155: } catch (InvalidDataException e) {
156: logger.error("Cannot set \"status\" out parameter of "
157: + "wait tool. Propably wrong declaration. " + auk
158: + " will be terminated.");
159: } catch (CannotCompleteException e) {
160: logger.error("Cannot complete " + auk
161: + " (will be terminated): " + e.getMessage());
162: }
163: if (act.typedState().workflowState() == State.OPEN) {
164: try {
165: act.terminate();
166: } catch (CannotStopException e) {
167: logger.error("Cannot terminate " + auk + ": "
168: + e.getMessage());
169: } catch (NotRunningException e) {
170: logger.debug(auk
171: + " not running although state is open?");
172: }
173: }
174: }
175:
176: /**
177: * Application cannot be terminated, throws exception.
178: *
179: * @param activity the activity to be canceled
180: * @throws ApplicationNotStoppedException if execution cannot be
181: * terminated (see {@link ApplicationNotStoppedException
182: * <code>ApplicationNotStoppedException</code>}).
183: */
184: public void terminate(Activity activity)
185: throws ApplicationNotStoppedException {
186: throw new ApplicationNotStoppedException(
187: "Cannot terminate TimerCanceler");
188: }
189:
190: // Implementation of de.danet.an.workflow.spis.aii.ResultProvider
191:
192: /**
193: * Returns the result.
194: *
195: * @return a <code>Map</code> value
196: */
197: public Object result() {
198: return null;
199: }
200:
201: }
|