View Javadoc

1   /*
2    * $Id: InitServlet.java,v 1.11 2004/11/27 13:20:03 pah Exp $
3    * 
4    * Created on 14-Apr-2004 by Paul Harrison (pah@jb.man.ac.uk)
5    *
6    * Copyright 2004 AstroGrid. All rights reserved.
7    *
8    * This software is published under the terms of the AstroGrid 
9    * Software License version 1.2, a copy of which has been included 
10   * with this distribution in the LICENSE.txt file.  
11   *
12   */
13  
14  package org.astrogrid.applications.component;
15  
16  
17  import java.io.IOException;
18  import java.io.PrintWriter;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.astrogrid.applications.CeaException;
28  import org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType;
29  import org.astrogrid.applications.description.registry.RegistryUploader;
30  import org.astrogrid.applications.manager.MetadataService;
31  import org.astrogrid.config.SimpleConfig;
32  import org.astrogrid.registry.client.RegistryDelegateFactory;
33  
34  /***
35   * A simple servlet that starts cea service, by instantiating the pico container
36   * on destroy, calls back into container, giving things a chance to clean themselves up if needed.
37   * <p>
38   * Also provides access to some of the metadata methods of the container via HTTP-GET
39   * @author Noel Winstanley
40   * @author Paul Harrison (pah@jb.man.ac.uk) 14-Apr-2004
41   * @version $Name:  $
42   * @since iteration5
43   */
44  public class InitServlet extends HttpServlet {
45  
46     static private org.apache.commons.logging.Log logger =
47        org.apache.commons.logging.LogFactory.getLog(InitServlet.class);
48  
49      
50  
51  
52   
53      /***
54       * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
55       */
56      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
57  //always do it for now         if (! LifecycleListener.storedEndpoint) {
58              URL url = makeEndPointURL(req);
59              LifecycleListener.writeEndpointConfig(url);
60  //        }
61          // expect a 'method' parameter.
62          PrintWriter writer = resp.getWriter();
63          String method = req.getParameter("method");
64          if (method == null || method.trim().length() == 0) {
65              usage(writer);
66          } else if (method.trim().toLowerCase().equals("returnregistryentry")){
67              resp.setContentType("text/xml");            
68              returnRegistryEntry(writer);
69          }
70          else if (method.trim().toLowerCase().equals("getexecutionsummary")) {
71              resp.setContentType("text/xml");
72              String execId = req.getParameter("id");
73              getExecutionSummary(writer, execId);
74  
75          }
76          else if (method.trim().toLowerCase().equals("register")) {
77             String endpoint = req.getParameter("endpoint");
78             if(endpoint == null)
79             {
80                endpoint = (String)SimpleConfig.getSingleton().getProperty(RegistryDelegateFactory.ADMIN_URL_PROPERTY);
81             }
82             resp.setContentType("text/html");
83             try {
84              writer.println("<html><head></head><body><p>registered with registry at "+endpoint+"</p>");
85              MetadataService voProvider = CEAComponentManagerFactory.getInstance().getMetadataService();
86             
87              RegistryUploader regUploader = CEAComponentManagerFactory.getInstance().getRegistryUploaderService();
88              regUploader.write(endpoint);
89             writer.println("</p><p>success</p></body></html>");
90           }
91           catch (Exception e) {
92              writer.println("<p>error registering</p><pre>");
93              e.printStackTrace(writer);
94              writer.println("</pre></body></html>");
95           }
96            
97         }
98          
99          else if (method.trim().toLowerCase().equals("startup")) {
100             logger.info("Starting CEA server");
101             CEAComponentManagerFactory.getInstance();
102             resp.setContentType("text/html");
103             writer.println("<html><head></head><body><p>Common Execution Controller Started..</p></body></html>");
104         }
105 
106         else {
107             usage(writer);
108         }
109     }
110     
111  
112    protected void returnRegistryEntry(PrintWriter pw) throws ServletException {
113         try {
114             String regEntry = CEAComponentManagerFactory.getInstance().getMetadataService().returnRegistryEntry();
115             pw.print(regEntry);
116         } catch (CeaException e) {
117             throw new ServletException(e);
118         }
119     }
120     
121     protected void getExecutionSummary(PrintWriter pw, String execId) throws ServletException {
122         try {
123             ExecutionSummaryType summary  = CEAComponentManagerFactory.getInstance().getQueryService().getSummary(execId);
124             summary.marshal(pw);
125         } catch (Exception e) {
126             throw new ServletException(e);
127         }
128     }
129     
130     
131    protected void usage(PrintWriter pw) {
132        pw.println("<html><body>");
133        pw.println("<h1>Usage</h1>");
134        pw.println("<ul>");
135        pw.println("<ul><tt>?method=startup</tt> - start the CommonExecutionController");
136        pw.println("<li><tt>?method=returnRegistryEntry</tt> - display the VODescription for this service</li>");
137        pw.println("<li><tt>?method=getExecutionSummary&amp;id=<i>executionId</i></tt> - display execution summary for <i>executionId</i>");
138        pw.println("</ul>");
139        pw.println("</body></html>");
140    }  
141    
142    private URL makeEndPointURL(HttpServletRequest request) {
143         URL url = null;
144         try {
145             url = new URL("http", request.getServerName(),
146                     request.getServerPort(), request.getContextPath()
147                             + "/services/CommonExecutionConnectorService");
148             logger.info("creating endpointURL="+url);
149 
150         }
151         catch (MalformedURLException e) {
152             logger.warn("could not create the context path from request data",
153                     e);
154         }
155         return url;
156 
157     }
158 
159 
160 
161 }