[jboss-svn-commits] JBL Code SVN: r9578 - labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Feb 18 03:16:33 EST 2007


Author: estebanschifman
Date: 2007-02-18 03:16:32 -0500 (Sun, 18 Feb 2007)
New Revision: 9578

Added:
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/JbpmCommandInterpreter.java
Log:
Preliminary version of a JbpmCommandInterpreter action class

Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/JbpmCommandInterpreter.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/JbpmCommandInterpreter.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/JbpmCommandInterpreter.java	2007-02-18 08:16:32 UTC (rev 9578)
@@ -0,0 +1,297 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.util.JbpmCommandVehicle;
+import org.jbpm.JbpmConfiguration;
+import org.jbpm.JbpmContext;
+import org.jbpm.graph.exe.ProcessInstance;
+import org.jbpm.graph.exe.Token;
+
+/**
+ * 
+ * jBPM command interpreter.
+ * 
+ * <p/>This class is tightly coupled with JbpmCommmandVehicle.
+ * <br/>It should be able to interpret all commands in the 'Operation' enum in JbpmCommandVehicle.
+ * <br/>If new operations are added there, the process() method in this class should be changed accordingly. 
+ * 
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a> 
+ *
+ */
+public class JbpmCommandInterpreter
+{
+	
+	
+	private JbpmCommandInterpreter() {}
+	public JbpmCommandInterpreter(ConfigTree config) throws ConfigurationException
+	{
+		_config = config;
+		checkMyParms();
+	} // ________________________________
+
+	public Message process(Message message)
+	{
+		_command = new JbpmCommandVehicle(message);
+		Enum operator = _command.getOperator();
+		
+		if (operator.equals(JbpmCommandVehicle.Operation.newProcessInstance))	newProcessInstance();
+		if (operator.equals(JbpmCommandVehicle.Operation.signalProcess))		signalProcess();
+		if (operator.equals(JbpmCommandVehicle.Operation.signalToken))			signalToken();
+		if (operator.equals(JbpmCommandVehicle.Operation.getProcessInstanceVariables))
+			getProcessInstanceVariabes();
+		if (operator.equals(JbpmCommandVehicle.Operation.setProcessInstanceVariables))
+			setProcessInstanceVariables();
+		if (operator.equals(JbpmCommandVehicle.Operation.getTokenVariables))	getTokenVariabes();
+		if (operator.equals(JbpmCommandVehicle.Operation.setTokenVariables))	setTokenVariables();
+		else
+		{
+			_logger.error("Unknown operator: "+operator.toString()+" - Returning message unchanged");
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_INVALID_OPCODE);
+		}
+		
+		Message retMsg = _command.toCommandMessage();
+		
+		return retMsg;
+	} // ________________________________
+	
+	public void newProcessInstance()
+	{
+		try
+		{
+			prepareJbpm();
+			_processInstance = _jbpmCtx.newProcessInstance(_command.getProcessDefinitionName());
+			_jbpmCtx.getSession().beginTransaction();
+			_jbpmCtx.save(_processInstance);
+			
+			_command.setProcessVersion(_processInstance.getProcessDefinition().getVersion());
+			_command.setInstanceId	(_processInstance.getId());
+			_command.setTokenId		(_processInstance.getRootToken().getId());
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_OK);
+		}
+		catch (Exception e)
+		{
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_EXCEPTION);
+			_command.setException(e);
+		}
+		finally	{ cleanupJbpm(); } 
+	} //________________________________
+
+	public void signalProcess()
+	{
+		try
+		{
+			long id	= _command.getInstanceId();
+			prepareJbpm();
+			_jbpmCtx.getSession().beginTransaction();
+			_processInstance = _jbpmCtx.loadProcessInstanceForUpdate(id);
+			_processInstance.signal();
+			_jbpmCtx.save(_processInstance);
+
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_OK);
+		}
+		catch (Exception e)
+		{
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_EXCEPTION);
+			_command.setException(e);
+		}
+		finally	{ cleanupJbpm(); } 
+	} //________________________________
+
+	public void signalToken()
+	{
+		try
+		{
+			long id	= _command.getTokenId();
+			prepareJbpm();
+			_jbpmCtx.getSession().beginTransaction();
+			Token token = _jbpmCtx.loadTokenForUpdate(id);
+			token.signal();
+			_jbpmCtx.save(token);
+
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_OK);
+		}
+		catch (Exception e)
+		{
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_EXCEPTION);
+			_command.setException(e);
+		}
+		finally	{ cleanupJbpm(); } 
+	} //________________________________
+
+	public void getProcessInstanceVariabes()
+	{
+		try
+		{
+			long id	= _command.getInstanceId();
+			prepareJbpm();
+			_processInstance = _jbpmCtx.loadProcessInstance(id);
+			Map allVars	= _processInstance.getContextInstance().getVariables();
+			_command.setVariableValues(pickVarsFromMap(allVars));
+
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_OK);
+		}
+		catch (Exception e)
+		{
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_EXCEPTION);
+			_command.setException(e);
+		}
+		finally	{ cleanupJbpm(); } 
+	} //________________________________
+
+	public void setProcessInstanceVariables()
+	{
+		try
+		{
+			Map<String,Object>newVals = _command.getVariableValues();
+			if (null==newVals || newVals.size()<1)
+			{
+				_command.setReturnCode(JbpmCommandVehicle.RETCODE_ERROR);
+				_command.setErrorMessage("Null or empty variable Map");
+				return;
+			}
+			long id	= _command.getInstanceId();
+			prepareJbpm();
+			_jbpmCtx.getSession().beginTransaction();
+			_processInstance = _jbpmCtx.loadProcessInstanceForUpdate(id);
+			_processInstance.getContextInstance().addVariables(newVals);
+			_jbpmCtx.save(_processInstance);
+
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_OK);
+		}
+		catch (Exception e)
+		{
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_EXCEPTION);
+			_command.setException(e);
+		}
+		finally	{ cleanupJbpm(); } 
+	} //________________________________
+
+	public void getTokenVariabes()
+	{
+		try
+		{
+			long id	= _command.getTokenId();
+			prepareJbpm();
+			_token = _jbpmCtx.loadToken(id);
+			Map allVars	= _token.getProcessInstance().getContextInstance().getVariables(_token);
+			_command.setVariableValues(pickVarsFromMap(allVars));
+
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_OK);
+		}
+		catch (Exception e)
+		{
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_EXCEPTION);
+			_command.setException(e);
+		}
+		finally	{ cleanupJbpm(); } 
+	} //________________________________
+
+	public void setTokenVariables()
+	{
+		try
+		{
+			Map<String,Object>newVals = _command.getVariableValues();
+			if (null==newVals || newVals.size()<1)
+			{
+				_command.setReturnCode(JbpmCommandVehicle.RETCODE_ERROR);
+				_command.setErrorMessage("Null or empty variable Map");
+				return;
+			}
+			long id	= _command.getTokenId();
+			prepareJbpm();
+			_jbpmCtx.getSession().beginTransaction();
+			_token = _jbpmCtx.loadToken(id);
+			_token.getProcessInstance().getContextInstance().addVariables(newVals,_token);
+			_jbpmCtx.save(_token);
+
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_OK);
+		}
+		catch (Exception e)
+		{
+			_command.setReturnCode(JbpmCommandVehicle.RETCODE_EXCEPTION);
+			_command.setException(e);
+		}
+		finally	{ cleanupJbpm(); } 
+	} //________________________________
+	
+	/**
+	 * Get list of variables requested in command, from map in arg0.
+	 * <br/>List of requested variables is obtained from command invoking the getVariableNames() method.
+	 * <br/>If the list of variable names is NULL, all variables in the map will be returned.
+	 * <br/>If it's an empty List, an empty map will be returned (why would anybody ask for an empty list of vars ?)
+	 * @param variables
+	 * @return
+	 */
+	@SuppressWarnings("unchecked")
+	protected Map<String,Object>pickVarsFromMap(Map variables)
+	{
+		Set<String>names = _command.getVariableNames();
+		if (null==names)
+			return variables;
+		
+		Map<String,Object>retMap = new HashMap<String, Object>();
+		for(String curr : names)
+			retMap.put(curr, variables.get(curr));
+		
+		return retMap;
+	} //________________________________
+	
+	
+	protected void checkMyParms() throws ConfigurationException
+	{
+	} // _______________________________
+    
+	protected void prepareJbpm()
+	{
+		_jbpmConfig	= null;
+		_jbpmCtx	= null;
+		_jbpmConfig = JbpmConfiguration.getInstance();
+		_jbpmCtx 	= _jbpmConfig.createJbpmContext();
+	} //________________________________
+	protected void cleanupJbpm()
+	{
+		if (null!=_jbpmCtx) 	_jbpmCtx.close();
+		if (null!=_jbpmConfig)	_jbpmConfig.close();
+		_jbpmConfig		= null;
+		_jbpmCtx		= null;
+		_processInstance= null;
+		_token			= null;
+	} //________________________________
+	
+	protected ConfigTree 		_config;
+	protected JbpmCommandVehicle _command;
+	protected JbpmConfiguration	_jbpmConfig;
+	protected JbpmContext 		_jbpmCtx;
+	protected ProcessInstance	_processInstance;
+	protected Token				_token;
+	
+	protected static Logger _logger = Logger.getLogger(JbpmCommandInterpreter.class);
+}


Property changes on: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/JbpmCommandInterpreter.java
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list