In-memory cache of the set of users, groups, and their associated roles,
stored in an XML-formatted file that conforms to DTD found in the
file in this directory.
Method from org.apache.tomcat.security.file.FileRealmDatabase Detail: |
void addGroup(FileRealmGroup group) {
groups.put(group.getName(), group);
}
[Package Private] Add this group to the set of defined groups. |
void addRole(String role) {
roles.put(role, role);
}
[Package Private] Add this role to the set of defined roles. |
void addUser(FileRealmUser user) {
users.put(user.getName(), user);
}
[Package Private] Add this user to the set of defined users. |
public FileRealmGroup createGroup(String name) {
if (getGroup(name) != null)
throw new IllegalArgumentException(
sm.getString("file.createGroup.exists", name));
return (new FileRealmGroup(this, name));
}
Create and return a new group. |
public FileRealmUser createUser(String name,
String password) {
if (getUser(name) != null)
throw new IllegalArgumentException(
sm.getString("file.createUser.exists", name));
return (new FileRealmUser(this, name, password));
}
Create and return a new user. |
public FileRealmUser createUser(String name,
byte[] password) {
if (getUser(name) != null)
throw new IllegalArgumentException(
sm.getString("file.createUser.exists", name));
return (new FileRealmUser(this, name, password));
}
Create and return a new user. |
public FileRealmGroup getGroup(String name) {
return ((FileRealmGroup) groups.get(name));
}
Return the group with the specified name, if any. |
public Enumeration getGroups() {
return (groups.elements());
}
Return an enumeration of the defined groups in this database. |
public Enumeration getRoles() {
return (roles.keys());
}
Return an enumeration of the defined roles in this database. |
public FileRealmUser getUser(String name) {
return ((FileRealmUser) users.get(name));
}
Return the user with the specified name, if any. |
public Enumeration getUsers() {
return (users.elements());
}
Return an enumeration of the defined users in this database. |
public boolean hasRole(String role) {
return (roles.get(role) != null);
}
Is the specified role valid within this database? |
public void read(InputStream stream) throws IOException, SAXParseException, SAXException {
reset();
// Parse the input stream into an XMLTree
XMLParser parser = new XMLParser();
XMLTree config = parser.process(stream);
if (!config.getName().equals(Constants.Element.TOMCAT_USERS))
return;
Enumeration e;
// Process the defined users
e = config.getElements(Constants.Element.USER).elements();
while (e.hasMoreElements())
readUser((XMLTree) e.nextElement());
// Process the defined groups
e = config.getElements(Constants.Element.GROUP).elements();
while (e.hasMoreElements())
readGroup((XMLTree) e.nextElement());
// Process the defined roles
e = config.getElements(Constants.Element.ROLE).elements();
while (e.hasMoreElements())
readRole((XMLTree) e.nextElement());
}
Load the contents of this database from the specified input stream.
IMPLEMENTATION NOTE: The order of processing (users, groups, and
then roles) is important to correctly process XML files with forward
references in them. |
void remove(FileRealmGroup group) {
groups.remove(group.getName());
}
[Package Private] Remove this group from the set of defined groups. |
void remove(String role) {
roles.remove(role);
}
[Package Private] Remove this role from the set of defined roles. |
void remove(FileRealmUser user) {
users.remove(user.getName());
}
[Package Private] Remove this user from the set of defined users. |
public void reset() {
groups.clear();
roles.clear();
users.clear();
}
Reset the contents of this database so that it can be reused |
public void write(OutputStream stream) throws IOException {
// XXX - Yes, this should really create a DOM tree and ask it to
// output itself. At this time, however, that approach would introduce
// another dependency on which XML parser is being used. Once
// a standardized XML interface is selected, this will be modified.
// XXX - Does not support "< anyone/ >" membership in groups or roles.
PrintWriter writer = new PrintWriter(stream);
writer.println("< tomcat-users >");
// Render user elements for all defined users
Enumeration users = getUsers();
while (users.hasMoreElements()) {
FileRealmUser user = (FileRealmUser) users.nextElement();
writer.println(" < user name=\"" + user.getName() +
"\" password=\"" +
HexUtils.convert(user.getPassword()) + "\" / >");
}
// Render group elements for all defined groups
Enumeration groups = getGroups();
while (groups.hasMoreElements()) {
FileRealmGroup group = (FileRealmGroup) groups.nextElement();
writer.println(" < group name=\"" + group.getName() + "\" >");
users = group.getUsers();
while (users.hasMoreElements()) {
FileRealmUser user = (FileRealmUser) users.nextElement();
writer.println(" < user-member name=\"" +
user.getName() + "\" / >");
}
writer.println(" < /group >");
}
// Render role elements for all defined roles
Enumeration roles = getRoles();
while (roles.hasMoreElements()) {
String role = (String) roles.nextElement();
writer.println(" < role name=\"" + role + "\" >");
users = getUsers();
while (users.hasMoreElements()) {
FileRealmUser user = (FileRealmUser) users.nextElement();
if (!user.hasRole(role))
continue;
writer.println(" < user-member name=\"" +
user.getName() + "\" / >");
}
groups = getGroups();
while (groups.hasMoreElements()) {
FileRealmGroup group = (FileRealmGroup) groups.nextElement();
if (!group.hasRole(role))
continue;
writer.println(" < group-member name=\"" +
group.getName() + "\" / >");
}
writer.println(" < /role >");
}
// Finish the output of this XML file
writer.println("< /tomcat-users >");
writer.flush();
}
Write the contents of this database to the specified output stream,
in a format suitable for loading via the read() method. |