1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.jes.util;
12
13 import org.astrogrid.jes.types.v1.cea.axis.JobIdentifierType;
14 import org.astrogrid.workflow.beans.v1.Step;
15 import org.astrogrid.workflow.beans.v1.Workflow;
16 import org.astrogrid.workflow.beans.v1.execution.StepExecutionRecord;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import java.util.Iterator;
22
23 /*** class of static helper methods.
24 * <p>
25 * primarily stuff for mappinig between castor and axis object models.
26 * @author Noel Winstanley nw@jb.man.ac.uk 03-Mar-2004
27 *
28 */
29 public class JesUtil {
30 /***
31 * Commons Logger for this class
32 */
33 private static final Log logger = LogFactory.getLog(JesUtil.class);
34
35 /*** Construct a new JesUtil
36 *
37 */
38 private JesUtil() {
39 super();
40 }
41
42 /*** return an iterator of all job steps in the workflow
43 *
44 * @param wf workflow
45 * @return non-null iterator of step objects
46 * @see org.astrogrid.workflow.beans.v1.Step
47 */
48 public static Iterator getJobSteps(Workflow wf) {
49 wf.addFunctions(JesFunctions.FUNCTIONS);
50 return wf.findXPathIterator("//*[jes:isStep()]");
51 }
52 /*** extract the jobURN portion of a job identifier
53 * @see #createJobId(org.astrogrid.workflow.beans.v1.execution.JobURN, String)
54 * @param id job identifier
55 * @return the urn portion of the identifier.
56 */
57 public static org.astrogrid.workflow.beans.v1.execution.JobURN extractURN(JobIdentifierType id) {
58 if (id == null) {
59 return null;
60 }
61 int pos = id.getValue().lastIndexOf('#');
62 org.astrogrid.workflow.beans.v1.execution.JobURN result = new org.astrogrid.workflow.beans.v1.execution.JobURN();
63 result.setContent(id.getValue().substring(0,pos));
64 return result;
65 }
66
67 /*** extract the xpath portion of a job identifier
68 * @see #createJobId(org.astrogrid.workflow.beans.v1.execution.JobURN, String)
69 * @param id job identifier
70 * @return xpath portion of the identifier
71 * @todo maybe rename, if scripting extension takes off - as its not an xpath any more.
72 */
73 public static String extractXPath(JobIdentifierType id) {
74 if (id == null) {
75 return null;
76 }
77 int pos = id.getValue().lastIndexOf('#');
78 return id.getValue().substring(pos + 1);
79 }
80
81 /*** create a job identifier - for passing out into an application contorller - from a jobURN and xpath of the step to execute */
82 public static JobIdentifierType createJobId(org.astrogrid.workflow.beans.v1.execution.JobURN urn, String xpath) {
83 JobIdentifierType id = new JobIdentifierType();
84 id.setValue(urn.getContent() + "#" + xpath);
85 return id;
86 }
87
88 /*** gets most recent record step. if none present, will insert one */
89 public static StepExecutionRecord getLatestOrNewRecord(Step s) {
90 int count = s.getStepExecutionRecordCount();
91 if (count ==0) {
92 StepExecutionRecord rec = new StepExecutionRecord();
93 s.addStepExecutionRecord(rec);
94 return rec;
95 } else {
96 return s.getStepExecutionRecord(count-1);
97 }
98 }
99
100
101
102 /*** extract mesasges from a chain of exceptions
103 * */
104 public static String getMessageChain(Throwable e) {
105 StringBuffer buff = new StringBuffer();
106 Throwable next = e;
107 boolean first = true;
108 while (next != null) {
109 if (!first) {
110 buff.append("\n caused by \n");
111 }
112 first = false;
113
114 buff.append(next.getClass().getName());
115 buff.append(" ");
116 buff.append(next.getMessage());
117 next = next.getCause();
118 }
119 return buff.toString();
120 }
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170