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