private Availability availability(String name, boolean defaultValue) { String property = "management.endpoint." + name + ".enabled"; if (!environment.getProperty(property, Boolean.TYPE, defaultValue)) { return Availability.unavailable("endpoint '" + name + "' deactivated (please check property '" + property + "')"); } else if (properties.getActuator().getExcludes().contains(name)) { return Availability.unavailable("command is present in exclusion (please check property '" + SshShellProperties.SSH_SHELL_PREFIX + ".actuator.excludes')"); } return Availability.available(); }
public CommandNotCurrentlyAvailable(String command, Availability availability) { super(String.format("Command '%s' exists but is not currently available because %s", command, availability.getReason())); this.command = command; this.availability = availability; }
public static Availability available() { return new Availability(null); }
public MethodTarget(Method method, Object bean, Help help, Supplier<Availability> availabilityIndicator) { Assert.notNull(method, "Method cannot be null"); Assert.notNull(bean, "Bean cannot be null"); Assert.hasText(help.getDescription(), String.format("Help cannot be blank when trying to define command based on '%s'", method)); ReflectionUtils.makeAccessible(method); this.method = method; this.bean = bean; this.help = help; this.availabilityIndicator = availabilityIndicator != null ? availabilityIndicator : () -> Availability.available(); }
private void documentAvailability(AttributedStringBuilder result, MethodTarget methodTarget) { Availability availability = methodTarget.getAvailability(); if (!availability.isAvailable()) { result.append("CURRENTLY UNAVAILABLE", AttributedStyle.BOLD).append("\n"); result.append('\t').append("This command is currently not available because ") .append(availability.getReason()) .append(".\n"); } }
private boolean isAvailable(MethodTarget methodTarget) { return methodTarget.getAvailability().isAvailable(); }
MethodTarget methodTarget = methodTargets.get(command); Availability availability = methodTarget.getAvailability(); if (availability.isAvailable()) { List<String> wordsForArgs = wordsForArguments(command, words); Method method = methodTarget.getMethod();
private Supplier<Availability> bridgeAvailabilityIndicator(String commandKey, Object bean) { Class<?> clazz = bean.getClass(); Set<Method> candidates = new HashSet<>(); ReflectionUtils.doWithMethods(clazz, candidates::add, method -> method.getAnnotation(CliAvailabilityIndicator.class) != null && Arrays.asList(method.getAnnotation(CliAvailabilityIndicator.class).value()).contains(commandKey)); switch (candidates.size()) { case 0: return null; case 1: return () -> { boolean available = (Boolean) ReflectionUtils.invokeMethod(candidates.iterator().next(), bean); return available ? Availability.available() : Availability.unavailable("[Unknown reason]"); }; default: throw new IllegalStateException("Looks like there are several @" + CliAvailabilityIndicator.class.getSimpleName() + " for '" + commandKey + "'. Found " + candidates); } }
public static Availability unavailable(String reason) { Assert.notNull(reason, "Reason for not being available must be provided"); return new Availability(reason); }
private Availability availability(String name, Class<?> clazz, boolean defaultValue) { if (!"info".equals(name)) { SshAuthentication auth = SshShellCommandFactory.SSH_THREAD_CONTEXT.get().getAuthentication(); List<String> authorities = auth != null ? auth.getAuthorities() : null; if (!helper.checkAuthorities(properties.getActuator().getAuthorizedRoles(), authorities, properties.getAuthentication() == SshShellProperties.AuthenticationType.simple)) { return Availability.unavailable("actuator commands are forbidden for current user"); } } String property = "management.endpoint." + name + ".enabled"; if (!environment.getProperty(property, Boolean.TYPE, defaultValue)) { return Availability.unavailable("endpoint '" + name + "' deactivated (please check property '" + property + "')"); } else if (properties.getActuator().getExcludes().contains(name)) { return Availability.unavailable("command is present in exclusion (please check property '" + SSH_SHELL_PREFIX + ".actuator.excludes')"); } try { applicationContext.getBean(clazz); } catch (NoSuchBeanDefinitionException e) { return Availability.unavailable(clazz.getName() + " is not in application context"); } return Availability.available(); }