1 17 18 package org.pentaho.plugin.shark; 19 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import org.dom4j.Document; 25 import org.dom4j.Node; 26 import org.enhydra.shark.api.SharkTransaction; 27 import org.enhydra.shark.api.internal.toolagent.AppParameter; 28 import org.enhydra.shark.api.internal.toolagent.ApplicationBusy; 29 import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined; 30 import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted; 31 import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException; 32 import org.enhydra.shark.toolagent.AbstractToolAgent; 33 import org.enhydra.shark.xpdl.elements.ExtendedAttribute; 34 import org.enhydra.shark.xpdl.elements.ExtendedAttributes; 35 import org.pentaho.core.runtime.IRuntimeContext; 36 import org.pentaho.core.services.HttpWebServiceRequestHandler; 37 import org.pentaho.core.session.StandaloneSession; 38 import org.pentaho.core.solution.SimpleOutputHandler; 39 import org.pentaho.core.solution.SimpleParameterProvider; 40 import org.pentaho.core.util.SoapUtil; 41 import org.pentaho.core.util.XmlHelper; 42 import org.pentaho.messages.Messages; 43 import org.pentaho.util.HttpUtil; 44 import org.pentaho.util.logging.Logger; 45 46 public class SharkToolAgent extends AbstractToolAgent { 47 48 public void invokeApplication(SharkTransaction t, long sharkHandle, String applicationName, String processInstanceId, String workitemId, AppParameter[] appParameters, Integer applicationMode) throws ApplicationNotStarted, ApplicationNotDefined, 49 ApplicationBusy, ToolAgentGeneralException { 50 51 super.invokeApplication(t, sharkHandle, applicationName, procInstId, assId, appParameters, appMode); 52 String instanceId = null; 53 String solutionId = null; 54 String activityPath = null; 55 String actionMode = "direct"; 57 String activityUrl = null; 59 try { 60 ExtendedAttributes exAttrs = readParamsFromExtAttributes(appParameters[0].the_value.toString()); 61 ExtendedAttribute attribute = exAttrs.getFirstExtendedAttributeForName("Pentaho_Activity_Mode"); if (attribute != null) { 64 activityUrl = attribute.get("Value").toValue(); } 66 attribute = exAttrs.getFirstExtendedAttributeForName("Pentaho_Activity_URL"); if (attribute != null) { 69 activityUrl = attribute.get("Value").toValue(); } 71 attribute = exAttrs.getFirstExtendedAttributeForName("Pentaho_Activity_Queue"); if (attribute != null) { 74 } 76 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 81 HashMap parameterValues = new HashMap (); 83 for (int i = 1; i < appParameters.length; i++) { 84 if ("solution".equals(appParameters[i].the_formal_name)) { solutionId = (String ) appParameters[i].the_value; 86 } else if ("action".equals(appParameters[i].the_formal_name)) { activityPath = (String ) appParameters[i].the_value; 88 } else if ("instance-id".equals(appParameters[i].the_formal_name)) { instanceId = (String ) appParameters[i].the_value; 90 if (instanceId.trim().length() == 0) { 91 instanceId = null; 92 } 93 } else if ("mode".equals(appParameters[i].the_formal_name)) { actionMode = (String ) appParameters[i].the_value; 95 } else if (!appParameters[i].the_mode.equals("OUT")) { parameterValues.put(appParameters[i].the_formal_name, appParameters[i].the_value); 97 } 98 } 99 100 if (activityUrl == null) { 102 throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0001_ATTRIBUTE_NOT_SET")); } 104 105 if (solutionId == null) { 106 throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0002_SOLUTION_ID_NOT_SET")); } 108 109 if (activityPath == null) { 110 throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0003_ACTION_NAME_NOT_SET")); } 112 113 if (activityUrl == null) { 114 throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0004_ACTIVITY_UTL_NOT_SET")); } 116 117 status = AbstractToolAgent.APP_STATUS_ACTIVE; 118 StringBuffer resultBuffer = new StringBuffer (); 119 try { 120 int pos = activityPath.lastIndexOf('/'); 121 String actionPath = null; 122 String actionName = null; 123 if (pos == -1) { 124 actionPath = ""; actionName = activityPath; 126 } else { 127 actionPath = activityPath.substring(0, pos); 128 actionName = activityPath.substring(pos + 1); 129 } 130 131 status = AbstractToolAgent.APP_STATUS_TERMINATED; 132 if ("URL".equalsIgnoreCase(actionMode)) { status = httpAction(instanceId, solutionId, actionPath, actionName, parameterValues, activityUrl, resultBuffer); 134 } else if ("direct".equalsIgnoreCase(actionMode)) { status = directAction(workitemId, instanceId, solutionId, actionPath, actionName, parameterValues, resultBuffer); 136 } else if ("JMS".equalsIgnoreCase(actionMode)) { } else if ("JCA".equalsIgnoreCase(actionMode)) { } 141 142 if (status == AbstractToolAgent.APP_STATUS_TERMINATED) { 143 } 145 } catch (Throwable throwable) { 146 cus.error(appName + Messages.getErrorString("SharkToolAgent.ERROR_0005_TERMINATED_BADLY") + throwable); status = APP_STATUS_INVALID; 148 throw new ToolAgentGeneralException(throwable); 149 } 150 String result = resultBuffer.toString(); 151 152 if (result.equals("")) { Logger.error("SharkToolAgent", Messages.getErrorString("SharkToolAgent.ERROR_0006_INVALID_RESPONSE_FROM_ACTION")); status = APP_STATUS_INVALID; 155 return; 156 } 157 try { 158 Document soapDoc = XmlHelper.getDocFromString(result); 159 160 if (soapDoc == null) { 161 status = APP_STATUS_TERMINATED; 164 return; 165 } 166 for (int i = 0; i < appParameters.length; i++) { 169 if (!appParameters[i].the_mode.equals("IN")) { Node resultNode = soapDoc.selectSingleNode("//ExecuteActivityResponse/" + appParameters[i].the_formal_name); if (resultNode != null) { 172 String value = resultNode.getText(); 173 if (value != null) { 175 appParameters[i].the_value = value; 176 } 177 } 178 } 179 } 180 status = APP_STATUS_FINISHED; 181 } catch (Throwable ex) { 182 cus.error(appName + Messages.getErrorString("SharkToolAgent.ERROR_0005_TERMINATED_BADLY") + ex); status = APP_STATUS_INVALID; 184 throw new ToolAgentGeneralException(ex); 185 } 186 187 } 188 189 public long httpAction(String instanceId, String solutionId, String actionPath, String actionName, HashMap parameterValues, String activityUrl, StringBuffer resultBuffer) { 190 StringBuffer urlStr = new StringBuffer (); 191 192 urlStr.append(activityUrl).append("?solution=").append(solutionId).append("&path=").append(actionPath).append("&action=").append(actionName); 194 if (instanceId != null) { 195 urlStr.append("&instance-id=" + instanceId); } 197 198 Iterator parameterValueIterator = parameterValues.keySet().iterator(); 200 while (parameterValueIterator.hasNext()) { 201 String name = (String ) parameterValueIterator.next(); 202 String value = (String ) parameterValues.get(name); 203 urlStr.append("&" + name + "=" + value); } 205 206 try { 207 HttpUtil.getURLContent(urlStr.toString(), resultBuffer); 208 return AbstractToolAgent.APP_STATUS_RUNNING; 209 } catch (MalformedURLException e) { 210 } catch (IOException e) { 212 } 214 return AbstractToolAgent.APP_STATUS_TERMINATED; 215 } 216 217 public long directAction(String workitemId, String instanceId, String solutionName, String actionPath, String actionName, HashMap parameterValues, StringBuffer resultBuffer) { 218 try { 220 StandaloneSession userSession = new StandaloneSession("Shark Workflow Tool Agent", workitemId); 222 String processId = this.getClass().getName(); 223 224 SimpleOutputHandler outputHandler = new SimpleOutputHandler(null, false); 225 SimpleParameterProvider parameterProvider = new SimpleParameterProvider(parameterValues); 226 227 HttpWebServiceRequestHandler requestHandler = new HttpWebServiceRequestHandler(userSession, instanceId, outputHandler, parameterProvider, null); 228 229 requestHandler.setProcessId(processId); 230 requestHandler.setAction(actionPath, actionName); 231 requestHandler.setSolutionName(solutionName); 232 233 IRuntimeContext runtime = null; 234 try { 235 runtime = requestHandler.handleActionRequest(0, 0); 236 resultBuffer.append( SoapUtil.getSoapHeader() ); 237 SoapUtil.generateSoapResponse(runtime, outputHandler, null, resultBuffer, requestHandler.getMessages()); 238 resultBuffer.append( SoapUtil.getSoapFooter() ); 239 } finally { 240 if (runtime != null) { 241 runtime.dispose(); 242 } 243 } 244 return AbstractToolAgent.APP_STATUS_TERMINATED; 245 } finally { 246 } 248 } 249 250 public String getInfo(SharkTransaction t) { 251 return Messages.getString("SharkToolAgent.TOOL_AGENT_INFO"); } 253 254 } 255 | Popular Tags |