Source code: com/yaftp/ftp/mvsjobs/JobSubmission.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.ftp.mvsjobs ;
21
22 import com.yaftp.ftp.* ;
23 import java.util.* ;
24 import java.io.* ;
25
26 /**
27
28 This class is the main entry point to MVS job subissions
29 through IBM documented FTP JES semantics
30
31 this class is based on the basic RFC ftp implemented by
32 the com.yaftp.ftp package.
33
34 Let's say that you should use this class whenever you need
35 to implement a JOB SUBMISSION without any graphical interface
36 because your JRE do not implement AWT SWING or just because you want
37 to implement your own customized GUI.
38
39 this class is also used of course by the FTP GUI bean and mainly
40
41
42 */
43
44 public class JobSubmission
45 {
46 private final static boolean _DEBUG_ = true ;
47 private final static String _MVS_ = "MVS" ;
48 private final static String _NO_JOBS_ = "No jobs" ;
49 private final static String _TMP_SUBMIT_MVS_FILE_NAME_ = "MVSJOB" ;
50 /**
51 nothing standard here but let's assume that when something strange
52 happens we observe following behavior :
53 a "200-blah blah blah..." with continuation is issued first
54 if the site command is acceptable
55 */
56 private final static String _SITE_OK_ = "200 " ;
57
58 /** defines the associated MVS Ftp infos */
59 private MvsFtpNode _mvsFtpNode ;
60 private FtpClientSession _ftpSession ;
61
62 public void set_mvsIpNode( MvsFtpNode node )
63 { _mvsFtpNode = node ; }
64
65 public FtpClientSession get_ftpSession()
66 { return _ftpSession ; }
67
68 /**
69 check the site command acceptance by FTP server, this is not part
70 of FTP STANDARD and is subject to providers idiosynchrasies.
71
72 @exception MvsJobsException if site seems rejected
73 */
74 private void isSiteOK( String message )
75 throws ClientFtpError
76 {
77 if ( message.startsWith(_SITE_OK_) )
78 return ;
79 throw new ClientFtpError(message) ;
80 }
81
82 private FtpVectorListener buildPutVector( String fileName )
83 throws ClientFtpError
84 {
85 FtpVectorListener jclStream = new FtpVectorListener() ;
86 Vector stream = new Vector() ;
87 jclStream.set_dataList(stream) ;
88 LineNumberReader reader ;
89 try {
90 reader = new LineNumberReader(
91 new FileReader( fileName )
92 ) ;
93 } catch ( FileNotFoundException e )
94 { throw new ClientFtpError("MVS FTP inexisting local file to send : "+fileName);}
95 String str ;
96 try {
97 while ( ( str = reader.readLine() ) != null )
98 stream.addElement(str) ;
99 } catch ( IOException e )
100 { throw new ClientFtpError("MVS FTP error reading local file to send : "+fileName);}
101 return jclStream ;
102 }
103
104 /**
105 submit a job to MVS just waiting for its completion
106
107 */
108 public FtpVectorListener submitFromHost( String hostFileName )
109 throws ClientFtpError
110 {
111 try {
112 System.out.println("starting JES MODE") ;
113 startJesMode() ; // make the internal JES reader ready
114
115 FtpVectorListener jobOutput = new FtpVectorListener() ;
116 System.out.println("starting JES MODE") ;
117 _ftpSession.getFile( hostFileName , jobOutput ) ;
118 System.out.println("returning from get file : " + _ftpSession.get_lastCommandMessage() ) ;
119 return jobOutput ;
120 } catch ( ClientFtpError e )
121 { throw new ClientFtpError("fail to read JES file : " + e.getMessage()) ; }
122 }
123
124 /**
125 we first transfert the local file to MVS user space using
126 the default _TMP_SUBMIT_MVS_FILE_NAME_ as tmp work file
127
128 then we request submission using the submitFromHost semantics
129
130 @param jclFileName the local JCL file candidate to submission
131 */
132 public FtpVectorListener submitFromLocal( String jclFileName )
133 throws ClientFtpError
134 {
135 String destFile = _TMP_SUBMIT_MVS_FILE_NAME_ ;
136 try {
137 stopJesMode() ;
138 _ftpSession.putFile( destFile , buildPutVector( jclFileName ) );
139 return submitFromHost(destFile) ;
140 }
141 catch ( ClientFtpError e )
142 { throw new ClientFtpError("fail to read JES file : " + e.getMessage()) ; }
143 }
144
145 /**
146 establish Ftp connection with site
147 */
148 public void init( boolean debug )
149 throws ClientFtpError
150 {
151 Ftp ftp = new Ftp() ;
152 ftp.set_graphicalDebug(_DEBUG_) ;
153 ftp.set_debug(_DEBUG_) ;
154 _ftpSession = new FtpClientSession() ;
155 _ftpSession.set_parent( ftp ) ;
156
157 try {
158 _ftpSession.ftpInit(_mvsFtpNode.get_ipAddress()) ;
159 _ftpSession.login( _mvsFtpNode.get_user() , _mvsFtpNode.get_password() ) ;
160 if ( _ftpSession.getOSFtp().indexOf(_MVS_) == -1 )
161 throw new ClientFtpError("Ftp is not an MVS ftp : " + _ftpSession.getOSFtp() ) ;
162
163 } catch ( ClientFtpError e )
164 { throw new ClientFtpError("MVS JOBSUBMISSION init error : " + e.getMessage() ) ;}
165 }
166
167 /** turn FTP session in JES2 mode */
168 public void startJesMode()
169 throws ClientFtpError
170 {
171 try {
172 isSiteOK( _ftpSession.site( Ftp.START_JES ) ) ;
173 } catch ( ClientFtpError e )
174 { throw new ClientFtpError(" FTP start JES error : " + e.getMessage() ) ;}
175 }
176
177 /** turn FTP session in SEQ standard filetransfert mode */
178 public void stopJesMode()
179 throws ClientFtpError
180 {
181 try {
182 isSiteOK( _ftpSession.site( Ftp.STOP_JES) ) ;
183 } catch ( ClientFtpError e )
184 { throw new ClientFtpError(" FTP stop JES error : " + e.getMessage() ) ;}
185 }
186
187 /**
188 basic rough jes 2 OUTPUT queue reading
189
190 @return a list of OutputJob object classes
191 */
192 public Hashtable listQueue()
193 throws ClientFtpError
194 {
195 try {
196 Hashtable returned = new Hashtable() ;
197 FtpVectorListener fileList = new FtpVectorListener() ;
198 _ftpSession.list( fileList ) ;
199 // even if queue is empty it will contain almost one row with infos :
200 // "No jobs found on Held queue"
201 Vector queueList = fileList.get_dataList() ;
202 String firstRow = (String)queueList.elementAt(0) ;
203 if ( firstRow.startsWith( _NO_JOBS_ ) )
204 return returned ; // return an empty hash
205
206 Enumeration list = queueList.elements() ;
207 while ( list.hasMoreElements() )
208 {
209 OutputJob job = new OutputJob( _ftpSession , (String) list.nextElement() ) ;
210 returned.put( job.get_jobID() , job ) ;
211 }
212 return returned ;
213 } catch ( ClientFtpError e )
214 { throw new ClientFtpError(" JES Listqueue error : " + e.getMessage() ) ;}
215 }
216
217
218 /**
219 delete given jobId from JOBQUEUE
220
221 @param jobId the MVS jobid to be deleted
222 */
223 public boolean deleteJob( String jobID )
224 throws ClientFtpError
225 {
226 try {
227 startJesMode() ;
228 boolean returned = _ftpSession.delete(jobID) ;
229 stopJesMode() ;
230 return returned ;
231 } catch ( ClientFtpError e )
232 {
233 throw new ClientFtpError(" JES deleteJob error : " + e.getMessage() ) ;
234 }
235 }
236
237 /**
238 just terminate the FTP JES session
239 */
240 public void terminate()
241 throws ClientFtpError
242 {
243 try {
244 _ftpSession.stop() ;
245 } catch ( ClientFtpError e )
246 { throw new ClientFtpError(" FTP JES terminate error : " + e.getMessage() ) ;}
247 }
248
249 public JobSubmission( FtpClientSession session )
250 {
251 _ftpSession = session ;
252 }
253
254 public JobSubmission()
255 {}
256
257 /**
258 testing basic MVS job Submission through FTP
259 */
260 public static void main( String args[] )
261 {
262 System.out.println("** test JobSubmission **" ) ;
263 String command = null ;
264 String ipAddress = null ;
265 String userId = null ;
266 String passWord = null ;
267 String file = null ;
268
269 if ( args.length > 0 )
270 command = args[0] ;
271 if ( args.length > 1 )
272 ipAddress = args[1] ;
273 if ( args.length > 2 )
274 userId = args[2] ;
275 if ( args.length > 3 )
276 passWord = args[3] ;
277 if ( args.length > 4 )
278 file = args[4] ;
279
280
281 MvsFtpNode node = new MvsFtpNode( ipAddress , userId , passWord ) ;
282 JobSubmission mvsJob = new JobSubmission() ;
283
284 mvsJob.set_mvsIpNode( node );
285
286 try {
287 mvsJob.init(_DEBUG_) ;
288
289 if ( command.equalsIgnoreCase( "LISTQ" ) )
290 {
291 mvsJob.startJesMode() ;
292 Hashtable outputQueue = mvsJob.listQueue() ;
293 if ( outputQueue.isEmpty() )
294 System.out.println("queueList is empty") ;
295 else
296 {
297 Enumeration list = outputQueue.elements() ;
298 while ( list.hasMoreElements() )
299 {
300 OutputJob curJob = (OutputJob) list.nextElement() ;
301 System.out.println( curJob ) ;
302 }
303 }
304 }
305 if ( command.equalsIgnoreCase( "DELETEQ" ) )
306 {
307
308 if ( mvsJob.deleteJob(file) )
309 System.out.println("JOB : "+ file + " has been deleted " ) ;
310 else
311 System.out.println("JOB : "+ file + " not found " ) ;
312 }
313 else if ( command.equalsIgnoreCase( "READJOB" ) )
314 {
315 System.out.println("reading...") ;
316 mvsJob.startJesMode();
317 OutputJob curJob = new OutputJob( mvsJob.get_ftpSession() ,
318 "XXXX " + file + " OUTPUT 1 Spool Files" ) ;
319 Vector job = curJob.readJesFile(OutputJob.ALL_FILE_TRANFERT) ;
320 Enumeration jobListing = job.elements();
321 try {
322 PrintWriter out = new PrintWriter(
323 new FileWriter( file )
324 ) ;
325 while ( jobListing.hasMoreElements() )
326 {
327 out.println( (String) jobListing.nextElement() );
328 }
329 out.close() ;
330 } catch ( IOException e )
331 { throw new ClientFtpError("MVS FTP IOERROR writing tmp file : " + e.getMessage() ) ; }
332 mvsJob.stopJesMode();
333 }
334 else if ( command.equalsIgnoreCase( "SUBMIT" ) )
335 {
336 FtpVectorListener out = mvsJob.submitFromLocal( file ) ;
337 try {
338 PrintWriter output = new PrintWriter (
339 new FileWriter("OUTPUT.JOB")
340 ) ;
341 Enumeration result = out.get_dataList().elements() ;
342 while ( result.hasMoreElements() )
343 output.println((String) result.nextElement() ) ;
344 output.close() ;
345
346 System.out.println("back from submit") ;
347 } catch ( IOException e )
348 { System.out.println("IOERR writing output : "+ e.getMessage() ); }
349 }
350 mvsJob.terminate() ;
351 }
352 catch ( ClientFtpError e )
353 {
354 System.out.println("MVS submission error : " + e.getMessage() ) ;
355 }
356 }
357
358 }