View Javadoc

1   package org.apache.torque.mojo;
2   
3   import java.util.Properties;
4   
5   import org.apache.cxf.common.util.StringUtils;
6   import org.apache.maven.plugin.MojoExecutionException;
7   import org.apache.maven.plugin.descriptor.PluginDescriptor;
8   import org.apache.torque.util.JdbcConfigurer;
9   import org.kuali.core.db.torque.DumpTask;
10  import org.kuali.core.db.torque.PropertyHandlingException;
11  import org.kuali.core.db.torque.StringFilter;
12  
13  /**
14   * Base Mojo for export related functionality
15   */
16  public abstract class ExportMojo extends AntTaskMojo {
17  
18      /**
19       * Additional properties for the JDBC driver
20       *
21       * @parameter
22       */
23      private Properties driverProperties;
24  
25      /**
26       * If true, files will be exported in a format that the Ant impex tasks can understand
27       *
28       * @parameter expression="${antCompatibilityMode}" default-value="false"
29       */
30      private boolean antCompatibilityMode;
31  
32      /**
33       * The Maven artifactId. Included here as a simple property of the mojo itself to facilitate usage of
34       * <code>BeanUtils.copyProperties()</code> to copy properties between Mojo's and Ant tasks
35       *
36       * @parameter expression="${artifactId}" default-value="${project.artifactId}"
37       * @required
38       */
39      private String artifactId;
40  
41      /**
42       * By default the version of the maven plugin being used during the build will be included as part of a comment in
43       * the XML. Set this to false to prevent this. Useful if you are committing files to SCM and don't want a change in
44       * the version of the plugin to show up as a change to the data.
45       *
46       * @parameter expression="${includeVersionInComment}" default-value="true"
47       * @required
48       */
49      private boolean includeVersionInComment;
50  
51      /**
52       * Additional comment that gets placed into the generated XML document. Anything included here gets appended to the
53       * default value <br>
54       * <b>Default value is:</b> Auto-generated by the maven-impex-plugin v1.0.2<br>
55       *
56       * @parameter expression="${comment}"
57       */
58      private String comment;
59  
60      /**
61       * Comma separated list of regular expressions for tables to include in the export
62       *
63       * @parameter expression="${includes}"
64       */
65      private String includes;
66  
67      /**
68       * Comma separated list of regular expressions for tables to exclude from the export
69       *
70       * @parameter expression="${excludes}"
71       */
72      private String excludes;
73  
74      /**
75       * The type of database we are targeting (eg oracle, mysql). This is optional if <code>url</code> is supplied as the
76       * database type will be automatically detected based on the <code>url</code>. If targetDatabase is explicitly
77       * supplied it will override the type selected by the automatic detection logic.
78       *
79       * @parameter expression="${targetDatabase}"
80       */
81      private String targetDatabase;
82  
83      /**
84       * The schema to export. This parameter is optional, as the schema to export is automatically derived from platform
85       * specific logic that converts the artifactId.<br>
86       * <br>
87       * For example:<br>
88       * ks-embedded-db becomes KSEMBEDDED for Oracle, and ksembedded for MySQL If <code>schema</code> is supplied, the
89       * supplied value will be used instead of the value derived from the artifactId
90       *
91       * @parameter expression="${schema}"
92       */
93      private String schema;
94  
95      /**
96       * Database driver classname. This parameter is optional, as the correct JDBC driver to use is detected from the
97       * <code>url</code> in almost all cases (works for Oracle, MySQL, Derby, PostGresSQL, DB2, H2, HSQL, SQL Server). If
98       * a driver is explicitly supplied, it will be used in place of the JDBC driver the automatic detection logic would
99       * have chosen.
100      *
101      * @parameter expression="${driver}"
102      */
103     private String driver;
104 
105     /**
106      * The connect URL of the database.
107      *
108      * @parameter expression="${url}"
109      * @required
110      */
111     private String url;
112 
113     /**
114      * The user name to connect to the database. If not specified this is automatically generated by platform specific
115      * logic for converting the artifact id.<br>
116      * <br>
117      * For example:<br>
118      * ks-embedded-db becomes KSEMBEDDED for Oracle and ksembedded for MySQL
119      *
120      * @parameter expression="${username}"
121      */
122     private String username;
123 
124     /**
125      * The password for the database user. If not specified this is automatically generated by platform specific logic
126      * for converting the artifact id.<br>
127      * <br>
128      * For example:<br>
129      * ks-embedded-db becomes KSEMBEDDED for Oracle and ksembedded for MySQL
130      *
131      * @parameter expression="${password}"
132      */
133     private String password;
134 
135     /**
136      * Returns the fully qualified class name of the database driver.
137      */
138     public String getDriver() {
139         return driver;
140     }
141 
142     /**
143      * Sets the fully qualified class name of the database driver.
144      *
145      * @param driver
146      *            the fully qualified class name of the database driver.
147      */
148     public void setDriver(final String driver) {
149         this.driver = driver;
150     }
151 
152     /**
153      * Returns the password of the database user.
154      *
155      * @return the password of the database user.
156      */
157     public String getPassword() {
158         return password;
159     }
160 
161     /**
162      * Sets the password of the database user.
163      *
164      * @param password
165      *            the password of the database user.
166      */
167     public void setPassword(final String password) {
168         this.password = password;
169     }
170 
171     /**
172      * Returns the connect URL to the database.
173      *
174      * @return the connect URL to the database.
175      */
176     public String getUrl() {
177         return url;
178     }
179 
180     /**
181      * Sets the connect URL to the database.
182      *
183      * @param url
184      *            the connect URL to the database.
185      */
186     public void setUrl(final String url) {
187         this.url = url;
188     }
189 
190     public String getUsername() {
191         return username;
192     }
193 
194     public void setUsername(final String username) {
195         this.username = username;
196     }
197 
198     public String getSchema() {
199         return schema;
200     }
201 
202     public void setSchema(final String schema) {
203         this.schema = schema;
204     }
205 
206     public String getTargetDatabase() {
207         return targetDatabase;
208     }
209 
210     public void setTargetDatabase(final String targetDatabase) {
211         this.targetDatabase = targetDatabase;
212     }
213 
214     public String getIncludes() {
215         return includes;
216     }
217 
218     public void setIncludes(final String includePatterns) {
219         this.includes = includePatterns;
220     }
221 
222     public String getExcludes() {
223         return excludes;
224     }
225 
226     public void setExcludes(final String excludePatterns) {
227         this.excludes = excludePatterns;
228     }
229 
230     public String getComment() {
231         return comment;
232     }
233 
234     public void setComment(final String comment) {
235         this.comment = comment;
236     }
237 
238     protected String getUpdatedComment() {
239         PluginDescriptor descriptor = (PluginDescriptor) this.getPluginContext().get("pluginDescriptor");
240         if (descriptor == null) {
241             // This is null for Maven 2.x
242             return " Auto-generated by the maven-impex-plugin " + getComment();
243         }
244         String name = descriptor.getName();
245         String version = descriptor.getVersion();
246         String comment = " Auto-generated by the " + name;
247         if (isIncludeVersionInComment()) {
248             comment += " v" + version + " ";
249         }
250         if (!StringUtils.isEmpty(getComment())) {
251             comment += getComment();
252         }
253         return comment;
254     }
255 
256     @Override
257     protected void configureTask() throws MojoExecutionException {
258         setComment(getUpdatedComment());
259         try {
260             JdbcConfigurer configurer = new JdbcConfigurer();
261             configurer.updateConfiguration(this);
262             configurer.validateConfiguration(this);
263         } catch (PropertyHandlingException e) {
264             throw new MojoExecutionException("Error handling properties", e);
265         }
266         super.configureTask();
267         DumpTask task = (DumpTask) super.getAntTask();
268         task.setIncludePatterns(StringFilter.getListFromCSV(getIncludes()));
269         task.setExcludePatterns(StringFilter.getListFromCSV(getExcludes()));
270         task.setDriverProperties(driverProperties);
271     }
272 
273     public String getArtifactId() {
274         return artifactId;
275     }
276 
277     public void setArtifactId(final String artifactId) {
278         this.artifactId = artifactId;
279     }
280 
281     public boolean isAntCompatibilityMode() {
282         return antCompatibilityMode;
283     }
284 
285     public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
286         this.antCompatibilityMode = antCompatibilityMode;
287     }
288 
289     /**
290      * @return the includeVersionInComment
291      */
292     public boolean isIncludeVersionInComment() {
293         return includeVersionInComment;
294     }
295 
296     /**
297      * @param includeVersionInComment
298      *            the includeVersionInComment to set
299      */
300     public void setIncludeVersionInComment(final boolean includeVersionInComment) {
301         this.includeVersionInComment = includeVersionInComment;
302     }
303 
304     public Properties getDriverProperties() {
305         return driverProperties;
306     }
307 
308     public void setDriverProperties(Properties driverProperties) {
309         this.driverProperties = driverProperties;
310     }
311 }