View Javadoc
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  
18  import java.io.File;
19  import java.io.PrintStream;
20  import java.util.Arrays;
21  import java.util.Properties;
22  
23  import org.apache.ibatis.migration.Environment;
24  import org.apache.ibatis.migration.MigrationException;
25  import org.apache.ibatis.migration.options.SelectedPaths;
26  
27  public class FileHookScriptFactory implements HookScriptFactory {
28  
29    protected final SelectedPaths paths;
30    protected final Environment environment;
31    protected final PrintStream printStream;
32  
33    public FileHookScriptFactory(SelectedPaths paths, Environment environment, PrintStream printStream) {
34      this.paths = paths;
35      this.environment = environment;
36      this.printStream = printStream;
37    }
38  
39    @Override
40    public HookScript create(String hookSetting) {
41      if (hookSetting == null) {
42        return null;
43      }
44      File hooksDir = paths.getHookPath();
45      if (hooksDir == null) {
46        throw new MigrationException("Hooks directory must not be null.");
47      }
48      if (!hooksDir.exists()) {
49        throw new MigrationException("Hooks directory not found : " + hooksDir.getAbsolutePath());
50      }
51      String[] segments = hookSetting.split(":");
52      if (segments.length < 2) {
53        throw new MigrationException(
54            "Error creating a HookScript. Hook setting must contain 'language' and 'file name' separated by ':' (e.g. SQL:post-up.sql).");
55      }
56      String charset = environment.getScriptCharset();
57      Properties variables = environment.getVariables();
58      // First segment is language
59      String scriptLang = segments[0];
60      // Second segment is file
61      File scriptFile = new File(hooksDir, segments[1]);
62      // The rest are script dependent options
63      String[] hookOptions = Arrays.copyOfRange(segments, 2, segments.length);
64      if (!scriptFile.exists()) {
65        throw new MigrationException("Hook script not found : " + scriptFile.getAbsolutePath());
66      }
67      if ("sql".equalsIgnoreCase(scriptLang)) {
68        return new SqlHookScript(scriptFile, charset, hookOptions, variables, printStream);
69      }
70      // Assuming it's JSR-223.
71      return new Jsr223HookScript(scriptLang, scriptFile, charset, hookOptions, paths, variables, printStream);
72    }
73  }