struts in action中少的一个类
package app;import java.io.IOException;import java.io.InputStream;import java.io.FileOutputStream;import java.util.Enumeration;import java.util.Properties;import java.net.URL;public class UserDirectory { private static final String UserDirectoryFile = "resources/users.properties"; private static final String UserDirectoryHeader = "${user}=${password}"; private static UserDirectory userDirectory = null; private static Properties p; private UserDirectory() throws UserDirectoryException { java.io.InputStream i = null; p = null; i = this.getClass().getClassLoader(). getResourceAsStream(UserDirectoryFile); if (null==i) { throw new UserDirectoryException(); } else { try { p = new Properties(); p.load(i); i.close(); } catch (java.io.IOException e) { p = null; System.out.println(e.getMessage()); throw new UserDirectoryException(); } finally { i = null; } } // end else } public static UserDirectory getInstance() throws UserDirectoryException {if (null==userDirectory) { userDirectory = new UserDirectory();}return userDirectory;}/**Transform id so that it will match any conventions used by userdirectory. The default implementation forces the id touppercase. Does not expect the userId to be null andwill throw a NPE if it is.@exception Throws Null Pointer Exception if userId is null.*/public String fixId(String userId) {return userId.toUpperCase();}public boolean isValidPassword(String userId, String password) { // no null passwordsif (null==password) return false; // conform userId to uppercaseString _userId = fixId(userId); // no passwords for non-usersif (!isUserExist(_userId)) return false; // does password match user's passwordreturn (password.equals(getPassword(_userId)));}public boolean isUserExist(String userId) { // no null usersif (null==userId) return false; // if not null, it's a userreturn !(null==p.getProperty(userId));}public String getPassword(String userId) {return p.getProperty(userId);}public Enumeration getUserIds() {return p.propertyNames();}public void setUser(String userId, String password) throws UserDirectoryException {// no nullsif ((null==userId) || (null==password)) { throw new UserDirectoryException();}try { // conform userId to uppercase when stored p.put(fixId(userId), password); String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile(); p.store(new FileOutputStream(o), UserDirectoryHeader);}catch (IOException e) { throw new UserDirectoryException();}}} // end UserDirectorypackage app;public class UserDirectoryException extends Exception{}