Home / Class/ OnResourceCondition Class — spring-boot Architecture

OnResourceCondition Class — spring-boot Architecture

Architecture documentation for the OnResourceCondition class in OnResourceCondition.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnResourceCondition.java lines 40–83

@Order(Ordered.HIGHEST_PRECEDENCE + 20)
class OnResourceCondition extends SpringBootCondition {

	@Override
	public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
		MultiValueMap<String, @Nullable Object> attributes = metadata
			.getAllAnnotationAttributes(ConditionalOnResource.class.getName(), true);
		Assert.state(attributes != null, "'attributes' must not be null");
		ResourceLoader loader = context.getResourceLoader();
		List<String> locations = new ArrayList<>();
		List<@Nullable Object> resources = attributes.get("resources");
		Assert.state(resources != null, "'resources' must not be null");
		collectValues(locations, resources);
		Assert.state(!locations.isEmpty(),
				"@ConditionalOnResource annotations must specify at least one resource location");
		List<String> missing = new ArrayList<>();
		for (String location : locations) {
			String resource = context.getEnvironment().resolvePlaceholders(location);
			if (!loader.getResource(resource).exists()) {
				missing.add(location);
			}
		}
		if (!missing.isEmpty()) {
			return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnResource.class)
				.didNotFind("resource", "resources")
				.items(Style.QUOTE, missing));
		}
		return ConditionOutcome.match(ConditionMessage.forCondition(ConditionalOnResource.class)
			.found("location", "locations")
			.items(locations));
	}

	private void collectValues(List<String> names, List<@Nullable Object> resources) {
		for (Object resource : resources) {
			Object[] items = (Object[]) resource;
			if (items != null) {
				for (Object item : items) {
					names.add((String) item);
				}
			}
		}
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free