1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
59 String scriptLang = segments[0];
60
61 File scriptFile = new File(hooksDir, segments[1]);
62
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
71 return new Jsr223HookScript(scriptLang, scriptFile, charset, hookOptions, paths, variables, printStream);
72 }
73 }