Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

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 
00013     public UploaderTask(int noOfModules, String cmd, boolean quitOnFinish) {
00014         lengthOfTask = noOfModules;
00015         command = cmd;
00016         this.quitOnFinish = quitOnFinish;
00017     }
00018 
00022     public void go() {
00023         final guiUtilities.SwingWorker worker = new guiUtilities.SwingWorker() {
00024             public Object construct() {
00025                 current = 0;
00026                 done = false;
00027                 cancelled = false;
00028                 statMessage = null;
00029                 return new ActualTask();
00030             }
00031         };
00032         worker.start();
00033     }
00034 
00038     public int getLengthOfTask() {
00039         return lengthOfTask;
00040     }
00041 
00044     public int getCurrent() {
00045         return current;
00046     }
00047 
00048     public void stop() {
00049         cancelled = true;
00050         statMessage = null;
00051     }
00052 
00056     public boolean isDone() {
00057         return done;
00058     }
00059 
00064     public String getMessage() {
00065         return statMessage;
00066     }
00067 
00071     class ActualTask {
00072         Pattern d14Pattern = Pattern.compile(".*(\\d{14}).*");
00073         ActualTask() {
00074           Process p = null;
00075            try {
00076 
00077                 String OS = System.getProperty("os.name").toLowerCase();
00078                 if (OS.indexOf("linux") >= 0) {
00079                     String [] cmds = {"/bin/sh","-c",command};
00080                    p = Runtime.getRuntime().exec( cmds );
00081                 } else {
00082                    p = Runtime.getRuntime().exec( command );
00083                 }
00084 
00085 //              p.waitFor();
00086 
00087                 BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
00088 
00089                 String t = in.readLine();
00090                 if(t!=null) System.out.println(t);
00091 
00092                 while( !cancelled && t!=null && current<lengthOfTask )
00093                    {
00094                         t = in.readLine();
00095                         if(t!=null) {
00096                           System.out.println(t);
00097                           Matcher matcher = d14Pattern.matcher(t);
00098                           if(matcher.matches()) statMessage = t.substring(matcher.start(1),matcher.end(1));
00099                           if(t.indexOf("DB transaction commited.")!=-1) current++;
00100                           }
00101                     }
00102                 in.close();
00103                 if(cancelled && p!=null) p.destroy();
00104                 done=true;
00105 
00106                 } catch(Exception rcException) {System.out.println("SctGUI::UploaderTask error "+rcException.toString()); done=true;}
00107        if(cancelled) {
00108            String msg = "Upload Cancelled!\n You had uploaded "+current+" tests out of "+lengthOfTask;
00109            javax.swing.JOptionPane.showMessageDialog(null,msg);
00110            }
00111        else {
00112            String msg = "Upload Completed.\n You successfully uploaded "+current+" tests out of "+lengthOfTask;
00113            if(current<lengthOfTask) msg+="\nOne or more uploads appear to have failed. Please check the log files in your upload directory.";
00114            javax.swing.JOptionPane.showMessageDialog(null,msg);
00115            }
00116        if(quitOnFinish) System.exit(0);
00117        }
00118     }
00119 }

Generated on Thu Jul 15 09:55:51 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5