Util.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.utils;

  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.util.Properties;

  20. public enum Util {
  21.   ;

  22.   private static final String MIGRATIONS_HOME = "MIGRATIONS_HOME";

  23.   /* TODO: remove in the next major release */
  24.   private static final String MIGRATIONS_HOME_PROPERTY_DEPRECATED = "migrationHome";

  25.   private static final String MIGRATIONS_HOME_PROPERTY = "migrationsHome";

  26.   private static final String MIGRATIONS_PROPERTIES = "migration.properties";

  27.   public static String migrationsHome() {
  28.     String migrationsHome = System.getenv(MIGRATIONS_HOME);
  29.     // Check if there is a system property
  30.     if (migrationsHome == null) {
  31.       migrationsHome = System.getProperty(MIGRATIONS_HOME_PROPERTY);
  32.       if (migrationsHome == null) {
  33.         migrationsHome = System.getProperty(MIGRATIONS_HOME_PROPERTY_DEPRECATED);
  34.       }
  35.     }
  36.     return migrationsHome;
  37.   }

  38.   public static boolean getPropertyOptionAsBoolean(String key) {
  39.     return Boolean.parseBoolean(getPropertyOption(key));
  40.   }

  41.   /**
  42.    * @param key
  43.    *          of the property.
  44.    *
  45.    * @return The value <code>null</code> if the property file does not exist or the <code>key</code> does not exist.
  46.    */
  47.   public static String getPropertyOption(String key) {
  48.     String migrationsHome = migrationsHome();
  49.     if (migrationsHome == null || migrationsHome.isEmpty()) {
  50.       return null;
  51.     }
  52.     Properties properties = new Properties();
  53.     String path = migrationsHome + File.separator + MIGRATIONS_PROPERTIES;
  54.     try (FileInputStream stream = new FileInputStream(path)) {
  55.       properties.load(stream);
  56.       return properties.getProperty(key);
  57.     } catch (Exception e) {
  58.       return null;
  59.     }
  60.   }

  61.   public static boolean isOption(String arg) {
  62.     return arg.startsWith("--") && !arg.trim().endsWith("=");
  63.   }

  64.   public static File file(File path, String fileName) {
  65.     return new File(path.getAbsolutePath() + File.separator + fileName);
  66.   }

  67.   public static String horizontalLine(String caption, int length) {
  68.     StringBuilder builder = new StringBuilder();
  69.     builder.append("==========");
  70.     if (caption.length() > 0) {
  71.       caption = " " + caption + " ";
  72.       builder.append(caption);
  73.     }
  74.     for (int i = 0; i < length - caption.length() - 10; i++) {
  75.       builder.append("=");
  76.     }
  77.     return builder.toString();
  78.   }
  79. }