View Javadoc

1   /*$Id: DefaultQueryService.java,v 1.6 2004/07/26 12:07:38 nw 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.CeaException;
15  import org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType;
16  import org.astrogrid.applications.beans.v1.cea.castor.MessageType;
17  import org.astrogrid.applications.beans.v1.cea.castor.ResultListType;
18  import org.astrogrid.applications.beans.v1.cea.castor.types.ExecutionPhase;
19  import org.astrogrid.applications.beans.v1.cea.castor.types.LogLevel;
20  import org.astrogrid.applications.manager.observer.RemoteProgressListener;
21  import org.astrogrid.applications.manager.observer.RemoteResultsListener;
22  import org.astrogrid.applications.manager.persist.ExecutionHistory;
23  import org.astrogrid.applications.manager.persist.SummaryHelper;
24  import org.astrogrid.component.descriptor.ComponentDescriptor;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import java.net.MalformedURLException;
30  import java.net.URI;
31  import java.util.Date;
32  import java.util.Observer;
33  
34  import javax.xml.rpc.ServiceException;
35  
36  import junit.framework.Test;
37  
38  /*** Default implementation of the {@link org.astrogrid.applications.manager.QueryService}
39   * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
40   *
41   */
42  public class DefaultQueryService implements QueryService, ComponentDescriptor {
43      /***
44       * Commons Logger for this class
45       */
46      private static final Log logger = LogFactory.getLog(DefaultQueryService.class);
47  
48      /*** Construct a new DefaultQueryService
49       * @param eh the store to use to service queries
50       */
51      public DefaultQueryService(ExecutionHistory eh) {
52          super();
53          this.executionHistory = eh;
54      }
55      protected final ExecutionHistory executionHistory;
56      
57      
58      public MessageType queryExecutionStatus(String executionId) throws CeaException {     
59          logger.debug("Getting execution status for " +executionId);
60          MessageType retval = null;
61          if (executionHistory.isApplicationInCurrentSet(executionId)) {
62             Application app = executionHistory.getApplicationFromCurrentSet(executionId);
63              retval = app.createTemplateMessage();
64             retval.setContent(app.getStatus().toString());
65             retval.setLevel(LogLevel.INFO);
66             retval.setPhase(app.getStatus().toExecutionPhase());
67          }
68          else // look in the persistance store
69          {
70             ExecutionSummaryType summary = executionHistory.getApplicationFromArchive(executionId);
71             retval = new MessageType();           
72             retval.setContent("The application is no longer running" + summary.getStatus().toString());
73             retval.setLevel(LogLevel.INFO);
74             retval.setPhase(ExecutionPhase.COMPLETED);
75             retval.setSource(summary.getApplicationName() + "\nid" + summary.getExecutionId());
76              retval.setTimestamp(new Date());            
77          }        
78          return retval;
79  
80       }
81  
82       public ResultListType getResults(String executionId) throws CeaException {
83           logger.debug("Getting results for " + executionId);
84           if (executionHistory.isApplicationInCurrentSet(executionId)) {
85               return executionHistory.getApplicationFromCurrentSet(executionId).getResult();
86           } else { //look in the store
87               return executionHistory.getApplicationFromArchive(executionId).getResultList();
88           }
89      
90       }
91  
92       public ExecutionSummaryType getSummary(String executionId) throws CeaException {
93           logger.debug("Getting summary for " + executionId);
94           if (executionHistory.isApplicationInCurrentSet(executionId)) {
95               Application app = executionHistory.getApplicationFromCurrentSet(executionId);
96               return SummaryHelper.summarize(executionId,app);
97           } else {
98               return executionHistory.getApplicationFromArchive(executionId);
99           }
100      }
101 
102     public boolean registerProgressListener(String executionId, URI endpoint) throws CeaException {
103         logger.debug("Registering progress listener for " + executionId + " at " + endpoint);
104         if (! executionHistory.isApplicationInCurrentSet(executionId)) {
105             logger.warn("applicaiton not in current set - no point registering listener, already finished");
106             return false;
107         }
108         Application app = executionHistory.getApplicationFromCurrentSet(executionId);
109         Observer obs = new RemoteProgressListener(endpoint);
110         app.addObserver(obs);
111         return true; 
112     }
113 
114     public boolean registerResultsListener(String executionId, URI endpoint) throws CeaException {
115         logger.debug("Registering results listener for " + executionId + " at " + endpoint);
116         if (! executionHistory.isApplicationInCurrentSet(executionId)) {
117             logger.warn("application not in current set - no point registering listener, its finished already");
118             return false;
119         }
120         Application app = executionHistory.getApplicationFromCurrentSet(executionId);
121         Observer obs;
122         try {
123             obs = new RemoteResultsListener(endpoint);
124         }
125         catch (MalformedURLException e) {
126             throw new CeaException("could not connect create client for remote service",e);
127         }
128         catch (ServiceException e) {
129             throw new CeaException("Could not create client for remote service",e);
130             
131         }
132         app.addObserver(obs);
133         return true; 
134     }
135     /***
136      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
137      */
138     public String getName() {
139         return "DefaultQueryService";
140     }
141     /***
142      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
143      */
144     public String getDescription() {
145         return "Component that queries status of appliations, and manages callbacks";
146     }
147     /***
148      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
149      */
150     public Test getInstallationTest() {
151         return null;
152     }
153 
154 }
155 
156 
157 /* 
158 $Log: DefaultQueryService.java,v $
159 Revision 1.6  2004/07/26 12:07:38  nw
160 renamed indirect package to protocol,
161 renamed classes and methods within protocol package
162 javadocs
163 
164 Revision 1.5  2004/07/23 08:42:57  nw
165 fixed phase reporting bug.
166 
167 Revision 1.4  2004/07/09 14:48:24  nw
168 updated to match change in type of register*Listener methods in cec wsdl
169 
170 Revision 1.3  2004/07/02 09:11:13  nw
171 improved logging
172 
173 Revision 1.2  2004/07/01 11:16:22  nw
174 merged in branch
175 nww-itn06-componentization
176 
177 Revision 1.1.2.2  2004/07/01 01:42:46  nw
178 final version, before merge
179 
180 Revision 1.1.2.1  2004/06/17 09:21:23  nw
181 finished all major functionality additions to core
182  
183 */