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