View Javadoc

1   /*$Id: DefaultQueryService.java,v 1.7 2005/07/05 08:27:00 clq2 Exp $
2    * Created on 16-Jun-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.manager;
12  
13  import org.astrogrid.applications.Application;
14  import org.astrogrid.applications.ApplicationEnvironmentUnavailableException;
15  import org.astrogrid.applications.ApplicationStillRunningException;
16  import org.astrogrid.applications.CeaException;
17  import org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType;
18  import org.astrogrid.applications.beans.v1.cea.castor.MessageType;
19  import org.astrogrid.applications.beans.v1.cea.castor.ResultListType;
20  import org.astrogrid.applications.beans.v1.cea.castor.types.ExecutionPhase;
21  import org.astrogrid.applications.beans.v1.cea.castor.types.LogLevel;
22  import org.astrogrid.applications.manager.observer.RemoteProgressListener;
23  import org.astrogrid.applications.manager.observer.RemoteResultsListener;
24  import org.astrogrid.applications.manager.persist.ExecutionHistory;
25  import org.astrogrid.applications.manager.persist.ExecutionIDNotFoundException;
26  import org.astrogrid.applications.manager.persist.PersistenceException;
27  import org.astrogrid.applications.manager.persist.SummaryHelper;
28  import org.astrogrid.component.descriptor.ComponentDescriptor;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  import java.io.File;
34  import java.io.FileNotFoundException;
35  import java.net.MalformedURLException;
36  import java.net.URI;
37  import java.util.Date;
38  import java.util.Observer;
39  
40  import javax.xml.rpc.ServiceException;
41  
42  import junit.framework.Test;
43  
44  /*** Default implementation of the {@link org.astrogrid.applications.manager.QueryService}
45   * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
46   *
47   */
48  public class DefaultQueryService implements QueryService, ComponentDescriptor {
49      /***
50       * Commons Logger for this class
51       */
52      private static final Log logger = LogFactory.getLog(DefaultQueryService.class);
53    
54      /*** Construct a new DefaultQueryService
55       * @param eh the store to use to service queries
56       */
57      public DefaultQueryService(ExecutionHistory eh, ApplicationEnvironmentRetriver envret) {
58          super();
59          this.executionHistory = eh;
60          this.environmentRetriever = envret;
61      }
62      protected final ExecutionHistory executionHistory;
63      protected final ApplicationEnvironmentRetriver environmentRetriever;
64    
65      
66      public MessageType queryExecutionStatus(String executionId) throws CeaException {     
67          logger.debug("Getting execution status for " +executionId);
68          MessageType retval = null;
69          if (executionHistory.isApplicationInCurrentSet(executionId)) {
70             Application app = executionHistory.getApplicationFromCurrentSet(executionId);
71              retval = app.createTemplateMessage();
72             retval.setContent(app.getStatus().toString());
73             retval.setLevel(LogLevel.INFO);
74             retval.setPhase(app.getStatus().toExecutionPhase());
75          }
76          else // look in the persistance store
77          {
78             ExecutionSummaryType summary = executionHistory.getApplicationFromArchive(executionId);
79             retval = new MessageType();           
80             retval.setContent("The application is no longer running" + summary.getStatus().toString());
81             retval.setLevel(LogLevel.INFO);
82             retval.setPhase(ExecutionPhase.COMPLETED);
83             retval.setSource(summary.getApplicationName() + "\nid" + summary.getExecutionId());
84              retval.setTimestamp(new Date());            
85          }        
86          return retval;
87  
88       }
89  
90       public ResultListType getResults(String executionId) throws CeaException {
91           logger.debug("Getting results for " + executionId);
92           if (executionHistory.isApplicationInCurrentSet(executionId)) {
93               return executionHistory.getApplicationFromCurrentSet(executionId).getResult();
94           } else { //look in the store
95               return executionHistory.getApplicationFromArchive(executionId).getResultList();
96           }
97      
98       }
99  
100      public ExecutionSummaryType getSummary(String executionId) throws CeaException {
101          logger.debug("Getting summary for " + executionId);
102          if (executionHistory.isApplicationInCurrentSet(executionId)) {
103              Application app = executionHistory.getApplicationFromCurrentSet(executionId);
104              return SummaryHelper.summarize(executionId,app);
105          } else {
106              return executionHistory.getApplicationFromArchive(executionId);
107          }
108      }
109 
110     public boolean registerProgressListener(String executionId, URI endpoint) throws CeaException {
111         logger.debug("Registering progress listener for " + executionId + " at " + endpoint);
112         if (! executionHistory.isApplicationInCurrentSet(executionId)) {
113             logger.warn("applicaiton not in current set - no point registering listener, already finished");
114             return false;
115         }
116         Application app = executionHistory.getApplicationFromCurrentSet(executionId);
117         Observer obs = new RemoteProgressListener(endpoint);
118         app.addObserver(obs);
119         return true; 
120     }
121 
122     public boolean registerResultsListener(String executionId, URI endpoint) throws CeaException {
123         logger.debug("Registering results listener for " + executionId + " at " + endpoint);
124         if (! executionHistory.isApplicationInCurrentSet(executionId)) {
125             logger.warn("application not in current set - no point registering listener, its finished already");
126             return false;
127         }
128         Application app = executionHistory.getApplicationFromCurrentSet(executionId);
129         Observer obs;
130         try {
131             obs = new RemoteResultsListener(endpoint);
132         }
133         catch (MalformedURLException e) {
134             throw new CeaException("could not connect create client for remote service",e);
135         }
136         catch (ServiceException e) {
137             throw new CeaException("Could not create client for remote service",e);
138             
139         }
140         app.addObserver(obs);
141         return true; 
142     }
143     /***
144      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
145      */
146     public String getName() {
147         return "DefaultQueryService";
148     }
149     /***
150      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
151      */
152     public String getDescription() {
153         return "Component that queries status of appliations, and manages callbacks";
154     }
155     /***
156      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
157      */
158     public Test getInstallationTest() {
159         return null;
160     }
161 
162    /* (non-Javadoc)
163     * @see org.astrogrid.applications.manager.QueryService#getLogFile(java.lang.String, org.astrogrid.applications.manager.QueryService.StdIOType)
164     */
165    public File getLogFile(String executionId, ApplicationEnvironmentRetriver.StdIOType type) throws ApplicationEnvironmentUnavailableException, PersistenceException, FileNotFoundException, ApplicationStillRunningException, ApplicationEnvironmentUnavailableException  {
166       if (type == ApplicationEnvironmentRetriver.StdIOType.out) {
167       
168          return environmentRetriever.retrieveStdOut(executionId);
169       }
170       else if(type == ApplicationEnvironmentRetriver.StdIOType.err)
171       {
172          return environmentRetriever.retrieveStdErr(executionId);
173       }
174       else { 
175       
176          //should not be able to happen
177          throw new InternalError("unknown ApplicationEnvironmentRetriver.StdIOType");
178        
179       }
180    }
181 
182 }
183 
184 
185 /* 
186 $Log: DefaultQueryService.java,v $
187 Revision 1.7  2005/07/05 08:27:00  clq2
188 paul's 559b and 559c for wo/apps and jes
189 
190 Revision 1.6.152.1  2005/06/09 08:47:32  pah
191 result of merging branch cea_pah_559b into HEAD
192 
193 Revision 1.6.138.1  2005/06/03 16:01:48  pah
194 first try at getting commandline execution log bz#1058
195 
196 Revision 1.6  2004/07/26 12:07:38  nw
197 renamed indirect package to protocol,
198 renamed classes and methods within protocol package
199 javadocs
200 
201 Revision 1.5  2004/07/23 08:42:57  nw
202 fixed phase reporting bug.
203 
204 Revision 1.4  2004/07/09 14:48:24  nw
205 updated to match change in type of register*Listener methods in cec wsdl
206 
207 Revision 1.3  2004/07/02 09:11:13  nw
208 improved logging
209 
210 Revision 1.2  2004/07/01 11:16:22  nw
211 merged in branch
212 nww-itn06-componentization
213 
214 Revision 1.1.2.2  2004/07/01 01:42:46  nw
215 final version, before merge
216 
217 Revision 1.1.2.1  2004/06/17 09:21:23  nw
218 finished all major functionality additions to core
219  
220 */