Search in sources :

Example 51 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project BroadleafCommerce by BroadleafCommerce.

the class AbstractRemoveBeanPostProcessor method postProcessBeforeInitialization.

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (beanName.equals(targetRef)) {
        if (bean instanceof ListFactoryBean || bean instanceof List) {
            Object beanToRemove = applicationContext.getBean(beanRef);
            try {
                List sourceList;
                if (bean instanceof ListFactoryBean) {
                    Field field = ListFactoryBean.class.getDeclaredField("sourceList");
                    field.setAccessible(true);
                    sourceList = (List) field.get(bean);
                } else {
                    sourceList = (List) bean;
                }
                Iterator itr = sourceList.iterator();
                while (itr.hasNext()) {
                    Object member = itr.next();
                    if (member.equals(beanToRemove)) {
                        itr.remove();
                    }
                }
            } catch (Exception e) {
                throw new BeanCreationException(e.getMessage());
            }
        } else if (bean instanceof SetFactoryBean || bean instanceof Set) {
            Object beanToRemove = applicationContext.getBean(beanRef);
            try {
                Set sourceSet;
                if (bean instanceof SetFactoryBean) {
                    Field field = SetFactoryBean.class.getDeclaredField("sourceSet");
                    field.setAccessible(true);
                    sourceSet = (Set) field.get(bean);
                } else {
                    sourceSet = (Set) bean;
                }
                List tempList = new ArrayList(sourceSet);
                Iterator itr = tempList.iterator();
                while (itr.hasNext()) {
                    Object member = itr.next();
                    if (member.equals(beanToRemove)) {
                        itr.remove();
                    }
                }
                sourceSet.clear();
                sourceSet.addAll(tempList);
            } catch (Exception e) {
                throw new BeanCreationException(e.getMessage());
            }
        } else if (bean instanceof MapFactoryBean || bean instanceof Map) {
            try {
                Map sourceMap;
                if (bean instanceof MapFactoryBean) {
                    Field field = MapFactoryBean.class.getDeclaredField("sourceMap");
                    field.setAccessible(true);
                    sourceMap = (Map) field.get(bean);
                } else {
                    sourceMap = (Map) bean;
                }
                Object key;
                if (mapKey != null) {
                    key = mapKey;
                } else {
                    key = applicationContext.getBean(mapKeyRef);
                }
                Map referenceMap = new LinkedHashMap(sourceMap);
                for (Object sourceKey : referenceMap.keySet()) {
                    if (sourceKey.equals(key)) {
                        sourceMap.remove(key);
                    }
                }
            } catch (Exception e) {
                throw new BeanCreationException(e.getMessage());
            }
        } else {
            throw new IllegalArgumentException("Bean (" + beanName + ") is specified as a merge target, " + "but is not" + " of type ListFactoryBean, SetFactoryBean or MapFactoryBean");
        }
    }
    return bean;
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) SetFactoryBean(org.springframework.beans.factory.config.SetFactoryBean) Set(java.util.Set) ArrayList(java.util.ArrayList) ListFactoryBean(org.springframework.beans.factory.config.ListFactoryBean) MapFactoryBean(org.springframework.beans.factory.config.MapFactoryBean) BeansException(org.springframework.beans.BeansException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 52 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project citrus-samples by christophd.

the class EndpointConfig method httpClient.

@Bean
public HttpClient httpClient() {
    try {
        SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new ClassPathResource("keys/citrus.jks").getFile(), "secret".toCharArray(), new TrustSelfSignedStrategy()).build();
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
        return HttpClients.custom().setSSLSocketFactory(sslSocketFactory).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).addInterceptorFirst(new HttpComponentsMessageSender.RemoveSoapHeadersInterceptor()).build();
    } catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        throw new BeanCreationException("Failed to create http client for ssl connection", e);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 53 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project kork by spinnaker.

the class DynomiteClientDelegateFactory method convertSpringProperties.

/**
 * Spring config parsing is v. dumb. It will start making any iterable a map after a certain
 * depth, so this method massages the data into something we can actually use.
 */
@SuppressWarnings("unchecked")
private DynomiteDriverProperties convertSpringProperties(Map<String, Object> properties) {
    Map<String, Object> props = new HashMap<>(properties);
    Map<String, Object> springHosts = (Map<String, Object>) properties.get("hosts");
    if (springHosts == null) {
        throw new BeanCreationException("Dynomite hosts must be set");
    }
    props.put("hosts", new ArrayList<>(springHosts.values()));
    ObjectMapper mapper = objectMapper.copy();
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addDeserializer(ConnectionPoolConfigurationImpl.class, new ConnectionPoolConfigurationImplDeserializer((String) props.get("applicationName")));
    mapper.registerModule(simpleModule);
    return mapper.convertValue(props, DynomiteDriverProperties.class);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 54 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project kork by spinnaker.

the class JedisHealthIndicatorFactory method build.

public static HealthIndicator build(Pool<Jedis> jedisPool) {
    try {
        final Pool<Jedis> src = jedisPool;
        final Field poolAccess = Pool.class.getDeclaredField("internalPool");
        poolAccess.setAccessible(true);
        GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
        return () -> {
            Jedis jedis = null;
            Health.Builder health;
            try {
                jedis = src.getResource();
                if ("PONG".equals(jedis.ping())) {
                    health = Health.up();
                } else {
                    health = Health.down();
                }
            } catch (Exception ex) {
                health = Health.down(ex);
            } finally {
                if (jedis != null)
                    jedis.close();
            }
            health.withDetail("maxIdle", internal.getMaxIdle());
            health.withDetail("minIdle", internal.getMinIdle());
            health.withDetail("numActive", internal.getNumActive());
            health.withDetail("numIdle", internal.getNumIdle());
            health.withDetail("numWaiters", internal.getNumWaiters());
            return health.build();
        };
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Field(java.lang.reflect.Field) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 55 with BeanCreationException

use of org.springframework.beans.factory.BeanCreationException in project spring-security by spring-projects.

the class CorsBeanDefinitionParser method parse.

public BeanMetadataElement parse(Element element, ParserContext parserContext) {
    if (element == null) {
        return null;
    }
    String filterRef = element.getAttribute(ATT_REF);
    if (StringUtils.hasText(filterRef)) {
        return new RuntimeBeanReference(filterRef);
    }
    BeanMetadataElement configurationSource = getSource(element, parserContext);
    if (configurationSource == null) {
        throw new BeanCreationException("Could not create CorsFilter");
    }
    BeanDefinitionBuilder filterBldr = BeanDefinitionBuilder.rootBeanDefinition(CorsFilter.class);
    filterBldr.addConstructorArgValue(configurationSource);
    return filterBldr.getBeanDefinition();
}
Also used : BeanMetadataElement(org.springframework.beans.BeanMetadataElement) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

BeanCreationException (org.springframework.beans.factory.BeanCreationException)133 Test (org.junit.Test)30 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)18 BeansException (org.springframework.beans.BeansException)16 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)13 IOException (java.io.IOException)12 Map (java.util.Map)12 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 Test (org.junit.jupiter.api.Test)11 HashMap (java.util.HashMap)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 ArrayList (java.util.ArrayList)7 PostConstruct (javax.annotation.PostConstruct)7 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)7 Bean (org.springframework.context.annotation.Bean)7 HasId (org.apache.camel.spi.HasId)6 BeanCurrentlyInCreationException (org.springframework.beans.factory.BeanCurrentlyInCreationException)6 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)6 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)5