Package de.chris_soft.nanodoa

Source Code of de.chris_soft.nanodoa.Restart

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.nanodoa;

import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;

import de.chris_soft.utilities.LogUtils;
import de.chris_soft.utilities.swing.SwingUtils;

/**
* Restart management for full NanoDoA system.
* @author Christian Packenius.
*/
public class Restart {
  private static volatile boolean restartAll = false;

  private static File restartBatchFile = null;

  private static JProgressBar progressBar;

  /**
   * This file will be deleted when NanoDoA system JRE has been stopped.
   */
  public static final File restartFlagFile = new File("restart.nanodoa.system");

  private static final int maxWaitTime = 20000;

  /**
   * Restart NanoDoA system now.
   */
  public static synchronized void prepareForRestart() {
    restartWithBatchfile(null);
  }

  static synchronized void restartWithBatchfile(File batchFile) {
    if (!restartAll) {
      restartAll = true;
      restartBatchFile = batchFile;
      ShutDown.stop();
    }
  }

  static void createRestartFlagFileAndStartAgain() {
    try {
      if (restartAll) {
        restartFlagFile.createNewFile();
        restartFlagFile.deleteOnExit();
        if (restartBatchFile == null) {
          startViaProcess("NanoDoA.exe");
        }
        else {
          String command = "cmd.exe /c start \"Updating NanoDoA\" " + restartBatchFile.getName();
          startViaProcess(command);
        }
      }
    }
    catch (Exception exception) {
      LogUtils.log(exception);
      SwingUtils.showError(null, "Can't restart NanoDoA. System will stop. Please restart it manually.");
    }
  }

  private static void startViaProcess(String command) throws Exception {
    Runtime.getRuntime().exec(command);
  }

  static boolean waitForRestart() {
    if (!restartFlagFile.exists()) {
      return true;
    }
    return waitForFlagFileDeletion();
  }

  private static boolean waitForFlagFileDeletion() {
    JFrame frame = createWaitFrame();
    long time = System.currentTimeMillis();
    long delta = 0;
    while (restartFlagFile.exists() && (delta = System.currentTimeMillis() - time) <= maxWaitTime) {
      progressBar.setValue((int) delta);
      try {
        Thread.sleep(100);
      }
      catch (InterruptedException exception) {
        // Ignore.
      }
    }
    frame.dispose();
    if (restartFlagFile.exists()) {
      SwingUtils.showError(null,
          "Error on restart; please check task/progress manager for NanoDoA and restart manually.");
      return false;
    }
    return true;
  }

  private static JFrame createWaitFrame() {
    JFrame frame = new JFrame();
    frame.setUndecorated(true);
    progressBar = new JProgressBar(0, maxWaitTime);
    progressBar.setValue(0);
    progressBar.setPreferredSize(new Dimension(400, 50));
    progressBar.setStringPainted(true);
    progressBar.setString("Restarting NanoDoA...");
    LineBorder border1 = new LineBorder(Color.BLACK, 3);
    LineBorder border2 = new LineBorder(Color.LIGHT_GRAY, 3);
    progressBar.setBorder(new CompoundBorder(border2, border1));
    frame.getContentPane().add(progressBar);
    frame.pack();
    frame.setAlwaysOnTop(true);
    frame.setVisible(true);
    SwingUtils.centerWindow(frame);
    return frame;
  }

  /**
   * Test method.
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    restartFlagFile.createNewFile();
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(5000);
        }
        catch (InterruptedException exception) {
          // Ignore.
        }
        restartFlagFile.delete();
      }
    }).start();
    System.out.println("Restart? " + Restart.waitForRestart());
  }
}
TOP

Related Classes of de.chris_soft.nanodoa.Restart

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.