String name = property.getMappedName();
if (name == null)
name = property.getName();
// Get the attribute value
ServiceValueMetaData attributeValue = null;
for (ServiceAttributeMetaData amd : md.getAttributes())
{
// The compare is case-insensitive due to the attribute/javabean case mismatch
if (amd.getName().equalsIgnoreCase(name))
{
attributeValue = amd.getValue();
break;
}
}
// There may not be an attribute value, see if there is a matching property
// Unwrap the value before, so that we can recreate empty values
Object plainValue = null;
// Look for a MetaMapper
MetaType propertyType = property.getMetaType();
MetaMapper metaMapper = property.getTransientAttachment(MetaMapper.class);
Type mappedType = null;
if(metaMapper != null)
{
mappedType = metaMapper.mapToType();
plainValue = metaMapper.unwrapMetaValue(value);
}
else
{
PropertyInfo propertyInfo = beanInfo.getProperty(name);
plainValue = metaValueFactory.unwrap(value, propertyInfo.getType());
}
if (attributeValue == null)
{
String aname = mapAttributeName(md, name);
if(aname != null)
{
ServiceAttributeMetaData attr = new ServiceAttributeMetaData();
attr.setName(aname);
// Check if this is mapped to a Element
if(mappedType != null && mappedType.equals(Element.class))
{
attributeValue = new ServiceElementValueMetaData();
}
else if(plainValue != null)
{
// Create a text value
String textValue = String.valueOf(plainValue);
// Don't create a empty value
if(textValue.trim().length() > 0 )
attributeValue = new ServiceTextValueMetaData(textValue);
}
// Don't create a null serviceAttribute
if(attributeValue == null)
return;
// Add
attr.setValue(attributeValue);
md.addAttribute(attr);
}
}
if (attributeValue != null)
{
// Unwrap the ServiceValueMetaData types
if (attributeValue instanceof ServiceTextValueMetaData)
{
String textValue = plainValue != null ? String.valueOf(plainValue) : null;
ServiceTextValueMetaData text = (ServiceTextValueMetaData) attributeValue;
text.setText(textValue);
}
else if (attributeValue instanceof ServiceElementValueMetaData)
{
if(plainValue != null)
((ServiceElementValueMetaData) attributeValue).setElement((Element) plainValue);
}
else if (attributeValue instanceof ServiceDependencyValueMetaData)
{
ServiceDependencyValueMetaData depends = (ServiceDependencyValueMetaData) attributeValue;
if (plainValue instanceof ObjectName)
depends.setObjectName((ObjectName) plainValue);
else
depends.setDependency(String.valueOf(plainValue));
}
// TODO: unwrap other ServiceValueMetaData types
else
{
throw new IllegalArgumentException("Unhandled attribute value type: " + name + "/" + md+", class="+attributeValue.getClass());
}
}
else
throw new IllegalArgumentException("No matching attribute found: " + name + "/" + md);
}