/*
* 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: DefaultToolAgentContext.java 2764 2008-06-22 22:04:12Z mlipp $
*
* $Log$
* Revision 1.3 2006/10/13 11:45:28 drmlipp
* Using ExceptionResult to pass exception information.
*
* Revision 1.2 2006/09/29 12:32:10 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.1 2004/08/18 15:17:38 drmlipp
* Update to 1.2
*
* Revision 1.1 2004/04/01 09:32:07 lipp
* Improved tool agent context implementtaion.
*
* Revision 1.2 2004/03/31 20:47:21 lipp
* Added processDirectory().
*
* Revision 1.1 2004/03/29 11:45:25 lipp
* Made engine context available to tool agents.
*
*/
package de.danet.an.workflow.ejbs.client;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.rmi.RemoteException;
import javax.ejb.Handle;
import de.danet.an.workflow.omgcore.CannotCompleteException;
import de.danet.an.workflow.omgcore.InvalidDataException;
import de.danet.an.workflow.omgcore.TransitionNotAllowedException;
import de.danet.an.workflow.api.Activity;
import de.danet.an.workflow.api.ActivityUniqueKey;
import de.danet.an.workflow.api.InvalidKeyException;
import de.danet.an.workflow.ejbs.WorkflowEngine;
import de.danet.an.workflow.spis.aii.ToolAgentContext;
import de.danet.an.workflow.spis.aii.ResultProvider.ExceptionResult;
/**
* This class provides an implementation of <code>EngineContext</code>.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 2764 $
*/
public class DefaultToolAgentContext implements ToolAgentContext {
/** Class id. */
static final long serialVersionUID = 8131847475946175709L;
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory.getLog
(DefaultToolAgentContext.class);
private transient WorkflowEngine engine = null;
private transient Activity activity = null;
private String applicationId = null;
private void writeObject (ObjectOutputStream out) throws IOException {
try {
out.writeObject(engine.getHandle ());
out.writeObject(activity.uniqueKey());
} catch (RemoteException e) {
throw (IOException)
(new IOException ("Cannot get EJB handle: " + e.getMessage ()))
.initCause (e);
}
}
private void readObject (ObjectInputStream in) throws IOException {
ActivityUniqueKey auk = null;
try {
engine = (WorkflowEngine)((Handle)in.readObject()).getEJBObject ();
auk = (ActivityUniqueKey)in.readObject();
activity = engine.processDirectory().lookupActivity (auk);
} catch (InvalidKeyException e) {
throw (IOException)
(new IOException ("Cannot find " + auk.toString() + ": "
+ e.getMessage ())).initCause (e);
} catch (ClassNotFoundException e) {
throw (IOException)
(new IOException ("Cannot get EJB from handle: "
+ e.getMessage ())).initCause (e);
} catch (RemoteException e) {
throw (IOException)
(new IOException ("Cannot get EJB from handle: "
+ e.getMessage ())).initCause (e);
}
}
/**
* Creates an instance of <code>DefaultToolAgentContext</code>
* with all attributes initialized to the given values.
* @param wfe the workflow engine
* @param act the invoking activity
* @param applicationId the application id
*/
public DefaultToolAgentContext
(WorkflowEngine wfe, Activity act, String applicationId) {
engine = wfe;
activity = act;
this.applicationId = applicationId;
}
/* Comment copied from interface. */
public String applicationId () {
return applicationId;
}
/* Comment copied from interface. */
public void finishActivity(Map map)
throws InvalidDataException, CannotCompleteException {
while (true) {
try {
engine.doFinish (activity, map);
break;
} catch (RemoteException e) {
logger.debug ("Error while finishing activity (repeating): "
+ e.getMessage (), e);
}
}
}
/* Comment copied from interface. */
public void abandonActivity(String string)
throws TransitionNotAllowedException {
while (true) {
try {
engine.doAbandon (activity, new ExceptionResult(string));
break;
} catch (RemoteException e) {
logger.debug ("Error while abandoning activity (repeating): "
+ e.getMessage (), e);
}
}
}
/* Comment copied from interface. */
public void abandonActivity(ExceptionResult result)
throws TransitionNotAllowedException {
while (true) {
try {
engine.doAbandon (activity, result);
break;
} catch (RemoteException e) {
logger.debug ("Error while abandoning activity (repeating): "
+ e.getMessage (), e);
}
}
}
/* Comment copied from interface. */
public Activity lookupActivity (ActivityUniqueKey auk)
throws InvalidKeyException, RemoteException {
return engine.processDirectory().lookupActivity (auk);
}
}