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

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

Introduction

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

Prototype

@Nullable
ClassLoader getClassLoader();

Source Link

Document

Return the ClassLoader that should be used to load additional classes (only null if even the system ClassLoader isn't accessible).

Usage

From source file:springfox.documentation.spring.web.VanillaSpringMvcCondition.java

@VisibleForTesting
Class<?> classByName(ConditionContext context, String clazz) throws ClassNotFoundException {
    return context.getClassLoader().loadClass(clazz);
}

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");
    }/*from  w  w  w  . j a  v  a2s .co 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:com.zxy.commons.hystrix.HystrixAutoConfigureCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String enabled = context.getEnvironment().getProperty("hystrix.enabled");
    if (StringUtils.isNotBlank(enabled) && "false".equalsIgnoreCase(enabled)) {
        return false;
    }/*from   w w  w  . jav  a  2  s.  c o m*/
    try {
        // ?jar?, ?, ??
        context.getClassLoader().loadClass(Hystrix.class.getName());
        return true;
    } catch (Exception ex) {
        return false;
    }
}

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;/*from w ww.  ja  va 2s .co m*/
                break;
            }
        }
    }

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

    return enabled;
}

From source file:com.zxy.commons.apidocs.conf.SwaggerConfiguareCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String enabled = context.getEnvironment().getProperty("apidocs.enabled");
    if (!StringUtils.isEmpty(enabled) && "false".equalsIgnoreCase(enabled)) {
        return false;
    }/* www  .j  a  v  a  2 s  .c o  m*/
    //        String xmlEnabled = context.getEnvironment().getProperty("apidocs.use.xml.enabled");
    //        if(!StringUtils.isEmpty(xmlEnabled) && "true".equalsIgnoreCase(xmlEnabled)) {
    //            return false;
    //        }
    try {
        // ?jar?, ?, ??
        context.getClassLoader().loadClass(Docket.class.getName());
        return true;
    } catch (Exception ex) {
        return false;
    }
}

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

private List<String> getMatchingBeans(ConditionContext context, BeanSearchSpec beans) {
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (beans.getStrategy() == SearchStrategy.PARENTS) {
        BeanFactory parent = beanFactory.getParentBeanFactory();
        Assert.isInstanceOf(ConfigurableListableBeanFactory.class, parent,
                "Unable to use SearchStrategy.PARENTS");
        beanFactory = (ConfigurableListableBeanFactory) parent;
    }/*from w ww  . j ava2 s  . co m*/
    if (beanFactory == null) {
        return Collections.emptyList();
    }
    List<String> beanNames = new ArrayList<String>();
    boolean considerHierarchy = beans.getStrategy() == SearchStrategy.ALL;
    for (String type : beans.getTypes()) {
        beanNames.addAll(getBeanNamesForType(beanFactory, type, context.getClassLoader(), considerHierarchy));
    }
    for (String ignoredType : beans.getIgnoredTypes()) {
        beanNames.removeAll(
                getBeanNamesForType(beanFactory, ignoredType, context.getClassLoader(), considerHierarchy));
    }
    for (String annotation : beans.getAnnotations()) {
        beanNames.addAll(Arrays.asList(getBeanNamesForAnnotation(beanFactory, annotation,
                context.getClassLoader(), considerHierarchy)));
    }
    for (String beanName : beans.getNames()) {
        if (containsBean(beanFactory, beanName, considerHierarchy)) {
            beanNames.add(beanName);
        }
    }
    return beanNames;
}

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

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(annotationClass().getName(),
            true);/*from  w  ww . j ava2  s  .co  m*/
    final List<String> beanClasses = collect(attributes, "value");
    final List<String> beanNames = collect(attributes, "name");

    if (beanClasses.size() == 0) {
        if (metadata instanceof MethodMetadata && metadata.isAnnotated(Bean.class.getName())) {
            try {
                final MethodMetadata methodMetadata = (MethodMetadata) metadata;
                // We should be safe to load at this point since we are in the
                // REGISTER_BEAN phase
                Class<?> configClass = ClassUtils.forName(methodMetadata.getDeclaringClassName(),
                        context.getClassLoader());
                ReflectionUtils.doWithMethods(configClass, new MethodCallback() {
                    @Override
                    public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                        if (methodMetadata.getMethodName().equals(method.getName())) {
                            beanClasses.add(method.getReturnType().getName());
                        }
                    }
                });
            } catch (Exception ex) {
                // swallow exception and continue
            }
        }
    }

    Assert.isTrue(beanClasses.size() > 0 || beanNames.size() > 0,
            "@" + ClassUtils.getShortName(annotationClass()) + " annotations must specify at least one bean");

    return matches(context, metadata, beanClasses, beanNames);
}

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

protected boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata, List<String> beanClasses,
        List<String> beanNames) throws LinkageError {

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

    SearchStrategy search = (SearchStrategy) metadata.getAnnotationAttributes(annotationClass().getName())
            .get("search");
    search = search == null ? SearchStrategy.ALL : search;

    boolean considerHierarchy = search == SearchStrategy.ALL;
    boolean parentOnly = search == SearchStrategy.PARENTS;

    List<String> beanClassesFound = new ArrayList<String>();
    List<String> beanNamesFound = new ArrayList<String>();

    // eagerInit set to false to prevent early instantiation (some
    // factory beans will not be able to determine their object type at this
    // stage, so those are not eligible for matching this condition)
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (parentOnly) {
        BeanFactory parent = beanFactory.getParentBeanFactory();
        if (!(parent instanceof ConfigurableListableBeanFactory)) {
            throw new IllegalStateException(
                    "Cannot use parentOnly if parent is not ConfigurableListableBeanFactory");
        }//from   w  w w  .j av  a  2  s  .co  m
        beanFactory = (ConfigurableListableBeanFactory) parent;
    }
    for (String beanClass : beanClasses) {
        try {
            Class<?> type = ClassUtils.forName(beanClass, context.getClassLoader());
            String[] beans = (considerHierarchy
                    ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, type, false, false)
                    : beanFactory.getBeanNamesForType(type, false, false));
            if (beans.length != 0) {
                beanClassesFound.add(beanClass);
            }
        } catch (ClassNotFoundException ex) {
            // swallow exception and continue
        }
    }
    for (String beanName : beanNames) {
        if (considerHierarchy ? beanFactory.containsBean(beanName) : beanFactory.containsLocalBean(beanName)) {
            beanNamesFound.add(beanName);
        }
    }

    boolean result = evaluate(beanClassesFound, beanNamesFound);
    if (this.logger.isDebugEnabled()) {
        logFoundResults(checking, "class", beanClasses, beanClassesFound);
        logFoundResults(checking, "name", beanNames, beanClassesFound);
        this.logger.debug(checking + "Match result is: " + result);
    }
    return result;
}

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

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

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

    MultiValueMap<String, Object> attributes = metadata
            .getAllAnnotationAttributes(ConditionalOnClass.class.getName(), true);
    if (attributes != null) {
        List<String> classNames = new ArrayList<String>();
        collectClassNames(classNames, attributes.get("value"));
        collectClassNames(classNames, attributes.get("name"));
        Assert.isTrue(classNames.size() > 0,
                "@ConditionalOnClass annotations must specify at least one class value");
        for (String className : classNames) {
            if (logger.isDebugEnabled()) {
                logger.debug(checking + "Looking for class: " + className);
            }/*from   w  ww . j  av  a  2 s.  c  o m*/
            if (!ClassUtils.isPresent(className, context.getClassLoader())) {
                if (logger.isDebugEnabled()) {
                    logger.debug(checking + "Class not found: " + className
                            + " (search terminated with matches=false)");
                }
                return false;
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug(checking + "Match result is: true");
    }
    return true;
}

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

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

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

    MultiValueMap<String, Object> attributes = metadata
            .getAllAnnotationAttributes(ConditionalOnMissingClass.class.getName(), true);
    if (attributes != null) {
        List<String> classNames = new ArrayList<String>();
        collectClassNames(classNames, attributes.get("value"));
        Assert.isTrue(classNames.size() > 0,
                "@ConditionalOnMissingClass annotations must specify at least one class value");
        for (String className : classNames) {
            if (logger.isDebugEnabled()) {
                logger.debug(checking + "Looking for class: " + className);
            }//  w w  w . jav a2  s . co  m
            if (ClassUtils.isPresent(className, context.getClassLoader())) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            checking + "Found class: " + className + " (search terminated with matches=false)");
                }
                return false;
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug(checking + "Match result is: true");
    }
    return true;
}