* @param principalToAdd Principal of interest
*/
protected void addAcl(List<Acl> existingAcls, char op, String principalToAdd) {
removeAcl(existingAcls, op, principalToAdd);
Acl firstAcl = (existingAcls.size() > 0) ? existingAcls.get(0) : new Acl();
if (existingAcls.size() == 0) {
existingAcls.add(firstAcl);
}
boolean opFound = false;
if (firstAcl.getAuthorizedEntities() != null) {
for (AuthorizedEntity authorizedEntity: firstAcl.getAuthorizedEntities()) {
if (authorizedEntity.getOperation().charAt(0) == op) {
opFound = true;
String[] currentPrincipals = authorizedEntity.getEntities();
if (currentPrincipals == null) {
authorizedEntity.setEntities(new String[]{principalToAdd});
} else {
String[] updatedPrincipals = new String[currentPrincipals.length + 1];
for (int i = 0; i < currentPrincipals.length; i++) {
updatedPrincipals[i] = currentPrincipals[i];
}
updatedPrincipals[currentPrincipals.length] = principalToAdd;
authorizedEntity.setEntities(updatedPrincipals);
}
}
}
}
if (!opFound) {
int currentLength = firstAcl.getAuthorizedEntities().length;
AuthorizedEntity[] updatedAuthorizedEntities =
new AuthorizedEntity[currentLength + 1];
for (int i = 0; i < currentLength; i++) {
updatedAuthorizedEntities[i] = firstAcl.getAuthorizedEntities()[i];
}
AuthorizedEntity authorizedEntity = new AuthorizedEntity();
authorizedEntity.setOperation(authorizedEntity.lookupOperation(op));
authorizedEntity.setEntities(new String[]{principalToAdd});
updatedAuthorizedEntities[currentLength] = authorizedEntity;
firstAcl.setAuthorizedEntities(updatedAuthorizedEntities);
}
}