All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.util.Restarter Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.java.net
 * SCM: https://bitbucket.org/tidalwave/openbluesky-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.util;

import it.tidalwave.netbeans.dialog.DialogRunner;
import java.util.logging.Logger;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.openide.LifecycleManager;

/*******************************************************************************
 *
 * The implementation of WorkspaceManager.
 *
 * @author Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public final class Restarter 
  {
    private final static String CLASS = Restarter.class.getName();
    private final static Logger logger = Logger.getLogger(CLASS);
    
    private Restarter() 
      {
      }
  
    /***************************************************************************
     *
     *
     **************************************************************************/
    public static void restart()
      {
        logger.info("restart()");
        final String osName = System.getProperty("os.name").toLowerCase();
        final String app = System.getProperty("it.tidalwave.netbeans.app");
        boolean restarted = false;
        
        if (app != null)
          {
            if (osName.indexOf("mac os x") >= 0)
              {
                restarted = restartMacOSX(app);  
              }
            
            else if (osName.indexOf("linux") >= 0)
              {
                restarted = restartLinux(app);  
              }
            
            else if (osName.indexOf("windows") >= 0)
              {
                restarted = restartWindows(app);  
              }
          } 
        
        if (!restarted)
          {
            DialogRunner.showMessage("Development mode - Please restart the application manually.");
          }
        
        LifecycleManager.getDefault().exit();
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    private static boolean restartMacOSX (final String app)
      {
        try 
          {
            logger.info("restartMacOSX(" + app + ")");
            final File file = File.createTempFile("blueMarine-restart-", ".sh");
            final PrintWriter pw = new PrintWriter(new FileWriter(file));
            pw.println("sleep 1");
            pw.println("open " + app);
            pw.close();
            Runtime.getRuntime().exec("/bin/sh " + file.getAbsolutePath());
            return true;
//            Runtime.getRuntime().exec("/usr/bin/open " + app);
//            return true;
          } 
        catch (IOException e) 
          {
            logger.throwing(CLASS, "restartMacOSX()", e);
            return false;
          }  
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    private static boolean restartLinux (final String app)
      {
        try 
          {
            logger.info("restartLinux(" + app + ")");
            Runtime.getRuntime().exec("/bin/sh " + app);
            return true;
          } 
        catch (IOException e) 
          {
            logger.throwing(CLASS, "restartLinux()", e);
            return false;
          }  
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    private static boolean restartWindows (final String app)
      {
        try 
          {
            logger.info("restartWindows(" + app + ")");
            Runtime.getRuntime().exec(app);
            return true;
          } 
        catch (IOException e) 
          {
            logger.throwing(CLASS, "restartWindows()", e);
            return false;
          }  
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy