Search in sources :

Example 41 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.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ConfigurationException("Cannot instantiate configuration " + configurationClass);
    }
    BeanWrapper configurationClassBean = new BeanWrapperImpl(configurationObject);
    for (Item<?, ?> configurationItem : configurationContainer.getItems()) {
        if (!(configurationItem instanceof PrismProperty<?>)) {
            throw new ConfigurationException("Only properties are supported for now");
        }
        PrismProperty<?> configurationProperty = (PrismProperty<?>) configurationItem;
        Object realValue = configurationProperty.getRealValue();
        configurationClassBean.setPropertyValue(configurationProperty.getElementName().getLocalPart(), realValue);
    }
    connectorBean.setPropertyValue(connectorConfigurationProp.getName(), configurationObject);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PropertyDescriptor(java.beans.PropertyDescriptor) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException)

Example 42 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project opennms by OpenNMS.

the class AbstractXmlCollectionHandler method parseString.

/**
 * Parses the string.
 *
 * <p>Valid placeholders are:</p>
 * <ul>
 * <li><b>ipAddr|ipAddress</b>, The Node IP Address</li>
 * <li><b>step</b>, The Collection Step in seconds</li>
 * <li><b>nodeId</b>, The Node ID</li>
 * <li><b>nodeLabel</b>, The Node Label</li>
 * <li><b>foreignId</b>, The Node Foreign ID</li>
 * <li><b>foreignSource</b>, The Node Foreign Source</li>
 * <li>Any asset property defined on the node.</li>
 * </ul>
 *
 * @param reference the reference
 * @param unformattedString the unformatted string
 * @param node the node
 * @param ipAddress the IP address
 * @param collectionStep the collection step
 * @param parameters the service parameters
 * @return the string
 * @throws IllegalArgumentException the illegal argument exception
 */
protected static String parseString(final String reference, final String unformattedString, final OnmsNode node, final String ipAddress, final Integer collectionStep, final Map<String, String> parameters) throws IllegalArgumentException {
    if (unformattedString == null || node == null)
        return null;
    String formattedString = unformattedString.replaceAll("[{](?i)(ipAddr|ipAddress)[}]", ipAddress);
    formattedString = formattedString.replaceAll("[{](?i)step[}]", collectionStep.toString());
    formattedString = formattedString.replaceAll("[{](?i)nodeId[}]", node.getNodeId());
    if (node.getLabel() != null)
        formattedString = formattedString.replaceAll("[{](?i)nodeLabel[}]", node.getLabel());
    if (node.getForeignId() != null)
        formattedString = formattedString.replaceAll("[{](?i)foreignId[}]", node.getForeignId());
    if (node.getForeignSource() != null)
        formattedString = formattedString.replaceAll("[{](?i)foreignSource[}]", node.getForeignSource());
    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        formattedString = formattedString.replaceAll("[{]parameter:" + entry.getKey() + "[}]", entry.getValue());
    }
    if (node.getAssetRecord() != null) {
        BeanWrapper wrapper = new BeanWrapperImpl(node.getAssetRecord());
        for (PropertyDescriptor p : wrapper.getPropertyDescriptors()) {
            Object obj = wrapper.getPropertyValue(p.getName());
            if (obj != null) {
                String objStr = obj.toString();
                try {
                    // NMS-7381 - if pulling from asset info you'd expect to not have to encode reserved words yourself.
                    objStr = URLEncoder.encode(obj.toString(), StandardCharsets.UTF_8.name());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                formattedString = formattedString.replaceAll("[{](?i)" + p.getName() + "[}]", objStr);
            }
        }
    }
    if (formattedString.matches(".*[{].+[}].*"))
        throw new IllegalArgumentException("The " + reference + " " + formattedString + " contains unknown placeholders.");
    return formattedString;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PropertyDescriptor(java.beans.PropertyDescriptor) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XmlObject(org.opennms.protocols.xml.config.XmlObject) Map(java.util.Map)

Example 43 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project opennms by OpenNMS.

the class XmlTest method assertDepthEquals.

private static void assertDepthEquals(final int depth, final String propertyName, final Object expected, Object actual) {
    if (expected == null && actual == null) {
        return;
    } else if (expected == null) {
        fail("expected " + propertyName + " was null but actual was not!");
    } else if (actual == null) {
        fail("actual " + propertyName + " was null but expected was not!");
    }
    final String assertionMessage = propertyName == null ? ("Top-level objects (" + expected.getClass().getName() + ") do not match.") : ("Properties " + propertyName + " do not match.");
    if (expected.getClass().getName().startsWith("java") || actual.getClass().getName().startsWith("java")) {
        // java primitives, just do assertEquals
        if (expected instanceof Object[] || actual instanceof Object[]) {
            assertTrue(assertionMessage, Arrays.equals((Object[]) expected, (Object[]) actual));
        } else {
            assertEquals(assertionMessage, expected, actual);
        }
        return;
    }
    final BeanWrapper expectedWrapper = new BeanWrapperImpl(expected);
    final BeanWrapper actualWrapper = new BeanWrapperImpl(actual);
    final Set<String> properties = new TreeSet<>();
    for (final PropertyDescriptor descriptor : expectedWrapper.getPropertyDescriptors()) {
        properties.add(descriptor.getName());
    }
    for (final PropertyDescriptor descriptor : actualWrapper.getPropertyDescriptors()) {
        properties.add(descriptor.getName());
    }
    properties.remove("class");
    for (final String property : properties) {
        final PropertyDescriptor expectedDescriptor = expectedWrapper.getPropertyDescriptor(property);
        final PropertyDescriptor actualDescriptor = actualWrapper.getPropertyDescriptor(property);
        if (expectedDescriptor != null && actualDescriptor != null) {
            // both have descriptors, so walk the sub-objects
            Object expectedValue = null;
            Object actualValue = null;
            try {
                expectedValue = expectedWrapper.getPropertyValue(property);
            } catch (final Exception e) {
            }
            try {
                actualValue = actualWrapper.getPropertyValue(property);
            } catch (final Exception e) {
            }
            assertDepthEquals(depth + 1, property, expectedValue, actualValue);
        } else if (expectedDescriptor != null) {
            fail("Should have '" + property + "' property on actual object, but there was none!");
        } else if (actualDescriptor != null) {
            fail("Should have '" + property + "' property on expected object, but there was none!");
        }
    }
    if (expected instanceof Object[] || actual instanceof Object[]) {
        final Object[] expectedArray = (Object[]) expected;
        final Object[] actualArray = (Object[]) actual;
        assertTrue(assertionMessage, Arrays.equals(expectedArray, actualArray));
    } else if (expected instanceof long[] || actual instanceof long[]) {
        final long[] expectedArray = (long[]) expected;
        final long[] actualArray = (long[]) actual;
        assertTrue(assertionMessage, Arrays.equals(expectedArray, actualArray));
    } else if (expected instanceof int[] || actual instanceof int[]) {
        final int[] expectedArray = (int[]) expected;
        final int[] actualArray = (int[]) actual;
        assertTrue(assertionMessage, Arrays.equals(expectedArray, actualArray));
    } else if (expected instanceof byte[] || actual instanceof byte[]) {
        final byte[] expectedArray = (byte[]) expected;
        final byte[] actualArray = (byte[]) actual;
        assertTrue(assertionMessage, Arrays.equals(expectedArray, actualArray));
    } else {
        expected.getClass().isPrimitive();
        assertEquals(assertionMessage, expected, actual);
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PropertyDescriptor(java.beans.PropertyDescriptor) TreeSet(java.util.TreeSet) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException)

Example 44 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project grails-core by grails.

the class DefaultGrailsPlugin method initialisePlugin.

private void initialisePlugin(Class<?> clazz) {
    pluginGrailsClass = new GrailsPluginClass(clazz);
    plugin = (GroovyObject) pluginGrailsClass.newInstance();
    if (plugin instanceof Plugin) {
        Plugin p = (Plugin) plugin;
        p.setApplicationContext(applicationContext);
        p.setPlugin(this);
        p.setGrailsApplication(grailsApplication);
        p.setPluginManager(manager);
    } else if (plugin instanceof GrailsApplicationAware) {
        ((GrailsApplicationAware) plugin).setGrailsApplication(grailsApplication);
    }
    pluginBean = new BeanWrapperImpl(plugin);
    // configure plugin
    evaluatePluginVersion();
    evaluatePluginDependencies();
    evaluatePluginLoadAfters();
    evaluateProvidedArtefacts();
    evaluatePluginEvictionPolicy();
    evaluateOnChangeListener();
    evaluateObservedPlugins();
    evaluatePluginStatus();
    evaluatePluginScopes();
    evaluatePluginExcludes();
    evaluateTypeFilters();
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsApplicationAware(grails.core.support.GrailsApplicationAware) GrailsPlugin(grails.plugins.GrailsPlugin) Plugin(grails.plugins.Plugin)

Example 45 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project grails-core by grails.

the class DefaultBeanConfiguration method createBeanDefinition.

protected AbstractBeanDefinition createBeanDefinition() {
    AbstractBeanDefinition bd = new GenericBeanDefinition();
    if (!constructorArgs.isEmpty()) {
        ConstructorArgumentValues cav = new ConstructorArgumentValues();
        for (Object constructorArg : constructorArgs) {
            cav.addGenericArgumentValue(constructorArg);
        }
        bd.setConstructorArgumentValues(cav);
    }
    if (clazz != null) {
        bd.setLazyInit(clazz.getAnnotation(Lazy.class) != null);
        bd.setBeanClass(clazz);
    }
    bd.setScope(singleton ? AbstractBeanDefinition.SCOPE_SINGLETON : AbstractBeanDefinition.SCOPE_PROTOTYPE);
    if (parentName != null) {
        bd.setParentName(parentName);
    }
    wrapper = new BeanWrapperImpl(bd);
    return bd;
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)85 BeanWrapper (org.springframework.beans.BeanWrapper)57 Test (org.junit.Test)47 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)21 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)20 ITestBean (org.springframework.tests.sample.beans.ITestBean)17 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)17 TestBean (org.springframework.tests.sample.beans.TestBean)17 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)11 PropertyDescriptor (java.beans.PropertyDescriptor)7 BeansException (org.springframework.beans.BeansException)7 ArrayList (java.util.ArrayList)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)4 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 PrivilegedAction (java.security.PrivilegedAction)4 Map (java.util.Map)4