use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class SnmpTrapNorthbounderConfigDaoTest method testBeanWrapper.
/**
* Test bean wrapper.
*
* @throws Exception the exception
*/
@Test
public void testBeanWrapper() throws Exception {
SnmpTrapSink sink = configDao.getConfig().getSnmpTrapSink("localTest2");
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(sink);
Map<String, String> params = new HashMap<String, String>();
params.put("ipAddress", "192.168.0.1");
Assert.assertEquals("127.0.0.2", sink.getIpAddress());
boolean modified = false;
for (final String key : params.keySet()) {
if (wrapper.isWritableProperty(key)) {
final String stringValue = params.get(key);
final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
wrapper.setPropertyValue(key, value);
modified = true;
}
}
Assert.assertTrue(modified);
Assert.assertEquals("192.168.0.1", sink.getIpAddress());
configDao.save();
configDao.reload();
Assert.assertEquals("192.168.0.1", configDao.getConfig().getSnmpTrapSink("localTest2").getIpAddress());
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class DefaultPluginRegistry method getPluginInstance.
/**
* {@inheritDoc}
*/
@Override
public <T> T getPluginInstance(Class<T> pluginClass, PluginConfig pluginConfig) {
T pluginInstance = beanWithNameOfType(pluginConfig.getPluginClass(), pluginClass);
if (pluginInstance == null) {
return null;
}
Map<String, String> parameters = new HashMap<String, String>(pluginConfig.getParameterMap());
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(pluginInstance);
try {
wrapper.setPropertyValues(parameters);
} catch (BeansException e) {
error(e, "Could not set properties on report definition: {}", e.getMessage());
}
return pluginInstance;
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class MockForeignSourceRepositoryTest method testBeanWrapperAccess.
/**
* This test ensures that the Spring Bean accessor classes work properly
* since our REST implementation uses bean access to update the values.
*/
@Test
public void testBeanWrapperAccess() throws Exception {
createRequisition();
Requisition r = m_foreignSourceRepository.getRequisition(m_defaultForeignSourceName);
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(r);
assertEquals("AC", wrapper.getPropertyValue("node[0].category[0].name"));
assertEquals("UK", wrapper.getPropertyValue("node[0].category[1].name"));
assertEquals("low", wrapper.getPropertyValue("node[0].category[2].name"));
try {
wrapper.getPropertyValue("node[1].category[0].name");
fail("Did not catch expected InvalidPropertyException exception");
} catch (InvalidPropertyException e) {
// Expected failure
}
assertEquals(0, ((RequisitionCategory[]) wrapper.getPropertyValue("node[1].category")).length);
wrapper.setPropertyValue("node[1].categories[0]", new RequisitionCategory("Hello world"));
wrapper.setPropertyValue("node[1].categories[1]", new RequisitionCategory("Hello again"));
assertEquals(2, ((RequisitionCategory[]) wrapper.getPropertyValue("node[1].category")).length);
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class OnmsAssetRecord method mergeRecord.
/**
* Used to merge the contents of one asset record to another. If equals implementation
* returns false, the merge is aborted.
*
* @param newRecord a {@link org.opennms.netmgt.model.OnmsAssetRecord} object.
*/
public void mergeRecord(OnmsAssetRecord newRecord) {
if (!this.equals(newRecord)) {
return;
}
OnmsGeolocation toGeolocation = this.getGeolocation();
if (toGeolocation == null) {
toGeolocation = new OnmsGeolocation();
this.setGeolocation(toGeolocation);
}
final OnmsGeolocation fromGeolocation = newRecord.getGeolocation();
// this works because all asset properties are strings
// if the model dependencies ever change to not include spring, this will break
final BeanWrapper currentBean = PropertyAccessorFactory.forBeanPropertyAccess(this);
final BeanWrapper newBean = PropertyAccessorFactory.forBeanPropertyAccess(newRecord);
final PropertyDescriptor[] pds = newBean.getPropertyDescriptors();
// Don't update these properties
final List<String> blackListedProperties = new ArrayList<>();
blackListedProperties.add("class");
blackListedProperties.add("city");
blackListedProperties.add("zip");
blackListedProperties.add("state");
blackListedProperties.add("country");
blackListedProperties.add("longitude");
blackListedProperties.add("latitude");
blackListedProperties.add("address1");
blackListedProperties.add("address2");
for (final PropertyDescriptor pd : pds) {
final String propertyName = pd.getName();
if (blackListedProperties.contains(propertyName)) {
continue;
}
// This should never fail since both of these objects are of the same type
if (newBean.getPropertyValue(propertyName) != null) {
currentBean.setPropertyValue(propertyName, newBean.getPropertyValue(propertyName));
}
}
toGeolocation.mergeGeolocation(fromGeolocation);
setGeolocation(toGeolocation);
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class SyslogNorthbounderConfigurationResource method updateSyslogDestination.
/**
* Update a specific Syslog Destination.
*
* @param destinationName the destination name
* @param params the parameters map
* @return the response
*/
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("destinations/{destinationName}")
public Response updateSyslogDestination(@PathParam("destinationName") final String destinationName, final MultivaluedMapImpl params) {
writeLock();
try {
boolean modified = false;
SyslogDestination destination = getSyslogDestination(destinationName);
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(destination);
for (final String key : params.keySet()) {
if (wrapper.isWritableProperty(key)) {
final String stringValue = params.getFirst(key);
final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
wrapper.setPropertyValue(key, value);
modified = true;
}
}
if (modified) {
saveConfiguration();
return Response.noContent().build();
}
return Response.notModified().build();
} finally {
writeUnlock();
}
}
Aggregations