1
2
3
4
5
6
7
8
9 package net.kwfgrid.gwes.servlet;
10
11 import net.kwfgrid.gwes.client.RemoteGWES;
12 import net.kwfgrid.gwes.WorkflowStatus;
13 import net.kwfgrid.gwes.exception.GWESException;
14 import org.antlr.stringtemplate.StringTemplate;
15 import org.apache.log4j.Logger;
16
17 import javax.servlet.ServletException;
18 import javax.servlet.http.HttpServlet;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.net.MalformedURLException;
24 import java.util.*;
25
26
27
28
29
30
31
32
33
34 public class WorkflowListServlet extends HttpServlet {
35
36 final static Logger logger = Logger.getLogger(WorkflowListServlet.class);
37 private static GWESProperties pr;
38
39 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40 ServletLogger.log(request);
41
42 response.setContentType("text/html");
43 PrintWriter out = response.getWriter();
44
45
46 if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
47
48 try {
49 List<WorkflowInformation> workflows = gatherWorkflowStatusArray(request.getUserPrincipal().getName());
50 String error = null;
51 String success = null;
52 String info = null;
53
54 Object obj = request.getAttribute("error");
55 if (obj instanceof String) error = (String) obj;
56
57 obj = request.getAttribute("success");
58 if (obj instanceof String) success = (String) obj;
59
60 obj = request.getAttribute("info");
61 if (obj instanceof String) info = (String) obj;
62
63 if (workflows == null) {
64 StringBuffer sb = new StringBuffer();
65 sb.append("<p>Currently there are no worfklows available. Please upload a <a href=\"")
66 .append(pr.gwesBaseUrlExternal)
67 .append("/servlet/WorkflowUploadServlet\">New Workflow</a> first.</p>");
68 info = sb.toString();
69 }
70
71 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("listWorkflows");
72 t.setAttribute("menu", new Menu(pr.gwesBaseUrlExternal, "List"));
73 if (workflows != null && workflows.size() > 0) t.setAttribute("workflows", workflows);
74 if (success != null) t.setAttribute("success", success);
75 if (error != null) t.setAttribute("error", error);
76 if (info != null) t.setAttribute("info", info);
77 t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
78 out.print(t.toString());
79 } catch (Exception e) {
80 out.println("<hr><b>ERROR connecting to GWES: " + e + "</b>");
81 out.println("<pre>");
82 e.printStackTrace(out);
83 out.println("</pre>");
84 out.println("<hr>");
85 } catch (GWESException e) {
86 out.println("<hr><b>ERROR connecting to GWES: " + e + "</b>");
87 out.println("<pre>");
88 e.printStackTrace(out);
89 out.println("</pre>");
90 out.println("<hr>");
91 }
92 }
93
94 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
95 String methodSelected = null;
96 List selectedWorkflowIDs = null;
97 int removeLevel = 0;
98
99
100 if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
101
102
103 try {
104 Map<String, String[]> parameters = request.getParameterMap();
105 String userID = request.getUserPrincipal().getName();
106 for (String key : parameters.keySet()) {
107 logger.debug("===========> original key = "+key+" value = "+parameters.get(key));
108
109 if (key.startsWith("RemoveLevel:")) {
110 String removeLevelStr = key.substring(12);
111 if (removeLevelStr.equals("memory")) removeLevel+=1;
112 else if (removeLevelStr.equals("database")) removeLevel+=2;
113 else if (removeLevelStr.equals("filesystems")) removeLevel+=4;
114 }
115
116 else if (key.endsWith(".x")) {
117 int i = key.indexOf(":");
118 String workflowID = key.substring(i + 1, key.length() - 2);
119 String method = key.substring(0, i);
120 logger.debug("method = " + method + "; workflowID = " + workflowID);
121 if (workflowID.equals("Selected")) {
122 methodSelected = method;
123 } else if (method.equals("Start")) {
124 RemoteGWES.getInstance().start(workflowID, request.getUserPrincipal().getName());
125 } else if (method.equals("Suspend")) {
126 RemoteGWES.getInstance().suspend(workflowID, userID);
127 } else if (method.equals("Resume")) {
128 RemoteGWES.getInstance().resume(workflowID, userID);
129 } else if (method.equals("Abort")) {
130 RemoteGWES.getInstance().abort(workflowID, userID);
131 } else if (method.equals("Remove")) {
132 removeLevel = 7;
133 RemoteGWES.getInstance().remove(workflowID, removeLevel, request.getUserPrincipal().getName());
134 request.getSession().removeAttribute("workflowid");
135 request.removeAttribute("workflowid");
136 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("successRemoveWorkflow");
137 t.setAttribute("workflowID", workflowID);
138 t.setAttribute("from", Utils.getFromString(removeLevel));
139 request.setAttribute("success", t.toString());
140 } else if (method.equals("Restart")) {
141 String workflowid = RemoteGWES.getInstance().restart(workflowID, request.getUserPrincipal().getName());
142 logger.info("session workflow = " + workflowid);
143 request.getSession().setAttribute("workflowid", workflowid);
144 request.setAttribute("workflowid", workflowid);
145 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("successRestartWorkflow");
146 t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
147 t.setAttribute("workflowID", workflowid);
148 t.setAttribute("oldworkflowID", workflowID);
149 request.setAttribute("success", t.toString());
150 } else if (method.equals("Restore")) {
151 String workflowid = RemoteGWES.getInstance().restore(workflowID, request.getUserPrincipal().getName());
152 logger.info("session workflow = " + workflowid);
153 request.getSession().setAttribute("workflowid", workflowid);
154 request.setAttribute("workflowid", workflowid);
155 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("successRestoreWorkflow");
156 t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
157 t.setAttribute("workflowID", workflowid);
158 t.setAttribute("oldworkflowID", workflowID);
159 request.setAttribute("success", t.toString());
160 } else if (method.equals("Store")) {
161 RemoteGWES.getInstance().store(workflowID, userID);
162 request.setAttribute("success", "Workflow checkpoint stored in database.");
163 }
164 } else if (key.startsWith("Select:")) {
165 int i = key.indexOf(":");
166 String workflowID = key.substring(i + 1);
167 logger.info("Selected workflowID = " + workflowID);
168 if (selectedWorkflowIDs==null) selectedWorkflowIDs = new ArrayList<String>();
169 selectedWorkflowIDs.add(workflowID);
170 }
171 }
172
173
174 if (selectedWorkflowIDs != null && methodSelected != null) {
175 PrintWriter out = response.getWriter();
176 RemoteGWES gwes = RemoteGWES.getInstance();
177 StringBuffer successWorkflows = new StringBuffer();
178 StringBuffer errorWorkflows = new StringBuffer();
179 Iterator workflowIter = selectedWorkflowIDs.iterator();
180 if (methodSelected.equals("Start")) {
181 while (workflowIter.hasNext()) {
182 String workflowID = (String) workflowIter.next();
183 int status = gwes.getStatus(workflowID, userID);
184 String level = gwes.getProperty(workflowID, "level", userID);
185 if (status == WorkflowStatus.STATUS_INITIATED && level.equals("MEMORY")) {
186 try {
187 gwes.start(workflowID, userID);
188 successWorkflows.append("<p>OK: "+workflowID+" started.</p>");
189 } catch (Exception e) {
190 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT started! Exception: "+e+"</p>");
191 logger.error("ERROR: "+workflowID+" NOT started! Exception: "+e);
192 } catch (GWESException e) {
193 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT started! Exception: "+e+"</p>");
194 logger.error("ERROR: "+workflowID+" NOT started! Exception: "+e);
195 }
196 } else if (status == WorkflowStatus.STATUS_SUSPENDED) {
197 try {
198 gwes.resume(workflowID, userID);
199 successWorkflows.append("<p>OK: "+workflowID+" resumed.</p>");
200 } catch (Exception e) {
201 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT resumed! Exception: "+e+"</p>");
202 logger.error("ERROR: "+workflowID+" NOT resumed! Exception: "+e);
203 } catch (GWESException e) {
204 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT resumed! Exception: "+e+"</p>");
205 logger.error("ERROR: "+workflowID+" NOT resumed! Exception: "+e);
206 }
207 } else {
208 successWorkflows.append("<p>WARNING: "+workflowID+" NOT STARTED OR RESUMED (wrong status "+ WorkflowStatus.getStatusAsString(status)+")!</p>");
209 }
210 }
211 } else if (methodSelected.equals("Suspend")) {
212 while (workflowIter.hasNext()) {
213 String workflowID = (String) workflowIter.next();
214 int status = gwes.getStatus(workflowID, userID);
215 if (status == WorkflowStatus.STATUS_ACTIVE || status == WorkflowStatus.STATUS_RUNNING) {
216 try {
217 gwes.suspend(workflowID, userID);
218 successWorkflows.append("<p>OK: "+workflowID+" suspended.</p>");
219 } catch (Exception e) {
220 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT suspended! Exception: "+e+"</p>");
221 logger.error("ERROR: "+workflowID+" NOT suspended! Exception: "+e);
222 } catch (GWESException e) {
223 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT suspended! Exception: "+e+"</p>");
224 logger.error("ERROR: "+workflowID+" NOT suspended! Exception: "+e);
225 }
226 } else {
227 successWorkflows.append("<p>WARNING: "+workflowID+" NOT SUSPENDED (wrong status "+ WorkflowStatus.getStatusAsString(status)+")!</p>");
228 }
229 }
230 } else if (methodSelected.equals("Abort")) {
231 while (workflowIter.hasNext()) {
232 String workflowID = (String) workflowIter.next();
233 int status = gwes.getStatus(workflowID, userID);
234 if (status != WorkflowStatus.STATUS_TERMINATED && status != WorkflowStatus.STATUS_COMPLETED) {
235 try {
236 gwes.abort(workflowID, userID);
237 successWorkflows.append("<p>OK: "+workflowID+" aborted.</p>");
238 } catch (Exception e) {
239 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT aborted! Exception: "+e+"</p>");
240 logger.error("ERROR: "+workflowID+" NOT aborted! Exception: "+e);
241 } catch (GWESException e) {
242 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT aborted! Exception: "+e+"</p>");
243 logger.error("ERROR: "+workflowID+" NOT aborted! Exception: "+e);
244 }
245 } else {
246 successWorkflows.append("<p>WARNING: "+workflowID+" NOT ABORTED (wrong status "+ WorkflowStatus.getStatusAsString(status)+")!</p>");
247 }
248 }
249 } else if (methodSelected.equals("Remove")) {
250 logger.info("removeLevel="+removeLevel);
251 while (workflowIter.hasNext()) {
252 String workflowID = (String) workflowIter.next();
253 try {
254 gwes.remove(workflowID,removeLevel,request.getUserPrincipal().getName());
255 successWorkflows.append("<p>OK: "+workflowID+" removed.</p>");
256 } catch (Exception e) {
257 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT removed! Exception: "+e+"</p>");
258 logger.error("ERROR: "+workflowID+" NOT removed! Exception: "+e);
259 } catch (GWESException e) {
260 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT removed! Exception: "+e+"</p>");
261 logger.error("ERROR: "+workflowID+" NOT removed! Exception: "+e);
262 }
263 }
264 } else if (methodSelected.equals("Restore")) {
265 while (workflowIter.hasNext()) {
266 String workflowID = (String) workflowIter.next();
267 try {
268 gwes.restore(workflowID,request.getUserPrincipal().getName());
269 successWorkflows.append("<p>OK: "+workflowID+" restored.</p>");
270 } catch (Exception e) {
271 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT restored! Exception: "+e+"</p>");
272 logger.error("ERROR: "+workflowID+" NOT restored! Exception: "+e);
273 } catch (GWESException e) {
274 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT restored! Exception: "+e+"</p>");
275 logger.error("ERROR: "+workflowID+" NOT restored! Exception: "+e);
276 }
277 }
278 } else if (methodSelected.equals("Store")) {
279 while (workflowIter.hasNext()) {
280 String workflowID = (String) workflowIter.next();
281 String level = gwes.getProperty(workflowID, "level", userID);
282 if (level.equals("MEMORY")) {
283 try {
284 gwes.store(workflowID, userID);
285 successWorkflows.append("<p>OK: "+workflowID+" stored.</p>");
286 } catch (Exception e) {
287 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT stored! Exception: "+e+"</p>");
288 logger.error("ERROR: "+workflowID+" NOT stored! Exception: "+e);
289 } catch (GWESException e) {
290 errorWorkflows.append("<p>ERROR: "+workflowID+" NOT stored! Exception: "+e+"</p>");
291 logger.error("ERROR: "+workflowID+" NOT stored! Exception: "+e);
292 }
293 } else {
294 successWorkflows.append("<p>WARNING: "+workflowID+" NOT STORED (not in memory).</p>");
295 }
296 }
297 }
298
299 if (successWorkflows.length()>0) request.setAttribute("success", successWorkflows.toString());
300 if (errorWorkflows.length()>0) request.setAttribute("error",errorWorkflows.toString());
301 }
302
303 } catch (Exception e) {
304 request.setAttribute("error", "Exception during workflow action :" + e);
305 logger.error("Exception during workflow action :" + e, e);
306 } catch (GWESException e) {
307 request.setAttribute("error", "Exception during workflow action :" + e);
308 logger.error("Exception during workflow action :" + e, e);
309 }
310
311 doGet(request, response);
312 }
313
314 private List<WorkflowInformation> gatherWorkflowStatusArray(String userID) throws Exception, GWESException {
315 String[][] states = RemoteGWES.getInstance().getWorkflowStatusArray(3, userID);
316 if (states == null) return null;
317 List<WorkflowInformation> workflows = new ArrayList<WorkflowInformation>(states.length);
318 for (String[] state : states) {
319 WorkflowInformation workflow = new WorkflowInformation(
320 pr.existUrl,
321 pr.gwesBaseUrlExternal,
322 state
323 );
324 workflows.add(workflow);
325 }
326
327 Collections.sort(workflows, new WorkflowComparator(WorkflowComparator.COMPARE_BIRTHDAY));
328
329 return workflows;
330 }
331
332 }