[jboss-svn-commits] JBL Code SVN: r11245 - labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Apr 23 13:02:43 EDT 2007


Author: estebanschifman
Date: 2007-04-23 13:02:42 -0400 (Mon, 23 Apr 2007)
New Revision: 11245

Added:
   labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java
Removed:
   labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/BaseActionHandler.java
Log:


Deleted: labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/BaseActionHandler.java
===================================================================
--- labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/BaseActionHandler.java	2007-04-23 16:50:20 UTC (rev 11244)
+++ labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/BaseActionHandler.java	2007-04-23 17:02:42 UTC (rev 11245)
@@ -1,112 +0,0 @@
-/*
- * 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.services.jbpm.actions;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.UUID;
-
-import org.apache.log4j.Logger;
-import org.jboss.soa.esb.ConfigurationException;
-import org.jboss.soa.esb.addressing.Call;
-import org.jboss.soa.esb.message.Message;
-import org.jboss.soa.esb.message.format.MessageFactory;
-import org.jboss.soa.esb.services.jbpm.Constants;
-import org.jboss.soa.esb.services.jbpm.util.Helper;
-import org.jbpm.graph.def.ActionHandler;
-import org.jbpm.graph.def.ProcessDefinition;
-import org.jbpm.graph.exe.ExecutionContext;
-import org.jbpm.graph.exe.ProcessInstance;
-import org.jbpm.graph.exe.Token;
-
-/**
- * 
- * Use to send messages to ESB services from jBPM applications.
- * <p/>Assumptions:
- * <br/> Message payload is in the context instance in a context variable.
- * <br/>Context variables "esbCategoryName" and "esbServiceName" will be supplied at runtime.
- * 
- * <p/> The name of the context variable that contains the user object to include in the
- * message payload is rendered by the getContentVariableName() method
- * so it can be changed just by extending this class and overriding that method.
- * 
- * <br/> This action handler will by default (whenever possible) include the process name
- * , version, processInstance id and token ID
- * 
- * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a> 
- *
- */
-
-public abstract class BaseActionHandler implements ActionHandler 
-{
-	
-	private static final long serialVersionUID = 1L;
-	
-	public String 		esbCategoryName;
-	public String 		esbServiceName;
-	
-	protected Logger	_logger = Logger.getLogger(getClass());
-	
-	public Message getTemplate(ExecutionContext context) throws Exception 
-	{
-		if (null==esbCategoryName)
-			throw new ConfigurationException("Service category (esbCategoryName element) must not be null");
-		if (null==esbServiceName)
-			throw new ConfigurationException("Service name (esbServiceName element) must not be null");
-		
-		Message template = MessageFactory.getInstance().getMessage();
-		URI	 uri	= null;
-		try  { uri = new URI(UUID.randomUUID().toString()); }
-		catch (URISyntaxException e)
-		{
-			_logger.fatal("Problem with UUID.randomUUID() - This should not happen");
-			return null;
-		}
-		template.getHeader().getCall().setMessageID(uri);
-
-		
-		ProcessDefinition process	= context.getProcessDefinition();
-		if (null!=process)
-		{
-			Helper.setObjectValue(template, Constants.PROCESS_DEFINITION_NAME, process.getName());
-			Helper.setIntValue(template, Constants.PROCESS_DEFINITION_VERSION, process.getVersion());
-		}
-		
-		ProcessInstance instance = context.getProcessInstance();
-		if (null!=instance)
-		{
-			Helper.setLongValue(template,Constants.PROCESS_INSTANCE_ID,context.getProcessInstance().getId());
-		}
-		
-		Token token	= context.getToken();
-		if (null!=token)
-		{
-			Helper.setLongValue(template,Constants.TOKEN_ID,token.getId());
-		}
-		
-		Call call = template.getHeader().getCall();
-		call.setMessageID(new URI(UUID.randomUUID().toString()));
-		
-		return template;
-	}
-
-}

Copied: labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java (from rev 11220, labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/BaseActionHandler.java)
===================================================================
--- labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/services/jbpm/src/main/java/org/jboss/soa/esb/services/jbpm/actions/EsbActionHandler.java	2007-04-23 17:02:42 UTC (rev 11245)
@@ -0,0 +1,284 @@
+/*
+ * 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.services.jbpm.actions;
+
+import java.net.URI;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.addressing.Call;
+import org.jboss.soa.esb.listeners.message.Invoker;
+import org.jboss.soa.esb.message.Body;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.jboss.soa.esb.services.jbpm.Constants;
+import org.jboss.soa.esb.services.jbpm.util.Helper;
+import org.jboss.soa.esb.util.Util;
+import org.jbpm.context.exe.ContextInstance;
+import org.jbpm.graph.def.ActionHandler;
+import org.jbpm.graph.def.ProcessDefinition;
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.graph.exe.ProcessInstance;
+import org.jbpm.graph.exe.Token;
+
+/**
+ * 
+ * Send messages to ESB services from jBPM applications, and optionally wait for response.
+ * <li/>esbCategoryName - for ESB registry lookup
+ * <li/>esbServiceName &nbsp; - for ESB registry lookup
+ * <li>jbpmToEsb_variables
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if this variable is null,
+ * then <b>ALL</b> context instance variables will be added to the body of the esb message using
+ * the jbpm variable name as the key in the message body
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;otherwise, you can specify a comma delimited list of 
+ * names indicating which jbpm context variables must be added to the message body
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if you wish NOT to add objects in the body of the
+ * outgoing message, you must specify a zero-length string  as the value
+ * </li>
+ * <li>jbpmToEsb_esbNames
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if this value is not specified, then the jbpm variable
+ * names will be used as a key to add the objects in the message body
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;otherwise, a comma delimited list of names
+ * <b>of the same lenght as</b>
+ * the jbpmToEsb_variables list, must be supplied and will be used as keys in the message body
+ * </li>
+ * <li>millisToWaitForResponse - if a value > 0 is supplied, a response will be expected from the ESB service
+ * and maximum wait time will be set accordingly.  If response is not received, a CourierTimeoutException will
+ * be thrown
+ * </li>
+ * <li>return_esbNames
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If this value is null, then an attempt will me made 
+ * to find <b>ALL</b> jbpm context variables in the body of the incoming message, and only those objects
+ * found with a valid jbpm context variable name, will update the jbpm context
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Otherwise, a comma delimited list of names must be supplied.  These will 
+ * be used to obtain values from the incoming response from the ESB service
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If you wish NOT to use objects in the body of the
+ * response to update the context, you must specify a zero-length string  as the value
+ * </li>
+ * <li>return_variables
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if this value is not specified, then the esb object
+ * names will be used both as key in the message body, and as the jbpm variable name
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;otherwise, a comma delimited list of names of the same lenght as
+ * the return_esbNames list, must be supplied and will be used as the jbpm variable names
+ * </li>
+ * <li>processVariables
+ * <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If this variable is specified with a 'true' value, then
+ * process instance level variables will be used to populate the message, and values returned
+ * in the response message, will go back to process instance level, otherwise, token variables will
+ * be used, (and set with response values)  
+ * </li>
+ * @author <a href="mailto:schifest at heuristica.com.ar">Esteban</a> 
+ *
+ */
+
+public class EsbActionHandler implements ActionHandler 
+{
+	
+	private static final long serialVersionUID = 1L;
+	
+	public String 	esbCategoryName;
+	public String 	esbServiceName;
+
+	public String	jbpmToEsb_variables;
+	public String	jbpmToEsb_esbNames;
+
+	public Integer	millisToWaitForResponse;
+	public String	return_esbNames;
+	public String	return_variables;
+	
+	public Boolean	processVariables;
+	
+	protected Logger	_logger = Logger.getLogger(getClass());
+	
+	protected Map<String, String>_outVarMapping;
+	protected Map<String, String>_retVarMapping;
+	
+	public void execute(ExecutionContext executionContext) throws Exception 
+	{
+		Message request = getMessageTemplate(executionContext);
+		varsToMessage(executionContext, request);
+		if (null==millisToWaitForResponse || millisToWaitForResponse < 1)
+		{
+			Invoker.invoke(request, esbCategoryName, esbServiceName);
+			return;
+		}
+		
+		Message response = Invoker.invokeAndAwaitResponse(request, millisToWaitForResponse);
+		if (null!=response)
+			varsFromMessage(executionContext, response);
+	} //________________________________
+
+
+	protected void varsToMessage(ExecutionContext context, Message request) throws Exception 
+	{
+		ContextInstance ctxInstance = context.getContextInstance();
+		Token	token = context.getToken();
+		Body body = request.getBody();
+		boolean global = (null==processVariables) ? false : processVariables;
+		if (null==jbpmToEsb_variables)
+		{
+			Map map = (global)? ctxInstance.getVariables() 
+					: ctxInstance.getVariables(token);
+			if (null!=map)
+				for (Iterator iter=map.entrySet().iterator(); iter.hasNext();)
+				{
+					Map.Entry curr = (Map.Entry)iter.next();
+					body.add(curr.getKey().toString(), curr.getValue());
+				}
+			return;
+		}
+		String[]jbpmNames	= jbpmToEsb_variables.split(",");
+		// do nothing if empty list
+		if (jbpmNames.length==1 && Util.isNullString(jbpmNames[0]))
+			return;
+		
+		String[]esbNames	= (null==jbpmToEsb_esbNames) 
+					? jbpmNames
+					: jbpmToEsb_esbNames.split(",");
+		if (jbpmNames.length != esbNames.length)
+			throw new ConfigurationException("One to one mapping required from jbpm variables to esb names");
+
+		int index = 0;
+		for (String curr: jbpmNames)
+		{
+			Object obj	= (global) ? ctxInstance.getVariable(curr)
+					: ctxInstance.getVariable(curr, token);
+			if (null!=obj)
+				body.add(esbNames[index], obj);
+			index++;
+		}
+	} //________________________________
+
+	protected void varsFromMessage(ExecutionContext context, Message response) throws Exception 
+	{
+		ContextInstance ctxInstance = context.getContextInstance();
+		Token token = context.getToken();
+		Body body = response.getBody();
+		boolean global = (null==processVariables) ? false : processVariables;
+		if (null==return_esbNames)
+		{
+			Map map = (global)? ctxInstance.getVariables() 
+					: ctxInstance.getVariables(token);
+			if (null!=map)
+				for (Iterator iter=map.keySet().iterator(); iter.hasNext();)
+				{
+					String key = iter.next().toString();
+					Object obj = body.get(key);
+					if (null!=obj)
+					{
+						if (global)
+							ctxInstance.setVariable(key, obj);
+						else
+							ctxInstance.setVariable(key, obj, token);
+					}
+				}
+			return;
+		}
+		String[]esbNames	= return_esbNames.split(",");
+		// do nothing if empty list
+		if (esbNames.length==1 && Util.isNullString(esbNames[0]))
+			return;
+		
+		String[]jbpmNames	= (null==return_variables) 
+					? esbNames
+					: return_variables.split(",");
+		if (jbpmNames.length != esbNames.length)
+			throw new ConfigurationException("One to one mapping required between esb returned variables and jbpm names");
+
+		int index = 0;
+		for (String curr: esbNames)
+		{
+			Object obj	= body.get(curr);
+			String jbpmVar = jbpmNames[index];
+			Object oldVal = (global) ? ctxInstance.getVariable(jbpmVar)
+							: ctxInstance.getVariable(jbpmVar,context.getToken());
+			if (null==oldVal)
+			{
+				if (null!=obj)
+				{
+					if (global)
+						ctxInstance.createVariable(jbpmVar, obj);
+					else
+						ctxInstance.createVariable(jbpmVar, obj, context.getToken());
+				}
+			}
+			else
+			{
+				if (null==obj)
+				{
+					if (global)
+						ctxInstance.deleteVariable(jbpmVar);
+					else
+						ctxInstance.deleteVariable(jbpmVar, context.getToken());
+				}
+				else
+				{
+					if (global)
+						ctxInstance.setVariable(jbpmVar,obj);
+					else
+						ctxInstance.setVariable(jbpmVar, context.getToken());
+				}
+			}
+			index++;
+		}
+	} //________________________________
+
+	protected Message getMessageTemplate(ExecutionContext context) throws Exception 
+	{
+		if (null==esbCategoryName)
+			throw new ConfigurationException("Service category (esbCategoryName element) must not be null");
+		if (null==esbServiceName)
+			throw new ConfigurationException("Service name (esbServiceName element) must not be null");
+		
+		Message template = MessageFactory.getInstance().getMessage();
+		Call call = template.getHeader().getCall();
+		call.setMessageID(new URI(UUID.randomUUID().toString()));
+		
+		ProcessDefinition process	= context.getProcessDefinition();
+		if (null!=process)
+		{
+			Helper.setObjectValue(template	,Constants.PROCESS_DEFINITION_NAME, process.getName());
+			Helper.setIntValue	 (template	,Constants.PROCESS_DEFINITION_VERSION, process.getVersion());
+			Helper.setLongValue  (template	,Constants.PROCESS_DEFINITION_ID, process.getId());
+		}
+		
+		ProcessInstance instance = context.getProcessInstance();
+		if (null!=instance)
+		{
+			Helper.setLongValue(template,Constants.PROCESS_INSTANCE_ID,context.getProcessInstance().getId());
+		}
+		
+		Token token	= context.getToken();
+		if (null!=token)
+		{
+			Helper.setLongValue	 (template,Constants.TOKEN_ID,token.getId());
+			Helper.setStringValue(template, Constants.CURRENT_NODE_NAME, token.getNode().getName());
+		}
+		
+		return template;
+	} //________________________________
+
+}




More information about the jboss-svn-commits mailing list