Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Make post processors generic and be able to chain them #163

Merged
merged 1 commit into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.util.CollectionUtils;

import javax.sql.DataSource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

/**
Expand All @@ -47,17 +51,53 @@ public DemoConfiguration(TasksCommand tasksCommand) {
}

@Bean
public PostProcessor<String> quotePostProcessor() {
return new PostProcessor<String>() {
public PostProcessor<String, String> quotePostProcessor() {
return new PostProcessor<String, String>() {

@Override
public String getName() {
return "quote";
}

@Override
public String process(String result, List<String> parameters) {
return "'" + result + "'";
public String process(String input, List<String> parameters) {
return "'" + input + "'";
}
};
}

@Bean
public PostProcessor<String, ZonedDateTime> datePostProcessor() {
return new PostProcessor<String, ZonedDateTime>() {

@Override
public String getName() {
return "date";
}

@Override
public ZonedDateTime process(String input, List<String> parameters) {
return ZonedDateTime.parse(input);
}
};
}

@Bean
public PostProcessor<ZonedDateTime, ZonedDateTime> uctPostProcessor() {
return new PostProcessor<ZonedDateTime, ZonedDateTime>() {

@Override
public String getName() {
return "zoned";
}

@Override
public ZonedDateTime process(ZonedDateTime input, List<String> parameters) {
String zone = "UTC";
if (!CollectionUtils.isEmpty(parameters)) {
zone = parameters.get(0);
}
return input.withZoneSameInstant(ZoneId.of(zone));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
public class ExtendedShell
extends Shell {

@SuppressWarnings("rawtypes")
private final ResultHandler resultHandler;

private final List<String> postProcessorNames = new ArrayList<>();
Expand All @@ -56,7 +57,7 @@ public class ExtendedShell
* @param resultHandler result handler
* @param postProcessors post processors list
*/
public ExtendedShell(ResultHandler resultHandler, List<PostProcessor> postProcessors) {
public ExtendedShell(ResultHandler resultHandler, List<PostProcessor<?,?>> postProcessors) {
super(resultHandler);
this.resultHandler = resultHandler;
if (postProcessors != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void init() {

@Bean
@Primary
public ExtendedShell sshShell(@Qualifier("main") ResultHandler<Object> resultHandler, List<PostProcessor> postProcessors) {
public ExtendedShell sshShell(@Qualifier("main") ResultHandler<Object> resultHandler, List<PostProcessor<?,?>> postProcessors) {
return new ExtendedShell(new TypePostProcessorResultHandler(resultHandler, postProcessors), postProcessors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public class PostProcessorsCommand extends AbstractCommand {
public static final String GROUP = "postprocessors";
public static final String COMMAND_POST_PROCESSORS = "postprocessors";

private final List<PostProcessor> postProcessors;
private final List<PostProcessor<?, ?>> postProcessors;

public PostProcessorsCommand(SshShellHelper helper, SshShellProperties properties,
List<PostProcessor> postProcessors) {
List<PostProcessor<?, ?>> postProcessors) {
super(helper, properties, properties.getCommands().getPostprocessors());
this.postProcessors = new ArrayList<>(postProcessors);
this.postProcessors.sort(Comparator.comparing(PostProcessor::getName));
Expand All @@ -60,11 +60,14 @@ public PostProcessorsCommand(SshShellHelper helper, SshShellProperties propertie
public CharSequence postprocessors() {
AttributedStringBuilder result = new AttributedStringBuilder();
result.append("Available Post-Processors\n\n", AttributedStyle.BOLD);
for (PostProcessor postProcessor : postProcessors) {
result.append("\t" + postProcessor.getName() + ": ", AttributedStyle.BOLD);
Class<?> cls =
((Class) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
result.append(cls.getName() + "\n", AttributedStyle.DEFAULT);
for (PostProcessor<?, ?> postProcessor : postProcessors) {
result.append("\t" + postProcessor.getName() + ":\n", AttributedStyle.BOLD);
Class<?> input =
((Class<?>) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
Class<?> output =
((Class<?>) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[1]);
result.append("\t\tinput : " + input.getName() + "\n", AttributedStyle.DEFAULT);
result.append("\t\toutput : " + output.getName() + "\n", AttributedStyle.DEFAULT);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
/**
* Post processor interface
*/
public interface PostProcessor<T> {
public interface PostProcessor<I, O> {

String getName();

String process(T result, List<String> parameters) throws PostProcessorException;
O process(I result, List<String> parameters) throws PostProcessorException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public class TypePostProcessorResultHandler

public static final ThreadLocal<Throwable> THREAD_CONTEXT = ThreadLocal.withInitial(() -> null);

private ResultHandler<Object> resultHandler;
private final ResultHandler<Object> resultHandler;

private Map<String, PostProcessor> postProcessorMap = new HashMap<>();
private final Map<String, PostProcessor<?,?>> postProcessorMap = new HashMap<>();

public TypePostProcessorResultHandler(ResultHandler<Object> resultHandler, List<PostProcessor> postProcessorList) {
public TypePostProcessorResultHandler(ResultHandler<Object> resultHandler, List<PostProcessor<?,?>> postProcessorList) {
this.resultHandler = resultHandler;
if (postProcessorList != null) {
for (PostProcessor postProcessor : postProcessorList) {
for (PostProcessor<?,?> postProcessor : postProcessorList) {
if (this.postProcessorMap.containsKey(postProcessor.getName())) {
LOGGER.warn("Unable to register post processor for name [{}], it has already been registered",
postProcessor.getName());
Expand All @@ -54,6 +54,7 @@ public TypePostProcessorResultHandler(ResultHandler<Object> resultHandler, List<
}
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void handleResult(Object result) {
if (result == null) {
Expand All @@ -73,7 +74,7 @@ public void handleResult(Object result) {
continue;
}
Class<?> cls =
((Class) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
((Class<?>) ((ParameterizedType) (postProcessor.getClass().getGenericInterfaces())[0]).getActualTypeArguments()[0]);
if (!cls.isAssignableFrom(obj.getClass())) {
printLogWarn("Post processor [" + name + "] can only apply to class [" + cls.getName() +
"] (current object class is " + obj.getClass().getName() + ")");
Expand All @@ -82,7 +83,7 @@ public void handleResult(Object result) {
postProcessorObject.getParameters());
try {
obj = postProcessor.process(obj, postProcessorObject.getParameters());
} catch (PostProcessorException e) {
} catch (Exception e) {
printError(e.getMessage());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
@Slf4j
public class GrepPostProcessor
implements PostProcessor<String> {
implements PostProcessor<String, String> {

@Override
public String getName() {
Expand All @@ -45,7 +45,7 @@ public String process(String result, List<String> parameters) {
sb.append(line).append("\n");
}
}
return sb.toString().isEmpty() ? sb.toString() : sb.toString().substring(0, sb.toString().length() - 1);
return sb.toString().isEmpty() ? sb.toString() : sb.substring(0, sb.toString().length() - 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
@Slf4j
public class HighlightPostProcessor
implements PostProcessor<String> {
implements PostProcessor<String, String> {

@Override
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
@Slf4j
public class JsonPointerPostProcessor
implements PostProcessor<String> {
implements PostProcessor<String, String> {

private static final ObjectMapper MAPPER = new ObjectMapper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
@Slf4j
public class PrettyJsonPostProcessor
implements PostProcessor<Object> {
implements PostProcessor<Object, String> {

private static final ObjectMapper MAPPER = new ObjectMapper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

@Slf4j
public class SavePostProcessor
implements PostProcessor<Object> {
implements PostProcessor<Object, String> {

public static final String SAVE = "save";

Expand Down