}
public String getAnonymousUser(Habitat habitat) {
String user = null;
// find the ADMIN_REALM
AuthRealm adminFileAuthRealm = null;
for (AuthRealm auth : domain.getConfigNamed(DAS_CONFIG).getSecurityService().getAuthRealm()) {
if (auth.getName().equals(ADMIN_REALM)) {
adminFileAuthRealm = auth;
break;
}
}
if (adminFileAuthRealm == null) {
// There must always be an admin realm
throw new IllegalStateException("Cannot find admin realm");
}
// Get FileRealm class name
String fileRealmClassName = adminFileAuthRealm.getClassname();
if (fileRealmClassName != null && !fileRealmClassName.equals(FILE_REALM_CLASSNAME)) {
// This condition can arise if admin-realm is not a File realm. Then the API to extract
// the anonymous user should be integrated for the logic below this line of code. for now,
// we treat this as an error and instead of throwing exception return false;
return null;
}
List<Property> props = adminFileAuthRealm.getProperty();
Property keyfileProp = null;
for (Property prop : props) {
if ("file".equals(prop.getName())) {
keyfileProp = prop;
}
}
if (keyfileProp == null) {
throw new IllegalStateException("Cannot find property 'file'");
}
String keyFile = keyfileProp.getValue();
if (keyFile == null) {
throw new IllegalStateException("Cannot find key file");
}
String[] usernames = getUserNames(adminFileAuthRealm.getName());
if (usernames.length == 1) {
try {
habitat.getByType(com.sun.enterprise.security.SecurityLifecycle.class);
LoginContextDriver.login(usernames[0], new char[0], ADMIN_REALM);
user = usernames[0];