You can write generic code to work with any Resource, ResourceList or ChangeableResource subclass. Such code may improve reusability and maintainability and will work with future Resource, ResourceList 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.
Example: Printing the contents of a ResourceList
Here is an example of generic code that prints some of the contents of a ResourceList:
void printContents(ResourceList resourceList, long numberOfItems) throws ResourceException { // Open the list and wait for the requested number of items // to become available. resourceList.open(); resourceList.waitForResource(numberOfItems); for(long i = 0; i < numberOfItems; ++i) { System.out.println(resourceList.resourceAt(i)); } }
Example: Using ResourceMetaData to access every attribute supported by a resource
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); } }
Example: Using ResourceMetaData to reset every attribute of a ChangeableResource
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(); }