Code:
- package com.free400.vengoal;
-
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Enumeration;
-
- import com.ibm.as400.access.AS400;
- import com.ibm.as400.access.AS400Exception;
- import com.ibm.as400.access.AS400File;
- import com.ibm.as400.access.AS400FileRecordDescription;
- import com.ibm.as400.access.AS400Message;
- import com.ibm.as400.access.CommandCall;
- import com.ibm.as400.access.Job;
- import com.ibm.as400.access.QSYSObjectPathName;
- import com.ibm.as400.access.Record;
- import com.ibm.as400.access.RecordFormat;
- import com.ibm.as400.access.SequentialFile;
- import com.ibm.as400.access.SpooledFile;
- import com.ibm.as400.access.SpooledFileList;
-
- public class AS400CommandOutput {
-
- private static final String FILE_SEPARATOR_PROP = "file.separator";
- public static java.text.SimpleDateFormat datetimeFmt = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- private AS400 as400;
- private CommandCall commandCall;
- private SequentialFile seqfile;
- private String lastSplfName=null, lastSplfJob=null, lastSplfUsr=null, lastSplfJobNbr=null;
- private int lastSplfNbr=0;
- private String file;
- private String commandJobNbr;
- private String ddmJobNbr;
- private File toFile = null;
- private PrintWriter writer_;
- private FileOutputStream os;
- private boolean fileOpened = false;
-
- public AS400CommandOutput(AS400 as400){
- this.as400 = as400;
- this.commandCall = new CommandCall(this.as400);
- }
-
- public AS400CommandOutput(AS400 as400, File toFile){
- this.as400 = as400;
- this.toFile = toFile;
- this.commandCall = new CommandCall(this.as400);
- }
-
- public AS400 getSystem(){
- return as400;
- }
-
- public String getCommandJobNbr(){
- String cmdJobNbr = null;
- try {
- as400.connectService(AS400.COMMAND);
- Job[] as400Jobs = as400.getJobs(AS400.COMMAND);
- for(int i=0; i < as400Jobs.length; i++){
- cmdJobNbr = as400Jobs[i].getNumber();
- System.out.println("Linking to AS400 job: " + as400Jobs[i].getNumber() + "/" + as400Jobs[i].getUser() + "/" + as400Jobs[i].getName());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return cmdJobNbr;
- }
-
- public String getDDMJobNbr(){
- String ddmJobNbr = null;
- try {
- as400.connectService(AS400.RECORDACCESS);
- Job[] as400Jobs = as400.getJobs(AS400.RECORDACCESS);
- for(int i=0; i < as400Jobs.length; i++){
- ddmJobNbr = as400Jobs[i].getNumber();
- System.out.println("Linking to AS400 job: " + as400Jobs[i].getNumber() + "/" + as400Jobs[i].getUser() + "/" + as400Jobs[i].getName());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ddmJobNbr;
- }
-
- public void getLastSplfInfo(String selectSplfName, boolean deleteSplf) throws Exception{
- String splfName, splfJob, splfUsr, splfJobNbr, splfDate,splfTime;
- int splfNbr;
- String lastSplfDateTime=" ";
- String splfDateTime;
-
- SpooledFileList splfList = new SpooledFileList( as400 );
- // set filters, all users, on all queues
- splfList.setUserFilter(as400.getUserId());
- splfList.setQueueFilter("/QSYS.LIB/%ALL%.LIB/%ALL%.OUTQ");
- // open list, openSynchronously() returns when the list is completed.
- splfList.openSynchronously();
- Enumeration enumer = splfList.getObjects();
- while( enumer.hasMoreElements() )
- {
- SpooledFile splf = (SpooledFile)enumer.nextElement();
- if ( splf != null )
- {
- // output this spooled file's name
- splfName = splf.getStringAttribute(SpooledFile.ATTR_SPOOLFILE);
- splfJob = splf.getStringAttribute(SpooledFile.ATTR_JOBNAME);
- splfUsr = splf.getStringAttribute(SpooledFile.ATTR_JOBUSER);
- splfJobNbr = splf.getStringAttribute(SpooledFile.ATTR_JOBNUMBER);
- splfDate = splf.getStringAttribute(SpooledFile.ATTR_DATE);
- splfTime = splf.getStringAttribute(SpooledFile.ATTR_TIME);
- splfNbr = splf.getIntegerAttribute(SpooledFile.ATTR_SPLFNUM);
- splfDateTime = splfDate + splfTime;
- //System.out.println("splfDate:" + splfDate + " splfTime:" + splfTime + " job:" + splfJobNbr + "/"+ splfUsr + "/" + splfJob + " spooled file = " + splfName + " splfNbr=" + splfNbr);
- if(splfName.equalsIgnoreCase(selectSplfName) && splfJob.equalsIgnoreCase("QPRTJOB")){
- if (deleteSplf){
- splf.delete();
- } else if (splfDateTime.compareTo(lastSplfDateTime) > 0){
- lastSplfDateTime = splfDateTime;
- lastSplfName = splfName;
- lastSplfJob = splfJob;
- lastSplfUsr = splfUsr;
- lastSplfJobNbr=splfJobNbr;
- lastSplfNbr = splfNbr;
- }
- }
- }
- //System.out.println("last SPLFInfo: job " + lastSplfJobNbr + "/"+ lastSplfUsr + "/" + lastSplfJob + " lastSplfName:" + lastSplfName + " lastSplfNbr:" + lastSplfNbr);
- }
- // clean up after we are done with the list
- splfList.close();
- }
-
- public void cpysplfWithQtemp(String splfName, boolean deleteTempFile) throws Exception{
- if(seqfile == null){
- seqfile = new SequentialFile();
- seqfile.setSystem(as400);
- seqfile.setPath("/QSYS.LIB/QGPL.LIB/QDDSSRC.FILE"); // for run following command use;
- }
-
- if(ddmJobNbr == null)
- ddmJobNbr = getDDMJobNbr();
-
- file = splfName.substring(0, 4) + ddmJobNbr;
-
- getLastSplfInfo(splfName, false);
- CPYSPLF(seqfile, lastSplfName, lastSplfJob, lastSplfUsr, lastSplfJobNbr, lastSplfNbr, true, "QTEMP", file, "M000000000", 201);
- seqfile.setPath(new QSYSObjectPathName("QTEMP", file, "M000000000", "MBR").getPath());
- setRecordFormat();
- if(toFile == null)
- readAll();
- else
- readAll(toFile);
- getLastSplfInfo(splfName, true);
- }
-
- public void readAll(){
- readAll(null);
- }
-
- public void readAll(File toFile){
- try {
- seqfile.open(AS400File.READ_ONLY, 100, AS400File.COMMIT_LOCK_LEVEL_NONE);
- Record dataRcd = seqfile.readNext();
- while (dataRcd != null) {
- if(toFile == null)
- onRecord(dataRcd);
- else
- onRecord(toFile, dataRcd);
- dataRcd = seqfile.readNext();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- seqfile.close();
- if(fileOpened){
- writer_.close();
- os.close();
- fileOpened = false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- public void onRecord(Record record) {
- System.out.println(getClass() + ":" + record);
- }
-
- public void onRecord(File file, Record record) throws IOException{
- if(!fileOpened){
- os = new FileOutputStream(file, file.exists());
- writer_ = new PrintWriter(os, true);
- fileOpened = true;
- }
- writer_.println(record);
- writer_.flush();
- }
-
- public void setRecordFormat() throws Exception{
- AS400FileRecordDescription recordDescription = new AS400FileRecordDescription(as400, seqfile.getPath());
- RecordFormat[] formats = recordDescription.retrieveRecordFormat();
- RecordFormat recordFormat = formats[0];
- seqfile.setRecordFormat(recordFormat);
- }
-
- public void CPYSPLF(SequentialFile seqFile, String splfName, String splfJob, String splfUsr, String splfJobNbr, int splfNbr , boolean includeIGCData, String toLibrary, String toFile, String toMbr, int toFileRecordLength){
-
- String cmdCRTPF = "CRTPF FILE("
- + toLibrary.toUpperCase().trim()
- + "/"
- + toFile.toUpperCase().trim()
- + ") RCDLEN("
- + toFileRecordLength
- + ") MBR("
- + toMbr.toUpperCase().trim()
- + ") MAXMBRS(*NOMAX) SIZE(*NOMAX)";
-
- String cmdCPYSPLF = "CPYSPLF FILE("
- + splfName
- + ") TOFILE("
- + toLibrary.toUpperCase().trim()
- + "/"
- + toFile.toUpperCase().trim()
- + ") JOB("
- + splfJobNbr
- + "/"
- + splfUsr
- + "/"
- + splfJob
- + ") SPLNBR("
- + splfNbr
- + ") MBROPT(*REPLACE)";
- try {
- if(!chkObjExist(seqFile, toLibrary, toFile, "*FILE"))
- seqFile.runCommand(cmdCRTPF);
-
- AS400Message[] messagelist = seqFile.runCommand(cmdCPYSPLF);
- for (int i = 0; i < messagelist.length; i++) {
- if (messagelist[i].getID() != null){
- if(messagelist[i].getID().equalsIgnoreCase("CPF3485")){
- System.out.println(cmdCPYSPLF +" Command successful");
- } else
- System.out.println(messagelist[i].getID() + " " + messagelist[i].getText());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public boolean chkObjExist(SequentialFile seqFile, String objLib, String objName, String objType) throws Exception {
- boolean objectExist = true;
- String cmdChkObj;
- cmdChkObj = "CHKOBJ OBJ(" + objLib + "/" + objName + ") OBJTYPE(" + objType + ")";
- AS400Message[] messagelist = seqFile.runCommand(cmdChkObj);
- for (int i = 0; i < messagelist.length; i++) {
- if (messagelist[i].getID() != null){
- System.out.println(messagelist[i].getID() + " " + messagelist[i].getText());
- if (messagelist[i].getID().equalsIgnoreCase("CPF9801")){
- objectExist = false;
- }
- }
- }
- return objectExist;
- }
-
- public boolean runCmd(String commandString) {
- boolean success = false;
- try {
- // Run the command.
- if (success = commandCall.run(commandString)){
- //System.out.println(commandString + " Command successful");
- }else{
- System.out.println(commandString + " Command failed");
- throw new AS400Exception(commandCall.getMessageList());
- }
- } catch (Exception e) {
- System.out.println("Command " + commandCall.getCommand() + " did not run");
- e.printStackTrace();
- }
- return success;
- }
-
- public static void main(String[] args) {
- try {
- AS400 as400 = new AS400("as400ip", "user", "userpass");
- AS400CommandOutput get400ACTJOB = new AS400CommandOutput(as400, new File("d:\\temp\\WRKACTJOB.TXT"));
- get400ACTJOB.runCmd("WRKACTJOB OUTPUT(*PRINT) RESET(*YES) SEQ(*CPUPCT)");
- Thread.sleep(5000);
- get400ACTJOB.runCmd("WRKACTJOB OUTPUT(*PRINT) SEQ(*CPUPCT)");
- get400ACTJOB.cpysplfWithQtemp("QPDSPAJB", true);
-
- get400ACTJOB.runCmd("WRKSYSSTS OUTPUT(*PRINT)");
- get400ACTJOB.cpysplfWithQtemp("QPDSPSTS", true);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
|
|