1 package org.apache.torque.mojo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.commons.lang.StringUtils.isEmpty;
23 import static org.apache.commons.lang.StringUtils.split;
24
25 import java.io.File;
26 import java.lang.reflect.InvocationTargetException;
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.util.Collection;
30 import java.util.Map;
31 import java.util.Properties;
32 import java.util.Vector;
33
34 import org.apache.commons.beanutils.BeanUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.settings.Server;
38 import org.apache.maven.shared.filtering.MavenFileFilter;
39 import org.apache.torque.engine.platform.Platform;
40 import org.apache.torque.engine.platform.PlatformFactory;
41 import org.apache.torque.util.JdbcConfigurer;
42 import org.apache.torque.util.MojoDatabaseListener;
43 import org.kuali.core.db.torque.PropertyHandlingException;
44 import org.kuali.core.db.torque.Utils;
45 import org.kuali.db.jdbc.ConnectionHandler;
46 import org.kuali.db.jdbc.Credentials;
47 import org.kuali.db.jdbc.JDBCUtils;
48 import org.kuali.db.jdbc.SQLExecutor;
49 import org.kuali.db.jdbc.Transaction;
50
51
52
53
54 public abstract class AbstractSQLExecutorMojo extends BaseMojo {
55 Utils utils = new Utils();
56 JDBCUtils jdbcUtils;
57 ConnectionHandler connectionHandler;
58 Platform platform;
59
60 public static final String DRIVER_INFO_PROPERTIES_USER = "user";
61 public static final String DRIVER_INFO_PROPERTIES_PASSWORD = "password";
62
63
64
65
66 public static final String FILE_SORTING_ASC = "ascending";
67
68
69
70
71 public static final String FILE_SORTING_DSC = "descending";
72
73
74
75
76
77
78
79
80
81
82 String targetDatabase;
83
84
85
86
87
88
89
90 String username;
91
92
93
94
95
96
97
98 String password;
99
100
101
102
103
104
105 boolean enableAnonymousPassword;
106
107
108
109
110
111
112 boolean enableAnonymousUsername;
113
114
115
116
117
118
119 String driverProperties;
120
121
122
123
124
125
126 boolean showPassword;
127
128
129
130
131
132
133 String settingsKey;
134
135
136
137
138
139
140
141 boolean skipOnConnectionError;
142
143
144
145
146
147
148 String sqlCommand = "";
149
150
151
152
153
154
155 File[] srcFiles;
156
157
158
159
160
161
162
163 String url;
164
165
166
167
168
169
170
171
172
173 String driver;
174
175
176
177
178
179
180
181 boolean autocommit;
182
183
184
185
186
187
188 String onError = SQLExecutor.ON_ERROR_ABORT;
189
190
191
192
193
194
195
196
197 String delimiter = "/";
198
199
200
201
202
203
204
205
206
207
208 String delimiterType = DelimiterType.ROW;
209
210
211
212
213
214
215 boolean keepFormat = true;
216
217
218
219
220
221
222 boolean showheaders = true;
223
224
225
226
227
228
229 boolean append = false;
230
231
232
233
234
235
236
237 boolean escapeProcessing = true;
238
239
240
241
242
243
244 int successfulStatements = 0;
245
246
247
248
249 int totalStatements = 0;
250
251
252
253
254 Connection conn = null;
255
256
257
258
259 Vector<Transaction> transactions = new Vector<Transaction>();
260
261
262
263
264 MavenFileFilter fileFilter;
265
266
267
268
269 Credentials credentials;
270
271 protected void configureTransactions() throws MojoExecutionException {
272
273 }
274
275 protected Properties getContextProperties() {
276 Properties properties = new Properties();
277 Map<String, String> environment = System.getenv();
278 for (String key : environment.keySet()) {
279 properties.put("env." + key, environment.get(key));
280 }
281 properties.putAll(getProject().getProperties());
282 properties.putAll(System.getProperties());
283 return properties;
284 }
285
286 protected Credentials getNewCredentials() {
287 Credentials credentials = new Credentials();
288 credentials.setUsername(getUsername());
289 credentials.setPassword(getPassword());
290 return credentials;
291 }
292
293 protected ConnectionHandler getNewConnectionHandler() throws MojoExecutionException {
294 ConnectionHandler connectionHandler = new ConnectionHandler();
295 try {
296 BeanUtils.copyProperties(connectionHandler, this);
297 return connectionHandler;
298 } catch (Exception e) {
299 throw new MojoExecutionException("Error establishing connection", e);
300 }
301 }
302
303
304
305
306
307
308 @Override
309 public void executeMojo() throws MojoExecutionException {
310 jdbcUtils = new JDBCUtils();
311 updateConfiguration();
312 Credentials credentials = getNewCredentials();
313 updateCredentials(credentials);
314 validateCredentials(credentials);
315 setCredentials(credentials);
316 validateConfiguration();
317
318 connectionHandler = getNewConnectionHandler();
319 conn = getConnection();
320
321 if (connectionHandler.isConnectionError() && skipOnConnectionError) {
322
323
324 return;
325 }
326
327
328 configureTransactions();
329
330
331 successfulStatements = 0;
332 totalStatements = 0;
333
334
335 SQLExecutor executor = getSqlExecutor();
336
337 try {
338 executor.execute();
339 } catch (SQLException e) {
340 throw new MojoExecutionException("Error executing SQL", e);
341 }
342 }
343
344
345
346
347
348
349
350 public void addText(final String sql) {
351 this.sqlCommand += sql;
352 }
353
354
355
356
357
358
359
360 public void setDelimiter(final String delimiter) {
361 this.delimiter = delimiter;
362 }
363
364
365
366
367
368
369
370 public void setDelimiterType(final String delimiterType) {
371 this.delimiterType = delimiterType;
372 }
373
374
375
376
377
378
379
380 public void setShowheaders(final boolean showheaders) {
381 this.showheaders = showheaders;
382 }
383
384
385
386
387
388
389
390 public void setAppend(final boolean append) {
391 this.append = append;
392 }
393
394
395
396
397
398
399
400 public void setKeepFormat(final boolean keepformat) {
401 this.keepFormat = keepformat;
402 }
403
404
405
406
407
408
409
410 public void setEscapeProcessing(final boolean enable) {
411 escapeProcessing = enable;
412 }
413
414 protected SQLExecutor getSqlExecutor() throws MojoExecutionException {
415 try {
416 SQLExecutor executor = new SQLExecutor();
417 BeanUtils.copyProperties(executor, this);
418 executor.addListener(new MojoDatabaseListener(getLog()));
419 return executor;
420 } catch (InvocationTargetException e) {
421 throw new MojoExecutionException("Error copying properties from the mojo to the SQL executor", e);
422 } catch (IllegalAccessException e) {
423 throw new MojoExecutionException("Error copying properties from the mojo to the SQL executor", e);
424 }
425 }
426
427
428
429
430
431 protected void updateConfiguration() throws MojoExecutionException {
432 try {
433 new JdbcConfigurer().updateConfiguration(this);
434 } catch (PropertyHandlingException e) {
435 throw new MojoExecutionException("Error handling properties", e);
436 }
437 platform = PlatformFactory.getPlatformFor(targetDatabase);
438 }
439
440
441
442
443 protected void validateConfiguration() throws MojoExecutionException {
444 new JdbcConfigurer().validateConfiguration(this);
445 }
446
447 protected void validateCredentials(final Credentials credentials, final boolean anonymousAccessAllowed,
448 final String validationFailureMessage) throws MojoExecutionException {
449 if (anonymousAccessAllowed) {
450
451 return;
452 }
453 String username = credentials.getUsername();
454 String password = credentials.getPassword();
455 if (!isEmpty(username) && !isEmpty(password)) {
456
457 return;
458 }
459 throw new MojoExecutionException(validationFailureMessage);
460 }
461
462 protected void validateCredentials(final Credentials credentials) throws MojoExecutionException {
463
464 StringBuffer sb = new StringBuffer();
465 sb.append("\n\n");
466 sb.append("Username and password must be specified.\n");
467 sb.append("Specify them in the plugin configuration or as a system property.\n");
468 sb.append("\n");
469 sb.append("For example:\n");
470 sb.append("-Dusername=myuser\n");
471 sb.append("-Dpassword=mypassword\n");
472 sb.append("\n.");
473 validateCredentials(credentials, enableAnonymousUsername && enableAnonymousPassword, sb.toString());
474 }
475
476 protected boolean isNullOrEmpty(final Collection<?> c) {
477 if (c == null) {
478 return true;
479 }
480 if (c.size() == 0) {
481 return true;
482 }
483 return false;
484 }
485
486 protected String convertNullToEmpty(final String s) {
487 if (s == null) {
488 return "";
489 } else {
490 return s;
491 }
492 }
493
494
495
496
497
498
499 protected void updateCredentials(final Credentials credentials) {
500 Server server = getServerFromSettingsKey();
501 String username = getUpdatedUsername(server, credentials.getUsername());
502 String password = getUpdatedPassword(server, credentials.getPassword());
503 credentials.setUsername(convertNullToEmpty(username));
504 credentials.setPassword(convertNullToEmpty(password));
505 }
506
507 protected Server getServerFromSettingsKey() {
508 Server server = getSettings().getServer(getSettingsKey());
509 if (server == null) {
510
511 return getSettings().getServer("impex." + getUrl());
512 } else {
513 return null;
514 }
515 }
516
517 protected String getUpdatedPassword(final Server server, final String password) {
518
519 if (!isEmpty(password)) {
520 return password;
521 }
522 if (server != null) {
523
524 getLog().info(
525 "Located a password in settings.xml under the server id '" + server.getId() + "' Password: "
526 + getDisplayPassword(server.getPassword()));
527 return server.getPassword();
528 }
529 getLog().info("Using default password generated from the artifact id");
530 return platform.getSchemaName(getProject().getArtifactId());
531 }
532
533 protected String getDisplayPassword(final String password) {
534 if (isShowPassword()) {
535 return password;
536 } else {
537 return StringUtils.repeat("*", password.length());
538 }
539 }
540
541 protected String getUpdatedUsername(final Server server, final String username) {
542
543 if (!isEmpty(username)) {
544 return username;
545 }
546 if (server != null) {
547
548 getLog().info(
549 "Located a username in settings.xml under the server id '" + server.getId() + "' Username: "
550 + server.getUsername());
551 return server.getUsername();
552 }
553 getLog().info("Using default username generated from the artifact id");
554 return platform.getSchemaName(getProject().getArtifactId());
555 }
556
557
558
559
560
561
562
563
564
565
566
567 protected Connection getConnection() throws MojoExecutionException {
568 try {
569 return connectionHandler.getConnection();
570 } catch (Exception e) {
571 throw new MojoExecutionException("Error establishing connection", e);
572 }
573 }
574
575
576
577
578
579
580
581 protected Properties getDriverProperties() throws MojoExecutionException {
582 Properties properties = new Properties();
583
584 if (isEmpty(this.driverProperties)) {
585 return properties;
586 }
587
588 String[] tokens = split(this.driverProperties, ",");
589 for (int i = 0; i < tokens.length; ++i) {
590 String[] keyValueTokens = split(tokens[i].trim(), "=");
591 if (keyValueTokens.length != 2) {
592 throw new MojoExecutionException("Invalid JDBC Driver properties: " + this.driverProperties);
593 }
594 properties.setProperty(keyValueTokens[0], keyValueTokens[1]);
595 }
596 return properties;
597 }
598
599 public String getUsername() {
600 return this.username;
601 }
602
603 public void setUsername(final String username) {
604 this.username = username;
605 }
606
607 public String getPassword() {
608 return this.password;
609 }
610
611 public void setPassword(final String password) {
612 this.password = password;
613 }
614
615 public String getUrl() {
616 return this.url;
617 }
618
619 public void setUrl(final String url) {
620 this.url = url;
621 }
622
623 public String getDriver() {
624 return this.driver;
625 }
626
627 public void setDriver(final String driver) {
628 this.driver = driver;
629 }
630
631 public void setAutocommit(final boolean autocommit) {
632 this.autocommit = autocommit;
633 }
634
635 public File[] getSrcFiles() {
636 return this.srcFiles;
637 }
638
639 public void setSrcFiles(final File[] files) {
640 this.srcFiles = files;
641 }
642
643
644
645
646
647
648 public int getSuccessfulStatements() {
649 return successfulStatements;
650 }
651
652
653
654
655
656
657 public int getTotalStatements() {
658 return totalStatements;
659 }
660
661 public String getOnError() {
662 return this.onError;
663 }
664
665 public void setOnError(final String action) {
666 if (SQLExecutor.ON_ERROR_ABORT.equalsIgnoreCase(action)) {
667 this.onError = SQLExecutor.ON_ERROR_ABORT;
668 } else if (SQLExecutor.ON_ERROR_CONTINUE.equalsIgnoreCase(action)) {
669 this.onError = SQLExecutor.ON_ERROR_CONTINUE;
670 } else if (SQLExecutor.ON_ERROR_ABORT_AFTER.equalsIgnoreCase(action)) {
671 this.onError = SQLExecutor.ON_ERROR_ABORT_AFTER;
672 } else {
673 throw new IllegalArgumentException(action + " is not a valid value for onError, only '"
674 + SQLExecutor.ON_ERROR_ABORT + "', '" + SQLExecutor.ON_ERROR_ABORT_AFTER + "', or '"
675 + SQLExecutor.ON_ERROR_CONTINUE + "'.");
676 }
677 }
678
679 public void setSettingsKey(final String key) {
680 this.settingsKey = key;
681 }
682
683 public void setDriverProperties(final String driverProperties) {
684 this.driverProperties = driverProperties;
685 }
686
687 public String getSqlCommand() {
688 return sqlCommand;
689 }
690
691 public void setSqlCommand(final String sqlCommand) {
692 this.sqlCommand = sqlCommand;
693 }
694
695 public Vector<Transaction> getTransactions() {
696 return transactions;
697 }
698
699 public void setTransactions(final Vector<Transaction> transactions) {
700 this.transactions = transactions;
701 }
702
703 public void setFileFilter(final MavenFileFilter filter) {
704 this.fileFilter = filter;
705 }
706
707 public String getTargetDatabase() {
708 return targetDatabase;
709 }
710
711 public void setTargetDatabase(final String targetDatabase) {
712 this.targetDatabase = targetDatabase;
713 }
714
715 public Connection getConn() {
716 return conn;
717 }
718
719 public void setConn(final Connection conn) {
720 this.conn = conn;
721 }
722
723 public String getDelimiter() {
724 return delimiter;
725 }
726
727 public String getDelimiterType() {
728 return delimiterType;
729 }
730
731 public boolean isKeepFormat() {
732 return keepFormat;
733 }
734
735 public boolean isShowheaders() {
736 return showheaders;
737 }
738
739 public boolean isAppend() {
740 return append;
741 }
742
743 public boolean isEscapeProcessing() {
744 return escapeProcessing;
745 }
746
747 public boolean isSkipOnConnectionError() {
748 return skipOnConnectionError;
749 }
750
751 public void setSkipOnConnectionError(final boolean skipOnConnectionError) {
752 this.skipOnConnectionError = skipOnConnectionError;
753 }
754
755 public MavenFileFilter getFileFilter() {
756 return fileFilter;
757 }
758
759 public boolean isShowPassword() {
760 return showPassword;
761 }
762
763 public void setShowPassword(final boolean showPassword) {
764 this.showPassword = showPassword;
765 }
766
767 public boolean isEnableAnonymousPassword() {
768 return enableAnonymousPassword;
769 }
770
771 public void setEnableAnonymousPassword(final boolean enableAnonymousPassword) {
772 this.enableAnonymousPassword = enableAnonymousPassword;
773 }
774
775 public String getSettingsKey() {
776 return settingsKey;
777 }
778
779 public boolean isAutocommit() {
780 return autocommit;
781 }
782
783 public void setSuccessfulStatements(final int successfulStatements) {
784 this.successfulStatements = successfulStatements;
785 }
786
787 public void setTotalStatements(final int totalStatements) {
788 this.totalStatements = totalStatements;
789 }
790
791 public void setCredentials(final Credentials credentials) {
792 this.credentials = credentials;
793 }
794
795 public Credentials getCredentials() {
796 return credentials;
797 }
798
799 }