View Javadoc

1   package liquibase.precondition.core;
2   
3   import liquibase.changelog.DatabaseChangeLog;
4   import liquibase.changelog.ChangeSet;
5   import liquibase.database.Database;
6   import liquibase.exception.PreconditionErrorException;
7   import liquibase.exception.PreconditionFailedException;
8   import liquibase.exception.ValidationErrors;
9   import liquibase.exception.Warnings;
10  import liquibase.precondition.Precondition;
11  
12  /**
13   * Precondition that checks the name of the user executing the change log.
14   */
15  public class RunningAsPrecondition implements Precondition {
16  
17      private String username;
18  
19      public RunningAsPrecondition() {
20          username = "";
21      }
22  
23      public void setUsername(String aUserName) {
24          username = aUserName;
25      }
26  
27      public String getUsername() {
28          return username;
29      }
30  
31      public Warnings warn(Database database) {
32          return new Warnings();
33      }
34  
35      public ValidationErrors validate(Database database) {
36          return new ValidationErrors();
37      }
38  
39      public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet)
40              throws PreconditionFailedException, PreconditionErrorException {
41          String loggedusername = database.getConnection().getConnectionUserName();
42          if (loggedusername.indexOf('@') >= 0) {
43              loggedusername = loggedusername.substring(0, loggedusername.indexOf('@'));
44          }
45          if (!username.equalsIgnoreCase(loggedusername)) {
46              throw new PreconditionFailedException("RunningAs Precondition failed: expected " + username + ", was "
47                      + loggedusername, changeLog, this);
48          }
49      }
50  
51      public String getName() {
52          return "runningAs";
53      }
54  
55  }