001 /** 002 * Copyright 2004-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.torque.mojo; 017 018 import java.util.Properties; 019 020 import org.apache.maven.plugin.MojoExecutionException; 021 import org.apache.maven.settings.Server; 022 import org.kuali.db.DatabaseCommand; 023 024 import static org.apache.commons.lang.StringUtils.*; 025 026 /** 027 * Common logic for running SQL commands on a database 028 */ 029 public abstract class AbstractDBACommandMojo extends AbstractSQLExecutorMojo { 030 public static final String DATABASE_PROPERTY = "database"; 031 public static final String DATABASE_PW_PROPERTY = "databasePassword"; 032 public static final String DATABASE_USERNAME_PROPERTY = "databaseUsername"; 033 034 /** 035 * URL to connect directly to the database server itself (ie: no database specified). This is optional as it is 036 * automatically detected in almost all cases from the <code>url</code>. If <code>serverUrl</code> is explicitly 037 * supplied it overrides the <code>serverUrl</code> chosen by the automatic detection logic. 038 * 039 * @parameter expression="${serverUrl}" 040 */ 041 String serverUrl; 042 043 /** 044 * The name of the database to DROP/CREATE. If not specified, this defaults to a database name that is compatible 045 * with ${targetDatabase} based on platform specific logic that converts the artifact id.<br> 046 * <br> 047 * 048 * For example:<br> 049 * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL) 050 * 051 * @parameter expression="${database}" 052 */ 053 String database; 054 055 /** 056 * The user to DROP/CREATE when issuing DBA commands for creating/dropping a user. If not specified, this defaults 057 * to a user that is compatible with ${targetDatabase} based on platform specific logic that converts the artifact 058 * id.<br> 059 * <br> 060 * 061 * For example:<br> 062 * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL 063 * 064 * @parameter expression="${databaseUser}" 065 */ 066 String databaseUser; 067 068 /** 069 * The password for the user that is DROPPED/CREATED. If not specified, this defaults to a password that is 070 * compatible with ${targetDatabase} based on platform specific logic that converts the artifact id.<br> 071 * <br> 072 * 073 * For example:<br> 074 * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL 075 * 076 * @parameter expression="${databasePassword}" 077 */ 078 String databasePassword; 079 080 /** 081 * Lookup DBA credentials in settings.xml using this key. If nothing is found under 082 * <code>impex.dba.${project.artifactId}</code> a second attempt will be made to locate a set of credentials under 083 * <code>impex.dba.${url}</code> 084 * 085 * @parameter expression="${dbaSettingsKey}" default-value="impex.dba.${project.artifactId}" 086 */ 087 String dbaSettingsKey; 088 089 protected String getTransactionDescription(DatabaseCommand command) { 090 return command + " " + getDatabase(); 091 } 092 093 protected void updateConfiguration() throws MojoExecutionException { 094 super.updateConfiguration(); 095 if (isEmpty(database)) { 096 database = platform.getSchemaName(getProject().getArtifactId()); 097 } 098 if (isEmpty(databasePassword)) { 099 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 }