View Javadoc

1   /*
2    * $Id: DefaultExecutionController.java,v 1.9 2004/10/08 20:01:10 pah Exp $
3    *
4    * Created on 13 November 2003 by Paul Harrison
5    * Copyright 2003 AstroGrid. All rights reserved.
6    *
7    * This software is published under the terms of the AstroGrid
8    * Software License version 1.2, a copy of which has been included
9    * with this distribution in the LICENSE.txt file.
10   */
11  
12  package org.astrogrid.applications.manager;
13  
14  import org.astrogrid.applications.Application;
15  import org.astrogrid.applications.CeaException;
16  import org.astrogrid.applications.Status;
17  import org.astrogrid.applications.description.ApplicationDescription;
18  import org.astrogrid.applications.description.ApplicationDescriptionLibrary;
19  import org.astrogrid.applications.manager.persist.ExecutionHistory;
20  import org.astrogrid.applications.manager.persist.PersistenceException;
21  import org.astrogrid.community.User;
22  import org.astrogrid.component.descriptor.ComponentDescriptor;
23  import org.astrogrid.workflow.beans.v1.Tool;
24  
25  import java.util.Observable;
26  import java.util.Observer;
27  
28  import junit.framework.Test;
29  
30  /***
31   * Default implementation of the {@link org.astrogrid.applications.manager.ExecutionController}
32   * <p>
33   * This component is itself an observer of all the applications it creates - by observing, it can archive applications once they have completed.
34   * @author Paul Harrison (pah@jb.man.ac.uk)
35   * @version $Name:  $
36   * @since iteration4
37   */ 
38  public class DefaultExecutionController
39     implements ExecutionController, Observer, ComponentDescriptor{
40  
41     static final protected org.apache.commons.logging.Log logger =
42        org.apache.commons.logging.LogFactory.getLog(ExecutionController.class);
43     /***
44      * The store for the descriptions of the applications that this application controller manages.
45      */
46     protected final ApplicationDescriptionLibrary applicationDescriptions;
47     /*** store of current and previously executing applications */
48     protected final ExecutionHistory executionHistory;
49      /***
50       *  Construct a new DefaultExecutionController
51       * @param library collection of available appliation descriptions
52       * @param executionHistory store of applications currently running, and archive of previous executions.
53       */
54     public DefaultExecutionController(final ApplicationDescriptionLibrary library, ExecutionHistory executionHistory ) {
55        logger.info("initializing application controller");
56        this.applicationDescriptions = library;
57  
58        this.executionHistory = executionHistory;
59     }
60  public  boolean execute(String executionId) throws CeaException {
61      logger.info("executing "+ executionId);
62        boolean success = false;
63  
64        if (executionHistory.isApplicationInCurrentSet(executionId)) {
65          Application app =
66              (Application)executionHistory.getApplicationFromCurrentSet(executionId);
67          //NWW - altered this, to handle new method of running applications.
68          // will still fallback to legacy method.
69          Runnable r = app.createExecutionTask();
70          if (r == null) {
71              return app.execute();
72          } else {
73              return startRunnable(r);
74          }
75        }
76  
77        return success;
78  
79     }
80  
81  
82  
83  /*** abstract out how we start a runnable running.
84   * @param r
85   */
86  protected boolean startRunnable(Runnable r) {
87      Thread t = new Thread(r);
88      t.start();
89      return true;
90  }
91  public String init(Tool tool, String jobstepID) throws CeaException {
92      logger.debug("Initializing application " + jobstepID);
93        int idx;
94        String toolname = tool.getName();
95            
96        try {
97            ApplicationDescription descr = applicationDescriptions.getDescription(toolname);
98            User user = new User(); //TODO this needs to be obtained from the context
99            Application app = descr.initializeApplication(jobstepID,user,tool);   
100           app.checkParameterValues();          
101           executionHistory.addApplicationToCurrentSet(app);          
102           app.addObserver(this);
103           return app.getID();
104       } catch (CeaException e) {
105           throw e;
106       } catch (Exception e) {
107           logger.error("Could not execute " + toolname,e);
108           throw new CeaException("Could not execute " + toolname,e);
109       }
110    }
111 
112    public boolean abort(String executionId) {
113        logger.debug("aborting " + executionId);
114        if(!   executionHistory.isApplicationInCurrentSet(executionId)) {
115            return false; // its already dead.
116        }
117        try {
118         Application app = executionHistory.getApplicationFromCurrentSet(executionId);
119         return app.attemptAbort();
120        } catch (PersistenceException e) {
121            // oh well
122            logger.error("Could not abort application, due to persistence error",e);
123            return false;
124        }
125    }
126 
127  /*** This component is itself an observer. it watches all applications, move them to the archive once they've completed.
128   * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
129   */
130  public void update(Observable o, Object arg) {
131      if (! (o instanceof Application )) {
132          logger.warn("Seem to be observing the wrong things.." + o.getClass().getName());
133          return;
134      }
135      if(!( arg instanceof Status))
136      {
137          //NB this observer is only interested if there are status changes - don't need to issue any log warnings....
138          return;
139      }
140      Status stat = (Status)arg;
141      if (stat.equals(Status.COMPLETED) || stat.equals(Status.ERROR)) {
142          Application app = (Application)o;
143          try {
144              logger.debug("moving "+app.getID()+" execution history to archive");
145             executionHistory.moveApplicationFromCurrentSetToArchive(app.getID());
146         } catch (PersistenceException e) { // oh well
147               logger.error("Could not move application status to archive " + app.getID(),e);
148         }
149      }
150    }
151 /***
152  * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
153  */
154 public String getName() {
155     return "Default Common Execution Controller";
156 }
157 /***
158  * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
159  */
160 public String getDescription() {
161     return "";
162 }
163 /***
164  * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
165  */
166 public Test getInstallationTest() {
167     return null;
168 }
169 
170 
171 
172 
173 
174 
175 
176 
177 }