KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > shark > SharkToolAgent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Apr 19, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.plugin.shark;
19
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
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 JavaDoc applicationName, String JavaDoc processInstanceId, String JavaDoc workitemId, AppParameter[] appParameters, Integer JavaDoc applicationMode) throws ApplicationNotStarted, ApplicationNotDefined,
49             ApplicationBusy, ToolAgentGeneralException {
50
51         super.invokeApplication(t, sharkHandle, applicationName, procInstId, assId, appParameters, appMode);
52         String JavaDoc instanceId = null;
53         String JavaDoc solutionId = null;
54         String JavaDoc activityPath = null;
55         String JavaDoc actionMode = "direct"; // default action mode is by direct invokation, the XPDL activity can override and specify HTML or JMS(todo) //$NON-NLS-1$
56

57         // Get the URL of the Pentaho server
58
String JavaDoc activityUrl = null;
59         try {
60             ExtendedAttributes exAttrs = readParamsFromExtAttributes(appParameters[0].the_value.toString());
61             // Get the action mode
62
ExtendedAttribute attribute = exAttrs.getFirstExtendedAttributeForName("Pentaho_Activity_Mode"); //$NON-NLS-1$
63
if (attribute != null) {
64                 activityUrl = attribute.get("Value").toValue(); //$NON-NLS-1$
65
}
66             // Get the URL for HTTP action mode
67
attribute = exAttrs.getFirstExtendedAttributeForName("Pentaho_Activity_URL"); //$NON-NLS-1$
68
if (attribute != null) {
69                 activityUrl = attribute.get("Value").toValue(); //$NON-NLS-1$
70
}
71             // Get the settings for JMS action mode
72
attribute = exAttrs.getFirstExtendedAttributeForName("Pentaho_Activity_Queue"); //$NON-NLS-1$
73
if (attribute != null) {
74                 // TODO
75
}
76             // TODO support JCA
77
} catch (Exception JavaDoc e) {
78             e.printStackTrace();
79         }
80
81         // Get the required parameters and any other "IN" or "INOUT" parameters
82
HashMap JavaDoc parameterValues = new HashMap JavaDoc();
83         for (int i = 1; i < appParameters.length; i++) {
84             if ("solution".equals(appParameters[i].the_formal_name)) { //$NON-NLS-1$
85
solutionId = (String JavaDoc) appParameters[i].the_value;
86             } else if ("action".equals(appParameters[i].the_formal_name)) { //$NON-NLS-1$
87
activityPath = (String JavaDoc) appParameters[i].the_value;
88             } else if ("instance-id".equals(appParameters[i].the_formal_name)) { //$NON-NLS-1$
89
instanceId = (String JavaDoc) appParameters[i].the_value;
90                 if (instanceId.trim().length() == 0) {
91                     instanceId = null;
92                 }
93             } else if ("mode".equals(appParameters[i].the_formal_name)) { //$NON-NLS-1$
94
actionMode = (String JavaDoc) appParameters[i].the_value;
95             } else if (!appParameters[i].the_mode.equals("OUT")) { //$NON-NLS-1$
96
parameterValues.put(appParameters[i].the_formal_name, appParameters[i].the_value);
97             }
98         }
99
100         // Validate that we have the minimum required parameters
101
if (activityUrl == null) {
102             throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0001_ATTRIBUTE_NOT_SET")); //$NON-NLS-1$
103
}
104
105         if (solutionId == null) {
106             throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0002_SOLUTION_ID_NOT_SET")); //$NON-NLS-1$
107
}
108
109         if (activityPath == null) {
110             throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0003_ACTION_NAME_NOT_SET")); //$NON-NLS-1$
111
}
112
113         if (activityUrl == null) {
114             throw new ApplicationNotDefined(Messages.getErrorString("SharkToolAgent.ERROR_0004_ACTIVITY_UTL_NOT_SET")); //$NON-NLS-1$
115
}
116
117         status = AbstractToolAgent.APP_STATUS_ACTIVE;
118         StringBuffer JavaDoc resultBuffer = new StringBuffer JavaDoc();
119         try {
120             int pos = activityPath.lastIndexOf('/');
121             String JavaDoc actionPath = null;
122             String JavaDoc actionName = null;
123             if (pos == -1) {
124                 actionPath = ""; //$NON-NLS-1$
125
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)) { //$NON-NLS-1$
133
status = httpAction(instanceId, solutionId, actionPath, actionName, parameterValues, activityUrl, resultBuffer);
134             } else if ("direct".equalsIgnoreCase(actionMode)) { //$NON-NLS-1$
135
status = directAction(workitemId, instanceId, solutionId, actionPath, actionName, parameterValues, resultBuffer);
136             } else if ("JMS".equalsIgnoreCase(actionMode)) { //$NON-NLS-1$
137
// TODO
138
} else if ("JCA".equalsIgnoreCase(actionMode)) { //$NON-NLS-1$
139
// TODO
140
}
141
142             if (status == AbstractToolAgent.APP_STATUS_TERMINATED) {
143                 // action aborted
144
}
145         } catch (Throwable JavaDoc throwable) {
146             cus.error(appName + Messages.getErrorString("SharkToolAgent.ERROR_0005_TERMINATED_BADLY") + throwable); //$NON-NLS-1$
147
status = APP_STATUS_INVALID;
148             throw new ToolAgentGeneralException(throwable);
149         }
150         String JavaDoc result = resultBuffer.toString();
151
152         if (result.equals("")) { //$NON-NLS-1$
153
Logger.error("SharkToolAgent", Messages.getErrorString("SharkToolAgent.ERROR_0006_INVALID_RESPONSE_FROM_ACTION")); //$NON-NLS-1$ //$NON-NLS-2$
154
status = APP_STATUS_INVALID;
155             return;
156         }
157         try {
158             Document soapDoc = XmlHelper.getDocFromString(result);
159
160             if (soapDoc == null) {
161                 // we did not get an understandable reply from the server
162
// TODO log this
163
status = APP_STATUS_TERMINATED;
164                 return;
165             }
166             // TODO check SOAP doc for any error messages
167
// set any returning values into the parameters
168
for (int i = 0; i < appParameters.length; i++) {
169                 if (!appParameters[i].the_mode.equals("IN")) { //$NON-NLS-1$
170
Node resultNode = soapDoc.selectSingleNode("//ExecuteActivityResponse/" + appParameters[i].the_formal_name); //$NON-NLS-1$
171
if (resultNode != null) {
172                         String JavaDoc value = resultNode.getText();
173                         // TODO convert this to another object type if necessary
174
if (value != null) {
175                             appParameters[i].the_value = value;
176                         }
177                     }
178                 }
179             }
180             status = APP_STATUS_FINISHED;
181         } catch (Throwable JavaDoc ex) {
182             cus.error(appName + Messages.getErrorString("SharkToolAgent.ERROR_0005_TERMINATED_BADLY") + ex); //$NON-NLS-1$
183
status = APP_STATUS_INVALID;
184             throw new ToolAgentGeneralException(ex);
185         }
186
187     }
188
189     public long httpAction(String JavaDoc instanceId, String JavaDoc solutionId, String JavaDoc actionPath, String JavaDoc actionName, HashMap JavaDoc parameterValues, String JavaDoc activityUrl, StringBuffer JavaDoc resultBuffer) {
190         StringBuffer JavaDoc urlStr = new StringBuffer JavaDoc();
191
192         urlStr.append(activityUrl).append("?solution=").append(solutionId).append("&path=").append(actionPath).append("&action=").append(actionName); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
193

194         if (instanceId != null) {
195             urlStr.append("&instance-id=" + instanceId); //$NON-NLS-1$
196
}
197
198         // set any returning values into the parameters
199
Iterator JavaDoc parameterValueIterator = parameterValues.keySet().iterator();
200         while (parameterValueIterator.hasNext()) {
201             String JavaDoc name = (String JavaDoc) parameterValueIterator.next();
202             String JavaDoc value = (String JavaDoc) parameterValues.get(name);
203             urlStr.append("&" + name + "=" + value); //$NON-NLS-1$ //$NON-NLS-2$
204
}
205
206         try {
207             HttpUtil.getURLContent(urlStr.toString(), resultBuffer);
208             return AbstractToolAgent.APP_STATUS_RUNNING;
209         } catch (MalformedURLException JavaDoc e) {
210             // TODO log this
211
} catch (IOException JavaDoc e) {
212             // TODO log this
213
}
214         return AbstractToolAgent.APP_STATUS_TERMINATED;
215     }
216
217     public long directAction(String JavaDoc workitemId, String JavaDoc instanceId, String JavaDoc solutionName, String JavaDoc actionPath, String JavaDoc actionName, HashMap JavaDoc parameterValues, StringBuffer JavaDoc resultBuffer) {
218         // PentahoSystem.systemEntryPoint();
219
try {
220             StandaloneSession userSession = new StandaloneSession("Shark Workflow Tool Agent", workitemId); //$NON-NLS-1$
221

222             String JavaDoc 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             // PentahoSystem.systemExitPoint();
247
}
248     }
249
250     public String JavaDoc getInfo(SharkTransaction t) {
251         return Messages.getString("SharkToolAgent.TOOL_AGENT_INFO"); //$NON-NLS-1$
252
}
253
254 }
255
Popular Tags