Search in sources :

Example 56 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry 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 57 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project camunda-bpm-platform by camunda.

the class StateHandlerAnnotationBeanFactoryPostProcessor method postProcessBeanFactory.

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (beanFactory instanceof BeanDefinitionRegistry) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        configureDefaultActivitiRegistry(ActivitiContextUtils.ACTIVITI_REGISTRY_BEAN_NAME, registry);
    } else {
        log.info("BeanFactory is not a BeanDefinitionRegistry. The default '" + ActivitiContextUtils.ACTIVITI_REGISTRY_BEAN_NAME + "' cannot be configured.");
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Example 58 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project camunda-bpm-platform by camunda.

the class ProcessScope method postProcessBeanFactory.

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.registerScope(ProcessScope.PROCESS_SCOPE_NAME, this);
    Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory, "BeanFactory was not a BeanDefinitionRegistry, so ProcessScope cannot be used.");
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
        // Replace this or any of its inner beans with scoped proxy if it has this scope
        boolean scoped = PROCESS_SCOPE_NAME.equals(definition.getScope());
        Scopifier scopifier = new Scopifier(registry, PROCESS_SCOPE_NAME, proxyTargetClass, scoped);
        scopifier.visitBeanDefinition(definition);
        if (scoped) {
            Scopifier.createScopedProxy(beanName, definition, registry, proxyTargetClass);
        }
    }
    beanFactory.registerSingleton(ProcessScope.PROCESS_SCOPE_PROCESS_VARIABLES_SINGLETON, this.processVariablesMap);
    beanFactory.registerResolvableDependency(ProcessInstance.class, createSharedProcessInstance());
}
Also used : Scopifier(org.camunda.bpm.engine.spring.components.aop.util.Scopifier) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 59 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project cuba by cuba-platform.

the class RemoteProxyBeanCreator method processSubstitutions.

protected void processSubstitutions(ConfigurableListableBeanFactory beanFactory) {
    if (substitutions != null) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        for (Map.Entry<String, String> entry : substitutions.entrySet()) {
            // replace bean with substitution bean
            if (beanFactory.containsBean(entry.getKey())) {
                String beanName = entry.getKey();
                String beanClass = entry.getValue();
                BeanDefinition definition = new RootBeanDefinition(beanClass);
                MutablePropertyValues propertyValues = definition.getPropertyValues();
                propertyValues.add("substitutedBean", beanFactory.getBean(beanName));
                registry.registerBeanDefinition(beanName, definition);
            }
        }
    }
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Map(java.util.Map)

Example 60 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spock by spockframework.

the class SpringMockTestExecutionListener method beforeTestMethod.

@Override
public void beforeTestMethod(SpringTestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    if (!(testInstance instanceof Specification))
        return;
    Specification specification = (Specification) testInstance;
    ScanScopedBeans scanScopedBeans = ReflectionUtil.getAnnotationRecursive(specification.getClass(), ScanScopedBeans.class);
    Set<String> scopes = scanScopedBeans == null ? Collections.<String>emptySet() : new HashSet<>(Arrays.asList(scanScopedBeans.value()));
    ApplicationContext applicationContext = testContext.getApplicationContext();
    String[] mockBeanNames = applicationContext.getBeanDefinitionNames();
    List<Object> mockedBeans = new ArrayList<>();
    for (String beanName : mockBeanNames) {
        BeanDefinition beanDefinition = ((BeanDefinitionRegistry) applicationContext).getBeanDefinition(beanName);
        if (beanDefinition.isAbstract() || beanDefinition.isLazyInit()) {
            continue;
        }
        if (beanDefinition.isSingleton() || scanScopedBean(scanScopedBeans, scopes, beanDefinition)) {
            Object bean = applicationContext.getBean(beanName);
            if (mockUtil.isMock(bean)) {
                mockUtil.attachMock(bean, specification);
                mockedBeans.add(bean);
            }
        }
    }
    testContext.setAttribute(MOCKED_BEANS_LIST, mockedBeans);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Specification(spock.lang.Specification) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)86 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)36 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 Test (org.junit.jupiter.api.Test)15 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)13 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)13 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)12 ApplicationContext (org.springframework.context.ApplicationContext)10 Map (java.util.Map)9 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)8 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)7 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)7 Test (org.junit.Test)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)5 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)5 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)4 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4