InfoCommand.java

  1. /*
  2.  *    Copyright 2010-2023 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.migration.commands;

  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.PrintStream;
  21. import java.nio.charset.Charset;
  22. import java.util.Locale;
  23. import java.util.Properties;

  24. public final class InfoCommand implements Command {
  25.   private final PrintStream out;

  26.   public InfoCommand(PrintStream out) {
  27.     this.out = out;
  28.   }

  29.   @Override
  30.   public void execute(String... params) {
  31.     Properties properties = new Properties();
  32.     try (InputStream is = getClass().getResourceAsStream("/mybatis-migrations.properties")) {
  33.       if (is != null) {
  34.         properties.load(is);
  35.       }
  36.     } catch (IOException e) {
  37.       // ignore
  38.     }

  39.     out.printf("%s %s (%s)%n", properties.getProperty("name"), properties.getProperty("version"),
  40.         properties.getProperty("build"));
  41.     out.printf("Java version: %s, vendor: %s%n", System.getProperty("java.version"), System.getProperty("java.vendor"));
  42.     out.printf("Java home: %s%n", System.getProperty("java.home"));
  43.     out.printf("Default locale: %s, platform encoding: %s%n", Locale.getDefault().toLanguageTag(),
  44.         Charset.defaultCharset().name());
  45.     out.printf("OS name: \"%s\", version: \"%s\", arch: \"%s\", family: \"%s\"%n", System.getProperty("os.name"),
  46.         System.getProperty("os.version"), System.getProperty("os.arch"), getOsFamily());
  47.   }

  48.   private static String getOsFamily() {
  49.     String osName = System.getProperty("os.name").toLowerCase();
  50.     String pathSep = File.pathSeparator;

  51.     if (osName.indexOf("windows") != -1) {
  52.       return "windows";
  53.     }

  54.     if (osName.indexOf("os/2") != -1) {
  55.       return "os/2";
  56.     }

  57.     if (osName.indexOf("z/os") != -1 || osName.indexOf("os/390") != -1) {
  58.       return "z/os";
  59.     }

  60.     if (osName.indexOf("os/400") != -1) {
  61.       return "os/400";
  62.     }

  63.     if (pathSep.equals(";")) {
  64.       return "dos";
  65.     }

  66.     if (osName.indexOf("mac") != -1) {
  67.       if (osName.endsWith("x")) {
  68.         return "mac"; // MACOSX
  69.       }
  70.       return "unix";
  71.     }

  72.     if (osName.indexOf("nonstop_kernel") != -1) {
  73.       return "tandem";
  74.     }

  75.     if (osName.indexOf("openvms") != -1) {
  76.       return "openvms";
  77.     }

  78.     if (pathSep.equals(":")) {
  79.       return "unix";
  80.     }

  81.     return "undefined";
  82.   }

  83. }