From cf1a6f7839520eb19bbc18facef47af28a2bdfc3 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Thu, 9 Feb 2023 21:18:22 +0000 Subject: [PATCH] Add support defining Availability with annotated method - This is for new annotation model - @CommandAvailability which takes names of AvailabilityProvider beans supplying Availability info. - Relates #663 --- .../shell/AvailabilityProvider.java | 27 ++++ .../annotation/CommandAvailability.java | 42 ++++++ .../CommandRegistrationFactoryBean.java | 28 ++++ .../CommandRegistrationFactoryBeanTests.java | 32 +++++ .../samples/e2e/AvailabilityCommands.java | 123 ++++++++++++++++++ 5 files changed, 252 insertions(+) create mode 100644 spring-shell-core/src/main/java/org/springframework/shell/AvailabilityProvider.java create mode 100644 spring-shell-core/src/main/java/org/springframework/shell/command/annotation/CommandAvailability.java create mode 100644 spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/AvailabilityCommands.java diff --git a/spring-shell-core/src/main/java/org/springframework/shell/AvailabilityProvider.java b/spring-shell-core/src/main/java/org/springframework/shell/AvailabilityProvider.java new file mode 100644 index 00000000..7eebb0b5 --- /dev/null +++ b/spring-shell-core/src/main/java/org/springframework/shell/AvailabilityProvider.java @@ -0,0 +1,27 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell; + +import java.util.function.Supplier; + +/** + * Interface resolving {@link Availability}. + * + * @author Janne Valkealahti + */ +@FunctionalInterface +public interface AvailabilityProvider extends Supplier { +} diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/annotation/CommandAvailability.java b/spring-shell-core/src/main/java/org/springframework/shell/command/annotation/CommandAvailability.java new file mode 100644 index 00000000..8d5afd66 --- /dev/null +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/annotation/CommandAvailability.java @@ -0,0 +1,42 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.command.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.shell.Availability; + +/** + * Annotation marking a method having {@link Availability}. + * + * @author Janne Valkealahti + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@Documented +public @interface CommandAvailability { + + /** + * Names of supplier beans for {@link Availability}. + * + * @return names of supplier beans + */ + String[] name() default {}; +} diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBean.java b/spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBean.java index 0cab040d..0c9f92ac 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBean.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBean.java @@ -18,6 +18,8 @@ package org.springframework.shell.command.annotation.support; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,6 +36,8 @@ import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; import org.springframework.messaging.handler.invocation.InvocableHandlerMethod; +import org.springframework.shell.Availability; +import org.springframework.shell.AvailabilityProvider; import org.springframework.shell.Utils; import org.springframework.shell.command.CommandExceptionResolver; import org.springframework.shell.command.CommandHandlingResult; @@ -42,6 +46,7 @@ import org.springframework.shell.command.CommandRegistration.Builder; import org.springframework.shell.command.CommandRegistration.OptionArity; import org.springframework.shell.command.CommandRegistration.OptionSpec; import org.springframework.shell.command.annotation.Command; +import org.springframework.shell.command.annotation.CommandAvailability; import org.springframework.shell.command.annotation.ExceptionResolverMethodResolver; import org.springframework.shell.command.annotation.Option; import org.springframework.shell.command.annotation.OptionValues; @@ -159,6 +164,29 @@ class CommandRegistrationFactoryBean implements FactoryBean InteractionMode deduceInteractionMode = CommandAnnotationUtils.deduceInteractionMode(classAnn, methodAnn); builder.interactionMode(deduceInteractionMode); + // availability + MergedAnnotation caAnn = MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY) + .get(CommandAvailability.class); + if (caAnn.isPresent()) { + String[] refs = caAnn.getStringArray("name"); + List avails = Stream.of(refs) + .map(r -> { + return this.applicationContext.getBean(r, AvailabilityProvider.class); + }) + .collect(Collectors.toList()); + if (!avails.isEmpty()) { + builder.availability(() -> { + for (AvailabilityProvider avail : avails) { + Availability a = avail.get(); + if (!a.isAvailable()) { + return a; + } + } + return Availability.available(); + }); + } + } + // alias String[] deduceAlias = CommandAnnotationUtils.deduceAlias(classAnn, methodAnn); if (deduceAlias.length > 0) { diff --git a/spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBeanTests.java b/spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBeanTests.java index 0b384619..40bb2bf9 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBeanTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBeanTests.java @@ -18,8 +18,12 @@ package org.springframework.shell.command.annotation.support; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.shell.Availability; +import org.springframework.shell.AvailabilityProvider; import org.springframework.shell.command.CommandRegistration; import org.springframework.shell.command.annotation.Command; +import org.springframework.shell.command.annotation.CommandAvailability; import org.springframework.shell.command.annotation.Option; import static org.assertj.core.api.Assertions.assertThat; @@ -112,6 +116,34 @@ class CommandRegistrationFactoryBeanTests { } } + @Test + void setsAvailabilitySupplier() { + configCommon(AvailabilityIndicator.class, new AvailabilityIndicator(), "command1", new Class[] { }) + .run((context) -> { + CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF, + CommandRegistrationFactoryBean.class); + assertThat(fb).isNotNull(); + CommandRegistration registration = fb.getObject(); + assertThat(registration).isNotNull(); + assertThat(registration.getAvailability()).isNotNull(); + assertThat(registration.getAvailability().getReason()).isEqualTo("fakereason"); + }); + } + + @Command + private static class AvailabilityIndicator { + + @Command + @CommandAvailability(name = "testAvailability") + void command1() { + } + + @Bean + public AvailabilityProvider testAvailability() { + return () -> Availability.unavailable("fakereason"); + } + } + private ApplicationContextRunner configCommon(Class type, T bean) { return configCommon(type, bean, "command", new Class[0]); } diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/AvailabilityCommands.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/AvailabilityCommands.java new file mode 100644 index 00000000..93ec30ad --- /dev/null +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/e2e/AvailabilityCommands.java @@ -0,0 +1,123 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.shell.samples.e2e; + +import org.springframework.context.annotation.Bean; +import org.springframework.shell.Availability; +import org.springframework.shell.AvailabilityProvider; +import org.springframework.shell.command.CommandRegistration; +import org.springframework.shell.command.annotation.Command; +import org.springframework.shell.command.annotation.CommandAvailability; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; +import org.springframework.shell.standard.ShellMethodAvailability; +import org.springframework.stereotype.Component; + +public class AvailabilityCommands { + + @ShellComponent + public static class LegacyAnnotation extends BaseE2ECommands { + + // find from Availability + @ShellMethod(key = LEGACY_ANNO + "availability-1", group = GROUP) + public String testAvailability1LegacyAnnotation( + ) { + return "Hello"; + } + + public Availability testAvailability1LegacyAnnotationAvailability() { + return Availability.unavailable("not available 1"); + } + + // find from method name in @ShellMethodAvailability + @ShellMethod(key = LEGACY_ANNO + "availability-2", group = GROUP) + @ShellMethodAvailability("testAvailability2LegacyAnnotationAvailability2") + public String testAvailability2LegacyAnnotation( + ) { + return "Hello"; + } + + public Availability testAvailability2LegacyAnnotationAvailability2() { + return Availability.unavailable("not available 2"); + } + + // find backwards from @ShellMethodAvailability command name + @ShellMethod(key = LEGACY_ANNO + "availability-3", group = GROUP) + public String testAvailability3LegacyAnnotation( + ) { + return "Hello"; + } + + @ShellMethodAvailability("e2e anno availability-3") + public Availability testAvailability3LegacyAnnotationAvailability3() { + return Availability.unavailable("not available 3"); + } + } + + @Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP) + public static class Annotation extends BaseE2ECommands { + + @Command(command = "availability-1") + @CommandAvailability(name = "testAvailability1AnnotationAvailability") + public String testAvailability1Annotation( + ) { + return "Hello"; + } + + @Bean + public AvailabilityProvider testAvailability1AnnotationAvailability() { + return () -> Availability.unavailable("not available"); + } + } + + @Component + public static class Registration extends BaseE2ECommands { + + @Bean + public CommandRegistration testAvailability1Registration() { + return getBuilder() + .command(REG, "availability-1") + .group(GROUP) + .availability(() -> { + return Availability.unavailable("not available"); + }) + .withTarget() + .function(ctx -> { + return "Hello"; + }) + .and() + .build(); + } + + @Bean + public CommandRegistration testAvailability2Registration() { + return getBuilder() + .command(REG, "availability-2") + .group(GROUP) + .availability(testAvailability2AnnotationAvailability()) + .withTarget() + .function(ctx -> { + return "Hello"; + }) + .and() + .build(); + } + + AvailabilityProvider testAvailability2AnnotationAvailability() { + return () -> Availability.unavailable("not available"); + } + } +}