FileHookScriptFactory.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.hook;

  17. import java.io.File;
  18. import java.io.PrintStream;
  19. import java.util.Arrays;
  20. import java.util.Properties;

  21. import org.apache.ibatis.migration.Environment;
  22. import org.apache.ibatis.migration.MigrationException;
  23. import org.apache.ibatis.migration.options.SelectedPaths;

  24. public class FileHookScriptFactory implements HookScriptFactory {

  25.   protected final SelectedPaths paths;
  26.   protected final Environment environment;
  27.   protected final PrintStream printStream;

  28.   public FileHookScriptFactory(SelectedPaths paths, Environment environment, PrintStream printStream) {
  29.     this.paths = paths;
  30.     this.environment = environment;
  31.     this.printStream = printStream;
  32.   }

  33.   @Override
  34.   public HookScript create(String hookSetting) {
  35.     if (hookSetting == null) {
  36.       return null;
  37.     }
  38.     File hooksDir = paths.getHookPath();
  39.     if (hooksDir == null) {
  40.       throw new MigrationException("Hooks directory must not be null.");
  41.     }
  42.     if (!hooksDir.exists()) {
  43.       throw new MigrationException("Hooks directory not found : " + hooksDir.getAbsolutePath());
  44.     }
  45.     String[] segments = hookSetting.split(":");
  46.     if (segments.length < 2) {
  47.       throw new MigrationException(
  48.           "Error creating a HookScript. Hook setting must contain 'language' and 'file name' separated by ':' (e.g. SQL:post-up.sql).");
  49.     }
  50.     String charset = environment.getScriptCharset();
  51.     Properties variables = environment.getVariables();
  52.     // First segment is language
  53.     String scriptLang = segments[0];
  54.     // Second segment is file
  55.     File scriptFile = new File(hooksDir, segments[1]);
  56.     // The rest are script dependent options
  57.     String[] hookOptions = Arrays.copyOfRange(segments, 2, segments.length);
  58.     if (!scriptFile.exists()) {
  59.       throw new MigrationException("Hook script not found : " + scriptFile.getAbsolutePath());
  60.     }
  61.     if ("sql".equalsIgnoreCase(scriptLang)) {
  62.       return new SqlHookScript(scriptFile, charset, hookOptions, variables, printStream);
  63.     }
  64.     // Assuming it's JSR-223.
  65.     return new Jsr223HookScript(scriptLang, scriptFile, charset, hookOptions, paths, variables, printStream);
  66.   }
  67. }