106 lines
5.0 KiB
HTML
106 lines
5.0 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Example: Using generic code to access resources" />
|
|
<meta name="abstract" content="" />
|
|
<meta name="description" content="" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="resourcegeneric" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Example: Using generic code to access resources</title>
|
|
</head>
|
|
<body id="resourcegeneric"><a name="resourcegeneric"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Using generic code to access resources</h1>
|
|
<div><p></p>
|
|
<div class="section"><p>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.</p>
|
|
<p>Every attribute has an associated attribute meta
|
|
data object (<a href="javadoc/com/ibm/as400/resource/ResourceMetaData.html#NAVBAR_TOP">com.ibm.as400.resource.ResourceMetaData</a>)
|
|
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.</p>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Examples:</h4><p><strong><span class="synph" id="resourcegeneric__usinggenericcode"><a name="resourcegeneric__usinggenericcode"><!-- --></a><span class="kwd"></span></span>Example:
|
|
Printing the contents of a ResourceList</strong></p>
|
|
<p>Here is an example of generic
|
|
code that prints some of the contents of a ResourceList:</p>
|
|
<div class="p"><pre> 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));
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<p><strong>Example: Using ResourceMetaData to access every
|
|
attribute supported by a resource</strong></p>
|
|
<p>This is an example of generic
|
|
code that prints the value of every attribute supported by a resource:</p>
|
|
<div class="p"><pre>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);
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<p><strong>Example: Using ResourceMetaData to reset every attribute
|
|
of a ChangeableResource</strong></p>
|
|
<p>This is an example of generic code that
|
|
resets all attributes of a ChangeableResource to their default values:</p>
|
|
<div class="p"><pre>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();
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |