Finding and retrieving resources by name
The finding and retrieving resources by name example shows how to find a specific resource type, in this case a CommandSet. Use this example to find a CommandSet or any other resource type.
In this example, the search operation is done based on
the name of the resource. In general, a search could find more than
one instance matching the search criteria, in which case a Collection
is returned. For example, CommandSets with the same name may exist
in multiple realms. Searching on a common parent of those realms will
return all matching resources. (NetworkRescources are the exception
in that the name must be globally unique.) Also, the name can include
wildcard characters, such as "cmdset*"
. It is highly
recommended to use the static fields on the Resource class to define
the resource types.
ApiSession session;
ResourceManager rMgr;
ResourceContainerKey containerKey;//realm to search the search
boolean subrealmFlag;//whether to include subrealms
String resourceName;//name of resource to find
.
.
.
//it is assumed the session has been established
rMgr = session.resourceManager();
.
.
.
//it is assumed that the search values have been set
ResourceSearch rs = rMgr.dataFactory().newResourceSearch();
rs.setCriterion(null);
rs.setResourceName(resourceName);
ArrayList<String> rTypeAL = new ArrayList<String>();
rTypeAL.add(Resource.RESOURCE_COMMANDSET); //set resource type(s) as needed
rs.setResourceTypes(rTypeAL);
rs.setStartingContainer(containerKey);
rs.setSubRealms(subrealmFlag);
//now do the search
Collection<ResourceContentDescriptor> csDescs = rMgr.search(rs);
if (csDescs == null || csDescs.isEmpty()) {
//handle case of no matches
} else {
//for each descriptor, retrieve the corresponding CommandSet object
ArrayList<CommandSet> csColl = new ArrayList<CommandSet>();
Iterator csDescIter = csDescs.iterator();
while (csDescIter.hasNext()) {
//retrieve the CommandSet for this descriptor
ResourceContentDescriptor csDesc =
(ResourceContentDescriptor)csDescIter.next();
csColl.add((CommandSet)rMgr.getResource(csDesc));
}
}