<commit_before>/*
 * Copyright (c) 2014 <NAME> (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */
package org.eclipse.oomph.util;

import org.eclipse.oomph.internal.util.UtilPlugin;

import org.eclipse.emf.common.CommonPlugin;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;

/**
 * @author <NAME>
 */
public abstract class OS
{
  public static final OS INSTANCE = create();

  private final String osgiOS;

  private final String osgiWS;

  private final String osgiArch;

  protected OS(String osgiOS, String osgiWS, String osgiArch)
  {
    this.osgiOS = osgiOS;
    this.osgiWS = osgiWS;
    this.osgiArch = osgiArch;
  }

  public String getOsgiOS()
  {
    return osgiOS;
  }

  public String getOsgiWS()
  {
    return osgiWS;
  }

  public String getOsgiArch()
  {
    return osgiArch;
  }

  public boolean isWin()
  {
    return false;
  }

  public boolean isMac()
  {
    return false;
  }

  public boolean isLinux()
  {
    return false;
  }

  public boolean isCurrent()
  {
    return Platform.getOS().equals(osgiOS) && Platform.getWS().equals(osgiWS) && Platform.getOSArch().equals(osgiArch);
  }

  public boolean isCurrentOS()
  {
    return Platform.getOS().equals(osgiOS);
  }

  public boolean is32BitAvailable()
  {
    return true;
  }

  public boolean isLineEndingConversionNeeded()
  {
    return false;
  }

  protected String getEncoding()
  {
    return "ISO-8859-1";
  }

  protected abstract String[] getOpenCommands();

  public boolean openSystemBrowser(String url)
  {
    try
    {
      for (String command : getOpenCommands())
      {
        if (openSystemBrowser(command, url))
        {
          return true;
        }
      }
    }
    catch (Throwable ex)
    {
      //$FALL-THROUGH$
    }

    try
    {
      // java.awt.Desktop was introduced with Java 1.6!
      Class<?> desktopClass = CommonPlugin.loadClass(UtilPlugin.INSTANCE.getSymbolicName(), "java.awt.Desktop");
      Method getDesktopMethod = ReflectUtil.getMethod(desktopClass, "getDesktop");
      Method browseMethod = ReflectUtil.getMethod(desktopClass, "browse", URI.class);

      Object desktop = getDesktopMethod.invoke(null);
      browseMethod.invoke(desktop, new URI(url));
      return true;
    }
    catch (Throwable ex)
    {
      UtilPlugin.INSTANCE.log(ex, IStatus.WARNING);
    }

    return false;
  }

  private boolean openSystemBrowser(String command, String url)
  {
    String[] cmdarray = { "\"" + command + "\"", "\"" + url + "\"" };

    try
    {
      Process process = Runtime.getRuntime().exec(cmdarray);
      if (process != null)
      {
        try
        {
          // See if we get an IllegalThreadStateException that indicates that the process is still running.
          process.exitValue();
        }
        catch (IllegalThreadStateException ex)
        {
          return true;
        }
      }
    }
    catch (IOException ex)
    {
      //$FALL-THROUGH$
    }

    return false;
  }

  public String getEclipseDir()
  {
    return "eclipse";
  }

  public abstract String getEclipseExecutable();

  public abstract String getEclipseIni();

  public abstract String getGitPrefix();

  public abstract String getJREsRoot();

  public abstract OS getForBitness(int bitness);

  public static void close(Closeable closeable)
  {
    if (closeable != null)
    {
      try
      {
        closeable.close();
      }
      catch (IOException ex)
      {
        throw new RuntimeException(ex);
      }
    }
  }

  private static OS create()
  {
    String os = Platform.getOS();
    String ws = Platform.getWS();
    String arch = Platform.getOSArch();

    if (Platform.OS_WIN32.equals(os))
    {
      if (Platform.ARCH_X86_64.equals(arch))
      {
        return new Win64(ws);
      }

      return new Win32(ws, arch);
    }

    if (Platform.OS_MACOSX.equals(os))
    {
      return new Mac(ws, arch);
    }

    if (Platform.OS_LINUX.equals(os))
    {
      return new Linux(ws, arch);
    }

    throw new IllegalStateException("Operating system not supported: " + os);
  }

  /**
   * @author <NAME>
   */
  private static class Win32 extends OS
  {
    private static final Win32 INSTANCE = new Win32(Platform.WS_WIN32, Platform.ARCH_X86);

    // Don't use "explorer" as it forks another process and returns a confusing exit value.
    private static final String[] OPEN_COMMANDS = {};

    public Win32(String osgiWS, String osgiArch)
    {
      super(Platform.OS_WIN32, osgiWS, osgiArch);
    }

    @Override
    public boolean isWin()
    {
      return true;
    }

    @Override
    public boolean isLineEndingConversionNeeded()
    {
      return true;
    }

    @Override
    public String getEclipseExecutable()
    {
      return "eclipse.exe";
    }

    @Override
    public String getEclipseIni()
    {
      return "eclipse.ini";
    }

    @Override
    public String getGitPrefix()
    {
      return "C:\\Program Files (x86)\\Git";
    }

    @Override
    public String getJREsRoot()
    {
      return "C:\\Program Files (x86)\\Java";
    }

    @Override
    public OS getForBitness(int bitness)
    {
      if (bitness == 64)
      {
        return Win64.INSTANCE;
      }

      return this;
    }

    @Override
    protected String[] getOpenCommands()
    {
      return OPEN_COMMANDS;
    }
  }

  /**
   * @author <NAME>
   */
  private static class Win64 extends Win32
  {
    private static final Win64 INSTANCE = new Win64(Platform.WS_WIN32);

    public Win64(String osgiWS)
    {
      super(osgiWS, Platform.ARCH_X86_64);
    }

    @Override
    public String getJREsRoot()
    {
      return "C:\\Program Files\\Java";
    }

    @Override
    public OS getForBitness(int bitness)
    {
      if (bitness == 32)
      {
        return Win32.INSTANCE;
      }

      return this;
    }
  }

  /**
   * @author <NAME>
   */
  private static final class Mac extends OS
  {
    private static final String[] OPEN_COMMANDS = { "open" };

    public Mac(String osgiWS, String osgiArch)
    {
      super(Platform.OS_MACOSX, osgiWS, osgiArch);
    }

    @Override
    public boolean isMac()
    {
      return true;
    }

    @Override
    public String getEclipseDir()
    {
      return "Eclipse.app/Contents/Eclipse";
    }

    @Override
    public String getEclipseExecutable()
    {
      return "../MacOS/eclipse";
    }

    @Override
    public String getEclipseIni()
    {
      return "eclipse.ini";
    }

    @Override
    public String getGitPrefix()
    {
      return "/";
    }

    @Override
    public String getJREsRoot()
    {
      return "/";
    }

    @Override
    public boolean is32BitAvailable()
    {
      return false;
    }

    @Override
    public OS getForBitness(int bitness)
    {
      return this;
    }

    @Override
    protected String[] getOpenCommands()
    {
      return OPEN_COMMANDS;
    }
  }

  /**
   * @author <NAME>
   */
  private static final class Linux extends OS
  {
    private static final String[] OPEN_COMMANDS = { "kde-open", "gnome-open", "xdg-open" };

    public Linux(String osgiWS, String osgiArch)
    {
      super(Platform.OS_LINUX, osgiWS, osgiArch);
    }

    @Override
    public boolean isLinux()
    {
      return true;
    }<commit_msg>[468304] Download JRE button does not work<commit_after>/*
 * Copyright (c) 2014 <NAME> (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */
package org.eclipse.oomph.util;

import org.eclipse.oomph.internal.util.UtilPlugin;

import org.eclipse.emf.common.CommonPlugin;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;

import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;

/**
 * @author <NAME>
 */
public abstract class OS
{
  public static final OS INSTANCE = create();

  private final String osgiOS;

  private final String osgiWS;

  private final String osgiArch;

  protected OS(String osgiOS, String osgiWS, String osgiArch)
  {
    this.osgiOS = osgiOS;
    this.osgiWS = osgiWS;
    this.osgiArch = osgiArch;
  }

  public String getOsgiOS()
  {
    return osgiOS;
  }

  public String getOsgiWS()
  {
    return osgiWS;
  }

  public String getOsgiArch()
  {
    return osgiArch;
  }

  public boolean isWin()
  {
    return false;
  }

  public boolean isMac()
  {
    return false;
  }

  public boolean isLinux()
  {
    return false;
  }

  public boolean isCurrent()
  {
    return Platform.getOS().equals(osgiOS) && Platform.getWS().equals(osgiWS) && Platform.getOSArch().equals(osgiArch);
  }

  public boolean isCurrentOS()
  {
    return Platform.getOS().equals(osgiOS);
  }

  public boolean is32BitAvailable()
  {
    return true;
  }

  public boolean isLineEndingConversionNeeded()
  {
    return false;
  }

  protected String getEncoding()
  {
    return "ISO-8859-1";
  }

  protected abstract String[] getOpenCommands();

  public boolean openSystemBrowser(String url)
  {
    try
    {
      for (String command : getOpenCommands())
      {
        if (openSystemBrowser(command, url))
        {
          return true;
        }
      }
    }
    catch (Throwable ex)
    {
      //$FALL-THROUGH$
    }

    try
    {
      // java.awt.Desktop was introduced with Java 1.6!
      Class<?> desktopClass = CommonPlugin.loadClass(UtilPlugin.INSTANCE.getSymbolicName(), "java.awt.Desktop");
      Method getDesktopMethod = ReflectUtil.getMethod(desktopClass, "getDesktop");
      Method browseMethod = ReflectUtil.getMethod(desktopClass, "browse", URI.class);

      Object desktop = getDesktopMethod.invoke(null);
      browseMethod.invoke(desktop, new URI(url));
      return true;
    }
    catch (Throwable ex)
    {
      UtilPlugin.INSTANCE.log(ex, IStatus.WARNING);
    }

    return false;
  }

  private boolean openSystemBrowser(String command, String url)
  {
    String[] cmdarray = { "\"" + command + "\"", "\"" + url + "\"" };

    try
    {
      Process process = Runtime.getRuntime().exec(cmdarray);
      if (process != null)
      {
        try
        {
          // See if we get an IllegalThreadStateException that indicates that the process is still running.
          process.exitValue();
        }
        catch (IllegalThreadStateException ex)
        {
          return true;
        }
      }
    }
    catch (IOException ex)
    {
      //$FALL-THROUGH$
    }

    return false;
  }

  public String getEclipseDir()
  {
    return "eclipse";
  }

  public abstract String getEclipseExecutable();

  public abstract String getEclipseIni();

  public abstract String getGitPrefix();

  public abstract String getJREsRoot();

  public abstract OS getForBitness(int bitness);

  public static void close(Closeable closeable)
  {
    if (closeable != null)
    {
      try
      {
        closeable.close();
      }
      catch (IOException ex)
      {
        throw new RuntimeException(ex);
      }
    }
  }

  private static OS create()
  {
    String os = Platform.getOS();
    String ws = Platform.getWS();
    String arch = Platform.getOSArch();

    if (Platform.OS_WIN32.equals(os))
    {
      if (Platform.ARCH_X86_64.equals(arch))
      {
        return new Win64(ws);
      }

      return new Win32(ws, arch);
    }

    if (Platform.OS_MACOSX.equals(os))
    {
      return new Mac(ws, arch);
    }

    if (Platform.OS_LINUX.equals(os))
    {
      return new Linux(ws, arch);
    }

    throw new IllegalStateException("Operating system not supported: " + os);
  }

  /**
   * @author <NAME>
   */
  private static class Win32 extends OS
  {
    private static final Win32 INSTANCE = new Win32(Platform.WS_WIN32, Platform.ARCH_X86);

    // Don't use "explorer" as it forks another process and returns a confusing exit value.
    private static final String[] OPEN_COMMANDS = {};

    public Win32(String osgiWS, String osgiArch)
    {
      super(Platform.OS_WIN32, osgiWS, osgiArch);
    }

    @Override
    public boolean isWin()
    {
      return true;
    }

    @Override
    public boolean isLineEndingConversionNeeded()
    {
      return true;
    }

    @Override
    public String getEclipseExecutable()
    {
      return "eclipse.exe";
    }

    @Override
    public String getEclipseIni()
    {
      return "eclipse.ini";
    }

    @Override
    public String getGitPrefix()
    {
      return "C:\\Program Files (x86)\\Git";
    }

    @Override
    public String getJREsRoot()
    {
      return "C:\\Program Files (x86)\\Java";
    }

    @Override
    public OS getForBitness(int bitness)
    {
      if (bitness == 64)
      {
        return Win64.INSTANCE;
      }

      return this;
    }

    @Override
    protected String[] getOpenCommands()
    {
      return OPEN_COMMANDS;
    }
  }

  /**
   * @author <NAME>
   */
  private static class Win64 extends Win32
  {
    private static final Win64 INSTANCE = new Win64(Platform.WS_WIN32);

    public Win64(String osgiWS)
    {
      super(osgiWS, Platform.ARCH_X86_64);
    }

    @Override
    public String getJREsRoot()
    {
      return "C:\\Program Files\\Java";
    }

    @Override
    public OS getForBitness(int bitness)
    {
      if (bitness == 32)
      {
        return Win32.INSTANCE;
      }

      return this;
    }
  }

  /**
   * @author <NAME>
   */
  private static final class Mac extends OS
  {
    private static final String[] OPEN_COMMANDS = { "open" };

    public Mac(String osgiWS, String osgiArch)
    {
      super(Platform.OS_MACOSX, osgiWS, osgiArch);
    }

    @Override
    public boolean isMac()
    {
      return true;
    }

    @Override
    public String getEclipseDir()
    {
      return "Eclipse.app/Contents/Eclipse";
    }

    @Override
    public String getEclipseExecutable()
    {
      return "../MacOS/eclipse";
    }

    @Override
    public String getEclipseIni()
    {
      return "eclipse.ini";
    }

    @Override
    public String getGitPrefix()
    {
      return "/";
    }

    @Override
    public String getJREsRoot()
    {
      return "/";
    }

    @Override
    public boolean is32BitAvailable()
    {
      return false;
    }

    @Override
    public OS getForBitness(int bitness)
    {
      return this;
    }

    @Override
    protected String[] getOpenCommands()
    {
      return OPEN_COMMANDS;
    }
  }

  /**
   * @author <NAME>
   */
  private static final class Linux extends OS
  {
    private static final String[] OPEN_COMMANDS = { "kde-open", "gnome-open", "xdg-open", "sensible-browser" };

    public Linux(String osgiWS, String osgiArch)
    {
      super(Platform.OS_LINUX, osgiWS, osgiArch);
    }

    @Override
    public boolean isLinux()
    {
      return true;
    }
{'fraction_non_alphanumeric': 0.10644257703081232, 'fraction_numerical': 0.009641065728617029, 'mean_word_length': 2.31648304169367, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1115505', 'n_tokens_mistral': 5100, 'n_tokens_neox': 4796, 'n_words': 1187}