001    package org.apache.torque.mojo;
002    
003    import java.util.Properties;
004    
005    import org.apache.maven.plugin.MojoExecutionException;
006    import org.apache.maven.settings.Server;
007    import org.kuali.db.jdbc.DatabaseCommand;
008    
009    import static org.apache.commons.lang.StringUtils.*;
010    
011    /**
012     * Common logic for running SQL commands on a database
013     */
014    public abstract class AbstractDBACommandMojo extends AbstractSQLExecutorMojo {
015            public static final String DATABASE_PROPERTY = "database";
016            public static final String DATABASE_PW_PROPERTY = "databasePassword";
017            public static final String DATABASE_USERNAME_PROPERTY = "databaseUsername";
018    
019            /**
020             * URL to connect directly to the database server itself (ie: no database specified). This is optional as it is
021             * automatically detected in almost all cases from the <code>url</code>. If <code>serverUrl</code> is explicitly
022             * supplied it overrides the <code>serverUrl</code> chosen by the automatic detection logic.
023             * 
024             * @parameter expression="${serverUrl}"
025             */
026            String serverUrl;
027    
028            /**
029             * The name of the database to DROP/CREATE. If not specified, this defaults to a database name that is compatible
030             * with ${targetDatabase} based on platform specific logic that converts the artifact id.<br>
031             * <br>
032             * 
033             * For example:<br>
034             * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL)
035             * 
036             * @parameter expression="${database}"
037             */
038            String database;
039    
040            /**
041             * The user to DROP/CREATE when issuing DBA commands for creating/dropping a user. If not specified, this defaults
042             * to a user that is compatible with ${targetDatabase} based on platform specific logic that converts the artifact
043             * id.<br>
044             * <br>
045             * 
046             * For example:<br>
047             * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL
048             * 
049             * @parameter expression="${databaseUser}"
050             */
051            String databaseUser;
052    
053            /**
054             * The password for the user that is DROPPED/CREATED. If not specified, this defaults to a password that is
055             * compatible with ${targetDatabase} based on platform specific logic that converts the artifact id.<br>
056             * <br>
057             * 
058             * For example:<br>
059             * ks-embedded-db is converted to KSEMBEDDED for Oracle, and ksembedded for MySQL
060             * 
061             * @parameter expression="${databasePassword}"
062             */
063            String databasePassword;
064    
065            /**
066             * Lookup DBA credentials in settings.xml using this key. If nothing is found under
067             * <code>impex.dba.${project.artifactId}</code> a second attempt will be made to locate a set of credentials under
068             * <code>impex.dba.${url}</code>
069             * 
070             * @parameter expression="${dbaSettingsKey}" default-value="impex.dba.${project.artifactId}"
071             */
072            String dbaSettingsKey;
073    
074            protected String getTransactionDescription(DatabaseCommand command) {
075                    return command + " " + getDatabase();
076            }
077    
078            protected void updateConfiguration() throws MojoExecutionException {
079                    super.updateConfiguration();
080                    if (isEmpty(database)) {
081                            database = platform.getSchemaName(getProject().getArtifactId());
082                    }
083                    if (isEmpty(databasePassword)) {
084                            databasePassword = platform.getSchemaName(getProject().getArtifactId());
085                    }
086                    if (isEmpty(databaseUser)) {
087                            databaseUser = platform.getSchemaName(getProject().getArtifactId());
088                    }
089                    if (isEmpty(serverUrl)) {
090                            serverUrl = platform.getServerUrl(url);
091                    }
092            }
093    
094            @Override
095            protected String getUpdatedPassword(Server server, String password) {
096                    // They already gave us a password, don't mess with it
097                    if (!isEmpty(password)) {
098                            return password;
099                    }
100                    if (server != null) {
101                            // We've successfully located a server in settings.xml, use the password from that
102                            getLog().info("Located a password in settings.xml under the server id '" + server.getId() + "' Password: " + getDisplayPassword(server.getPassword()));
103                            return server.getPassword();
104                    }
105                    // Do not return a default value
106                    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                    if (!isEmpty(username)) {
113                            return username;
114                    }
115                    if (server != null) {
116                            // We've successfully located a server in settings.xml, use the username from that
117                            getLog().info("Located a username in settings.xml under the server id '" + server.getId() + "' Username: " + server.getUsername());
118                            return server.getUsername();
119                    }
120                    // Do not return a default value
121                    return null;
122            }
123    
124            @Override
125            protected Properties getContextProperties() {
126                    Properties properties = super.getContextProperties();
127                    properties.setProperty(DATABASE_PROPERTY, getDatabase());
128                    properties.setProperty(DATABASE_PW_PROPERTY, getDatabasePassword());
129                    properties.setProperty(DATABASE_USERNAME_PROPERTY, getDatabaseUser());
130                    return properties;
131            }
132    
133            @Override
134            protected Server getServerFromSettingsKey() {
135                    Server server = getSettings().getServer(dbaSettingsKey);
136                    if (server != null) {
137                            return server;
138                    }
139    
140                    String settingsKey = "impex.dba." + getUrl();
141                    return getSettings().getServer(settingsKey);
142            }
143    
144            @Override
145            protected void validateConfiguration() throws MojoExecutionException {
146                    super.validateConfiguration();
147                    if (isEmpty(database)) {
148                            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            }
151    
152            public String getDatabase() {
153                    return database;
154            }
155    
156            public void setDatabase(String schema) {
157                    this.database = schema;
158            }
159    
160            public String getDatabasePassword() {
161                    return databasePassword;
162            }
163    
164            public void setDatabasePassword(String databasePassword) {
165                    this.databasePassword = databasePassword;
166            }
167    
168            public String getDatabaseUser() {
169                    return databaseUser;
170            }
171    
172            public void setDatabaseUser(String databaseUsername) {
173                    this.databaseUser = databaseUsername;
174            }
175    
176            public String getServerUrl() {
177                    return serverUrl;
178            }
179    
180            public void setServerUrl(String serverUrl) {
181                    this.serverUrl = serverUrl;
182            }
183    
184            public String getDbaSettingsKey() {
185                    return dbaSettingsKey;
186            }
187    
188            public void setDbaSettingsKey(String dbaSettingsKey) {
189                    this.dbaSettingsKey = dbaSettingsKey;
190            }
191    }