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