// 4) Creates a proxy handler for service invocation.
ProxyHandler metadataProxyHandler = createProxyHandler();
// 5) ..and invokes the GetMetadata on the first member.
WsResourceClient wsResourceClient = resources[0];
wsResourceClient.setTrace(true);
// Resource Metadata Descriptor
String dialect = "http://docs.oasis-open.org/wsrf/rmd-1";
Object [] inputParameters = {dialect};
// RDM is the first element of the returned array.
// The first element is a wsx:Metadata containing all resource properties.
Element [] metadata = (Element[]) wsResourceClient.invoke(metadataProxyHandler, inputParameters);
Element resourceMetadataDescriptor = metadata[0];
// 6) Now we need WSDL in order to catch datatypes
dialect = "http://schemas.xmlsoap.org/wsdl/";
inputParameters = new Object[]{dialect};
metadata = (Element[]) wsResourceClient.invoke(metadataProxyHandler, inputParameters);
Element wsdl = metadata[0];
//7) Defines sample values used for update property.
Map<String, Object> sampleValues = new HashMap<String, Object>();
sampleValues.put("xsd:string","This is a string.");
sampleValues.put("xsd:integer",new Integer(12345));
sampleValues.put("xsd:int",new Integer(54321));
sampleValues.put("xsd:long",new Integer(12345));
sampleValues.put("xsd:double",new Double(12345.6d));
sampleValues.put("xsd:float",new Float(123.4f));
sampleValues.put("xsd:short",new Short((short)12));
// 8) using XPath navigates xml in order to get the list of all properties.
Element [] properties = XmlUtils.findInSubTree(
resourceMetadataDescriptor,
new QName("http://docs.oasis-open.org/wsrf/rmd-1","Property","wsrmd"));
Element [] wsdlElements = XmlUtils.findInSubTree(
wsdl,
new QName("http://www.w3.org/2001/XMLSchema","element","xsd"));
// Did we find at least one writable property?
boolean atLeastOnePropertyHasBeenFound = false;
for (Element property : properties)
{
// Sanity check : if the property is read-only then proceed with next
// property.
if (!"read-write".equals(property.getAttribute("modifiability")))
{
continue;
}
String attributeName = property.getAttribute("name"); // = qman:<Attribute Name>
// For this example we are only interested on qman namespace related properties...
if (attributeName.startsWith("qman"))
{
String attributeNameWithoutPrefix = attributeName.replaceFirst("qman:", ""); // = <Attribute Name>
for (Element wsdlElement : wsdlElements)
{
String name = wsdlElement.getAttribute("name");
String type = wsdlElement.getAttribute("type");
if ((name != null) && (attributeNameWithoutPrefix.equals(name)) && (type != null))
{
Object newValue = sampleValues.get(type);
if (newValue != null)
{
atLeastOnePropertyHasBeenFound = true;
inputParameters = new Object[] {newValue};
// 9) Makes a GetResourcePropertiesRequest in order to get the current value.
QName propertyQName = new QName(
"http://amqp.apache.org/qpid/management/qman",
name,
"qman");
// The returned value is really an array because property shoudl be a multi-value property.
// So in order to get its value we need to extract the first value.
Object currentValue = wsResourceClient.getPropertyAsObject(propertyQName,newValue.getClass());
// 10a) If the property is not set (value is null) then an "Insert" request must be sent.
if (currentValue == null || Array.getLength(currentValue) == 0)
{
wsResourceClient.insertResourceProperty(propertyQName,inputParameters);
}
// 10b) If the property is not null then an "Update" request must be sent.
else
{
wsResourceClient.updateResourceProperty(propertyQName,inputParameters);
}
// 11) Let's query again the resource using GetResourceProperties in order to ensure the
// previous property has been properly updated.
currentValue = wsResourceClient.getPropertyAsObject(propertyQName,newValue.getClass());
String resultMessage = (newValue.equals(Array.get(currentValue, 0)))
? "Resource has been correctly updated."
: "Something was wrong : resource seems not to be properly updated.";