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

View File

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