Example usage for org.springframework.core.type AnnotatedTypeMetadata getAnnotationAttributes

List of usage examples for org.springframework.core.type AnnotatedTypeMetadata getAnnotationAttributes

Introduction

In this page you can find the example usage for org.springframework.core.type AnnotatedTypeMetadata getAnnotationAttributes.

Prototype

@Nullable
default Map<String, Object> getAnnotationAttributes(String annotationName) 

Source Link

Document

Retrieve the attributes of the annotation of the given type, if any (i.e.

Usage

From source file:com.mobilepetroleum.AvailableOnClasspathCondition.java

public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> attributes = metadata.getAnnotationAttributes(AvailableOnClasspath.class.getName());
    String className = String.valueOf(attributes.get("value"));
    try {//from  w ww  .  j  av a2s  .  com
        Class.forName(className);
        return true;
    } catch (ClassNotFoundException ignored) {
    }
    return false;
}

From source file:lodsve.core.condition.OnJavaCondition.java

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnJava.class.getName());
    Range range = (Range) attributes.get("range");
    JavaVersion version = (JavaVersion) attributes.get("value");
    return getMatchOutcome(range, JVM_VERSION, version);
}

From source file:name.abhijitsarkar.javaee.coffeehouse.spring.support.DayOfTheWeekCondition.java

@Override
public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
    final Map<String, Object> attributes = metadata
            .getAnnotationAttributes(ConditionalOnDayOfTheWeek.class.getName());

    final ConditionalOnDayOfTheWeek.DayOfTheWeek dayOfTheWeek = (ConditionalOnDayOfTheWeek.DayOfTheWeek) attributes
            .get("value");
    final boolean isWeekend = Boolean.valueOf(System.getProperty("isWeekend", "false"));

    LOGGER.debug("Day of the week: {}, isWeekend: {}.", dayOfTheWeek, isWeekend);

    return (dayOfTheWeek.equals(ConditionalOnDayOfTheWeek.DayOfTheWeek.WEEKEND) && isWeekend)
            || (dayOfTheWeek.equals(ConditionalOnDayOfTheWeek.DayOfTheWeek.WEEKDAY) && !isWeekend);
}

From source file:com.torchmind.stockpile.server.configuration.condition.CacheAggressivenessCondition.java

/**
 * {@inheritDoc}/*from   w ww.  j  a  v a  2s.com*/
 */
@Override
public boolean matches(@Nonnull ConditionContext context, @Nonnull AnnotatedTypeMetadata metadata) {
    Aggressiveness aggressiveness = (Aggressiveness) metadata
            .getAnnotationAttributes(ConditionalOnCacheAggressiveness.class.getName())
            .getOrDefault("value", Aggressiveness.HIGH);
    return Aggressiveness.valueOf(context.getEnvironment().getProperty("cache.aggressiveness", "high")
            .toUpperCase()) == aggressiveness;
}

From source file:at.porscheinformatik.common.spring.web.extended.annotation.DefaultBeanCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(DefaultBean.class.getName());

    Class<?>[] classes = (Class<?>[]) annotationAttributes.get("value");
    ConfigurableListableBeanFactory factory = context.getBeanFactory();

    for (Class<?> c : classes) {
        String[] beanNamesForType = factory.getBeanNamesForType(c, false, false);

        if (beanNamesForType != null) {
            for (String beanName : beanNamesForType) {
                if (factory.containsBean(beanName)) {
                    return false;
                }// w  w  w .  j a v a  2  s.  c  o m
            }
        }
    }

    return true;
}

From source file:com.haulmont.cuba.core.sys.OnConfigPropertyCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnAppProperty.class.getName());

    if (attributes != null) {
        String configPropertyName = (String) attributes.get("property");
        String configPropertyValue = (String) attributes.get("value");
        String configPropertyDefaultValue = (String) attributes.get("defaultValue");

        return isConditionSatisfied(configPropertyName, configPropertyValue, configPropertyDefaultValue);
    } else {/*from   w ww .j ava2s  .c  o m*/
        Map<String, Object> valueMap = metadata
                .getAnnotationAttributes(ConditionalOnAppProperties.class.getName());
        AnnotationAttributes[] properties = (AnnotationAttributes[]) valueMap.get("value");

        for (AnnotationAttributes propertyCondition : properties) {
            String configPropertyName = (String) propertyCondition.get("property");
            String configPropertyValue = (String) propertyCondition.get("value");
            String configPropertyDefaultValue = (String) propertyCondition.get("defaultValue");

            if (!isConditionSatisfied(configPropertyName, configPropertyValue, configPropertyDefaultValue)) {
                return false;
            }
        }
    }

    return true;
}

From source file:lodsve.core.condition.OnExpressionCondition.java

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {

    String expression = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName())
            .get("value");
    String rawExpression = expression;
    if (!expression.startsWith("#{")) {
        // For convenience allow user to provide bare expression with no #{} wrapper
        expression = "#{" + expression + "}";
    }//from www .j av  a2s.c o  m

    // Explicitly allow environment placeholders inside the expression
    expression = context.getEnvironment().resolvePlaceholders(expression);
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    BeanExpressionResolver resolver = (beanFactory != null) ? beanFactory.getBeanExpressionResolver() : null;
    BeanExpressionContext expressionContext = (beanFactory != null)
            ? new BeanExpressionContext(beanFactory, null)
            : null;
    if (resolver == null) {
        resolver = new StandardBeanExpressionResolver();
    }
    boolean result = (Boolean) resolver.evaluate(expression, expressionContext);

    StringBuilder message = new StringBuilder("SpEL expression");
    if (metadata instanceof ClassMetadata) {
        message.append(" on " + ((ClassMetadata) metadata).getClassName());
    }
    message.append(": " + rawExpression);
    return new ConditionOutcome(result, message.toString());
}

From source file:my.school.spring.rest.controllers.UseSchedulesCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

    final Map<String, Object> annotationAttributes = Optional
            .ofNullable(metadata.getAnnotationAttributes(UseSchedules.class.getName())).orElse(new HashMap<>());
    Boolean isScheduledInstance = (Boolean) annotationAttributes.getOrDefault("value", false);

    Boolean allowScheduled = Optional
            .ofNullable(context.getEnvironment().getProperty("setup.versionProvider.scheduled", Boolean.class))
            .orElse(false);//ww  w . ja v a2s .co m
    final boolean scheduled = Objects.equals(allowScheduled, isScheduledInstance);
    LOG.info("Application will reuse {} implementation of the VersionProvider",
            (scheduled) ? "SCHEDULED" : "NOT SCHEDULED");
    return scheduled;
}

From source file:com.github.kpavlov.commons.spring.annotations.BooleanValueCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    final Environment environment = context.getEnvironment();
    if (environment != null) {
        Map<String, Object> attributes = metadata.getAnnotationAttributes(Enabled.class.getName());
        if (attributes != null) {
            String expression = (String) attributes.get("value");
            final String value = environment.resolvePlaceholders(expression);
            return (Boolean.parseBoolean(value));
        }//  w w w  . ja  va  2  s.  co m
    }
    return true;
}

From source file:lodsve.core.condition.OnJndiCondition.java

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    AnnotationAttributes annotationAttributes = AnnotationAttributes
            .fromMap(metadata.getAnnotationAttributes(ConditionalOnJndi.class.getName()));
    String[] locations = annotationAttributes.getStringArray("value");
    try {/*from  ww w  . j a  v  a2  s. c om*/
        return getMatchOutcome(locations);
    } catch (NoClassDefFoundError ex) {
        return ConditionOutcome.noMatch("JNDI class not found");
    }
}