UploaderTask.java

00001 package ProdDatabase;
00002 import java.util.regex.*;
00003 import java.io.*;
00004 public class UploaderTask {
00005     private int lengthOfTask;
00006     private int current = 0;
00007     private boolean done = false;
00008     private boolean cancelled = false;
00009     private String statMessage;
00010     private String command;
00011     private boolean quitOnFinish;
00012     Pattern aPattern = Pattern.compile(".*\\d{14}\\.jassm.*");
00013     private boolean isAssemblyUpload;
00014 
00015     public UploaderTask(int noOfModules, String cmd, boolean quitOnFinish) {
00016         lengthOfTask = noOfModules;
00017         command = cmd;
00018         this.quitOnFinish = quitOnFinish;
00019         isAssemblyUpload = aPattern.matcher(cmd).matches();
00020     }
00021 
00025     public void go() {
00026         final guiUtilities.SwingWorker worker = new guiUtilities.SwingWorker() {
00027             public Object construct() {
00028                 current = 0;
00029                 done = false;
00030                 cancelled = false;
00031                 statMessage = null;
00032                 return new ActualTask();
00033             }
00034         };
00035         worker.start();
00036     }
00037 
00041     public int getLengthOfTask() {
00042         return lengthOfTask;
00043     }
00044 
00047     public int getCurrent() {
00048         return current;
00049     }
00050 
00051     public void stop() {
00052         cancelled = true;
00053         statMessage = null;
00054     }
00055 
00059     public boolean isDone() {
00060         return done;
00061     }
00062 
00067     public String getMessage() {
00068         return statMessage;
00069     }
00070 
00074     class ActualTask {
00075         Pattern d14Pattern = Pattern.compile(".*(\\d{14}).*");
00076         Pattern assemblyPattern = Pattern.compile("^Item assembled.*");
00077         ActualTask() {
00078           Process p = null;
00079            try {
00080 
00081                 String OS = System.getProperty("os.name").toLowerCase();
00082                 if (OS.indexOf("linux") >= 0) {
00083                     String [] cmds = {"/bin/sh","-c",command};
00084                    p = Runtime.getRuntime().exec( cmds );
00085                 } else {
00086                    p = Runtime.getRuntime().exec( command );
00087                 }
00088 
00089 //              p.waitFor();
00090 
00091                 BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
00092 
00093                 String t = in.readLine();
00094                 if(t!=null) System.out.println(t);
00095 
00096                 while( !cancelled && t!=null && current<lengthOfTask )
00097                    {
00098                         t = in.readLine();
00099                         if(t!=null) {
00100                           System.out.println(t);
00101                           Matcher matcher = d14Pattern.matcher(t);
00102                           if(matcher.matches()) statMessage = t.substring(matcher.start(1),matcher.end(1));
00103                           matcher = assemblyPattern.matcher(t);
00104                           if(t.indexOf("DB transaction commited.")!=-1 || matcher.matches()) current++;
00105                           }
00106                     }
00107                 in.close();
00108                 if(cancelled && p!=null) p.destroy();
00109                 done=true;
00110 
00111                 } catch(Exception rcException) {System.out.println("SctGUI::UploaderTask error "+rcException.toString()); done=true;}
00112        int total = isAssemblyUpload ? lengthOfTask-1 : lengthOfTask;
00113        int ncurrent = current;
00114        if(isAssemblyUpload) {
00115          if(ncurrent==lengthOfTask) ncurrent--;
00116          }
00117        String info = isAssemblyUpload ? Integer.toString(ncurrent)+" items out of "+Integer.toString(total) : Integer.toString(ncurrent)+" tests out of "+Integer.toString(total);
00118        if(cancelled) {
00119            String msg = "Uploader Task Cancelled!\nYou had uploaded "+info;
00120            javax.swing.JOptionPane.showMessageDialog(null,msg);
00121            }
00122        else {
00123            String msg = "Uploader Task Completed.\nYou had uploaded "+info;
00124            if(current<lengthOfTask) msg+="\nOne or more uploads FAILED. Please check the log files in your upload directory.";
00125            javax.swing.JOptionPane.showMessageDialog(null,msg);
00126            }
00127        if(quitOnFinish) System.exit(0);
00128        }
00129     }
00130 }

Generated on Mon Feb 6 14:12:22 2006 for SCT DAQ/DCS Software - Java by  doxygen 1.4.6