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
This commit is contained in:
Janne Valkealahti
2023-02-09 21:18:22 +00:00
parent cb998fae08
commit cf1a6f7839
5 changed files with 252 additions and 0 deletions

View File

@@ -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<Availability> {
}

View File

@@ -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 {};
}

View File

@@ -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<CommandRegistration>
InteractionMode deduceInteractionMode = CommandAnnotationUtils.deduceInteractionMode(classAnn, methodAnn);
builder.interactionMode(deduceInteractionMode);
// availability
MergedAnnotation<CommandAvailability> caAnn = MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY)
.get(CommandAvailability.class);
if (caAnn.isPresent()) {
String[] refs = caAnn.getStringArray("name");
List<AvailabilityProvider> 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) {