Search in sources :

Example 1 with NotReadablePropertyException

use of org.springframework.beans.NotReadablePropertyException in project spring-framework by spring-projects.

the class SpringValidatorAdapter method processConstraintViolations.

/**
	 * Process the given JSR-303 ConstraintViolations, adding corresponding errors to
	 * the provided Spring {@link Errors} object.
	 * @param violations the JSR-303 ConstraintViolation results
	 * @param errors the Spring errors object to register to
	 */
protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
    for (ConstraintViolation<Object> violation : violations) {
        String field = determineField(violation);
        FieldError fieldError = errors.getFieldError(field);
        if (fieldError == null || !fieldError.isBindingFailure()) {
            try {
                ConstraintDescriptor<?> cd = violation.getConstraintDescriptor();
                String errorCode = determineErrorCode(cd);
                Object[] errorArgs = getArgumentsForConstraint(errors.getObjectName(), field, cd);
                if (errors instanceof BindingResult) {
                    // Can do custom FieldError registration with invalid value from ConstraintViolation,
                    // as necessary for Hibernate Validator compatibility (non-indexed set path in field)
                    BindingResult bindingResult = (BindingResult) errors;
                    String nestedField = bindingResult.getNestedPath() + field;
                    if ("".equals(nestedField)) {
                        String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
                        bindingResult.addError(new ObjectError(errors.getObjectName(), errorCodes, errorArgs, violation.getMessage()));
                    } else {
                        Object rejectedValue = getRejectedValue(field, violation, bindingResult);
                        String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
                        bindingResult.addError(new FieldError(errors.getObjectName(), nestedField, rejectedValue, false, errorCodes, errorArgs, violation.getMessage()));
                    }
                } else {
                    // got no BindingResult - can only do standard rejectValue call
                    // with automatic extraction of the current field value
                    errors.rejectValue(field, errorCode, errorArgs, violation.getMessage());
                }
            } catch (NotReadablePropertyException ex) {
                throw new IllegalStateException("JSR-303 validated property '" + field + "' does not have a corresponding accessor for Spring data binding - " + "check your DataBinder's configuration (bean property versus direct field access)", ex);
            }
        }
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) ObjectError(org.springframework.validation.ObjectError) NotReadablePropertyException(org.springframework.beans.NotReadablePropertyException) FieldError(org.springframework.validation.FieldError)

Example 2 with NotReadablePropertyException

use of org.springframework.beans.NotReadablePropertyException in project spring-integration by spring-projects.

the class JmsMessageDrivenChannelAdapterParserTests method testAdapterWithRecoveryInterval.

@Test
public void testAdapterWithRecoveryInterval() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jmsInboundWithContainerSettings.xml", this.getClass());
    JmsMessageDrivenEndpoint adapter = (JmsMessageDrivenEndpoint) context.getBean("adapterWithRecoveryInterval.adapter");
    adapter.start();
    AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) new DirectFieldAccessor(adapter).getPropertyValue("listenerContainer");
    Object recoveryInterval;
    try {
        recoveryInterval = TestUtils.getPropertyValue(container, "recoveryInterval");
    } catch (NotReadablePropertyException e) {
        recoveryInterval = TestUtils.getPropertyValue(container, "backOff.interval");
    }
    assertEquals(2222L, recoveryInterval);
    adapter.stop();
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) NotReadablePropertyException(org.springframework.beans.NotReadablePropertyException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JmsMessageDrivenEndpoint(org.springframework.integration.jms.JmsMessageDrivenEndpoint) AbstractMessageListenerContainer(org.springframework.jms.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 3 with NotReadablePropertyException

use of org.springframework.beans.NotReadablePropertyException in project spring-integration by spring-projects.

the class JmsInboundGatewayParserTests method testGatewayWithContainerSettings.

@Test
public void testGatewayWithContainerSettings() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jmsGatewayWithContainerSettings.xml", this.getClass());
    JmsMessageDrivenEndpoint gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithConcurrentConsumers");
    AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    assertEquals(3, new DirectFieldAccessor(container).getPropertyValue("concurrentConsumers"));
    gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithMaxConcurrentConsumers");
    container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    assertEquals(22, new DirectFieldAccessor(container).getPropertyValue("maxConcurrentConsumers"));
    gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithMaxMessagesPerTask");
    container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    assertEquals(99, new DirectFieldAccessor(container).getPropertyValue("maxMessagesPerTask"));
    gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithReceiveTimeout");
    container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    assertEquals(1111L, new DirectFieldAccessor(container).getPropertyValue("receiveTimeout"));
    gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithRecoveryInterval");
    container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    Object recoveryInterval;
    try {
        recoveryInterval = TestUtils.getPropertyValue(container, "recoveryInterval");
    } catch (NotReadablePropertyException e) {
        recoveryInterval = TestUtils.getPropertyValue(container, "backOff.interval");
    }
    assertEquals(2222L, recoveryInterval);
    gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithIdleTaskExecutionLimit");
    container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    assertEquals(7, new DirectFieldAccessor(container).getPropertyValue("idleTaskExecutionLimit"));
    gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithIdleConsumerLimit");
    container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    assertEquals(33, new DirectFieldAccessor(container).getPropertyValue("idleConsumerLimit"));
    gateway = (JmsMessageDrivenEndpoint) context.getBean("gatewayWithMessageSelector");
    container = (AbstractMessageListenerContainer) new DirectFieldAccessor(gateway).getPropertyValue("listenerContainer");
    String messageSelector = (String) new DirectFieldAccessor(container).getPropertyValue("messageSelector");
    assertEquals("TestProperty = 'foo'", messageSelector);
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) NotReadablePropertyException(org.springframework.beans.NotReadablePropertyException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JmsMessageDrivenEndpoint(org.springframework.integration.jms.JmsMessageDrivenEndpoint) AbstractMessageListenerContainer(org.springframework.jms.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 4 with NotReadablePropertyException

use of org.springframework.beans.NotReadablePropertyException in project spring-integration by spring-projects.

the class JmsOutboundGatewayParserTests method testWithDeliveryPersistentAttribute.

@Test
public void testWithDeliveryPersistentAttribute() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jmsOutboundGatewayWithDeliveryPersistent.xml", this.getClass());
    EventDrivenConsumer endpoint = (EventDrivenConsumer) context.getBean("jmsGateway");
    DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint);
    JmsOutboundGateway gateway = (JmsOutboundGateway) accessor.getPropertyValue("handler");
    accessor = new DirectFieldAccessor(gateway);
    int deliveryMode = (Integer) accessor.getPropertyValue("deliveryMode");
    assertEquals(DeliveryMode.PERSISTENT, deliveryMode);
    assertTrue(TestUtils.getPropertyValue(gateway, "async", Boolean.class));
    DefaultMessageListenerContainer container = TestUtils.getPropertyValue(gateway, "replyContainer", DefaultMessageListenerContainer.class);
    assertEquals(4, TestUtils.getPropertyValue(container, "concurrentConsumers"));
    assertEquals(5, TestUtils.getPropertyValue(container, "maxConcurrentConsumers"));
    assertEquals(10, TestUtils.getPropertyValue(container, "maxMessagesPerTask"));
    assertEquals(2000L, TestUtils.getPropertyValue(container, "receiveTimeout"));
    Object recoveryInterval;
    try {
        recoveryInterval = TestUtils.getPropertyValue(container, "recoveryInterval");
    } catch (NotReadablePropertyException e) {
        recoveryInterval = TestUtils.getPropertyValue(container, "backOff.interval");
    }
    assertEquals(10000L, recoveryInterval);
    assertEquals(7, TestUtils.getPropertyValue(container, "idleConsumerLimit"));
    assertEquals(2, TestUtils.getPropertyValue(container, "idleTaskExecutionLimit"));
    assertEquals(3, TestUtils.getPropertyValue(container, "cacheLevel"));
    assertTrue(container.isSessionTransacted());
    assertSame(context.getBean("exec"), TestUtils.getPropertyValue(container, "taskExecutor"));
    assertEquals(1234000L, TestUtils.getPropertyValue(gateway, "idleReplyContainerTimeout"));
    context.close();
}
Also used : DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) NotReadablePropertyException(org.springframework.beans.NotReadablePropertyException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) JmsOutboundGateway(org.springframework.integration.jms.JmsOutboundGateway) Test(org.junit.Test)

Aggregations

NotReadablePropertyException (org.springframework.beans.NotReadablePropertyException)4 Test (org.junit.Test)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 JmsMessageDrivenEndpoint (org.springframework.integration.jms.JmsMessageDrivenEndpoint)2 AbstractMessageListenerContainer (org.springframework.jms.listener.AbstractMessageListenerContainer)2 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)1 JmsOutboundGateway (org.springframework.integration.jms.JmsOutboundGateway)1 DefaultMessageListenerContainer (org.springframework.jms.listener.DefaultMessageListenerContainer)1 BindingResult (org.springframework.validation.BindingResult)1 FieldError (org.springframework.validation.FieldError)1 ObjectError (org.springframework.validation.ObjectError)1