01: package liquibase.preconditions;
02:
03: import liquibase.DatabaseChangeLog;
04: import liquibase.database.Database;
05: import liquibase.exception.PreconditionFailedException;
06:
07: import java.sql.SQLException;
08:
09: /**
10: * Precondition that checks the name of the user executing the change log.
11: */
12: public class RunningAsPrecondition implements Precondition {
13:
14: private String username;
15:
16: public RunningAsPrecondition() {
17: username = "";
18: }
19:
20: public void setUsername(String aUserName) {
21: username = aUserName;
22: }
23:
24: public String getUsername() {
25: return username;
26: }
27:
28: public void check(Database database, DatabaseChangeLog changeLog)
29: throws PreconditionFailedException {
30: try {
31: String loggedusername = database.getConnection()
32: .getMetaData().getUserName();
33: if (loggedusername.indexOf('@') >= 0) {
34: loggedusername = loggedusername.substring(0,
35: loggedusername.indexOf('@'));
36: }
37: if (!username.equalsIgnoreCase(loggedusername)) {
38: throw new PreconditionFailedException(
39: "RunningAs Precondition failed: expected "
40: + username + ", was " + loggedusername,
41: changeLog, this );
42: }
43: } catch (SQLException e) {
44: throw new RuntimeException("Cannot determine username", e);
45: }
46: }
47:
48: public String getTagName() {
49: return "runningAs";
50: }
51:
52: }
|