Example usage for org.springframework.beans PropertyAccessor setPropertyValue

List of usage examples for org.springframework.beans PropertyAccessor setPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans PropertyAccessor setPropertyValue.

Prototype

void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;

Source Link

Document

Set the specified value as current property value.

Usage

From source file:org.uimafit.spring.util.ResourceInitializationUtil.java

/**
 * Handle Spring-initialization of resoures produced by the UIMA framework.
 *///  w w w . j  ava2 s .c om
public static Resource initResource(Resource aResource, ApplicationContext aApplicationContext) {
    AutowireCapableBeanFactory beanFactory = aApplicationContext.getAutowireCapableBeanFactory();

    if (aResource instanceof PrimitiveAnalysisEngine_impl) {
        PropertyAccessor pa = PropertyAccessorFactory.forDirectFieldAccess(aResource);

        // Access the actual AnalysisComponent and initialize it
        AnalysisComponent analysisComponent = (AnalysisComponent) pa.getPropertyValue("mAnalysisComponent");
        initializeBean(beanFactory, analysisComponent, aResource.getMetaData().getName());
        pa.setPropertyValue("mAnalysisComponent", analysisComponent);

        return aResource;
    } else {
        return (Resource) beanFactory.initializeBean(aResource, aResource.getMetaData().getName());
    }
}

From source file:com.netflix.spinnaker.fiat.roles.google.GoogleDirectoryUserRolesProvider.java

private Directory getDirectoryService() {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jacksonFactory = new JacksonFactory();
    GoogleCredential credential = getGoogleCredential();

    PropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(credential);
    accessor.setPropertyValue("serviceAccountUser", config.getAdminUsername());
    accessor.setPropertyValue("serviceAccountScopes", SERVICE_ACCOUNT_SCOPES);

    return new Directory.Builder(httpTransport, jacksonFactory, credential).setApplicationName("Spinnaker-Fiat")
            .build();/*from   www  .j av a  2 s  .c o  m*/
}

From source file:net.solarnetwork.node.rfxcom.RFXCOMTransceiver.java

public void updateModeSetting(String name, Object value) {
    if (this.status == null) {
        updateStatus();/*from w  w w. ja  v a  2 s .  co  m*/
    }
    if (this.status != null) {
        SetModeMessage msg = new SetModeMessage(mf.incrementAndGetSequenceNumber(), this.status);
        PropertyAccessor bean = PropertyAccessorFactory.forBeanPropertyAccess(msg);
        Object currValue = bean.getPropertyValue(name);
        if (value != null && !value.equals(currValue)) {
            bean.setPropertyValue(name, value);
            setMode(msg);
        }
    }
}

From source file:net.solarnetwork.node.rfxcom.RFXCOMTransceiver.java

/**
 * Update the settings of this class./* ww  w . jav a 2 s.com*/
 * 
 * <p>This method is designed to work with Spring's bean-managed OSGi Configuration
 * Admin service, rather than the container-managed approach of setting properties
 * directly. This is because many of the supported properties require communicating
 * with the RFXCOM device, but those can all be set via a single call. Thus the
 * supported properties of this method are those properties directly available on
 * this class itself, and those available on the {@link SetModeMessage} class.
 * 
 * @param properties the properties to change
 */
public void updateConfiguration(Map<String, ?> properties) {
    Map<String, Object> setModeProperties = new HashMap<String, Object>(properties);
    PropertyAccessor bean = PropertyAccessorFactory.forBeanPropertyAccess(this);

    // if this is NOT something that must be handled via a SetMode command, apply those directly...
    for (Map.Entry<String, ?> me : properties.entrySet()) {
        if (bean.isWritableProperty(me.getKey())) {
            bean.setPropertyValue(me.getKey(), me.getValue());
        } else {
            setModeProperties.put(me.getKey(), me.getValue());
        }
    }

    // and now apply remaining properties via single SetMode, so we only have to talk to
    // device one time

    if (this.status == null) {
        updateStatus();
    }
    if (this.status != null) {
        SetModeMessage msg = new SetModeMessage(mf.incrementAndGetSequenceNumber(), this.status);
        bean = PropertyAccessorFactory.forBeanPropertyAccess(msg);
        boolean changed = false;
        for (Map.Entry<String, Object> me : setModeProperties.entrySet()) {
            if (bean.isReadableProperty(me.getKey())) {
                Object currValue = bean.getPropertyValue(me.getKey());
                if (me.getValue() != null && me.getValue().equals(currValue)) {
                    continue;
                }
            }
            if (bean.isWritableProperty(me.getKey())) {
                bean.setPropertyValue(me.getKey(), me.getValue());
                changed = true;
            }
        }
        if (changed) {
            log.debug("Updating RFXCOM settings to {}", msg);
            setMode(msg);
        }
    }

}

From source file:net.solarnetwork.support.XmlSupport.java

/**
 * Populate JavaBean properties via XPath extraction.
 * //from ww  w .j  a  v a  2s.  c  o  m
 * @param obj
 *        the object to set properties on, or a BeanWrapper
 * @param xml
 *        the XML
 * @param xpathMap
 *        the mapping of JavaBean property names to XPaths
 */
public void extractBeanDataFromXml(PropertyAccessor bean, Node xml, Map<String, XPathExpression> xpathMap) {
    for (Map.Entry<String, XPathExpression> me : xpathMap.entrySet()) {
        String val = extractStringFromXml(xml, me.getValue());
        if (val != null && !"".equals(val)) {
            bean.setPropertyValue(me.getKey(), val);
        }
    }
}

From source file:br.edu.ifes.bd2dao.cgt.Menu.java

private void setValue(String field, Object objeto) {

    PropertyAccessor propAccessor = PropertyAccessorFactory.forBeanPropertyAccess(objeto);
    Scanner sc = new Scanner(System.in);

    Object valorModificado = null;
    Object valorAtual;//from ww w. j  a  va 2  s.  co  m

    do {
        try {
            valorModificado = scanSpecific(field, sc, objeto.getClass());
        } catch (NoSuchFieldException ex) {
            ex.printStackTrace();
        }

        propAccessor.setPropertyValue(field, valorModificado);
        valorAtual = propAccessor.getPropertyValue(field);

        if (valorAtual == null)
            valorAtual = new Object();

    } while (!valorAtual.equals(valorModificado));
}

From source file:org.dkpro.lab.engine.impl.DefaultLifeCycleManager.java

@Override
public void configure(TaskContext aParentContext, Task aTask, Map<String, Object> aConfiguration) {
    PropertyAccessor paBean = PropertyAccessorFactory.forBeanPropertyAccess(aTask);
    PropertyAccessor paDirect = PropertyAccessorFactory.forDirectFieldAccess(aTask);
    for (Entry<String, Object> property : aConfiguration.entrySet()) {
        String key = property.getKey();
        Object value = property.getValue();

        // Find all fields that are annotated with a discriminator/property that have
        // a non-default name and might apply.
        for (String prop : ParameterUtil.findBeanPropertiesWithName(aTask, key)) {
            // Try setter - there may be extra logic in the setter
            if (paBean.isWritableProperty(prop)) {
                paBean.setPropertyValue(prop, value);
            }// www  .ja  v  a  2  s.  co m
            // Otherwise try direct access
            else if (paDirect.isWritableProperty(prop)) {
                paDirect.setPropertyValue(prop, value);
            }
        }

        // And try once again for all fields where the name is not explicitly set

        // Try setter - there may be extra logic in the setter
        if (paBean.isWritableProperty(key)) {
            paBean.setPropertyValue(key, value);
        }
        // Otherwise try direct access
        else if (paDirect.isWritableProperty(key)) {
            paDirect.setPropertyValue(key, value);
        }
    }

    if (aTask instanceof ConfigurationAware) {
        ((ConfigurationAware) aTask).setConfiguration(aConfiguration);
    }

    if (aParentContext != null) {
        aParentContext.message("Injected parameters into [" + aTask.getType() + "]");
    }
}

From source file:net.solarnetwork.node.power.modbus.ModbusPowerDatumDataSource.java

private void setRegisterAddressValue(final PropertyAccessor bean, final Integer addr, final String propertyName,
        final Integer propertyValue) {
    if (bean.isWritableProperty(propertyName)) {
        Number value = propertyValue;
        if (registerScaleFactor != null && registerScaleFactor.containsKey(addr)) {
            value = Double.valueOf(value.intValue() * registerScaleFactor.get(addr));
        }//  w  w  w .  j a v a 2  s. c o  m
        log.trace("Setting property {} for address 0x{} to [{}]", propertyName, Integer.toHexString(addr),
                value);
        try {
            bean.setPropertyValue(propertyName, value);
        } catch (PropertyAccessException e) {
            log.warn("Unable to set property {} to {} for address 0x{}: {}", propertyName, value,
                    Integer.toHexString(addr), e.getMostSpecificCause().getMessage());
        }
    } else {
        log.warn("Property {} not available; bad configuration", propertyName);
    }
}

From source file:com.nestedbird.modules.formparser.FormParse.java

/**
 * Write object to the entity//from  w ww  .  j a va  2s .c o m
 *
 * @param accessor The accessor for the existing entity
 * @param key      The fields name we are overwriting
 * @param value    The new value
 * @throws JSONException the json exception
 */
private void writeObjectToEntity(final PropertyAccessor accessor, final String key, final JSONObject value)
        throws JSONException {
    final ResolvableType type = accessor.getPropertyTypeDescriptor(key).getResolvableType();

    accessor.setPropertyValue(key, parseObject(value, type));
}