| Method from org.apache.catalina.users.MemoryUserDatabase Detail: |
public void close() throws Exception {
save();
synchronized (groups) {
synchronized (users) {
users.clear();
groups.clear();
}
}
}
Finalize access to this user database. |
public Group createGroup(String groupname,
String description) {
MemoryGroup group = new MemoryGroup(this, groupname, description);
synchronized (groups) {
groups.put(group.getGroupname(), group);
}
return (group);
}
Create and return a new Group defined in this user database. |
public Role createRole(String rolename,
String description) {
MemoryRole role = new MemoryRole(this, rolename, description);
synchronized (roles) {
roles.put(role.getRolename(), role);
}
return (role);
}
Create and return a new Role defined in this user database. |
public User createUser(String username,
String password,
String fullName) {
MemoryUser user = new MemoryUser(this, username, password, fullName);
synchronized (users) {
users.put(user.getUsername(), user);
}
return (user);
}
Create and return a new User defined in this user database. |
public Group findGroup(String groupname) {
synchronized (groups) {
return ((Group) groups.get(groupname));
}
}
Return the Group with the specified group name, if any;
otherwise return null. |
public Role findRole(String rolename) {
synchronized (roles) {
return ((Role) roles.get(rolename));
}
}
Return the Role with the specified role name, if any;
otherwise return null. |
public User findUser(String username) {
synchronized (users) {
return ((User) users.get(username));
}
}
Return the User with the specified user name, if any;
otherwise return null. |
public Iterator getGroups() {
// ------------------------------------------------------------- Properties
synchronized (groups) {
return (groups.values().iterator());
}
}
Return the set of Group s defined in this user database. |
public String getId() {
return (this.id);
}
Return the unique global identifier of this user database. |
public String getPathname() {
return (this.pathname);
}
Return the relative or absolute pathname to the persistent storage file. |
public boolean getReadonly() {
return (this.readonly);
}
Returning the readonly status of the user database |
public Iterator getRoles() {
synchronized (roles) {
return (roles.values().iterator());
}
}
Return the set of Role s defined in this user database. |
StringManager getStringManager() {
return (sm);
}
Return the StringManager for use in looking up messages. |
public Iterator getUsers() {
synchronized (users) {
return (users.values().iterator());
}
}
Return the set of User s defined in this user database. |
public boolean isWriteable() {
File file = new File(pathname);
if (!file.isAbsolute()) {
file = new File(System.getProperty("catalina.base"),
pathname);
}
File dir = file.getParentFile();
return dir.exists() && dir.isDirectory() && dir.canWrite();
}
Check for permissions to save this user database
to persistent storage location |
public void open() throws Exception {
synchronized (groups) {
synchronized (users) {
// Erase any previous groups and users
users.clear();
groups.clear();
roles.clear();
// Construct a reader for the XML input file (if it exists)
File file = new File(pathname);
if (!file.isAbsolute()) {
file = new File(System.getProperty("catalina.base"),
pathname);
}
if (!file.exists()) {
return;
}
FileInputStream fis = new FileInputStream(file);
// Construct a digester to read the XML input file
Digester digester = new Digester();
digester.addFactoryCreate
("tomcat-users/group",
new MemoryGroupCreationFactory(this));
digester.addFactoryCreate
("tomcat-users/role",
new MemoryRoleCreationFactory(this));
digester.addFactoryCreate
("tomcat-users/user",
new MemoryUserCreationFactory(this));
// Parse the XML input file to load this database
try {
digester.parse(fis);
fis.close();
} catch (Exception e) {
try {
fis.close();
} catch (Throwable t) {
;
}
throw e;
}
}
}
}
Initialize access to this user database. |
public void removeGroup(Group group) {
synchronized (groups) {
Iterator users = getUsers();
while (users.hasNext()) {
User user = (User) users.next();
user.removeGroup(group);
}
groups.remove(group.getGroupname());
}
}
Remove the specified Group from this user database. |
public void removeRole(Role role) {
synchronized (roles) {
Iterator groups = getGroups();
while (groups.hasNext()) {
Group group = (Group) groups.next();
group.removeRole(role);
}
Iterator users = getUsers();
while (users.hasNext()) {
User user = (User) users.next();
user.removeRole(role);
}
roles.remove(role.getRolename());
}
}
Remove the specified Role from this user database. |
public void removeUser(User user) {
synchronized (users) {
users.remove(user.getUsername());
}
}
Remove the specified User from this user database. |
public void save() throws Exception {
if (getReadonly()) {
return;
}
if (!isWriteable()) {
log.warn(sm.getString("memoryUserDatabase.notPersistable"));
return;
}
// Write out contents to a temporary file
File fileNew = new File(pathnameNew);
if (!fileNew.isAbsolute()) {
fileNew =
new File(System.getProperty("catalina.base"), pathnameNew);
}
PrintWriter writer = null;
try {
// Configure our PrintWriter
FileOutputStream fos = new FileOutputStream(fileNew);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
writer = new PrintWriter(osw);
// Print the file prolog
writer.println("< ?xml version='1.0' encoding='utf-8'? >");
writer.println("< tomcat-users >");
// Print entries for each defined role, group, and user
Iterator values = null;
values = getRoles();
while (values.hasNext()) {
writer.print(" ");
writer.println(values.next());
}
values = getGroups();
while (values.hasNext()) {
writer.print(" ");
writer.println(values.next());
}
values = getUsers();
while (values.hasNext()) {
writer.print(" ");
writer.println(values.next());
}
// Print the file epilog
writer.println("< /tomcat-users >");
// Check for errors that occurred while printing
if (writer.checkError()) {
writer.close();
fileNew.delete();
throw new IOException
(sm.getString("memoryUserDatabase.writeException",
fileNew.getAbsolutePath()));
}
writer.close();
} catch (IOException e) {
if (writer != null) {
writer.close();
}
fileNew.delete();
throw e;
}
// Perform the required renames to permanently save this file
File fileOld = new File(pathnameOld);
if (!fileOld.isAbsolute()) {
fileOld =
new File(System.getProperty("catalina.base"), pathnameOld);
}
fileOld.delete();
File fileOrig = new File(pathname);
if (!fileOrig.isAbsolute()) {
fileOrig =
new File(System.getProperty("catalina.base"), pathname);
}
if (fileOrig.exists()) {
fileOld.delete();
if (!fileOrig.renameTo(fileOld)) {
throw new IOException
(sm.getString("memoryUserDatabase.renameOld",
fileOld.getAbsolutePath()));
}
}
if (!fileNew.renameTo(fileOrig)) {
if (fileOld.exists()) {
fileOld.renameTo(fileOrig);
}
throw new IOException
(sm.getString("memoryUserDatabase.renameNew",
fileOrig.getAbsolutePath()));
}
fileOld.delete();
}
Save any updated information to the persistent storage location for
this user database. |
public void setPathname(String pathname) {
this.pathname = pathname;
this.pathnameOld = pathname + ".old";
this.pathnameNew = pathname + ".new";
}
Set the relative or absolute pathname to the persistent storage file. |
public void setReadonly(boolean readonly) {
this.readonly = readonly;
}
Setting the readonly status of the user database |
public String toString() {
StringBuffer sb = new StringBuffer("MemoryUserDatabase[id=");
sb.append(this.id);
sb.append(",pathname=");
sb.append(pathname);
sb.append(",groupCount=");
sb.append(this.groups.size());
sb.append(",roleCount=");
sb.append(this.roles.size());
sb.append(",userCount=");
sb.append(this.users.size());
sb.append("]");
return (sb.toString());
}
Return a String representation of this UserDatabase. |