View Javadoc

1   /*
2    * $Id: GWESConfigurationServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
3    *
4    * Copyright (c) 2005-2006, The K-Wf Grid Consortium
5    * Fraunhofer Institute for Computer Architecture and Software Technology
6    * See http://www.kwfgrid.eu and 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.GWESException;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServlet;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  import java.net.MalformedURLException;
21  import java.rmi.RemoteException;
22  
23  import org.apache.log4j.Logger;
24  import org.antlr.stringtemplate.StringTemplate;
25  
26  /**
27   * Configuration Servlet for GWES.
28   * ///ToDo: Configure gwes.properties
29   *
30   * @author Andreas Hoheisel
31   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
32   * @version $Id: GWESConfigurationServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
33   */
34  public class GWESConfigurationServlet extends HttpServlet {
35  
36      private static GWESProperties pr;
37      final static Logger logger = Logger.getLogger(GWESOverviewServlet.class);
38  
39      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40          ServletLogger.log(request);
41          response.setContentType("text/html");
42          PrintWriter out = response.getWriter();
43  
44          String error = null;
45          String success = null;
46  
47          //read gwes.properties
48          try {
49              pr = GWESProperties.getInstance("/gwes.properties");
50          } catch (Exception e) {
51              error += "Could not load gwes.properties:"+e.getMessage()+"\n";
52          }
53  
54          try {
55              success = getStatus(request);
56          } catch (Throwable e) {
57              error += "ERROR connecting to GWES: " + e + "\n";
58          }
59  
60          // print properties html
61          StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("configure");
62          t.setAttribute("menu", new Menu(pr.gwesBaseUrlExternal, "Configuration/Test"));
63          if (error != null) t.setAttribute("error",error);
64          if (success != null) t.setAttribute("success",success);
65          if (pr != null) t.setAttribute("properties",GWESProperties.getPropertiesWithoutSecrets("/gwes.properties"));
66          if (pr != null) t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
67          out.print(t.toString());
68      }
69  
70      private String getStatus(HttpServletRequest request) throws Exception, GWESException {
71          StringBuffer ret = new StringBuffer();
72          String testWorkflowID = startTestWorkflow(request);
73          ret.append("The GWES is operating!");
74          ret.append("<p>Tested with workflow ID <a href=\"")
75                  .append(pr.gwesBaseUrlExternal)
76                  .append("/servlet/WorkflowDetailsServlet?workflowid=")
77                  .append(testWorkflowID)
78                  .append("\">")
79                  .append(testWorkflowID)
80                  .append("</a> on GWES at internal base URL ")
81                  .append(pr.gwesBaseUrlInternal)
82                  .append("</p>");
83          String[] workflowIDs = getWorkflowIDs(request);
84          if (workflowIDs.length > 0) {
85               // print out current workflows
86              ret.append("<p>Currently the Generic Workflow Execution Service handles ")
87                      .append(workflowIDs.length)
88                      .append(" workflow")
89                      .append((workflowIDs.length == 1) ? "</p>" : "s</p>");
90          }
91          return ret.toString();
92      }
93  
94      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
95          doGet(request, response);
96      }
97  
98      private String[] getWorkflowIDs(HttpServletRequest request) throws Exception, GWESException {
99          return RemoteGWES.getInstance().getWorkflowIDs(1, request.getUserPrincipal().getName());
100     }
101 
102     private String startTestWorkflow(HttpServletRequest request) throws Exception, GWESException {
103         String gworkflowdlXsd = pr.gworkflowdlXsdPath+"/gworkflowdl_1_1.xsd";
104 
105         String workflowXml =
106                 "<workflow xmlns=\"http://www.gridworkflow.org/gworkflowdl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "+
107                 "xsi:schemaLocation=\"http://www.gridworkflow.org/gworkflowdl "+ gworkflowdlXsd + "\" ID=\"No_ID\">\n" +
108                 "  <description>Test workflow</description>\n" +
109                 "  <place ID=\"begin\">\n" +
110                 "    <token><control>true</control></token>\n" +
111                 "  </place>\n" +
112                 "  <place ID=\"end\" />\n" +
113                 "  <transition ID=\"t\">\n" +
114                 "    <inputPlace placeID=\"begin\" />\n" +
115                 "    <outputPlace placeID=\"end\" />\n" +
116                 "  </transition>\n" +
117                 "</workflow>";
118         RemoteGWES gwes = RemoteGWES.getInstance();
119         String userID = request.getUserPrincipal().getName();
120         String workflowID = gwes.initiate(workflowXml, userID);
121         gwes.start(workflowID, userID);
122         return workflowID;
123     }
124 
125 }