Jsr223HookScript.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.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.io.PrintStream;
  22. import java.nio.charset.Charset;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import java.util.Properties;
  29. import java.util.Set;

  30. import javax.script.Bindings;
  31. import javax.script.Invocable;
  32. import javax.script.ScriptContext;
  33. import javax.script.ScriptEngine;
  34. import javax.script.ScriptEngineManager;
  35. import javax.script.ScriptException;

  36. import org.apache.ibatis.migration.MigrationException;
  37. import org.apache.ibatis.migration.options.SelectedPaths;
  38. import org.apache.ibatis.migration.utils.Util;

  39. public class Jsr223HookScript implements HookScript {

  40.   private static final String MIGRATION_PATHS = "migrationPaths";

  41.   private static final String KEY_FUNCTION = "_function";
  42.   private static final String KEY_OBJECT = "_object";
  43.   private static final String KEY_METHOD = "_method";
  44.   private static final String KEY_ARG = "_arg";

  45.   protected final String language;
  46.   protected final File scriptFile;
  47.   protected final String charset;
  48.   protected final Properties variables;
  49.   protected final SelectedPaths paths;
  50.   protected final PrintStream printStream;

  51.   protected String functionName;
  52.   protected String objectName;
  53.   protected String methodName;
  54.   protected List<String> args = new ArrayList<>();
  55.   protected Map<String, String> localVars = new HashMap<>();

  56.   public Jsr223HookScript(String language, File scriptFile, String charset, String[] options, SelectedPaths paths,
  57.       Properties variables, PrintStream printStream) {
  58.     this.language = language;
  59.     this.scriptFile = scriptFile;
  60.     this.charset = charset;
  61.     this.paths = paths;
  62.     this.variables = variables;
  63.     this.printStream = printStream;
  64.     for (String option : options) {
  65.       int sep = option.indexOf('=');
  66.       if (sep > -1) {
  67.         String key = option.substring(0, sep);
  68.         String value = option.substring(sep + 1);
  69.         if (KEY_FUNCTION.equals(key)) {
  70.           functionName = value;
  71.         } else if (KEY_METHOD.equals(key)) {
  72.           methodName = value;
  73.         } else if (KEY_OBJECT.equals(key)) {
  74.           objectName = value;
  75.         } else if (KEY_ARG.equals(key)) {
  76.           args.add(value);
  77.         } else {
  78.           localVars.put(key, value);
  79.         }
  80.       }
  81.     }
  82.   }

  83.   @Override
  84.   public void execute(Map<String, Object> bindingMap) {
  85.     ScriptEngineManager manager = new ScriptEngineManager();
  86.     ScriptEngine engine = manager.getEngineByName(language);
  87.     // bind global/local variables defined in the environment file
  88.     Bindings bindings = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
  89.     bindVariables(bindingMap, variables.entrySet());
  90.     bindVariables(bindingMap, localVars.entrySet());
  91.     bindings.put(MIGRATION_PATHS, paths);
  92.     bindings.putAll(bindingMap);
  93.     try {
  94.       printStream.println(Util.horizontalLine("Applying JSR-223 hook : " + scriptFile.getName(), 80));
  95.       try (
  96.           InputStreamReader stream = new InputStreamReader(new FileInputStream(scriptFile), Charset.forName(charset))) {
  97.         engine.eval(stream);
  98.       }
  99.       if (functionName != null || objectName != null && methodName != null) {
  100.         Invocable invocable = (Invocable) engine;
  101.         if (functionName != null) {
  102.           printStream.println(Util.horizontalLine("Invoking function : " + functionName, 80));
  103.           invocable.invokeFunction(functionName, args.toArray());
  104.         } else {
  105.           printStream.println(Util.horizontalLine("Invoking method : " + methodName, 80));
  106.           Object targetObject = engine.get(objectName);
  107.           invocable.invokeMethod(targetObject, methodName, args.toArray());
  108.         }
  109.       }
  110.       // store vars in bindings to the per-operation map
  111.       bindVariables(bindingMap, bindings.entrySet());
  112.     } catch (ClassCastException e) {
  113.       throw new MigrationException(
  114.           "Script engine '" + engine.getClass().getName() + "' does not support function/method invocation.", e);
  115.     } catch (IOException e) {
  116.       throw new MigrationException("Failed to read JSR-223 hook script file.", e);
  117.     } catch (ScriptException e) {
  118.       throw new MigrationException("Failed to execute JSR-223 hook script.", e);
  119.     } catch (NoSuchMethodException e) {
  120.       throw new MigrationException("Method or function not found in JSR-223 hook script: " + functionName, e);
  121.     }
  122.   }

  123.   private <S, T> void bindVariables(Map<String, Object> bindingMap, Set<Entry<S, T>> vars) {
  124.     for (Entry<S, T> entry : vars) {
  125.       bindingMap.put((String) entry.getKey(), entry.getValue());
  126.     }
  127.   }
  128. }