Search in sources :

Example 31 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-integration by spring-projects.

the class IntegrationComponentScanRegistrar method registerBeanDefinitions.

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    Map<String, Object> componentScan = importingClassMetadata.getAnnotationAttributes(IntegrationComponentScan.class.getName());
    Collection<String> basePackages = getBasePackages(importingClassMetadata, registry);
    if (basePackages.isEmpty()) {
        basePackages = Collections.singleton(ClassUtils.getPackageName(importingClassMetadata.getClassName()));
    }
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false) {

        @Override
        protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
            return beanDefinition.getMetadata().isIndependent() && !beanDefinition.getMetadata().isAnnotation();
        }
    };
    if ((boolean) componentScan.get("useDefaultFilters")) {
        for (TypeFilter typeFilter : this.componentRegistrars.keySet()) {
            scanner.addIncludeFilter(typeFilter);
        }
    }
    for (AnnotationAttributes filter : (AnnotationAttributes[]) componentScan.get("includeFilters")) {
        for (TypeFilter typeFilter : typeFiltersFor(filter, registry)) {
            scanner.addIncludeFilter(typeFilter);
        }
    }
    for (AnnotationAttributes filter : (AnnotationAttributes[]) componentScan.get("excludeFilters")) {
        for (TypeFilter typeFilter : typeFiltersFor(filter, registry)) {
            scanner.addExcludeFilter(typeFilter);
        }
    }
    scanner.setResourceLoader(this.resourceLoader);
    for (String basePackage : basePackages) {
        Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(basePackage);
        for (BeanDefinition candidateComponent : candidateComponents) {
            if (candidateComponent instanceof AnnotatedBeanDefinition) {
                for (ImportBeanDefinitionRegistrar registrar : this.componentRegistrars.values()) {
                    registrar.registerBeanDefinitions(((AnnotatedBeanDefinition) candidateComponent).getMetadata(), registry);
                }
            }
        }
    }
}
Also used : IntegrationComponentScan(org.springframework.integration.annotation.IntegrationComponentScan) AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) TypeFilter(org.springframework.core.type.filter.TypeFilter) AspectJTypeFilter(org.springframework.core.type.filter.AspectJTypeFilter) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) RegexPatternTypeFilter(org.springframework.core.type.filter.RegexPatternTypeFilter) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) ImportBeanDefinitionRegistrar(org.springframework.context.annotation.ImportBeanDefinitionRegistrar) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 32 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-integration by spring-projects.

the class GlobalChannelInterceptorInitializer method initialize.

@Override
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    for (String beanName : registry.getBeanDefinitionNames()) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
        if (beanDefinition instanceof AnnotatedBeanDefinition) {
            AnnotationMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
            Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(GlobalChannelInterceptor.class.getName());
            if (CollectionUtils.isEmpty(annotationAttributes) && beanDefinition.getSource() instanceof MethodMetadata) {
                MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
                annotationAttributes = beanMethod.getAnnotationAttributes(GlobalChannelInterceptor.class.getName());
            }
            if (!CollectionUtils.isEmpty(annotationAttributes)) {
                BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(GlobalChannelInterceptorWrapper.class).addConstructorArgReference(beanName).addPropertyValue("patterns", annotationAttributes.get("patterns")).addPropertyValue("order", annotationAttributes.get("order"));
                BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), registry);
            }
        }
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) MethodMetadata(org.springframework.core.type.MethodMetadata) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GlobalChannelInterceptorWrapper(org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata)

Example 33 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-integration by spring-projects.

the class SecurityIntegrationConfigurationInitializer method initialize.

@Override
@SuppressWarnings("unchecked")
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    Map<String, ManagedSet<String>> securityInterceptors = new ManagedMap<String, ManagedSet<String>>();
    Map<String, Map<Pattern, ChannelAccessPolicy>> policies = new HashMap<String, Map<Pattern, ChannelAccessPolicy>>();
    for (String beanName : registry.getBeanDefinitionNames()) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
        if (ChannelSecurityInterceptor.class.getName().equals(beanDefinition.getBeanClassName())) {
            BeanDefinition metadataSource = (BeanDefinition) beanDefinition.getConstructorArgumentValues().getIndexedArgumentValue(0, BeanDefinition.class).getValue();
            Map<String, ?> value = (Map<String, ?>) metadataSource.getConstructorArgumentValues().getIndexedArgumentValue(0, Map.class).getValue();
            ManagedSet<String> patterns = new ManagedSet<String>();
            if (!securityInterceptors.containsKey(beanName)) {
                securityInterceptors.put(beanName, patterns);
            } else {
                patterns = securityInterceptors.get(beanName);
            }
            patterns.addAll(value.keySet());
        } else if (beanDefinition instanceof AnnotatedBeanDefinition) {
            if (beanDefinition.getSource() instanceof MethodMetadata) {
                MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
                String annotationType = SecuredChannel.class.getName();
                if (beanMethod.isAnnotated(annotationType)) {
                    Map<String, Object> securedAttributes = beanMethod.getAnnotationAttributes(annotationType);
                    String[] interceptors = (String[]) securedAttributes.get("interceptor");
                    String[] sendAccess = (String[]) securedAttributes.get("sendAccess");
                    String[] receiveAccess = (String[]) securedAttributes.get("receiveAccess");
                    ChannelAccessPolicy accessPolicy = new DefaultChannelAccessPolicy(sendAccess, receiveAccess);
                    for (String interceptor : interceptors) {
                        ManagedSet<String> patterns = new ManagedSet<String>();
                        if (!securityInterceptors.containsKey(interceptor)) {
                            securityInterceptors.put(interceptor, patterns);
                        } else {
                            patterns = securityInterceptors.get(interceptor);
                        }
                        patterns.add(beanName);
                        Map<Pattern, ChannelAccessPolicy> mapping = new HashMap<Pattern, ChannelAccessPolicy>();
                        if (!policies.containsKey(interceptor)) {
                            policies.put(interceptor, mapping);
                        } else {
                            mapping = policies.get(interceptor);
                        }
                        mapping.put(Pattern.compile(beanName), accessPolicy);
                    }
                }
            }
        }
    }
    if (!securityInterceptors.isEmpty()) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ChannelSecurityInterceptorBeanPostProcessor.class).addConstructorArgValue(securityInterceptors);
        if (!policies.isEmpty()) {
            builder.addConstructorArgValue(policies);
        }
        registry.registerBeanDefinition(CHANNEL_SECURITY_INTERCEPTOR_BPP_BEAN_NAME, builder.getBeanDefinition());
    }
}
Also used : ManagedSet(org.springframework.beans.factory.support.ManagedSet) Pattern(java.util.regex.Pattern) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) DefaultChannelAccessPolicy(org.springframework.integration.security.channel.DefaultChannelAccessPolicy) HashMap(java.util.HashMap) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) ChannelAccessPolicy(org.springframework.integration.security.channel.ChannelAccessPolicy) DefaultChannelAccessPolicy(org.springframework.integration.security.channel.DefaultChannelAccessPolicy) SecuredChannel(org.springframework.integration.security.channel.SecuredChannel) MethodMetadata(org.springframework.core.type.MethodMetadata) ManagedMap(org.springframework.beans.factory.support.ManagedMap) HashMap(java.util.HashMap) Map(java.util.Map) ChannelSecurityInterceptor(org.springframework.integration.security.channel.ChannelSecurityInterceptor) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 34 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project pinpoint by naver.

the class TargetBeanFilter method isTarget.

public boolean isTarget(final SpringBeansTargetScope scope, final String beanName, final BeanDefinition beanDefinition) {
    if (scope == null || beanName == null || beanDefinition == null) {
        return false;
    }
    final String className = beanDefinition.getBeanClassName();
    if (className == null) {
        return false;
    }
    if (cache.contains(className)) {
        return false;
    }
    for (SpringBeansTarget target : targets) {
        // check scope.
        if (target.getScope() != scope) {
            continue;
        }
        boolean condition = false;
        // check base packages.
        final List<String> basePackages = target.getBasePackages();
        if (CollectionUtils.hasLength(basePackages)) {
            if (!isBasePackage(target, className)) {
                continue;
            }
            condition = true;
        }
        // check bean name pattern.
        final List<PathMatcher> namePatterns = target.getNamePatterns();
        if (CollectionUtils.hasLength(namePatterns)) {
            if (!isBeanNameTarget(target, beanName)) {
                continue;
            }
            condition = true;
        }
        // check class name pattern.
        final List<PathMatcher> classPatterns = target.getClassPatterns();
        if (CollectionUtils.hasLength(classPatterns)) {
            if (!isClassNameTarget(target, className)) {
                continue;
            }
            condition = true;
        }
        // check class annotation.
        final List<String> annotations = target.getAnnotations();
        if (CollectionUtils.hasLength(annotations)) {
            if (!(beanDefinition instanceof AnnotatedBeanDefinition) || !isAnnotationTarget(target, (AnnotatedBeanDefinition) beanDefinition)) {
                continue;
            }
            condition = true;
        }
        if (condition) {
            // AND condition.
            return true;
        }
    }
    return false;
}
Also used : SpringBeansTarget(com.navercorp.pinpoint.plugin.spring.beans.SpringBeansTarget) PathMatcher(com.navercorp.pinpoint.bootstrap.util.PathMatcher) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)

Example 35 with AnnotatedBeanDefinition

use of org.springframework.beans.factory.annotation.AnnotatedBeanDefinition in project spring-framework by spring-projects.

the class AnnotationBeanNameGeneratorTests method generateBeanNameWithDefaultNamedComponent.

@Test
public void generateBeanNameWithDefaultNamedComponent() {
    BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class);
    String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
    assertThat(beanName).as("The generated beanName must *never* be null.").isNotNull();
    assertThat(StringUtils.hasText(beanName)).as("The generated beanName must *never* be blank.").isTrue();
    assertThat(beanName).isEqualTo("thoreau");
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)43 Test (org.junit.jupiter.api.Test)22 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)17 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)16 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)13 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)9 MethodMetadata (org.springframework.core.type.MethodMetadata)7 AnnotationMetadata (org.springframework.core.type.AnnotationMetadata)5 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)4 Map (java.util.Map)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)3 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)3 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)3 MetadataReader (org.springframework.core.type.classreading.MetadataReader)3 ManagedMap (org.springframework.beans.factory.support.ManagedMap)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 StandardMethodMetadata (org.springframework.core.type.StandardMethodMetadata)2 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)2