TSK-960: Refactor initTaskanaRoles

This commit is contained in:
Benjamin Eckstein 2019-11-19 08:56:28 +01:00 committed by Mustapha Zorgati
parent 76bbc94ad2
commit 518e55d75d
2 changed files with 33 additions and 35 deletions

View File

@ -1,5 +1,11 @@
package pro.taskana;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import pro.taskana.exceptions.SystemException;
/**
* This enum contains all roles that are known to taskana.
*/
@ -16,17 +22,17 @@ public enum TaskanaRole {
}
public static TaskanaRole fromPropertyName(String name) {
if (USER.propertyName.equalsIgnoreCase(name)) {
return TaskanaRole.USER;
} else if (BUSINESS_ADMIN.propertyName.equalsIgnoreCase(name)) {
return TaskanaRole.BUSINESS_ADMIN;
} else if (ADMIN.propertyName.equalsIgnoreCase(name)) {
return TaskanaRole.ADMIN;
} else if (MONITOR.propertyName.equalsIgnoreCase(name)) {
return TaskanaRole.MONITOR;
} else {
return null;
}
return Arrays.stream(TaskanaRole.values())
.filter(x -> x.propertyName.equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new SystemException(
"Internal System error when processing role property " + name));
}
public static List<String> getValidPropertyNames() {
return Arrays.stream(values())
.map(TaskanaRole::getPropertyName)
.collect(Collectors.toList());
}
public String getPropertyName() {

View File

@ -12,6 +12,7 @@ import java.time.Instant;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -224,7 +225,7 @@ public class TaskanaEngineConfiguration {
LOGGER.debug("CleanupJob configuration: first run at {}", cleanupJobFirstRun);
LOGGER.debug("CleanupJob configuration: runs every {}", cleanupJobRunEvery);
LOGGER.debug("CleanupJob configuration: minimum age of tasks to be cleanup up is {}",
cleanupJobMinimumAge);
cleanupJobMinimumAge);
LOGGER.debug("TaskCleanupJob configuration: all completed task with the same parent business property id {}",
taskCleanupJobAllCompletedSameParentBusiness);
}
@ -298,36 +299,27 @@ public class TaskanaEngineConfiguration {
}
private void initTaskanaRoles(Properties props, String rolesSeparator) {
List<String> validPropertyNames = Arrays.stream(TaskanaRole.values())
.map(TaskanaRole::getPropertyName)
.collect(Collectors.toList());
for (Object obj : props.keySet()) {
String propertyName = ((String) obj);
if (validPropertyNames.contains(propertyName.toLowerCase().trim())) {
String propertyValue = props.getProperty(propertyName);
Set<String> roleMemberSet = new HashSet<>();
StringTokenizer st = new StringTokenizer(propertyValue, rolesSeparator);
while (st.hasMoreTokens()) {
String token = st.nextToken().toLowerCase().trim();
roleMemberSet.add(token);
}
TaskanaRole key = TaskanaRole.fromPropertyName(propertyName);
if (key != null) {
roleMap.put(key, roleMemberSet);
} else {
LOGGER.error("Internal System error when processing role property {}.", propertyName);
throw new SystemException(
"Internal System error when processing role property " + propertyName);
}
}
}
List<String> validPropertyNames = TaskanaRole.getValidPropertyNames();
props.keySet()
.stream()
.map(String::valueOf)
.filter(propertyName -> validPropertyNames.contains(propertyName.toLowerCase().trim()))
.forEach(validPropertyName -> roleMap.put(TaskanaRole.fromPropertyName(validPropertyName),
getTokensWithCollection(props.getProperty(validPropertyName), rolesSeparator)));
ensureRoleMapIsFullyInitialized();
if (LOGGER.isDebugEnabled()) {
roleMap.forEach(
(k, v) -> LOGGER.debug("Found Taskana RoleConfig {} : {} ", k, LoggerUtils.setToString(v)));
}
}
private HashSet<String> getTokensWithCollection(String str, String rolesSeparator) {
return Collections.list(new StringTokenizer(str, rolesSeparator)).stream()
.map(token -> String.valueOf(token).toLowerCase().trim())
.collect(Collectors.toCollection(HashSet::new));
}
private Properties readPropertiesFromFile(String propertiesFile) {