Search in sources :

Example 51 with BeanWrapperImpl

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

the class WebLogicRequestUpgradeStrategy method handleSuccess.

@Override
protected void handleSuccess(HttpServletRequest request, HttpServletResponse response, UpgradeInfo upgradeInfo, TyrusUpgradeResponse upgradeResponse) throws IOException, ServletException {
    response.setStatus(upgradeResponse.getStatus());
    upgradeResponse.getHeaders().forEach((key, value) -> response.addHeader(key, Utils.getHeaderFromList(value)));
    AsyncContext asyncContext = request.startAsync();
    asyncContext.setTimeout(-1L);
    Object nativeRequest = getNativeRequest(request);
    BeanWrapper beanWrapper = new BeanWrapperImpl(nativeRequest);
    Object httpSocket = beanWrapper.getPropertyValue("connection.connectionHandler.rawConnection");
    Object webSocket = webSocketHelper.newInstance(request, httpSocket);
    webSocketHelper.upgrade(webSocket, httpSocket, request.getServletContext());
    response.flushBuffer();
    boolean isProtected = request.getUserPrincipal() != null;
    Writer servletWriter = servletWriterHelper.newInstance(webSocket, isProtected);
    Connection connection = upgradeInfo.createConnection(servletWriter, noOpCloseListener);
    new BeanWrapperImpl(webSocket).setPropertyValue("connection", connection);
    new BeanWrapperImpl(servletWriter).setPropertyValue("connection", connection);
    webSocketHelper.registerForReadEvent(webSocket);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Connection(org.glassfish.tyrus.spi.Connection) AsyncContext(jakarta.servlet.AsyncContext) Writer(org.glassfish.tyrus.spi.Writer)

Example 52 with BeanWrapperImpl

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

the class JmxUtilsTests method getAttributeNameWithoutStrictCasing.

@Test
void getAttributeNameWithoutStrictCasing() {
    PropertyDescriptor pd = new BeanWrapperImpl(AttributeTestBean.class).getPropertyDescriptor("name");
    String attributeName = JmxUtils.getAttributeName(pd, false);
    assertThat(attributeName).as("Incorrect casing on attribute name").isEqualTo("name");
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.jupiter.api.Test)

Example 53 with BeanWrapperImpl

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

the class BeanPropertyRowMapper method mapRow.

/**
 * Extract the values for all columns in the current row.
 * <p>Utilizes public setters and result set meta-data.
 * @see java.sql.ResultSetMetaData
 */
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    BeanWrapperImpl bw = new BeanWrapperImpl();
    initBeanWrapper(bw);
    T mappedObject = constructMappedInstance(rs, bw);
    bw.setBeanInstance(mappedObject);
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<>() : null);
    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        String field = lowerCaseName(StringUtils.delete(column, " "));
        PropertyDescriptor pd = (this.mappedFields != null ? this.mappedFields.get(field) : null);
        if (pd != null) {
            try {
                Object value = getColumnValue(rs, index, pd);
                if (rowNumber == 0 && logger.isDebugEnabled()) {
                    logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "'");
                }
                try {
                    bw.setPropertyValue(pd.getName(), value);
                } catch (TypeMismatchException ex) {
                    if (value == null && this.primitivesDefaultedForNullValue) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with null value when setting property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "' on object: " + mappedObject, ex);
                        }
                    } else {
                        throw ex;
                    }
                }
                if (populatedProperties != null) {
                    populatedProperties.add(pd.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException("Unable to map column '" + column + "' to property '" + pd.getName() + "'", ex);
            }
        } else {
            // No PropertyDescriptor found
            if (rowNumber == 0 && logger.isDebugEnabled()) {
                logger.debug("No property found for column '" + column + "' mapped to field '" + field + "'");
            }
        }
    }
    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of " + this.mappedClass + ": " + this.mappedProperties);
    }
    return mappedObject;
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PropertyDescriptor(java.beans.PropertyDescriptor) TypeMismatchException(org.springframework.beans.TypeMismatchException) ResultSetMetaData(java.sql.ResultSetMetaData) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) HashSet(java.util.HashSet)

Example 54 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project midpoint by Evolveum.

the class AbstractManagedConnectorInstance method applyConfigurationToConfigurationClass.

private void applyConfigurationToConfigurationClass(PrismContainerValue<?> configurationContainer) throws ConfigurationException {
    BeanWrapper connectorBean = new BeanWrapperImpl(this);
    PropertyDescriptor connectorConfigurationProp = UcfUtil.findAnnotatedProperty(connectorBean, ManagedConnectorConfiguration.class);
    if (connectorConfigurationProp == null) {
        return;
    }
    Class<?> configurationClass = connectorConfigurationProp.getPropertyType();
    Object configurationObject;
    try {
        configurationObject = configurationClass.getDeclaredConstructor().newInstance();
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new ConfigurationException("Cannot instantiate configuration " + configurationClass);
    }
    BeanWrapper configurationClassBean = new BeanWrapperImpl(configurationObject);
    for (Item<?, ?> configurationItem : configurationContainer.getItems()) {
        ItemDefinition<?> configurationItemDefinition = configurationItem.getDefinition();
        String itemLocalName = configurationItem.getElementName().getLocalPart();
        if (configurationItemDefinition != null && configurationItemDefinition.isMultiValue()) {
            Object[] realValuesArray = configurationItem.getRealValuesArray(Object.class);
            configurationClassBean.setPropertyValue(itemLocalName, realValuesArray);
        } else {
            Object realValue = configurationItem.getRealValue();
            configurationClassBean.setPropertyValue(itemLocalName, realValue);
        }
    }
    connectorBean.setPropertyValue(connectorConfigurationProp.getName(), configurationObject);
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PropertyDescriptor(java.beans.PropertyDescriptor) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanWrapper(org.springframework.beans.BeanWrapper) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException)

Example 55 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project midpoint by Evolveum.

the class ConnectorFactoryBuiltinImpl method generateConnectorConfigurationSchema.

private PrismSchema generateConnectorConfigurationSchema(ConnectorStruct struct) {
    Class<? extends ConnectorInstance> connectorClass = struct.connectorClass;
    PropertyDescriptor connectorConfigurationProp = UcfUtil.findAnnotatedProperty(connectorClass, ManagedConnectorConfiguration.class);
    MutablePrismSchema connectorSchema = prismContext.schemaFactory().createPrismSchema(struct.connectorObject.getNamespace());
    // Create configuration type - the type used by the "configuration" element
    MutablePrismContainerDefinition<?> configurationContainerDef = connectorSchema.createContainerDefinition(ResourceType.F_CONNECTOR_CONFIGURATION.getLocalPart(), SchemaConstants.CONNECTOR_SCHEMA_CONFIGURATION_TYPE_LOCAL_NAME);
    Class<?> configurationClass = connectorConfigurationProp.getPropertyType();
    BeanWrapper configurationClassBean = new BeanWrapperImpl(configurationClass);
    for (PropertyDescriptor prop : configurationClassBean.getPropertyDescriptors()) {
        if (UcfUtil.hasAnnotation(prop, ConfigurationItem.class)) {
            ItemDefinition<?> itemDef = createConfigurationItemDefinition(configurationContainerDef, prop);
            LOGGER.trace("Configuration item definition for {}: {}", prop.getName(), itemDef);
        }
    }
    return connectorSchema;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) PropertyDescriptor(java.beans.PropertyDescriptor) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) MutablePrismSchema(com.evolveum.midpoint.prism.schema.MutablePrismSchema)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)113 BeanWrapper (org.springframework.beans.BeanWrapper)75 Test (org.junit.jupiter.api.Test)32 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 Test (org.junit.Test)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)17 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)17 TestBean (org.springframework.beans.testfixture.beans.TestBean)17 PropertyDescriptor (java.beans.PropertyDescriptor)14 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)12 HashSet (java.util.HashSet)11 BeansException (org.springframework.beans.BeansException)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 IOException (java.io.IOException)4