InfoCenter Home >
5: Securing applications -- special topics >
5.2: Introduction to custom registries >
5.2.2: Implementing the CustomRegistry interface >
5.2.2.2: Writing the sample application >
5.2.2.2.4: The getUsers and getGroups methods

5.2.2.2.4: The getUsers and getGroups methods

The getUsers and getGroups methods are used to retrieve lists of user or group names in the registry. The CustomRegistry interface defines two of each method: a version that takes no arguments and returns the names of all users or groups, and a version that takes a string and returns the names of the users or groups that match a string:

  • getUsers()
  • getUsers(pattern)
  • getGroups()
  • getGroups(pattern)

WebSphere Application Server expects these methods to return null values if no users or groups, or none matching the pattern, are found. All the methods are expected to throw the CustomRegistryException exception for any other conditions.

The getUsers(pattern) and getGroups(pattern) methods must be able to handle arguments consisting of the full name of an existing user or group, which matches a single user or group, and of the asterisk (*) character, which matches all users or groups. At a minimum, these methods must behave as follows:

  • If the argument is the complete name of a user or group, that user or group must be returned.
  • If the argument is the asterisk (*) character, the names of all users or groups must be returned. This can be implemented by calling either getUsers() or getGroups().

Developers can introduce more sophisticated pattern-matching techniques. The techniques implemented here determine the retrieval strategies available on the administrative console. For registries involving thousands of users or groups, the ability to retrieve names based on partial matches, common endings, and so forth can greatly enhance the usability of the console.

Figure 8 shows the implementation of the getUsers() method for the example registry. The method iterates through the user-information file and collects the user name from each entry. When the file is exhausted, the method returns the list of user names. The getGroups() method does the same work on the group-information file.

Figure 8. Code example: The getUsers() and getGroups() methods in the FileRegistrySample class

public List getUsers()
throws CustomRegistryException
{
String s;
List allUsers = new ArrayList();
BufferedReader in = null;

try {
in = fileOpen(USERFILENAME);
while ((s=in.readLine())!=null) {
if (!s.startsWith("#")) {
int index = s.indexOf(":");
allUsers.add(s.substring(0,index));
}
}
}
catch (Exception ex) {
throw new CustomRegistryException(ex.getMessage());
}
finally {
fileClose(in);
}

return allUsers;
}

public List getGroups()
throws CustomRegistryException
{
String s;
List allGroups = new ArrayList();
BufferedReader in = null;

try {
in = fileOpen(GROUPFILENAME);
...
}
catch (Exception ex) { ... }
finally { ... }

return allGroups;
}

The behavior of the methods that retrieve the names of all users or groups is straightforward, but the definition of what constitutes a match, used by the pattern-matching methods, varies with the registry used and the types of information it stores. Matching on complete user or group name yields at most one match. Partial and wildcard matches can be implemented to increase the number of matches. For other registries, different characteristics can make different matching strategies useful.

Figure 9 shows the implementation of the getUser(pattern) method for the example registry. The method iterates through the user-information file and attempts to match the user name with the provided string. If they match, the user name is added to a list. When the file is exhausted, the method returns the list of user names. The getGroups(pattern) method does the same work on the group-information file. The match method is a local, private method that returns TRUE if the two arguments match. This method makes use of the RegExpSample class.

Figure 9. Code example: The getUsers(pattern) and getGroups(pattern) methods in the FileRegistrySample class

public List getUsers(String pattern)
throws CustomRegistryException
{
String s;
List allUsers = new ArrayList();
BufferedReader in = null;
try {
in = fileOpen(USERFILENAME);
while ((s=in.readLine())!=null) {
if (!s.startsWith("#")) {
int index = s.indexOf(":");
String user = s.substring(0,index);
if (match(user, pattern))
allUsers.add(user);
}
}
}
catch (Exception ex) {
throw new CustomRegistryException(ex.getMessage());
}
finally {
fileClose(in);
}

return allUsers;
}

public List getGroups(String pattern)
throws CustomRegistryException
{
String s;
List allGroups = new ArrayList();
BufferedReader in = null;

try {
in = fileOpen(GROUPFILENAME);
...
}
catch (Exception ex) { ... }
finally { ... }

return allGroups;
}

Go to previous article: The isValidUser and isValidGroup methods Go to next article: The getUniqueUserId and getUniqueGroupId methods

 

 
Go to previous article: The isValidUser and isValidGroup methods Go to next article: The getUniqueUserId and getUniqueGroupId methods