Skip to content

Commit cf1a6f7

Browse files
committed
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
1 parent cb998fa commit cf1a6f7

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell;
17+
18+
import java.util.function.Supplier;
19+
20+
/**
21+
* Interface resolving {@link Availability}.
22+
*
23+
* @author Janne Valkealahti
24+
*/
25+
@FunctionalInterface
26+
public interface AvailabilityProvider extends Supplier<Availability> {
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.command.annotation;
17+
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
import org.springframework.shell.Availability;
25+
26+
/**
27+
* Annotation marking a method having {@link Availability}.
28+
*
29+
* @author Janne Valkealahti
30+
*/
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target(ElementType.METHOD)
33+
@Documented
34+
public @interface CommandAvailability {
35+
36+
/**
37+
* Names of supplier beans for {@link Availability}.
38+
*
39+
* @return names of supplier beans
40+
*/
41+
String[] name() default {};
42+
}

spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBean.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.lang.reflect.Method;
1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
2123

2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
@@ -34,6 +36,8 @@
3436
import org.springframework.core.annotation.MergedAnnotations;
3537
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
3638
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
39+
import org.springframework.shell.Availability;
40+
import org.springframework.shell.AvailabilityProvider;
3741
import org.springframework.shell.Utils;
3842
import org.springframework.shell.command.CommandExceptionResolver;
3943
import org.springframework.shell.command.CommandHandlingResult;
@@ -42,6 +46,7 @@
4246
import org.springframework.shell.command.CommandRegistration.OptionArity;
4347
import org.springframework.shell.command.CommandRegistration.OptionSpec;
4448
import org.springframework.shell.command.annotation.Command;
49+
import org.springframework.shell.command.annotation.CommandAvailability;
4550
import org.springframework.shell.command.annotation.ExceptionResolverMethodResolver;
4651
import org.springframework.shell.command.annotation.Option;
4752
import org.springframework.shell.command.annotation.OptionValues;
@@ -159,6 +164,29 @@ private CommandRegistration buildRegistration() {
159164
InteractionMode deduceInteractionMode = CommandAnnotationUtils.deduceInteractionMode(classAnn, methodAnn);
160165
builder.interactionMode(deduceInteractionMode);
161166

167+
// availability
168+
MergedAnnotation<CommandAvailability> caAnn = MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY)
169+
.get(CommandAvailability.class);
170+
if (caAnn.isPresent()) {
171+
String[] refs = caAnn.getStringArray("name");
172+
List<AvailabilityProvider> avails = Stream.of(refs)
173+
.map(r -> {
174+
return this.applicationContext.getBean(r, AvailabilityProvider.class);
175+
})
176+
.collect(Collectors.toList());
177+
if (!avails.isEmpty()) {
178+
builder.availability(() -> {
179+
for (AvailabilityProvider avail : avails) {
180+
Availability a = avail.get();
181+
if (!a.isAvailable()) {
182+
return a;
183+
}
184+
}
185+
return Availability.available();
186+
});
187+
}
188+
}
189+
162190
// alias
163191
String[] deduceAlias = CommandAnnotationUtils.deduceAlias(classAnn, methodAnn);
164192
if (deduceAlias.length > 0) {

spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBeanTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
import org.junit.jupiter.api.Test;
1919

2020
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.shell.Availability;
23+
import org.springframework.shell.AvailabilityProvider;
2124
import org.springframework.shell.command.CommandRegistration;
2225
import org.springframework.shell.command.annotation.Command;
26+
import org.springframework.shell.command.annotation.CommandAvailability;
2327
import org.springframework.shell.command.annotation.Option;
2428

2529
import static org.assertj.core.api.Assertions.assertThat;
@@ -112,6 +116,34 @@ void command2(@Option(required = false) String arg) {
112116
}
113117
}
114118

119+
@Test
120+
void setsAvailabilitySupplier() {
121+
configCommon(AvailabilityIndicator.class, new AvailabilityIndicator(), "command1", new Class[] { })
122+
.run((context) -> {
123+
CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF,
124+
CommandRegistrationFactoryBean.class);
125+
assertThat(fb).isNotNull();
126+
CommandRegistration registration = fb.getObject();
127+
assertThat(registration).isNotNull();
128+
assertThat(registration.getAvailability()).isNotNull();
129+
assertThat(registration.getAvailability().getReason()).isEqualTo("fakereason");
130+
});
131+
}
132+
133+
@Command
134+
private static class AvailabilityIndicator {
135+
136+
@Command
137+
@CommandAvailability(name = "testAvailability")
138+
void command1() {
139+
}
140+
141+
@Bean
142+
public AvailabilityProvider testAvailability() {
143+
return () -> Availability.unavailable("fakereason");
144+
}
145+
}
146+
115147
private <T> ApplicationContextRunner configCommon(Class<T> type, T bean) {
116148
return configCommon(type, bean, "command", new Class[0]);
117149
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.samples.e2e;
17+
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.shell.Availability;
20+
import org.springframework.shell.AvailabilityProvider;
21+
import org.springframework.shell.command.CommandRegistration;
22+
import org.springframework.shell.command.annotation.Command;
23+
import org.springframework.shell.command.annotation.CommandAvailability;
24+
import org.springframework.shell.standard.ShellComponent;
25+
import org.springframework.shell.standard.ShellMethod;
26+
import org.springframework.shell.standard.ShellMethodAvailability;
27+
import org.springframework.stereotype.Component;
28+
29+
public class AvailabilityCommands {
30+
31+
@ShellComponent
32+
public static class LegacyAnnotation extends BaseE2ECommands {
33+
34+
// find from <methodName>Availability
35+
@ShellMethod(key = LEGACY_ANNO + "availability-1", group = GROUP)
36+
public String testAvailability1LegacyAnnotation(
37+
) {
38+
return "Hello";
39+
}
40+
41+
public Availability testAvailability1LegacyAnnotationAvailability() {
42+
return Availability.unavailable("not available 1");
43+
}
44+
45+
// find from method name in @ShellMethodAvailability
46+
@ShellMethod(key = LEGACY_ANNO + "availability-2", group = GROUP)
47+
@ShellMethodAvailability("testAvailability2LegacyAnnotationAvailability2")
48+
public String testAvailability2LegacyAnnotation(
49+
) {
50+
return "Hello";
51+
}
52+
53+
public Availability testAvailability2LegacyAnnotationAvailability2() {
54+
return Availability.unavailable("not available 2");
55+
}
56+
57+
// find backwards from @ShellMethodAvailability command name
58+
@ShellMethod(key = LEGACY_ANNO + "availability-3", group = GROUP)
59+
public String testAvailability3LegacyAnnotation(
60+
) {
61+
return "Hello";
62+
}
63+
64+
@ShellMethodAvailability("e2e anno availability-3")
65+
public Availability testAvailability3LegacyAnnotationAvailability3() {
66+
return Availability.unavailable("not available 3");
67+
}
68+
}
69+
70+
@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
71+
public static class Annotation extends BaseE2ECommands {
72+
73+
@Command(command = "availability-1")
74+
@CommandAvailability(name = "testAvailability1AnnotationAvailability")
75+
public String testAvailability1Annotation(
76+
) {
77+
return "Hello";
78+
}
79+
80+
@Bean
81+
public AvailabilityProvider testAvailability1AnnotationAvailability() {
82+
return () -> Availability.unavailable("not available");
83+
}
84+
}
85+
86+
@Component
87+
public static class Registration extends BaseE2ECommands {
88+
89+
@Bean
90+
public CommandRegistration testAvailability1Registration() {
91+
return getBuilder()
92+
.command(REG, "availability-1")
93+
.group(GROUP)
94+
.availability(() -> {
95+
return Availability.unavailable("not available");
96+
})
97+
.withTarget()
98+
.function(ctx -> {
99+
return "Hello";
100+
})
101+
.and()
102+
.build();
103+
}
104+
105+
@Bean
106+
public CommandRegistration testAvailability2Registration() {
107+
return getBuilder()
108+
.command(REG, "availability-2")
109+
.group(GROUP)
110+
.availability(testAvailability2AnnotationAvailability())
111+
.withTarget()
112+
.function(ctx -> {
113+
return "Hello";
114+
})
115+
.and()
116+
.build();
117+
}
118+
119+
AvailabilityProvider testAvailability2AnnotationAvailability() {
120+
return () -> Availability.unavailable("not available");
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)