You can write generic code to work with any Resource or ChangeableResource subclass. Such code may improve reusability and maintainability and will work with future Resource or ChangeableResource subclasses without modification.
Every attribute has an associated attribute meta data object (com.ibm.as400.resource.ResourceMetaData) that describes various properties of the attribute. These properties include whether or not the attribute is read only and what the default and possible values are.
This is an example of generic code that prints the value of every attribute supported by a resource:
void printAllAttributeValues(Resource resource) throws ResourceException { // Get the attribute meta data. ResourceMetaData[] attributeMetaData = resource.getAttributeMetaData();
// Loop through all attributes and print the values. for(int i = 0; i < attributeMetaData.length; ++i) { Object attributeID = attributeMetaData[i].getID(); Object value = resource.getAttributeValue(attributeID); System.out.println("Attribute " + attributeID + " = " + value); } }
This is an example of generic code that resets all attributes of a ChangeableResource to their default values:
void resetAttributeValues(ChangeableResource resource) throws ResourceException { // Get the attribute meta data. ResourceMetaData[] attributeMetaData = resource.getAttributeMetaData();
// Loop through all attributes. for(int i = 0; i < attributeMetaData.length; ++i) { // If the attribute is changeable (not read only), then // reset its value to the default. if (! attributeMetaData[i].isReadOnly()) { Object attributeID = attributeMetaData[i].getID(); Object defaultValue = attributeMetaData[i].getDefaultValue(); resource.setAttributeValue(attributeID, defaultValue); } } // Commit all of the attribute changes. resource.commitAttributeChanges(); }