<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
package usda.weru.weps.webstart.Installer;

import java.io.File;
import javax.swing.JProgressBar;
import static usda.weru.weps.serverControl.ServerControlData.wepsWsDataDirName;
import usda.weru.weps.webstart.CsipRunner.WsCsipRunner;
import weps.ws.Csip.utils.About;
import weps.ws.Csip.utils.WepsCsipUtil;
import weps.ws.JettyWebstart.CsipInstance.Downloader.CsipDownloadInterface;


/**
 *
 * @author mhaas
 */
public class WsWepsInstaller {
    
    protected boolean running;
    protected String endPointPath;
    protected wepsModelessDialog dlg;
    private Thread downloadThread;
    private boolean isNRCSmode;
    
    public WsWepsInstaller (String endPointPath) {
        running = false;
        this.endPointPath = endPointPath;
        dlg = new wepsModelessDialog();
        isNRCSmode = false;
    }

    public void addMessage (String msg) {
        dlg.appendText(msg);
    }
    
    public void clearMessage () {
        dlg.setMainText("");
    }
    
    public void waitForFinish () {
        waitForData();
        dlg.dispose();
    }
    
    public void setNRCSMode () {
        isNRCSmode = true;
    }

    public void doInstall (boolean autoCloseDlg) {
        doInstall (autoCloseDlg, null);
    }
    
    // The WsCsipRunner is (optionally) passed into here only so that it can be terminated at the end of an install
    public void doInstall (boolean autoCloseDlg, WsCsipRunner dataRunner) {
        
        running = true;
        
        dlg.setMainText("WEPS data Install / Update");
        dlg.setProgressIndeterminate(true);
        dlg.setAlwaysOnTop(true);
        dlg.setLocation(dlg.getWidth()/2, dlg.getHeight()/2);
        dlg.setVisible(true);

        doInstallInProcess(dlg.getProgressBar());

        if (autoCloseDlg) {
            // Make a thread to wait for finish and then dispose the dialog.
            Runnable runnableWaitEnd = new Runnable() {
                public void run() {
                    waitForFinish();
                    if (dataRunner != null) {
                        dataRunner.stopRunningData();
                    }
                }
            };
            new Thread(runnableWaitEnd).start();
        }
    }

        
    public void doInstallInProcess (JProgressBar pBar) {
        
        Runnable runnableDownLoader = new Runnable() {
            public void run() {
                int timeoutMillis = 300000;
                if (endPointPath.contains("localhost")) {
                    // If endpoint is localhost, we are running the server locally via Jetty
                    // The local server takes longerr to start up, so give more time in that case.
                    timeoutMillis *= 2;
                }
                if (WepsCsipUtil.clientCheckConnect(endPointPath, timeoutMillis)) {
                    CsipDownloadInterface csipInterface = new CsipDownloadInterface(
                        endPointPath,  
                        About.getUserWepsRoot().getAbsolutePath() + "/" + wepsWsDataDirName);
                    
                    if (isNRCSmode) {
                        csipInterface.setNRCSMode();
                        dlg.appendText("NRCS mode");
                    }
                    if (!csipInterface.checkLocalDownloadVersion()) {
                        addMessage ("New data package version found:" + csipInterface.getVersionStr());
                        addMessage ("Installing updated data package.");
                        csipInterface.doDownload(pBar);
                    }
                } else {
                    System.err.println ("Did not connect to endpoint:" + endPointPath);
                }

                // Check for WEPS user dir
                File f = new File (System.getProperty("user.home")+ "/Documents/WEPS Files/Project.wpj");
                if (!f.exists()) {
                    f.mkdirs();
                }
                // Check for WEPS runs dir
                f = new File (System.getProperty("user.home")+ "/Documents/WEPS Files/Runs");
                if (!f.exists()) {
                    f.mkdirs();
                }
            //System.err.println("doInstallInProcess thread finishing");
            }
        };
        downloadThread = new Thread(runnableDownLoader);
        downloadThread.start();
    }
    
    public void waitForData () {
        if (downloadThread != null) {
            while (downloadThread.isAlive()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    System.err.println("Interrupt found in downloadServices");
                    break;
                }
            }
            if (!downloadThread.isAlive()) {
                downloadThread = null;
            }
        }
    }
    
    public void stopRunning() {
        if (downloadThread != null &amp;&amp; !downloadThread.isAlive()) {
            downloadThread.interrupt();
        }
        
    }
}
</pre></body></html>