View Javadoc
1   /*
2    *    Copyright 2010-2022 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.mybatis.maven.mvnmigrate;
17  
18  import java.io.File;
19  import java.io.PrintStream;
20  import java.text.MessageFormat;
21  import java.util.Locale;
22  import java.util.ResourceBundle;
23  
24  import org.apache.ibatis.migration.commands.BaseCommand;
25  import org.apache.ibatis.migration.options.SelectedOptions;
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.mybatis.maven.mvnmigrate.util.MavenOutputStream;
31  
32  /**
33   * Provides to an abstract class that extends {@link AbstractMojo}.
34   */
35  abstract class AbstractCommandMojo<T extends BaseCommand> extends AbstractMojo {
36  
37    private final Locale locale = Locale.ENGLISH;
38  
39    /**
40     * Location of migrate repository.
41     */
42    @Parameter(property = "migration.path", defaultValue = ".")
43    private File repository;
44  
45    /**
46     * Environment to configure. Default environment is 'development'.
47     */
48    @Parameter(property = "migration.env", defaultValue = "development")
49    private String environment;
50  
51    /**
52     * Forces script to continue even if SQL errors are encountered.
53     */
54    @Parameter(property = "migration.force", defaultValue = "false")
55    private boolean force;
56  
57    /**
58     * Skip migration actions.
59     */
60    @Parameter(property = "migration.skip", defaultValue = "false")
61    private boolean skip;
62  
63    /**
64     * The command to execute.
65     */
66    private T command;
67  
68    /**
69     * execute the command.
70     */
71    @Override
72    public void execute() throws MojoExecutionException, MojoFailureException {
73      if (this.isSkip()) {
74        return;
75      }
76      this.init();
77      this.command.execute();
78    }
79  
80    /**
81     * Initialize the MyBatis Migration command.
82     */
83    protected void init() throws MojoFailureException {
84      try {
85        final SelectedOptions options = new SelectedOptions();
86        options.getPaths().setBasePath(this.getRepository());
87        options.setEnvironment(this.getEnvironment());
88        options.setForce(this.isForce());
89  
90        this.command = this.createCommandClass(options);
91        final PrintStream out = new PrintStream(new MavenOutputStream(this.getLog()));
92        this.command.setPrintStream(out);
93        this.command.setDriverClassLoader(this.getClass().getClassLoader());
94  
95        if (this.getLog().isInfoEnabled()) {
96          final String[] args = { this.command.getClass().getSimpleName(),
97              this.getBundle(this.locale).getString("migration.plugin.name") };
98          final MessageFormat format = new MessageFormat(
99              this.getBundle(this.locale).getString("migration.plugin.execution.command"));
100         this.getLog().info(format.format(args));
101       }
102     } catch (final RuntimeException e) {
103       throw e;
104     } catch (final Exception e) {
105       throw new MojoFailureException(this, e.getMessage(), e.getLocalizedMessage());
106     }
107   }
108 
109   protected Locale getLocale() {
110     return this.locale;
111   }
112 
113   protected File getRepository() {
114     return this.repository;
115   }
116 
117   protected String getEnvironment() {
118     return this.environment;
119   }
120 
121   protected boolean isForce() {
122     return this.force;
123   }
124 
125   /**
126    * Return the command.
127    *
128    * @return {@link BaseCommand} the command created.
129    */
130   protected T getCommand() {
131     return this.command;
132   }
133 
134   /**
135    * Test if the skip flag is setted.
136    *
137    * @return the skip flag.
138    */
139   protected boolean isSkip() {
140     if (this.skip && this.getLog().isInfoEnabled()) {
141       final String[] args = { this.getBundle(this.locale).getString("migration.plugin.name") };
142       final MessageFormat format = new MessageFormat(
143           this.getBundle(this.locale).getString("migration.plugin.execution.command.skipped"));
144       this.getLog().info(format.format(args));
145     }
146     return this.skip;
147   }
148 
149   /**
150    * The current locale.
151    *
152    * @param locale
153    *          the locale
154    *
155    * @return the bundle
156    */
157   protected ResourceBundle getBundle(final Locale locale) {
158     return ResourceBundle.getBundle("migration-plugin", locale, this.getClass().getClassLoader());
159   }
160 
161   /**
162    * Creates the specific mojo command.
163    *
164    * @param options
165    *          the options
166    *
167    * @return The command created.
168    */
169   protected abstract T createCommandClass(final SelectedOptions options);
170 
171 }