Example usage for org.springframework.context.annotation ConditionContext getEnvironment

List of usage examples for org.springframework.context.annotation ConditionContext getEnvironment

Introduction

In this page you can find the example usage for org.springframework.context.annotation ConditionContext getEnvironment.

Prototype

Environment getEnvironment();

Source Link

Document

Return the Environment for which the current application is running.

Usage

From source file:org.juiser.spring.boot.config.JuiserSpringSecurityCondition.java

private boolean isSpringSecurityEnabled(ConditionContext ctx) {

    boolean enabled = true;

    Environment env = ctx.getEnvironment();

    for (String propName : props) {
        if (env.containsProperty(propName)) {
            if (!Boolean.parseBoolean(env.getProperty(propName))) {
                enabled = false;/*  w  ww  . jav  a  2s.  c  o  m*/
                break;
            }
        }
    }

    if (enabled) {
        enabled = ClassUtils.isPresent(SPRING_SEC_CLASS_NAME, ctx.getClassLoader());
    }

    return enabled;
}

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

/**
 * {@inheritDoc}/*  w ww  .  ja  v a  2 s .  c  om*/
 */
@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: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);/*w ww  .  ja v a  2s .c  o 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: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 ww w. jav a2 s .c om*/

    // 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:lodsve.core.condition.OnWebApplicationCondition.java

private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata) {

    if (!ClassUtils.isPresent(WEB_CONTEXT_CLASS, context.getClassLoader())) {
        return ConditionOutcome.noMatch("web application classes not found");
    }/*  w w  w.  j av a  2  s. c  o  m*/

    if (context.getBeanFactory() != null) {
        String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
        if (ObjectUtils.containsElement(scopes, "session")) {
            return ConditionOutcome.match("found web application 'session' scope");
        }
    }

    if (context.getEnvironment() instanceof StandardServletEnvironment) {
        return ConditionOutcome.match("found web application StandardServletEnvironment");
    }

    if (context.getResourceLoader() instanceof WebApplicationContext) {
        return ConditionOutcome.match("found web application WebApplicationContext");
    }

    return ConditionOutcome.noMatch("not a web application");
}

From source file:org.springframework.boot.autoconfigure.condition.OnExpressionCondition.java

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

    String checking = ConditionLogUtils.getPrefix(logger, metadata);

    String value = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName())
            .get("value");
    if (!value.startsWith("#{")) {
        // For convenience allow user to provide bare expression with no #{} wrapper
        value = "#{" + value + "}";
    }/*w w  w.  j  av  a2s .c om*/
    if (logger.isDebugEnabled()) {
        StringBuilder builder = new StringBuilder(checking).append("Evaluating expression");
        if (metadata instanceof ClassMetadata) {
            builder.append(" on " + ((ClassMetadata) metadata).getClassName());
        }
        builder.append(": " + value);
        logger.debug(builder.toString());
    }
    // Explicitly allow environment placeholders inside the expression
    value = context.getEnvironment().resolvePlaceholders(value);
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();
    BeanExpressionContext expressionContext = (beanFactory != null)
            ? new BeanExpressionContext(beanFactory, null)
            : null;
    if (resolver == null) {
        resolver = new StandardBeanExpressionResolver();
    }
    boolean result = (Boolean) resolver.evaluate(value, expressionContext);
    if (logger.isDebugEnabled()) {
        logger.debug(checking + "Finished matching and result is matches=" + result);
    }
    return result;
}

From source file:org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition.java

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

    String checking = ConditionLogUtils.getPrefix(logger, metadata);

    if (!ClassUtils.isPresent("org.springframework.web.context.support.GenericWebApplicationContext", null)) {
        if (logger.isDebugEnabled()) {
            logger.debug(checking + "Web application classes not found");
        }/*from   w  ww  .  jav  a 2s  .  co m*/
        return false;
    }
    boolean result = StringUtils.arrayToCommaDelimitedString(context.getBeanFactory().getRegisteredScopeNames())
            .contains("session") || context.getEnvironment() instanceof StandardServletEnvironment;
    if (logger.isDebugEnabled()) {
        logger.debug(checking + "Web application context found: " + result);
    }
    return result;
}