📄 userdatabaseplugin.java
字号:
package addressbook;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.MissingResourceException;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.digester.Digester;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.util.MessageResources;
import addressbook.model.UserBean;
import org.apache.struts.action.PlugIn;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.action.ActionServlet;
/**
* <strong>UserDatabasePlugIn</strong> is used to read username/password information
* to an XML file.
*<p>
* The Addressbook sample application uses this file for demonstration purposes by reading the database
* file from <code> /WEB-INF/userdatabase.xml </code>. In a production
* application you would not want to store username/password information in a clear text file. More
* likely, you would have this information contained within a database for a lookup at logon time.
*/
public final class UserDatabasePlugIn implements PlugIn{
private Hashtable database = null;
private Log log = LogFactory.getLog(this.getClass().getName());
private String pathname;
public String getPathname() {
return pathname;
}
public void setPathname(String pathname) {
this.pathname = pathname;
}
public void destroy() {
database = null;
}
public void addUser(UserBean user){
database.put(user.getUserName(),user);
}
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException{
try {
/* Try and load the database.xml file. If we have loaded,
* store the contents into our context so that we can use it
* to compare against for user logons.
*/
load(servlet);
servlet.getServletContext().setAttribute(Constants.DATABASE_KEY,
database);
} catch (Exception e) {
log.debug("Database load exception", e);
throw new UnavailableException
("Cannot load database from '" + pathname + "'"+e.getMessage());
}
}
/**
* Load the current database.xml file. Using the <code>Digester<code>
* @see org.apache.commons.digester.Digester
*/
private synchronized void load(ActionServlet servlet) throws Exception {
database = new Hashtable();
log.debug("Loading database from '" + pathname + "'");
InputStream is = servlet.getServletContext().getResourceAsStream(pathname);
if (is == null) {
log.debug("No such resource available - loading empty database");
return;
}
BufferedInputStream bis = new BufferedInputStream(is);
Digester digester = new Digester();
digester.push(this);
digester.setDebug(2);
digester.setValidating(false);
digester.addObjectCreate("database/user", "addressbook.model.UserBean");
digester.addSetProperties("database/user");
digester.addSetNext("database/user", "addUser");
digester.parse(bis);
bis.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -