View Javadoc

1   /*
2    * $Id: WorkflowActivityListServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
3    *
4    * Copyright (c) 2007
5    * Fraunhofer Institute for Computer Architecture and Software Technology
6    * See http://www.first.fraunhofer.de for more details.
7    */
8   
9   package net.kwfgrid.gwes.servlet;
10  
11  import net.kwfgrid.gwes.client.RemoteGWES;
12  import net.kwfgrid.gwes.exception.NoSuchWorkflowException;
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.util.*;
24  import java.net.MalformedURLException;
25  
26  /**
27   * Servlet that shows all activities of a specific workflow.
28   * Security should be implemented by means of the Servlet container (e.g. using tomcat-users.xml)
29   *
30   * @author Andreas Hoheisel
31   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
32   * @version $Id: WorkflowActivityListServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
33   */
34  public class WorkflowActivityListServlet extends HttpServlet {
35  
36      private static GWESProperties pr;
37      final static Logger logger = Logger.getLogger(WorkflowActivityListServlet.class);
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          String error = null;
45          String success = null;
46          String info = null;
47  
48          Object obj = request.getAttribute("error");
49          if (obj instanceof String) error = (String) obj;
50  
51          obj = request.getAttribute("success");
52          if (obj instanceof String) success = (String) obj;
53  
54          obj = request.getAttribute("info");
55          if (obj instanceof String) info = (String) obj;
56  
57          //read gwes.properties
58          if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
59  
60          // get user
61          String userID = request.getRemoteUser();
62          if (userID == null) userID = "nn";
63  
64          // get workflow ID
65          String workflowID = request.getParameter("workflowid");
66          if (workflowID == null) {
67              obj = request.getSession().getAttribute("workflowid");
68              if (obj instanceof String) workflowID = (String) obj;
69          } else {
70              logger.debug("session workflow = " + workflowID);
71              request.getSession().setAttribute("workflowid", workflowID);
72          }
73  
74          List<ActivityInformation> activities = null;
75          WorkflowInformation workflow = null;
76  
77          if (error == null && (workflowID == null || workflowID.length() == 0)) {
78              info = "<p>Please select the workflow in the <a href=\"" + pr.gwesBaseUrlExternal + "/servlet/WorkflowListServlet\">Workflow List</a> first!</p>";
79          } else {
80              try {
81                  activities = gatherActivityStatusArray(pr.gwesBaseUrlExternal, workflowID, request.getUserPrincipal().getName());
82                  workflow = getWorkflowInformation(workflowID, request.getUserPrincipal().getName());
83                  if (activities == null) info = "Currently the workflow \""+workflowID+ "\" does not contain active or running activities.";
84              } catch (NoSuchWorkflowException e) {
85                  error = "<p>Workflow \""+ workflowID + "\" is not available in memory. Please select a workflow with level \"MEMORY\" in the <a href=\"" + pr.gwesBaseUrlExternal + "/servlet/WorkflowListServlet\">Workflow List</a>!</p>";
86              } catch (Exception e) {
87                  // gatherActivityStatusArray throws "AxisFault" and not "net.kwfgrid.gwes.client.NoSuchWorkflowException"!
88                  if (e.getMessage().startsWith("net.kwfgrid.gwes.exception.NoSuchWorkflowException:")) {
89                      error = "<p>Workflow \""+ workflowID + "\" is not available in memory. Please select a workflow with level \"MEMORY\" in the <a href=\"" + pr.gwesBaseUrlExternal + "/servlet/WorkflowListServlet\">Workflow List</a>!</p>";
90                  } else {
91                      out.println("<hr><b>ERROR connecting to GWES: " + e + "</b>");
92                      out.println("<pre>");
93                      e.printStackTrace(out);
94                      out.println("</pre>");
95                      out.println("<hr>");
96                  }
97              } catch (GWESException e) {
98                  out.println("<hr><b>ERROR connecting to GWES: " + e + "</b>");
99                  out.println("<pre>");
100                 e.printStackTrace(out);
101                 out.println("</pre>");
102                 out.println("<hr>");
103             }
104         }
105 
106         try {
107             StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("listActivities");
108             t.setAttribute("menu", new Menu(pr.gwesBaseUrlExternal, "Activities"));
109             if (workflow != null) t.setAttribute("workflow",workflow);
110             if (activities != null && activities.size() > 0) t.setAttribute("activities", activities);
111             if (success != null) t.setAttribute("success", success);
112             if (error != null) t.setAttribute("error", error);
113             if (info != null) t.setAttribute("info", info);
114             t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
115             out.print(t.toString());
116         } catch (Exception e) {
117             logger.error("exception: " + e, e);
118             error = ((error == null) ? "" : error) + "Could not get workflow information: " + e.getMessage();
119             info = "<p>Please select an available workflow in the <a href=\"" + pr.gwesBaseUrlExternal + "/servlet/WorkflowListServlet\">Workflow List</a> first!</p>";
120             workflowID = null;
121         }
122     }
123 
124     /**
125      * name="Start"
126      * name="Suspend"
127      * name="Resume"
128      * name="Abort"
129      * name="Remove"
130      * name="Restart"
131      * name="Restore"
132      * name="Checkpoints"
133      * name="Xml"
134      * name="Store"
135      * name="Description"
136      *
137      * @param request
138      * @param response
139      * @throws javax.servlet.ServletException
140      * @throws java.io.IOException
141      */
142     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
143         doGet(request, response);
144     }
145 
146     private List<ActivityInformation> gatherActivityStatusArray(String gwesBaseUrlExternal, String workflowID, String userID) throws Exception, GWESException {
147         String[][] states = RemoteGWES.getInstance().getActivityStatusArray(workflowID, userID);
148         if (states == null || states.length == 0 || states[0] == null || states[0].length == 0) return null;
149         List<ActivityInformation> activities = new ArrayList<ActivityInformation>(states.length);
150         for (String[] state : states) {
151             ActivityInformation activity = new ActivityInformation(gwesBaseUrlExternal, state);
152             activities.add(activity);
153         }
154 
155         return activities;
156     }
157 
158     private WorkflowInformation getWorkflowInformation(String workflowID, String userID) throws Exception, GWESException {
159         if (workflowID == null) return null;
160 
161         // get workflow properties from GWES
162         String[][] p = RemoteGWES.getInstance().getProperties(workflowID, userID);
163         Map<String, String> wfprops = new HashMap<String, String>(p.length);
164         for (String[] aP : p) {
165             wfprops.put(aP[0], aP[1]);
166         }
167 
168         // create workflow information object
169         WorkflowInformation wf = new WorkflowInformation(
170                 pr.existUrl,
171                 pr.gwesBaseUrlExternal,
172                 workflowID,
173                 RemoteGWES.getInstance().getDescription(workflowID, userID),
174                 wfprops
175         );
176 
177         return wf;
178     }
179 
180 
181 
182 }