001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006 007package org.fcrepo.camel.common.config; 008 009import org.slf4j.Logger; 010import org.slf4j.LoggerFactory; 011import org.springframework.context.annotation.ConditionContext; 012import org.springframework.context.annotation.ConfigurationCondition; 013import org.springframework.core.type.AnnotatedTypeMetadata; 014 015import java.util.Objects; 016 017/** 018 * This condition enables a bean/configuration when the specified property matches the expected value 019 * 020 * Implementations must provide a no-arg constructor. 021 * 022 * @author pwinckles 023 */ 024public abstract class ConditionOnProperty<T> implements ConfigurationCondition { 025 026 private static final Logger LOGGER = 027 LoggerFactory.getLogger(org.fcrepo.camel.common.config.ConditionOnProperty.class); 028 029 private final String name; 030 private final T expected; 031 private final T defaultValue; 032 private final Class<T> clazz; 033 034 public ConditionOnProperty(final String name, final T expected, final T defaultValue, final Class<T> clazz) { 035 this.name = name; 036 this.expected = expected; 037 this.defaultValue = defaultValue; 038 this.clazz = clazz; 039 } 040 041 @Override 042 public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) { 043 LOGGER.debug("Prop {}: {}", name, context.getEnvironment().getProperty(name)); 044 return Objects.equals(expected, context.getEnvironment().getProperty(name, clazz, defaultValue)); 045 } 046 047 @Override 048 public ConfigurationPhase getConfigurationPhase() { 049 // This forces spring to not evaluate these conditions until after it has loaded other @Configuration classes, 050 // ensuring that the properties have been loaded. 051 return ConfigurationPhase.REGISTER_BEAN; 052 } 053}