1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.torque.mojo;
17
18 import java.util.Properties;
19
20 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.settings.Server;
22 import org.kuali.db.DatabaseCommand;
23
24 import static org.apache.commons.lang.StringUtils.*;
25
26
27
28
29 public abstract class AbstractDBACommandMojo extends AbstractSQLExecutorMojo {
30 public static final String DATABASE_PROPERTY = "database";
31 public static final String DATABASE_PW_PROPERTY = "databasePassword";
32 public static final String DATABASE_USERNAME_PROPERTY = "databaseUsername";
33
34
35
36
37
38
39
40
41 String serverUrl;
42
43
44
45
46
47
48
49
50
51
52
53 String database;
54
55
56
57
58
59
60
61
62
63
64
65
66 String databaseUser;
67
68
69
70
71
72
73
74
75
76
77
78 String databasePassword;
79
80
81
82
83
84
85
86
87 String dbaSettingsKey;
88
89 protected String getTransactionDescription(DatabaseCommand command) {
90 return command + " " + getDatabase();
91 }
92
93 protected void updateConfiguration() throws MojoExecutionException {
94 super.updateConfiguration();
95 if (isEmpty(database)) {
96 database = platform.getSchemaName(getProject().getArtifactId());
97 }
98 if (isEmpty(databasePassword)) {
99 databasePassword = platform.getSchemaName(getProject().getArtifactId());
100 }
101 if (isEmpty(databaseUser)) {
102 databaseUser = platform.getSchemaName(getProject().getArtifactId());
103 }
104 if (isEmpty(serverUrl)) {
105 serverUrl = platform.getServerUrl(url);
106 }
107 }
108
109 @Override
110 protected String getUpdatedPassword(Server server, String password) {
111
112 if (!isEmpty(password)) {
113 return password;
114 }
115 if (server != null) {
116
117 getLog().info("Located a password in settings.xml under the server id '" + server.getId() + "' Password: " + getDisplayPassword(server.getPassword()));
118 return server.getPassword();
119 }
120
121 return null;
122 }
123
124 @Override
125 protected String getUpdatedUsername(Server server, String username) {
126
127 if (!isEmpty(username)) {
128 return username;
129 }
130 if (server != null) {
131
132 getLog().info("Located a username in settings.xml under the server id '" + server.getId() + "' Username: " + server.getUsername());
133 return server.getUsername();
134 }
135
136 return null;
137 }
138
139 @Override
140 protected Properties getContextProperties() {
141 Properties properties = super.getContextProperties();
142 properties.setProperty(DATABASE_PROPERTY, getDatabase());
143 properties.setProperty(DATABASE_PW_PROPERTY, getDatabasePassword());
144 properties.setProperty(DATABASE_USERNAME_PROPERTY, getDatabaseUser());
145 return properties;
146 }
147
148 @Override
149 protected Server getServerFromSettingsKey() {
150 Server server = getSettings().getServer(dbaSettingsKey);
151 if (server != null) {
152 return server;
153 }
154
155 String settingsKey = "impex.dba." + getUrl();
156 return getSettings().getServer(settingsKey);
157 }
158
159 @Override
160 protected void validateConfiguration() throws MojoExecutionException {
161 super.validateConfiguration();
162 if (isEmpty(database)) {
163 throw new MojoExecutionException("\n\nNo database was specified.\nSpecify a database in the plugin configuration or as a system property.\n\nFor example:\n-Ddatabase=MYDB\n\n.");
164 }
165 }
166
167 public String getDatabase() {
168 return database;
169 }
170
171 public void setDatabase(String schema) {
172 this.database = schema;
173 }
174
175 public String getDatabasePassword() {
176 return databasePassword;
177 }
178
179 public void setDatabasePassword(String databasePassword) {
180 this.databasePassword = databasePassword;
181 }
182
183 public String getDatabaseUser() {
184 return databaseUser;
185 }
186
187 public void setDatabaseUser(String databaseUsername) {
188 this.databaseUser = databaseUsername;
189 }
190
191 public String getServerUrl() {
192 return serverUrl;
193 }
194
195 public void setServerUrl(String serverUrl) {
196 this.serverUrl = serverUrl;
197 }
198
199 public String getDbaSettingsKey() {
200 return dbaSettingsKey;
201 }
202
203 public void setDbaSettingsKey(String dbaSettingsKey) {
204 this.dbaSettingsKey = dbaSettingsKey;
205 }
206 }