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: TimerCreator.java,v 1.3 2006/09/29 12:32:10 drmlipp Exp $
021: *
022: * $Log: TimerCreator.java,v $
023: * Revision 1.3 2006/09/29 12:32:10 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.2 2004/10/20 20:39:57 drmlipp
027: * Added debug info.
028: *
029: * Revision 1.1.1.1 2004/08/18 15:17:39 drmlipp
030: * Update to 1.2
031: *
032: * Revision 1.5 2004/04/13 14:34:23 lipp
033: * Added DirectInvocable support.
034: *
035: * Revision 1.4 2004/04/12 19:33:52 lipp
036: * Clarified application invocation interface.
037: *
038: * Revision 1.3 2004/03/31 19:36:20 lipp
039: * Completed implementation of Activity.abandon(String).
040: *
041: * Revision 1.2 2004/02/24 13:51:21 lipp
042: * Fixed typo.
043: *
044: * Revision 1.1 2004/02/20 18:56:35 lipp
045: * Renamed package waittool to timing (much better ;-)).
046: *
047: * Revision 1.4 2004/02/20 15:58:22 lipp
048: * Several WaitTool fixes.
049: *
050: * Revision 1.3 2004/02/19 21:14:48 lipp
051: * Several WaitTool fixes.
052: *
053: * Revision 1.2 2004/02/19 18:06:42 lipp
054: * Fixed application name handling.
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.Date;
063: import java.util.HashMap;
064: import java.util.Map;
065:
066: import java.rmi.RemoteException;
067:
068: import de.danet.an.workflow.api.Activity;
069: import de.danet.an.workflow.api.FormalParameter;
070: import de.danet.an.workflow.api.InvalidKeyException;
071:
072: import de.danet.an.workflow.spis.aii.ApplicationNotStoppedException;
073: import de.danet.an.workflow.spis.aii.CannotExecuteException;
074: import de.danet.an.workflow.spis.aii.ResultProvider;
075: import de.danet.an.workflow.tools.util.DirectInvocable;
076:
077: /**
078: * This class provides a tool agent that creates a timer.
079: *
080: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
081: * @version $Revision: 1.3 $
082: */
083:
084: public class TimerCreator extends ToolAgentBase implements
085: ResultProvider, DirectInvocable {
086:
087: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
088: .getLog(TimerCreator.class);
089:
090: /** The result container. */
091: private ThreadLocal result = new ThreadLocal();
092:
093: /**
094: * Creates an instance of <code>TimerCreator</code>
095: * with all attributes initialized to default values.
096: */
097: public TimerCreator() {
098: }
099:
100: /**
101: * Executes the tool.
102: *
103: * @param activity an <code>Activity</code> value
104: * @param fps the formal parameters
105: * @param map a <code>Map</code> value
106: * @exception CannotExecuteException if an error occurs
107: * @exception RemoteException if an error occurs
108: */
109: public void invoke(Activity activity, FormalParameter[] fps, Map map)
110: throws CannotExecuteException, RemoteException {
111: try {
112: if (fps[0].mode() == FormalParameter.Mode.IN
113: || fps[0].type() != Long.class
114: || fps[1].mode() == FormalParameter.Mode.OUT
115: || fps[1].type() != Date.class) {
116: throw new CannotExecuteException(
117: "First parameter must be OUT or INOUT (is "
118: + fps[0].mode()
119: + ") and of type INTEGER (is "
120: + fps[0].type()
121: + ", "
122: + "second parameter must be IN or INOUT (is "
123: + fps[1].mode()
124: + ") and of type STRING (is "
125: + fps[1].type() + ")");
126: }
127: Date waitUntil = (Date) map.get(fps[1].id());
128: if (logger.isDebugEnabled()) {
129: logger.debug("Creating timer for " + waitUntil);
130: }
131: long applId = applicationDirectory().registerInstance(
132: WaitTool.class.getName(), activity, null, false);
133: if (logger.isDebugEnabled()) {
134: logger.debug("Application " + applId + " created for "
135: + activity);
136: }
137: Object timer = timerHandler()
138: .createTimer(applId, waitUntil);
139: applicationDirectory().updateState(applId,
140: new Object[] { timer, null });
141: applicationDirectory().updateInvokingActivity(applId, null);
142: Map res = new HashMap();
143: res.put(fps[0].id(), new Long(applId));
144: result.set(res);
145: } catch (InvalidKeyException e) {
146: String msg = "Cannot use newly created key?!: "
147: + e.getMessage();
148: logger.error(msg);
149: throw new CannotExecuteException(msg);
150: }
151: }
152:
153: /**
154: * Application cannot be terminated, throws exception.
155: *
156: * @param activity the activity to be canceled
157: * @throws ApplicationNotStoppedException if execution cannot be
158: * terminated (see {@link ApplicationNotStoppedException
159: * <code>ApplicationNotStoppedException</code>}).
160: */
161: public void terminate(Activity activity)
162: throws ApplicationNotStoppedException {
163: throw new ApplicationNotStoppedException(
164: "Cannot terminate TimerCreator");
165: }
166:
167: // Implementation of de.danet.an.workflow.spis.aii.ResultProvider
168:
169: /**
170: * Returns the result.
171: *
172: * @return a <code>Map</code> value
173: */
174: public Object result() {
175: Map res = (Map) result.get();
176: result.set(null);
177: return res;
178: }
179:
180: }
|