NIHVIVO-336 Improve the messages to StartupStatus.
This commit is contained in:
parent
c7899805e8
commit
b97b214e72
1 changed files with 43 additions and 20 deletions
|
@ -60,19 +60,26 @@ public class RootUserPolicy implements PolicyIface {
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
public static class Setup implements ServletContextListener {
|
public static class Setup implements ServletContextListener {
|
||||||
|
private ServletContext ctx;
|
||||||
|
private StartupStatus ss;
|
||||||
|
private String configRootEmail;
|
||||||
|
private UserAccountsDao uaDao;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void contextInitialized(ServletContextEvent sce) {
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
ServletContext ctx = sce.getServletContext();
|
ctx = sce.getServletContext();
|
||||||
StartupStatus ss = StartupStatus.getBean(ctx);
|
ss = StartupStatus.getBean(ctx);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
UserAccountsDao uaDao = getUserAccountsDao(ctx);
|
uaDao = getUserAccountsDao();
|
||||||
|
configRootEmail = getRootEmailFromConfig();
|
||||||
|
|
||||||
checkForWrongRootUser(ctx, uaDao);
|
checkForWrongRootUser();
|
||||||
|
|
||||||
if (!rootUserExists(uaDao)) {
|
if (rootUserExists()) {
|
||||||
createRootUser(ctx, uaDao);
|
ss.info(this, "root user is " + configRootEmail);
|
||||||
|
} else {
|
||||||
|
createRootUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
ServletPolicyList.addPolicy(ctx, new RootUserPolicy());
|
ServletPolicyList.addPolicy(ctx, new RootUserPolicy());
|
||||||
|
@ -81,7 +88,7 @@ public class RootUserPolicy implements PolicyIface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserAccountsDao getUserAccountsDao(ServletContext ctx) {
|
private UserAccountsDao getUserAccountsDao() {
|
||||||
WebappDaoFactory wadf = (WebappDaoFactory) ctx
|
WebappDaoFactory wadf = (WebappDaoFactory) ctx
|
||||||
.getAttribute("webappDaoFactory");
|
.getAttribute("webappDaoFactory");
|
||||||
if (wadf == null) {
|
if (wadf == null) {
|
||||||
|
@ -91,28 +98,43 @@ public class RootUserPolicy implements PolicyIface {
|
||||||
return wadf.getUserAccountsDao();
|
return wadf.getUserAccountsDao();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkForWrongRootUser(ServletContext ctx,
|
private String getRootEmailFromConfig() {
|
||||||
UserAccountsDao uaDao) {
|
String email = ConfigurationProperties.getBean(ctx).getProperty(
|
||||||
UserAccount root = getRootUser(uaDao);
|
PROPERTY_ROOT_USER_EMAIL);
|
||||||
|
if (email == null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"deploy.properties must contain a value for '"
|
||||||
|
+ PROPERTY_ROOT_USER_EMAIL + "'");
|
||||||
|
} else {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkForWrongRootUser() {
|
||||||
|
UserAccount root = getRootUser();
|
||||||
if (root == null) {
|
if (root == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String actualRootEmail = root.getEmailAddress();
|
|
||||||
|
|
||||||
String configRootEmail = ConfigurationProperties.getBean(ctx)
|
String actualRootEmail = root.getEmailAddress();
|
||||||
.getProperty(PROPERTY_ROOT_USER_EMAIL);
|
|
||||||
if (actualRootEmail.equals(configRootEmail)) {
|
if (actualRootEmail.equals(configRootEmail)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log.warn("Root user '" + actualRootEmail + "' already exists.");
|
ss.warning(
|
||||||
|
this,
|
||||||
|
"The deploy.properties file specifies a root user of '"
|
||||||
|
+ configRootEmail
|
||||||
|
+ "', but the system already contains a root user named '"
|
||||||
|
+ actualRootEmail + "'. The user '"
|
||||||
|
+ configRootEmail + "' will not be created.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean rootUserExists(UserAccountsDao uaDao) {
|
private boolean rootUserExists() {
|
||||||
return (getRootUser(uaDao) != null);
|
return (getRootUser() != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserAccount getRootUser(UserAccountsDao uaDao) {
|
private UserAccount getRootUser() {
|
||||||
for (UserAccount ua : uaDao.getAllUserAccounts()) {
|
for (UserAccount ua : uaDao.getAllUserAccounts()) {
|
||||||
if (ua.isRootUser()) {
|
if (ua.isRootUser()) {
|
||||||
return ua;
|
return ua;
|
||||||
|
@ -125,7 +147,7 @@ public class RootUserPolicy implements PolicyIface {
|
||||||
* TODO The first and last name should be left blank, so the user will
|
* TODO The first and last name should be left blank, so the user will
|
||||||
* be forced to edit them. However, that's not in place yet.
|
* be forced to edit them. However, that's not in place yet.
|
||||||
*/
|
*/
|
||||||
private void createRootUser(ServletContext ctx, UserAccountsDao uaDao) {
|
private void createRootUser() {
|
||||||
String emailAddress = ConfigurationProperties.getBean(ctx)
|
String emailAddress = ConfigurationProperties.getBean(ctx)
|
||||||
.getProperty(PROPERTY_ROOT_USER_EMAIL);
|
.getProperty(PROPERTY_ROOT_USER_EMAIL);
|
||||||
if (emailAddress == null) {
|
if (emailAddress == null) {
|
||||||
|
@ -159,7 +181,8 @@ public class RootUserPolicy implements PolicyIface {
|
||||||
|
|
||||||
uaDao.insertUserAccount(ua);
|
uaDao.insertUserAccount(ua);
|
||||||
|
|
||||||
log.info("Created root user as '" + emailAddress + "'");
|
StartupStatus.getBean(ctx).info(this,
|
||||||
|
"Created root user as '" + emailAddress + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Add table
Reference in a new issue