View Javadoc

1   package fr.in2p3.jsaga.adaptor.job;
2   
3   import fr.in2p3.jsaga.adaptor.base.defaults.Default;
4   import fr.in2p3.jsaga.adaptor.base.usage.Usage;
5   import fr.in2p3.jsaga.adaptor.job.control.JobControlAdaptor;
6   import fr.in2p3.jsaga.adaptor.job.control.advanced.CleanableJobAdaptor;
7   import fr.in2p3.jsaga.adaptor.job.control.description.JobDescriptionTranslator;
8   import fr.in2p3.jsaga.adaptor.job.control.description.JobDescriptionTranslatorXSLT;
9   import fr.in2p3.jsaga.adaptor.job.monitor.JobMonitorAdaptor;
10  import org.apache.log4j.Logger;
11  import org.globus.gram.*;
12  import org.globus.gram.internal.GRAMConstants;
13  import org.globus.gram.internal.GRAMProtocolErrorConstants;
14  import org.globus.rsl.*;
15  import org.ietf.jgss.GSSException;
16  import org.ogf.saga.error.*;
17  
18  import java.util.Map;
19  
20  /* ***************************************************
21   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
22   * ***             http://cc.in2p3.fr/             ***
23   * ***************************************************
24   * File:   GkCommonJobControlAdaptor
25   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
26   * Date:   21 juin 2010
27   * ***************************************************
28   * Description:                                      */
29  /**
30   *
31   */
32  public class GkCommonJobControlAdaptor extends GatekeeperJobAdaptorAbstract implements JobControlAdaptor, CleanableJobAdaptor {
33      private Logger logger = Logger.getLogger(GkCommonJobControlAdaptor.class);
34      private boolean twoPhaseUsed = false;
35  
36      public String getType() {
37          return "gk";
38      }
39  
40      public Usage getUsage() {
41          return null;
42      }
43  
44      public Default[] getDefaults(Map attributes) throws IncorrectStateException {
45          return null;
46      }
47  
48      public JobMonitorAdaptor getDefaultJobMonitor() {
49          return new GkCommonJobMonitorAdaptor();
50      }
51  
52      public void connect(String userInfo, String host, int port, String basePath, Map attributes) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, BadParameterException, TimeoutException, NoSuccessException {
53          super.connect(userInfo, host, port, basePath, attributes);
54      }
55  
56      public void disconnect() throws NoSuccessException {
57          super.disconnect();
58      }
59  
60      public JobDescriptionTranslator getJobDescriptionTranslator() throws NoSuccessException {
61          return new JobDescriptionTranslatorXSLT("xsl/job/rsl-1.0.xsl");
62      }
63  
64      public String submit(String jobDesc, boolean checkMatch, String uniqId)  throws PermissionDeniedException, TimeoutException, NoSuccessException, BadResource {
65      	RslNode rslTree;
66          try {
67          	rslTree = RSLParser.parse(jobDesc);
68          } catch (ParseException e) {
69              throw new NoSuccessException(e);
70          }
71          return submit(rslTree, checkMatch, false);
72      }
73  
74      protected String submit(RslNode rslTree, boolean checkMatch, boolean isInteractive) throws PermissionDeniedException, TimeoutException, NoSuccessException, BadResource {
75          if(checkMatch) {
76  			logger.debug("CheckMatch not supported");
77  		}
78          GramJob job = new GramJob(m_credential,rslTree.toRSL(true));
79          try {
80          	try {
81              	Gram.request(m_serverUrl, job, isInteractive);
82              	logger.debug("'two_phase' attribute was ignored.");
83          	}
84          	catch (WaitingForCommitException e) {
85          		// send signal to start job
86          		try {
87  					Thread.sleep(1000);
88  				} catch (InterruptedException e1) {
89  					// TODO Auto-generated catch block
90  					e1.printStackTrace();
91  				}
92          		job.signal(GRAMConstants.SIGNAL_COMMIT_REQUEST);
93          		twoPhaseUsed = true;
94          	}
95          } catch (GramException e) {
96              this.rethrowException(e);
97          } catch (GSSException e) {
98              throw new NoSuccessException(e);
99          }
100         return job.getIDAsString();
101     }
102 
103     public void cancel(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
104         GramJob job = getGramJobById(nativeJobId);
105         try {
106             job.cancel();
107         } catch (GramException e) {
108             this.rethrowException(e);
109         } catch (GSSException e) {
110             throw new NoSuccessException(e);
111         }
112     }
113 
114     /*public boolean suspend(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
115         return this.signal(nativeJobId, GRAMConstants.SIGNAL_SUSPEND);
116     }
117 
118     public boolean resume(String nativeJobId) throws PermissionDeniedException, TimeoutException, NoSuccessException {
119         return this.signal(nativeJobId, GRAMConstants.SIGNAL_RESUME);
120     }
121 
122     public boolean signal(String nativeJobId, int signal) throws PermissionDeniedException, TimeoutException, NoSuccessException {
123         GramJob job = new GramJob(m_credential, null);
124         try {
125             job.setID(nativeJobId);
126         } catch (MalformedURLException e) {
127             throw new NoSuccessException(e);
128         }
129         int errorCode = -1;
130         try {
131             errorCode = job.signal(signal);
132         } catch (GramException e) {
133             this.rethrowException(e);
134         } catch (GSSException e) {
135             throw new NoSuccessException(e);
136         }
137         return errorCode==0;
138     }*/
139 
140     private void rethrowException(GramException e) throws PermissionDeniedException, TimeoutException, NoSuccessException, BadResource{
141     	switch(e.getErrorCode()) {
142     		case GRAMProtocolErrorConstants.BAD_DIRECTORY:
143     			throw new BadResource(e);
144             case GRAMProtocolErrorConstants.ERROR_AUTHORIZATION:
145                 throw new PermissionDeniedException(e);
146             case GRAMProtocolErrorConstants.INVALID_JOB_CONTACT:
147             case GRAMProtocolErrorConstants.ERROR_CONNECTION_FAILED:
148                 throw new TimeoutException(e);
149             default:
150                 throw new NoSuccessException(e);
151         }
152     }
153 
154 	public void clean(String nativeJobId) throws PermissionDeniedException, TimeoutException,
155             NoSuccessException {
156 		try {
157 			if(twoPhaseUsed ) {
158 				GramJob job = getGramJobById(nativeJobId);
159 				// Send signal to clean jobmanager
160 				job.signal(GRAMConstants.SIGNAL_COMMIT_END);
161 			}
162 		} catch (GramException e) {
163 			throw new NoSuccessException("Unable to send commit end signal", e);
164 		} catch (GSSException e) {
165 			throw new NoSuccessException("Unable to send commit end signal", e);
166 		}
167 	}
168 }