Coverage Report - org.apache.torque.mojo.AbstractDBACommandMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDBACommandMojo
0%
0/53
0%
0/20
1.941
 
 1  
 package org.apache.torque.mojo;
 2  
 
 3  
 import java.util.Properties;
 4  
 
 5  
 import org.apache.maven.plugin.MojoExecutionException;
 6  
 import org.apache.maven.settings.Server;
 7  
 import org.kuali.db.DatabaseCommand;
 8  
 
 9  
 import static org.apache.commons.lang.StringUtils.*;
 10  
 
 11  
 /**
 12  
  * Common logic for running SQL commands on a database
 13  
  */
 14  0
 public abstract class AbstractDBACommandMojo extends AbstractSQLExecutorMojo {
 15  
         public static final String DATABASE_PROPERTY = "database";
 16  
         public static final String DATABASE_PW_PROPERTY = "databasePassword";
 17  
         public static final String DATABASE_USERNAME_PROPERTY = "databaseUsername";
 18  
 
 19  
         /**
 20  
          * URL to connect directly to the database server itself (ie: no database specified). This is optional as it is
 21  
          * automatically detected in almost all cases from the <code>url</code>. If <code>serverUrl</code> is explicitly
 22  
          * supplied it overrides the <code>serverUrl</code> chosen by the automatic detection logic.
 23  
          * 
 24  
          * @parameter expression="${serverUrl}"
 25  
          */
 26  
         String serverUrl;
 27  
 
 28  
         /**
 29  
          * The name of the database to DROP/CREATE. If not specified, this defaults to a database name that is compatible
 30  
          * with ${targetDatabase} based on platform specific logic that converts the artifact id.<br>
 31  
          * <br>
 32  
          * 
 33  
          * For example:<br>
 34  
          * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL)
 35  
          * 
 36  
          * @parameter expression="${database}"
 37  
          */
 38  
         String database;
 39  
 
 40  
         /**
 41  
          * The user to DROP/CREATE when issuing DBA commands for creating/dropping a user. If not specified, this defaults
 42  
          * to a user that is compatible with ${targetDatabase} based on platform specific logic that converts the artifact
 43  
          * id.<br>
 44  
          * <br>
 45  
          * 
 46  
          * For example:<br>
 47  
          * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL
 48  
          * 
 49  
          * @parameter expression="${databaseUser}"
 50  
          */
 51  
         String databaseUser;
 52  
 
 53  
         /**
 54  
          * The password for the user that is DROPPED/CREATED. If not specified, this defaults to a password that is
 55  
          * compatible with ${targetDatabase} based on platform specific logic that converts the artifact id.<br>
 56  
          * <br>
 57  
          * 
 58  
          * For example:<br>
 59  
          * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL
 60  
          * 
 61  
          * @parameter expression="${databasePassword}"
 62  
          */
 63  
         String databasePassword;
 64  
 
 65  
         /**
 66  
          * Lookup DBA credentials in settings.xml using this key. If nothing is found under
 67  
          * <code>impex.dba.${project.artifactId}</code> a second attempt will be made to locate a set of credentials under
 68  
          * <code>impex.dba.${url}</code>
 69  
          * 
 70  
          * @parameter expression="${dbaSettingsKey}" default-value="impex.dba.${project.artifactId}"
 71  
          */
 72  
         String dbaSettingsKey;
 73  
 
 74  
         protected String getTransactionDescription(DatabaseCommand command) {
 75  0
                 return command + " " + getDatabase();
 76  
         }
 77  
 
 78  
         protected void updateConfiguration() throws MojoExecutionException {
 79  0
                 super.updateConfiguration();
 80  0
                 if (isEmpty(database)) {
 81  0
                         database = platform.getSchemaName(getProject().getArtifactId());
 82  
                 }
 83  0
                 if (isEmpty(databasePassword)) {
 84  0
                         databasePassword = platform.getSchemaName(getProject().getArtifactId());
 85  
                 }
 86  0
                 if (isEmpty(databaseUser)) {
 87  0
                         databaseUser = platform.getSchemaName(getProject().getArtifactId());
 88  
                 }
 89  0
                 if (isEmpty(serverUrl)) {
 90  0
                         serverUrl = platform.getServerUrl(url);
 91  
                 }
 92  0
         }
 93  
 
 94  
         @Override
 95  
         protected String getUpdatedPassword(Server server, String password) {
 96  
                 // They already gave us a password, don't mess with it
 97  0
                 if (!isEmpty(password)) {
 98  0
                         return password;
 99  
                 }
 100  0
                 if (server != null) {
 101  
                         // We've successfully located a server in settings.xml, use the password from that
 102  0
                         getLog().info("Located a password in settings.xml under the server id '" + server.getId() + "' Password: " + getDisplayPassword(server.getPassword()));
 103  0
                         return server.getPassword();
 104  
                 }
 105  
                 // Do not return a default value
 106  0
                 return null;
 107  
         }
 108  
 
 109  
         @Override
 110  
         protected String getUpdatedUsername(Server server, String username) {
 111  
                 // They already gave us a username, don't mess with it
 112  0
                 if (!isEmpty(username)) {
 113  0
                         return username;
 114  
                 }
 115  0
                 if (server != null) {
 116  
                         // We've successfully located a server in settings.xml, use the username from that
 117  0
                         getLog().info("Located a username in settings.xml under the server id '" + server.getId() + "' Username: " + server.getUsername());
 118  0
                         return server.getUsername();
 119  
                 }
 120  
                 // Do not return a default value
 121  0
                 return null;
 122  
         }
 123  
 
 124  
         @Override
 125  
         protected Properties getContextProperties() {
 126  0
                 Properties properties = super.getContextProperties();
 127  0
                 properties.setProperty(DATABASE_PROPERTY, getDatabase());
 128  0
                 properties.setProperty(DATABASE_PW_PROPERTY, getDatabasePassword());
 129  0
                 properties.setProperty(DATABASE_USERNAME_PROPERTY, getDatabaseUser());
 130  0
                 return properties;
 131  
         }
 132  
 
 133  
         @Override
 134  
         protected Server getServerFromSettingsKey() {
 135  0
                 Server server = getSettings().getServer(dbaSettingsKey);
 136  0
                 if (server != null) {
 137  0
                         return server;
 138  
                 }
 139  
 
 140  0
                 String settingsKey = "impex.dba." + getUrl();
 141  0
                 return getSettings().getServer(settingsKey);
 142  
         }
 143  
 
 144  
         @Override
 145  
         protected void validateConfiguration() throws MojoExecutionException {
 146  0
                 super.validateConfiguration();
 147  0
                 if (isEmpty(database)) {
 148  0
                         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.");
 149  
                 }
 150  0
         }
 151  
 
 152  
         public String getDatabase() {
 153  0
                 return database;
 154  
         }
 155  
 
 156  
         public void setDatabase(String schema) {
 157  0
                 this.database = schema;
 158  0
         }
 159  
 
 160  
         public String getDatabasePassword() {
 161  0
                 return databasePassword;
 162  
         }
 163  
 
 164  
         public void setDatabasePassword(String databasePassword) {
 165  0
                 this.databasePassword = databasePassword;
 166  0
         }
 167  
 
 168  
         public String getDatabaseUser() {
 169  0
                 return databaseUser;
 170  
         }
 171  
 
 172  
         public void setDatabaseUser(String databaseUsername) {
 173  0
                 this.databaseUser = databaseUsername;
 174  0
         }
 175  
 
 176  
         public String getServerUrl() {
 177  0
                 return serverUrl;
 178  
         }
 179  
 
 180  
         public void setServerUrl(String serverUrl) {
 181  0
                 this.serverUrl = serverUrl;
 182  0
         }
 183  
 
 184  
         public String getDbaSettingsKey() {
 185  0
                 return dbaSettingsKey;
 186  
         }
 187  
 
 188  
         public void setDbaSettingsKey(String dbaSettingsKey) {
 189  0
                 this.dbaSettingsKey = dbaSettingsKey;
 190  0
         }
 191  
 }