Examples: Resource list

The following examples show various ways of working with resource lists:

Example: Getting and printing the contents of a ResourceList

One example of a concrete subclass of ResourceList is com.ibm.as400.resource.RJobList, which represents a list of iSeries jobs. RJobList supports many selection IDs and sort IDs, each of which can be used to filter or sort the list.This example prints the contents of an RJobList:

   // Create an RJobList object to represent a list of jobs.
   AS400 system = new AS400("MYSYSTEM", "MYUSERID", "MYPASSWORD");
   RJobList jobList = new RJobList(system);

   // Filter the list to include only interactive jobs.
   jobList.setSelectionValue(RJobList.JOB_TYPE, RJob.JOB_TYPE_INTERACTIVE);

   // Sort the list by user name, then job name.
   Object[] sortValue = new Object[] { RJob.USER_NAME, RJob.JOB_NAME };
   jobList.setSortValue(sortValue);

   // Open the list and wait for it to complete.
   jobList.open();
   jobList.waitForComplete();

   // Read and print the contents of the list.
   long length = jobList.getListLength();
   for(long i = 0; i < length; ++i)
   {
       System.out.println(jobList.resourceAt(i));
   }

   // Close the list.
   jobList.close();

Example: Using generic code to work with resources

In addition to using concrete subclasses directly, you can write generic code to work with any ResourceList subclass. Such code may improve reusability and maintainability and will work with future ResourceList subclasses without modification.

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

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); } }
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(); }

Example: Presenting a resource list in a servlet

Use the ResourceListRowData class in conjunction with the HTMLFormConverter or HTMLTableConverter class to present a resource list in a servlet.

The columns for a ResourceListRowData object are specified as an array of column attribute IDs while each row represents a resource object.

   // Create the resource list.  This example creates 
   // a list of all messages in the current user's message
   // queue.
   AS400 system = new AS400("MYSYSTEM", "MYUSERID", "MYPASSWORD");
   RMessageQueue messageQueue = new RMessageQueue(system, RMessageQueue.CURRENT);

   // Create the ResourceListRowData object.  In this example,
   // there are four columns in the table.  The first column 
   // contains the icons and names for each message in the
   // message queue.  The remaining columns contain the text, 
   // severity, and type for each message.
   ResourceListRowData rowdata = new ResourceListRowData(messageQueue, 
       new Object[] { null, RQueuedMessage.MESSAGE_TEXT, RQueuedMessage.MESSAGE_SEVERITY,
                      RQueuedMessage.MESSAGE_TYPE } );

   // Create HTMLTable and HTMLTableConverter objects to
   // use for generating and customizing the HTML tables.
   HTMLTable table = new HTMLTable();
   table.setCellSpacing(6);
   table.setBorderWidth(8);

   HTMLTableConverter converter = new HTMLTableConverter();
   converter.setTable(table);
   converter.setUseMetaData(true);

   // Generate the HTML table.
   String[] html = converter.convert(rowdata);
   System.out.println(html[0]);