Skip to content

Commit

Permalink
Merge pull request #421 from wttech/reformat-message
Browse files Browse the repository at this point in the history
Reformat message
  • Loading branch information
dprzybyl authored Oct 30, 2023
2 parents dbb23d3 + abb4ebc commit 9275951
Show file tree
Hide file tree
Showing 31 changed files with 61 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public final class MessagingUtils {

private MessagingUtils() {
// intentionally empty
}

public static String createMessage(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.cognifide.apm.main.utils.MessagingUtils;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.slf4j.Logger;
Expand Down Expand Up @@ -64,8 +63,7 @@ private ActionResult process(Context context, boolean execute) {
try {
group = context.getCurrentGroup();
actionResult.setAuthorizable(group.getID());
LOGGER.info(String.format("Adding authorizables %s to group with id = %s",
StringUtils.join(authorizableIds, ", "), group.getID()));
LOGGER.info("Adding authorizables {} to group with id={}", String.join(", ", authorizableIds), group.getID());
} catch (RepositoryException | ActionExecutionException e) {
actionResult.logError(MessagingUtils.createMessage(e));
return actionResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ private ActionResult process(Context context, boolean execute) {
if (authorizable.isGroup()) {
ActionUtils.checkCyclicRelations(group, (Group) authorizable);
}
LOGGER.info(String.format("Adding Authorizable with id = %s to group with id = %s",
authorizable.getID(), group.getID()));
LOGGER.info("Adding Authorizable with id={} to group with id={}", authorizable.getID(), group.getID());

boolean flag = true;
if (execute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ private ActionResult process(Context context, boolean simulate) {
context.getSession().getNode(path);
PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
context.getValueFactory(), path, permissions, restrictions);
LOGGER.info(String.format("Adding permissions %s for authorizable with id = %s for path = %s %s",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
LOGGER.info("Adding permissions {} for authorizable with id={} for path={} {}",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions);
if (simulate) {
permissionActionHelper.checkPermissions(context.getAccessControlManager());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ private ActionResult process(Context context, boolean execute) {
if (context.isCompositeNodeStore() && PathUtils.isAppsOrLibsPath(path)) {
actionResult.changeStatus(Status.SKIPPED, "Skipped purging privileges for " + authorizable.getID() + " on " + path);
} else {
LOGGER.info(String.format("Purging privileges for authorizable with id=%s under path=%s",
authorizable.getID(), path));
LOGGER.info("Purging privileges for authorizable with id={} under path={}", authorizable.getID(), path);
if (execute) {
purge(context, actionResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ private ActionResult process(final Context context, boolean execute) {
if (context.isCompositeNodeStore() && PathUtils.isAppsOrLibsPath(path)) {
actionResult.changeStatus(Status.SKIPPED, "Skipped removing all privileges for " + authorizable.getID() + " on " + path);
} else {
LOGGER.info(String.format("Removing all priveleges for authorizable with id = %s on path = %s",
authorizable.getID(), path));
LOGGER.info("Removing all priveleges for authorizable with id={} on path={}", authorizable.getID(), path);
if (execute) {
removeAll(context, authorizable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,16 @@ public ActionResult process(Context context, boolean simulate) {
private void logMessage(ActionResult actionResult, Authorizable authorizable) throws RepositoryException {
if (!ignoreIfExists) {
if (authorizable instanceof Group) {
actionResult.logError(authorizableExists(authorizable.getID(), "Group"));
actionResult.logError(MessagingUtils.authorizableExists(authorizable.getID(), "Group"));
} else {
actionResult.logError(authorizableExists(authorizable.getID(), "User"));
actionResult.logError(MessagingUtils.authorizableExists(authorizable.getID(), "User"));
}
} else {
if (authorizable instanceof Group) {
actionResult.changeStatus(Status.SKIPPED, authorizableExists(authorizable.getID(), "Group"));
actionResult.changeStatus(Status.SKIPPED, MessagingUtils.authorizableExists(authorizable.getID(), "Group"));
} else {
actionResult.changeStatus(Status.SKIPPED, authorizableExists(authorizable.getID(), "User"));
actionResult.changeStatus(Status.SKIPPED, MessagingUtils.authorizableExists(authorizable.getID(), "User"));
}
}
}

private static String authorizableExists(String authorizableId, String type) {
return "Authorizable with id: " + authorizableId + " already exists, and is a " + type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.cognifide.apm.main.utils.MessagingUtils;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.Group;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -56,7 +55,7 @@ public ActionResult execute(Context context) {
private ActionResult process(Context context, boolean execute) {
ActionResult actionResult = context.createActionResult();

LOGGER.info(String.format("Removing groups with ids = %s", StringUtils.join(ids, ", ")));
LOGGER.info("Removing groups with ids={}", String.join(", ", ids));
for (String id : ids) {
try {
Group group = context.getAuthorizableManager().getGroup(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.cognifide.apm.main.utils.MessagingUtils;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -55,7 +54,7 @@ public ActionResult execute(Context context) {

private ActionResult process(Context context, boolean execute) {
ActionResult actionResult = context.createActionResult();
LOGGER.info(String.format("Removing users with ids = %s", StringUtils.join(ids, ", ")));
LOGGER.info("Removing users with ids={}", String.join(", ", ids));
for (String id : ids) {
try {
User user = context.getAuthorizableManager().getUser(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ private ActionResult process(Context context, boolean simulate) {
context.getSession().getNode(path);
PermissionActionHelper permissionActionHelper = new PermissionActionHelper(
context.getValueFactory(), path, permissions, restrictions);
LOGGER.info(String.format("Denying permissions %s for authorizable with id = %s for path = %s %s",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions));
LOGGER.info("Denying permissions {} for authorizable with id={} for path={} {}",
permissions.toString(), context.getCurrentAuthorizable().getID(), path, restrictions);
if (simulate) {
permissionActionHelper.checkPermissions(context.getAccessControlManager());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.cognifide.apm.main.utils.MessagingUtils;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.slf4j.Logger;
Expand Down Expand Up @@ -60,8 +59,7 @@ private ActionResult process(Context context, boolean execute) {
try {
group = context.getCurrentGroup();
actionResult.setAuthorizable(group.getID());
LOGGER.info(String.format("Removing authorizables %s from group with id = %s",
StringUtils.join(authorizableIds, ", "), group.getID()));
LOGGER.info("Removing authorizables {} from group with id={}", String.join(", ", authorizableIds), group.getID());
} catch (RepositoryException | ActionExecutionException e) {
actionResult.logError(MessagingUtils.createMessage(e));
return actionResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ActionResult detachMembersFromGroup() {

if (authorizable.isGroup()) {
final Group group = context.getCurrentGroup();
LOGGER.info(String.format("Removing all members of group with id = %s", group.getID()));
LOGGER.info("Removing all members of group with id={}", group.getID());
Iterator<Authorizable> groupMembers = getGroupMembers(actionResult, group);

detachAllMembers(actionResult, group, groupMembers);
Expand All @@ -71,8 +71,7 @@ public ActionResult detachAuthorizableFromParents() {
Authorizable currentAuthorizable = context.getCurrentAuthorizable();
Iterator<Group> groups = getGroupParents(actionResult, currentAuthorizable);

LOGGER.info(String.format("Removing all memberships of authorizable with id = %s",
currentAuthorizable.getID()));
LOGGER.info("Removing all memberships of authorizable with id={}", currentAuthorizable.getID());
detachFromParents(actionResult, currentAuthorizable, groups);
} catch (RepositoryException | ActionExecutionException e) {
actionResult.logError(MessagingUtils.createMessage(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public ActionResult process(Context context, boolean execute) {
try {
authorizable = context.getCurrentAuthorizable();
actionResult.setAuthorizable(authorizable.getID());
LOGGER.info(
String.format("Removing authorizable with id = %s from groups %s", authorizable.getID(),
groupIds));
LOGGER.info("Removing authorizable with id={} from groups {}", authorizable.getID(), String.join(", ", groupIds));
} catch (RepositoryException | ActionExecutionException e) {
actionResult.logError(MessagingUtils.createMessage(e));
return actionResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ private ActionResult process(final Context context, boolean simulate) {
try {
Authorizable authorizable = context.getCurrentAuthorizable();
actionResult.setAuthorizable(authorizable.getID());
LOGGER.info(String.format("Removing property %s from authorizable with id = %s", nameProperty,
authorizable.getID()));
LOGGER.info("Removing property {} from authorizable with id={}", nameProperty, authorizable.getID());
if (!simulate) {
authorizable.removeProperty(nameProperty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private ActionResult process(final Context context, boolean execute) {
try {
User user = context.getCurrentUser();
actionResult.setAuthorizable(user.getID());
LOGGER.info(String.format("Setting password for user with id = %s", user.getID()));
LOGGER.info("Setting password for user with id={}", user.getID());
if (execute) {
user.changePassword(password);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,15 @@ private ActionResult process(final Context context, boolean simulate) {
try {
Authorizable authorizable = context.getCurrentAuthorizable();
actionResult.setAuthorizable(authorizable.getID());
LOGGER.info(String.format("Setting property %s for authorizable with id = %s", nameProperty,
authorizable.getID()));
LOGGER.info("Setting property {} for authorizable with id={}", nameProperty, authorizable.getID());
final Value value = context.getValueFactory().createValue(valueProperty);

if (!simulate) {
authorizable.setProperty(nameProperty, value);
}

actionResult.logMessage(
"Property " + nameProperty + " for " + authorizable.getID() + " added vith value: "
"Property " + nameProperty + " for " + authorizable.getID() + " added with value: "
+ valueProperty);
} catch (RepositoryException | ActionExecutionException e) {
actionResult.logError(MessagingUtils.createMessage(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
*/
package com.cognifide.apm.main.utils;

import com.cognifide.apm.api.scripts.Script;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

public final class MessagingUtils {

private MessagingUtils() {
// intentionally empty
}

public static String createMessage(Exception e) {
Expand All @@ -54,8 +53,8 @@ public static String addingGroupToItself(String groupId) {
return "You can not add group " + groupId + " to itself";
}

public static String authorizableNotExists(String authorizableId) {
return "Authorizable with id: " + authorizableId + " does not exists";
public static String authorizableExists(String authorizableId, String type) {
return "Authorizable with id: " + authorizableId + " already exists, and is a " + type;
}

public static String groupHasNoMembers(String groupId) {
Expand All @@ -72,31 +71,7 @@ public static String cyclicRelationsForbidden(String currentGroup, String groupT
}

public static String unknownPermissions(List<String> permissions) {
if (permissions.size() == 1) {
return "Unknown permission: " + permissions.get(0);
}
StringBuilder result = new StringBuilder();
result.append("Unknown permissions: ");
Iterator<String> it = permissions.iterator();

while (it.hasNext()) {
result.append(it.next());
if (it.hasNext()) {
result.append(", ");
}
}

return result.toString();
return permissions.stream()
.collect(Collectors.joining(", ", "Unknown permissions: ", ""));
}

public static String describeScripts(List<Script> scripts) {
List<String> paths = new LinkedList<>();

for (Script script : scripts) {
paths.add(script.getPath());
}

return StringUtils.join(paths, "\n");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ interface Entry {

List<String> getMessages();

List<String> getPrettyMessages();

List<String> getParameters();

List<String> getPrettyParameters();

Status getStatus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public Status visitForEach(ForEachContext ctx) {
String valueStr = value.entrySet()
.stream()
.map(entry -> String.format("%s=%s", entry.getKey(), entry.getValue()))
.collect(Collectors.joining());
.collect(Collectors.joining("\n"));
progress(ctx, Status.SUCCESS, "for-each", String.format("%d. Begin: %s", index, valueStr));
value.forEach(executionContext::setVariable);
visit(ctx.body());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import com.cognifide.apm.api.services.ExecutionMode;
import com.cognifide.apm.api.services.ExecutionResult;
import com.cognifide.apm.api.services.ScriptManager;
import com.cognifide.apm.core.utils.MessagingUtils;
import java.util.List;
import java.util.stream.Collectors;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceResolver;
Expand All @@ -41,8 +41,10 @@ public AbstractLauncher() {

protected void processScripts(List<Script> scripts, ResourceResolver resolver) throws PersistenceException {
if (!scripts.isEmpty()) {
logger.info("Launcher will try to run following scripts: {}", scripts.size());
logger.info(MessagingUtils.describeScripts(scripts));
String scriptPathsStr = scripts.stream()
.map(Script::getPath)
.collect(Collectors.joining("\n"));
logger.info("Launcher will try to run following scripts: {}\n{}", scripts.size(), scriptPathsStr);
for (Script script : scripts) {
processScript(script, resolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.collect.ImmutableList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ProgressEntry implements Entry {

Expand Down Expand Up @@ -70,11 +71,25 @@ public List<String> getMessages() {
return messages;
}

@Override
public List<String> getPrettyMessages() {
return messages.stream()
.map(message -> message.replaceAll("\n", "<br>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"))
.collect(Collectors.toList());
}

@Override
public List<String> getParameters() {
return parameters;
}

@Override
public List<String> getPrettyParameters() {
return parameters.stream()
.map(message -> message.replaceAll("\n", "<br>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;"))
.collect(Collectors.toList());
}

@Override
public Status getStatus() {
return status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void afterCreated() {
try {
scriptManager.process(this, ExecutionMode.VALIDATION, resource.getResourceResolver());
} catch (RepositoryException | PersistenceException e) {
LOGGER.error("", e);
LOGGER.error(e.getMessage(), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public List<Script> findAll(Predicate<Script> filter, ResourceResolver resolver)
modified.add(script);
}
} catch (ScriptExecutionException e) {
LOGGER.error(e.getMessage());
LOGGER.error(e.getMessage(), e);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void updateVersionIfNeeded(ResourceResolver resolver, Script... scripts)
createVersion(resolver, script);
}
} catch (ScriptExecutionException | PersistenceException e) {
LOGGER.error(e.getMessage());
LOGGER.error(e.getMessage(), e);
}
}
}
Expand Down
Loading

0 comments on commit 9275951

Please sign in to comment.