View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Common logic for running SQL commands on a database
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  	 * URL to connect directly to the database server itself (ie: no database specified). This is optional as it is
36  	 * automatically detected in almost all cases from the <code>url</code>. If <code>serverUrl</code> is explicitly
37  	 * supplied it overrides the <code>serverUrl</code> chosen by the automatic detection logic.
38  	 * 
39  	 * @parameter expression="${serverUrl}"
40  	 */
41  	String serverUrl;
42  
43  	/**
44  	 * The name of the database to DROP/CREATE. If not specified, this defaults to a database name that is compatible
45  	 * with ${targetDatabase} based on platform specific logic that converts the artifact id.<br>
46  	 * <br>
47  	 * 
48  	 * For example:<br>
49  	 * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL)
50  	 * 
51  	 * @parameter expression="${database}"
52  	 */
53  	String database;
54  
55  	/**
56  	 * The user to DROP/CREATE when issuing DBA commands for creating/dropping a user. If not specified, this defaults
57  	 * to a user that is compatible with ${targetDatabase} based on platform specific logic that converts the artifact
58  	 * id.<br>
59  	 * <br>
60  	 * 
61  	 * For example:<br>
62  	 * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL
63  	 * 
64  	 * @parameter expression="${databaseUser}"
65  	 */
66  	String databaseUser;
67  
68  	/**
69  	 * The password for the user that is DROPPED/CREATED. If not specified, this defaults to a password that is
70  	 * compatible with ${targetDatabase} based on platform specific logic that converts the artifact id.<br>
71  	 * <br>
72  	 * 
73  	 * For example:<br>
74  	 * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL
75  	 * 
76  	 * @parameter expression="${databasePassword}"
77  	 */
78  	String databasePassword;
79  
80  	/**
81  	 * Lookup DBA credentials in settings.xml using this key. If nothing is found under
82  	 * <code>impex.dba.${project.artifactId}</code> a second attempt will be made to locate a set of credentials under
83  	 * <code>impex.dba.${url}</code>
84  	 * 
85  	 * @parameter expression="${dbaSettingsKey}" default-value="impex.dba.${project.artifactId}"
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 		// They already gave us a password, don't mess with it
112 		if (!isEmpty(password)) {
113 			return password;
114 		}
115 		if (server != null) {
116 			// We've successfully located a server in settings.xml, use the password from that
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 		// Do not return a default value
121 		return null;
122 	}
123 
124 	@Override
125 	protected String getUpdatedUsername(Server server, String username) {
126 		// They already gave us a username, don't mess with it
127 		if (!isEmpty(username)) {
128 			return username;
129 		}
130 		if (server != null) {
131 			// We've successfully located a server in settings.xml, use the username from that
132 			getLog().info("Located a username in settings.xml under the server id '" + server.getId() + "' Username: " + server.getUsername());
133 			return server.getUsername();
134 		}
135 		// Do not return a default value
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 }