Commits

Dmitry Danileyko committed ebb2f31

1) Added possibility to work using command line.

Comments (0)

Files changed (8)

src/org/narsereg/nbnetcatupdater/NBNetCatUpdater.java

 /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
+ * NBNetCatUpdater - tool for NetCAT participants.
+ * Application can be used in command line and visual mode.
  */
 package org.narsereg.nbnetcatupdater;
 
-/**
- *
- * @author narsereg
- */
 public class NBNetCatUpdater{
 
     public NBNetCatUpdater() {
       
     }
-    
     public static void main(String[] args){
-        NBUpdaterForm nbuf = new NBUpdaterForm();
-        nbuf.setVisible(true);
+        boolean noGUI = false;
+        String bundle = null, platform = null;
+            
+        for (int i = 0; i < args.length; i++) {
+            
+            if (args[i].equalsIgnoreCase("-n")) {
+                noGUI = true;
+            } else if (args[i].equalsIgnoreCase("-b")) {
+                bundle = args[i + 1];
+                for (int bund = 0; bund < NBUpdaterUtils.options.length - 1; bund++){
+                    if ((!bundle.equalsIgnoreCase(NBUpdaterUtils.options[bund])) && (bund == NBUpdaterUtils.options.length - 1)) {
+                        System.out.println("Wrong bundle.");
+                        System.exit(0);
+                    }
+                }
+            } else if (args[i].equalsIgnoreCase("-p")) {
+                platform = args[i + 1];
+                for (int platf = 0; platf < NBUpdaterUtils.platforms.length - 1; platf++){
+                    if ((!platform.equalsIgnoreCase(NBUpdaterUtils.platforms[platf])) && (platf == NBUpdaterUtils.platforms.length - 1)) {
+                        System.out.println("Wrong platform.");
+                        System.exit(0);
+                    }
+                }
+            } else if (args[i].equalsIgnoreCase("-h")) {
+                System.out.println("NBNetCatUpdater.");
+                System.out.println("Usage:");
+                System.out.println("-n Run in No GUI mode. In this mode you should use -p and -b parameters.");
+                System.out.println("-b Choose bundle (list of bundles: JavaSE, JavaEE, CPP, PHP, All).");
+                System.out.println("-p Choose plaform (list of platforms: Windows, Linux, Solaris-x86, Solaris-Sparc, MacOSX, ZIP).");
+                System.out.println("-h Show this help and exit.");
+                System.exit(0);
+            }
+        }
+        
+        if (noGUI){
+            NBUpdaterUtils nbUtils = new NBUpdaterUtils();
+            if (bundle != null){
+                for (int bund = 0; bund < NBUpdaterUtils.options.length - 1; bund++){
+                    if (bundle.equalsIgnoreCase(NBUpdaterUtils.options[bund])) {
+                        nbUtils.setOption(NBUpdaterUtils.options[bund]);
+                    }
+                }
+            } else {
+                nbUtils.setOption(NBUpdaterUtils.options[0]);
+            }
+
+            if (platform != null) {
+                for (int platf = 0; platf < NBUpdaterUtils.platforms.length - 1; platf++){
+                    if (platform.equalsIgnoreCase(NBUpdaterUtils.platforms[platf])) {
+                        nbUtils.setPlatform(NBUpdaterUtils.platforms[platf]);
+                    }
+                }
+            } else {
+                nbUtils.setPlatform(NBUpdaterUtils.platforms[0]);
+            }
+            
+            
+            NBUpdaterProcessor proc = new NBUpdaterProcessor(nbUtils.getDownloadFileURL(), null);
+            proc.noGUI = noGUI;
+            proc.prepareData();
+        } else {
+            NBUpdaterForm nbuf = new NBUpdaterForm(bundle, platform);
+            nbuf.setVisible(true);
+        }
+            
     }
 }

src/org/narsereg/nbnetcatupdater/NBUpdaterConsole.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.narsereg.nbnetcatupdater;
+
+/**
+ *
+ * @author DDanileyko
+ */
+public class NBUpdaterConsole {
+
+    public NBUpdaterConsole() {
+        System.out.println();
+    }
+
+    public void publishPercent(int percent){
+        if ((percent >= 0) && (percent < 11)) 
+            System.out.print("\r|=         | " + percent + "%");
+        if ((percent > 10) && (percent < 21)) 
+            System.out.print("\r|==        | " + percent + "%");
+        if ((percent > 20) && (percent < 31)) 
+            System.out.print("\r|===       | " + percent + "%");
+        if ((percent > 30) && (percent < 41)) 
+            System.out.print("\r|====      | " + percent + "%");
+        if ((percent > 40) && (percent < 51)) 
+            System.out.print("\r|=====     | " + percent + "%");
+        if ((percent > 50) && (percent < 61)) 
+            System.out.print("\r|======    | " + percent + "%");
+        if ((percent > 60) && (percent < 71)) 
+            System.out.print("\r|=======   | " + percent + "%");
+        if ((percent > 70) && (percent < 81)) 
+            System.out.print("\r|========  | " + percent + "%");
+        if ((percent > 80) && (percent < 91)) 
+            System.out.print("\r|========= | " + percent + "%");
+        if ((percent > 90) && (percent < 100)) 
+            System.out.print("\r|==========| " + percent + "%");
+        if (percent >= 100) 
+            System.out.print("\r|      done| " + percent + "%");    
+    }
+    
+
+}

src/org/narsereg/nbnetcatupdater/NBUpdaterDownloadFile.java

-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-package org.narsereg.nbnetcatupdater;
-
-import java.io.*;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLConnection;
-import java.nio.channels.Channels;
-import java.nio.channels.ReadableByteChannel;
-import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import javax.swing.SwingWorker;
-
-/**
- *
- * @author narsereg
- */
-public class NBUpdaterDownloadFile extends SwingWorker<Boolean, Integer> implements Runnable{
-    NBUpdaterDownloadFile(String fileURL, NBUpdaterForm nbuf){
-        this.nbuf = nbuf;
-        
-        String pathToFile = (!nbuf.getDirectoryToDownload().equals("")) ? (nbuf.getDirectoryToDownload() + File.separator) : ""; 
-        downloadedFile = new File(pathToFile + fileURL.substring(fileURL.lastIndexOf("/") + 1));    
-        try {
-            fileLink = new URL(fileURL);
-        } catch (MalformedURLException ex) {
-            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
-        }
-        
-        fileSize = getFileSize();
-    }
-    
-
-    int fileSize;
-    private NBUpdaterForm nbuf = null;
-    private ReadableByteChannel rbc = null;
-    private File downloadedFile = null;
-    private URL fileLink;
-    FileOutputStream fos = null;
-    boolean wasCancelled = false;
-    
-    
-    @Override
-    public Boolean doInBackground(){
-            PublishThread pt = null;
-            try {
-                fos = new FileOutputStream(downloadedFile);
-            } catch (FileNotFoundException ex) {
-                Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
-            }
-            try {
-                pt = new PublishThread(fos, this);
-                pt.start();
-                try {
-                    rbc = Channels.newChannel(fileLink.openStream());
-                    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
-                } catch (IOException ex) {
-                    Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
-                }
-        } finally {
-            try {
-                publish(fileSize);
-                if(isCancelled()){
-                    downloadedFile.delete();
-                    publish(0);
-                }
-                pt.isDownloaded = true;
-                fos.flush();
-                fos.close();
-            } catch (IOException ex) {
-                Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
-            }
-        }
-        nbuf.unzipDownloadedFile(downloadedFile.getAbsolutePath());
-        return true;
-    }
-    
-    protected void closeConnection() throws InterruptedException{
-        Thread.currentThread().interrupt();
-    }
-    
-    @Override
-    protected void process(List<Integer> downloadedBytes){
-        for(Integer i : downloadedBytes){
-            nbuf.updateDownloadProgressbar(i);
-        }
-    }
-    
-    private int getFileSize(){
-        URLConnection conn = null;
-        try {
-            conn = fileLink.openConnection();
-        } catch (IOException ex) {
-            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
-        }
-
-        return conn.getContentLength();
-    }
-    
-    class PublishThread extends Thread{
-
-        FileOutputStream fos = null;
-        NBUpdaterDownloadFile nbudf = null;
-        boolean isDownloaded = false;
-
-        public PublishThread(FileOutputStream fos, NBUpdaterDownloadFile nbudf) {
-            this.fos = fos;
-            this.nbudf = nbudf;
-        }
-
-        @Override
-        public void run() {
-            while(!isDownloaded){
-                try {
-                    nbudf.publish((int)fos.getChannel().size());
-                } catch (IOException ex) {
-                    Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
-                }
-                try {
-                    Thread.sleep(1000);
-                } catch (InterruptedException ex) {
-                    Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
-                }
-
-            }
-        }
-    }
-    
-}
-
-

src/org/narsereg/nbnetcatupdater/NBUpdaterDownloader.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.narsereg.nbnetcatupdater;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author DDanileyko
+ */
+public class NBUpdaterDownloader {
+    
+    private File downloadedFile = null;
+    private FileOutputStream fos = null;
+    private URL fileLink;
+    private int fileSize;
+    private ReadableByteChannel rbc = null;
+
+    public NBUpdaterDownloader(String fileURL, String pathToFile) {
+        pathToFile = (!pathToFile.equals("")) ? (pathToFile + File.separator) : ""; 
+        downloadedFile = new File(pathToFile + fileURL.substring(fileURL.lastIndexOf("/") + 1));    
+        try {
+            fileLink = new URL(fileURL);
+        } catch (MalformedURLException ex) {
+            Logger.getLogger(NBUpdaterProcessor.class.getName()).log(Level.SEVERE, null, ex);
+        }
+        
+        setFileSize();
+    }
+    
+    public void download(){
+        try {
+            fos = new FileOutputStream(downloadedFile);
+        } catch (FileNotFoundException ex) {
+            Logger.getLogger(NBUpdaterDownloader.class.getName()).log(Level.SEVERE, null, ex);
+        }
+        
+        try {
+            rbc = Channels.newChannel(fileLink.openStream());
+            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
+        } catch (IOException ex) {
+            Logger.getLogger(NBUpdaterProcessor.class.getName()).log(Level.SEVERE, null, ex);
+        } finally {
+            flush();
+        }
+        
+    }
+    
+    public void deleteFile(){
+        downloadedFile.delete();
+        flush();
+    }
+    
+    public String getAbsolutePath(){
+        return downloadedFile.getAbsolutePath();
+    }
+    
+    public void flush(){
+        try {
+            fos.flush();
+            fos.close();
+        } catch (IOException ex) {
+            Logger.getLogger(NBUpdaterDownloader.class.getName()).log(Level.SEVERE, null, ex);
+        }
+    }
+    
+    public long getSizeOfDownloadedFile(){
+        long downloadedSize = 0;
+        try {
+            downloadedSize = fos.getChannel().size();
+        } catch (IOException ex) {
+            Logger.getLogger(NBUpdaterDownloader.class.getName()).log(Level.SEVERE, null, ex);
+        }
+        return downloadedSize;
+    }
+    
+    private void setFileSize(){
+        URLConnection conn;
+        try {
+            conn = fileLink.openConnection();
+            this.fileSize = conn.getContentLength();
+        } catch (IOException ex) {
+            Logger.getLogger(NBUpdaterProcessor.class.getName()).log(Level.SEVERE, null, ex);
+        }
+    }
+    
+    public int getFileSize(){
+        return this.fileSize;
+    }
+}

src/org/narsereg/nbnetcatupdater/NBUpdaterForm.form

   </Properties>
   <SyntheticProperties>
     <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
+    <SyntheticProperty name="generateCenter" type="boolean" value="false"/>
   </SyntheticProperties>
   <AuxValues>
     <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>

src/org/narsereg/nbnetcatupdater/NBUpdaterForm.java

  * @author narsereg
  */
 public class NBUpdaterForm extends javax.swing.JFrame {
-    String[] options = { "javase", "javaee", "cpp", "php", "all" };
-    String[] platforms = { "windows", "linux", "solaris-x86", "solaris-sparc", "macosx", "zip" };
     String pathToDownloadedFile;
-    NBUpdaterDownloadFile df = null;
+    NBUpdaterProcessor df = null;
 
     /**
      * Creates new form NewJFrame
      */
-    public NBUpdaterForm() {
+    public NBUpdaterForm(String bundle, String platform) {
         initComponents();
+        
+        if (bundle != null){
+            for (int bund = 0; bund < NBUpdaterUtils.options.length - 1; bund++){
+                if (bundle.equalsIgnoreCase(NBUpdaterUtils.options[bund])) {
+                    bundleComboBox.getModel().setSelectedItem(bundleComboBox.getModel().getElementAt(bund));
+                }
+            }
+        }
+        
+        if (platform != null) {
+            for (int platf = 0; platf < NBUpdaterUtils.platforms.length - 1; platf++){
+                if (platform.equalsIgnoreCase(NBUpdaterUtils.platforms[platf])) {
+                    platformComboBox.getModel().setSelectedItem(platformComboBox.getModel().getElementAt(platf));
+                }
+            }
+        }
     }
 
     /**
     private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed
         if (downloadButton.getText().equals("Download")) {
             NBUpdaterUtils nbUtils = new NBUpdaterUtils();
-            nbUtils.setOption(options[bundleComboBox.getSelectedIndex()]);
-            nbUtils.setPlatform(platforms[platformComboBox.getSelectedIndex()]);
-            df = new NBUpdaterDownloadFile(nbUtils.getDownloadFileURL(), this);
+            nbUtils.setOption(NBUpdaterUtils.options[bundleComboBox.getSelectedIndex()]);
+            nbUtils.setPlatform(NBUpdaterUtils.platforms[platformComboBox.getSelectedIndex()]);
+            df = new NBUpdaterProcessor(nbUtils.getDownloadFileURL(), this);
+            this.setComponentsEnabled(false);
+            df.prepareData();
+            downloadProgressBar.setMinimum(0);
+            downloadProgressBar.setMaximum(df.getFileSize());
             
-            this.setComponentsEnabled(false);
-            
-            downloadProgressBar.setMinimum(0);
-            downloadProgressBar.setMaximum(df.fileSize);
             downloadProgressBar.setStringPainted(true);
             df.execute();
+            
             downloadButton.setText("Cancel");
         } else {
             this.setComponentsEnabled(true);

src/org/narsereg/nbnetcatupdater/NBUpdaterProcessor.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.narsereg.nbnetcatupdater;
+
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.SwingWorker;
+
+/**
+ *
+ * @author narsereg
+ */
+public class NBUpdaterProcessor extends SwingWorker<Boolean, Integer> implements Runnable{
+    NBUpdaterProcessor(String fileURL, NBUpdaterForm nbuf){
+        this.fileURL = fileURL;
+        this.nbuf = nbuf;
+    }
+    
+    public void prepareData(){
+        if (!noGUI) {
+            this.pathToFile = nbuf.getDirectoryToDownload();
+        } else {
+            this.pathToFile = ".";
+        }
+        downloader = new NBUpdaterDownloader(fileURL, pathToFile);
+        
+        if (noGUI){
+            PublishThread publishThread = null;
+            try {
+                publishThread = new PublishThread(downloader, this, noGUI);
+                publishThread.start();
+                downloader.download();
+            } finally {
+                if (publishThread != null) {
+                    publishThread.isDownloaded = true;
+                }
+            }
+        }
+    }
+   
+    NBUpdaterDownloader downloader;
+    private NBUpdaterForm nbuf = null;
+    boolean wasCancelled = false;
+    String fileURL = null;
+    String pathToFile = null;
+    boolean noGUI = false;
+    
+    public int getFileSize(){
+        return downloader.getFileSize();
+    }
+    
+    
+    @Override
+    public Boolean doInBackground(){
+        PublishThread publishThread = null;
+
+        try {
+            publishThread = new PublishThread(downloader, this, noGUI);
+            publishThread.start();
+            downloader.download();
+        } finally {
+            publish(downloader.getFileSize());
+            if(isCancelled()){
+                downloader.deleteFile();
+                publish(0);
+            }
+            if (publishThread != null) {
+                publishThread.isDownloaded = true;
+            }
+        }
+        nbuf.unzipDownloadedFile(downloader.getAbsolutePath());
+        return true;
+    }
+    
+    protected void closeConnection() throws InterruptedException{
+        Thread.currentThread().interrupt();
+    }
+    
+    @Override
+    protected void process(List<Integer> downloadedBytes){
+        for(Integer i : downloadedBytes){
+            nbuf.updateDownloadProgressbar(i);
+        }
+    }
+    
+    class PublishThread extends Thread{
+
+        NBUpdaterDownloader downloader = null;
+        NBUpdaterProcessor nbudf = null;
+        boolean isDownloaded = false;
+        boolean noGUI;
+        
+        public PublishThread(NBUpdaterDownloader downloader, NBUpdaterProcessor nbudf, boolean noGUI) {
+            this.downloader = downloader;
+            this.nbudf = nbudf;
+            this.noGUI = noGUI;
+        }
+
+        @Override
+        public void run() {
+            NBUpdaterConsole console = new NBUpdaterConsole();
+            while(!this.isDownloaded){
+                if (!this.noGUI) {
+                    nbudf.publish((int)downloader.getSizeOfDownloadedFile());
+                } else {
+                    if (downloader.getSizeOfDownloadedFile() > 0) {
+                        console.publishPercent(Math.round(downloader.getSizeOfDownloadedFile()*100/downloader.getFileSize()));
+                    }
+                }
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException ex) {
+                    Logger.getLogger(NBUpdaterProcessor.class.getName()).log(Level.SEVERE, null, ex);
+                }
+            }
+            console.publishPercent(100);
+            
+        }
+    }
+    
+}
+
+

src/org/narsereg/nbnetcatupdater/NBUpdaterUtils.java

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
  * @author narsereg
  */
 public class NBUpdaterUtils {
+    static String[] options = { "javase", "javaee", "cpp", "php", "all" };
+    static String[] platforms = { "windows", "linux", "solaris-x86", "solaris-sparc", "macosx", "zip" };
+    
     private static String URL_FILE_TEMPLATE = "http://bits.netbeans.org/dev/nightly/latest/[type]/netbeans-trunk-nightly-[date]-[option][platform].[extension]";
     private static String URL_PAGE_TEMPLATE = "http://bits.netbeans.org/dev/nightly/";
     
     
     public String getDownloadFileURL(){
         if (platform.equalsIgnoreCase("zip")){
-            if (option.equalsIgnoreCase("all"))
+            if (option.equalsIgnoreCase("all")) {
                 return URL_FILE_TEMPLATE.replace("[type]", "zip").replace("[date]", parseLastUpdate()).replace("[option]", "").replace("[platform]", "").replace("-.[extension]", ".zip");
-            else
+            } else {
                 return URL_FILE_TEMPLATE.replace("[type]", "zip").replace("[date]", parseLastUpdate()).replace("[option]", this.option).replace("[platform]", "").replace("[extension]", "zip");
+            }
         } else {
             String fileURL;
-            if (option.equalsIgnoreCase("all"))
-                fileURL = URL_FILE_TEMPLATE.replace("[type]", "bundles").replace("[date]", parseLastUpdate()).replace("[option]", "").replace("[platform]", this.platform);                
-            else
+            if (option.equalsIgnoreCase("all")) {
+                fileURL = URL_FILE_TEMPLATE.replace("[type]", "bundles").replace("[date]", parseLastUpdate()).replace("[option]", "").replace("[platform]", this.platform);
+            } else {
                 fileURL = URL_FILE_TEMPLATE.replace("[type]", "bundles").replace("[date]", parseLastUpdate()).replace("[option]", this.option).replace("[platform]", "-" + this.platform);
-            if (platform.equalsIgnoreCase("windows"))
+            }
+            if (platform.equalsIgnoreCase("windows")) {
                 return fileURL.replace("[extension]", "exe");
-            else if (platform.equalsIgnoreCase("macosx"))
+            } else if (platform.equalsIgnoreCase("macosx")) {
                 return fileURL.replace("[extension]", "dmg");
-            else
+            } else {
                 return fileURL.replace("[extension]", "sh");
+            }
         }
         
     }