View Javadoc

1   package org.astrogrid.jes.delegate.impl;
2   
3   import org.astrogrid.common.bean.Axis2Castor;
4   import org.astrogrid.community.beans.v1.Account;
5   import org.astrogrid.community.beans.v1.axis.Identifier;
6   import org.astrogrid.community.beans.v1.axis._Account;
7   import org.astrogrid.jes.beans.v1.axis.executionrecord.WorkflowString;
8   import org.astrogrid.jes.beans.v1.axis.executionrecord.WorkflowSummaryType;
9   import org.astrogrid.jes.beans.v1.axis.executionrecord._workflowSummaryList;
10  import org.astrogrid.jes.delegate.JesDelegateException;
11  import org.astrogrid.jes.delegate.v1.jobcontroller.JobController;
12  import org.astrogrid.jes.delegate.v1.jobcontroller.JobControllerServiceLocator;
13  import org.astrogrid.jes.delegate.v1.jobcontroller.JobControllerServiceSoapBindingStub;
14  import org.astrogrid.workflow.beans.v1.Workflow;
15  import org.astrogrid.workflow.beans.v1.execution.JobURN;
16  
17  import org.exolab.castor.xml.CastorException;
18  
19  import java.io.IOException;
20  import java.io.StringReader;
21  import java.io.StringWriter;
22  import java.net.URL;
23  
24  
25  /***
26   * SOAP-based implementation of a job controller delegate.
27   */
28  public class JobControllerDelegateImpl extends JobControllerDelegate {
29  
30      public JobControllerDelegateImpl( String targetEndPoint ) {
31        this.targetEndPoint = targetEndPoint;      
32      }
33      
34      public JobControllerDelegateImpl( String targetEndPoint, int timeout ) {
35        this.targetEndPoint = targetEndPoint;
36        this.timeout = timeout;
37      }
38      
39      /*** used for testing */
40      public JobControllerDelegateImpl(JobController binding) {
41          this.theBinding = binding;
42      }
43      
44      public JobURN submitWorkflow(Workflow j) throws JesDelegateException {
45   
46              
47          try {
48              StringWriter sw = new StringWriter();
49              j.marshal(sw);
50              sw.close();
51              String req = sw.toString();
52              JobController jc= getBinding(); 
53              org.astrogrid.jes.beans.v1.axis.executionrecord.JobURN axisURN = jc.submitWorkflow(new WorkflowString(req));
54              JobURN result = new JobURN();
55              result.setContent(axisURN.toString()); 
56              return result;
57          }
58          catch( IOException rex) {
59              throw new JesDelegateException( rex ) ;            
60          }
61          catch (CastorException e) {
62              throw new JesDelegateException(e);
63          }
64  
65      }
66  
67      private synchronized JobController getBinding() throws JesDelegateException {
68          if (theBinding == null) {
69          try {
70          JobControllerServiceSoapBindingStub binding = (JobControllerServiceSoapBindingStub)
71              new JobControllerServiceLocator().getJobControllerService( new URL( this.getTargetEndPoint() ) );                        
72          binding.setTimeout( this.getTimeout() ) ;   
73          this.theBinding = binding;
74          } catch (Exception e) {
75              throw new JesDelegateException("Could not create service binding",e);
76          }
77          }
78          return theBinding;
79      }
80      
81      private JobController theBinding;
82  
83      /***
84       * @see org.astrogrid.jes.delegate.JobController#cancelJob(org.astrogrid.workflow.beans.v1.execution.JobURN)
85       */
86      public void cancelJob(JobURN urn) throws JesDelegateException {
87        // Tests often try to cancel non-existant jobs. This turns the
88        // exception from an NPE to a checked exception.
89        if (urn == null) {
90          throw new JesDelegateException("Can't cancel a job with a null URN!");
91        }
92        try {
93          JobController jc = getBinding();
94          jc.cancelJob(convertJobUrn(urn));
95        } catch (IOException e) {
96          throw new JesDelegateException(e);
97        }
98      }
99  
100     /***
101      * @see org.astrogrid.jes.delegate.JobController#deleteJob(org.astrogrid.workflow.beans.v1.execution.JobURN)
102      */
103     public void deleteJob(JobURN urn) throws JesDelegateException {
104       // Tests often try to delete non-existant jobs. This turns the
105       // exception from an NPE to a checked exception.
106       if (urn == null) {
107         throw new JesDelegateException("Can't delete a job with a null URN!");
108       }
109         try {
110             JobController jc = getBinding();
111             jc.deleteJob(convertJobUrn(urn));
112         } catch (IOException e) {
113             throw new JesDelegateException(e);
114         }        
115     }
116  
117     /***
118      * Derives a JES JobURN from a workflow JobURN.
119      */
120     private org.astrogrid.jes.beans.v1.axis.executionrecord.JobURN convertJobUrn
121         (org.astrogrid.workflow.beans.v1.execution.JobURN urn) throws IOException {
122       String content = urn.getContent();
123       return new org.astrogrid.jes.beans.v1.axis.executionrecord.JobURN(content);
124     }
125 
126     public org.astrogrid.workflow.beans.v1.execution.WorkflowSummaryType[] listJobs(Account acc) throws JesDelegateException {
127         try {
128             JobController jc = getBinding();
129             _Account axisAcc = new _Account();
130             Identifier name = new Identifier();            
131             Identifier community = new Identifier();
132             
133             name.setValue(acc.getName());
134             community.setValue(acc.getCommunity());
135             
136             axisAcc.setCommunity(community);
137             axisAcc.setName(name);
138             
139             _workflowSummaryList l = jc.readJobList(axisAcc);
140             WorkflowSummaryType[] wl = l.getItem();
141             if (wl == null) { // returns null if none found - change this to an empy array;
142                 wl = new WorkflowSummaryType[]{};
143             }
144             // check for nulls.. unlikely, but may happen.
145             int length = 0;
146             for (int i = 0; i < wl.length; i++) {
147                 if (wl[i] != null) {
148                     length++;
149                 }
150             }
151             org.astrogrid.workflow.beans.v1.execution.WorkflowSummaryType[] result = new org.astrogrid.workflow.beans.v1.execution.WorkflowSummaryType[length];
152             for (int i = 0; i < wl.length; i++) {
153                 if (wl[i] != null) {
154                  //   System.err.println(wl[i].getStartTime());
155                  //   System.err.println(wl[i].getStatus());
156                     result[i]= Axis2Castor.convert(wl[i]); 
157                 }
158             }
159             return result;
160             
161         } catch (IOException e) {
162             e.printStackTrace();
163             throw new JesDelegateException(e);
164         }
165     }
166 
167     /***
168      * @see org.astrogrid.jes.delegate.JobController#readJob(org.astrogrid.workflow.beans.v1.execution.JobURN)
169      */
170     public Workflow readJob(JobURN urn) throws JesDelegateException {
171         try {
172             JobController jc = getBinding();
173             String result = jc.readJob(new org.astrogrid.jes.beans.v1.axis.executionrecord.JobURN(urn.getContent())).getValue();
174             if (result == null || result.trim().length() == 0) {
175                 throw new JesDelegateException("Null workflow returned");
176             }
177             StringReader sw = new StringReader(result);
178             return Workflow.unmarshalWorkflow(sw);
179         } catch (IOException e) {
180             throw new JesDelegateException(e);
181         }catch (CastorException e) {
182             throw new JesDelegateException(e);
183         }
184     }
185 
186         
187     
188    
189 
190 } // end of class JobControllerDelegateImpl