嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
package sample.zk;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.webapp.example.memory.MemoryUserDatabase;
public final class MemoryDatabasePlugIn implements ServletContextListener {
private MemoryUserDatabase database = null;
private Log log = LogFactory.getLog(this.getClass());
private String pathname = "/WEB-INF/database.xml";
public String getPathname() {
return (this.pathname);
}
public void setPathname(String pathname) {
this.pathname = pathname;
}
public void contextDestroyed(ServletContextEvent sec) {
log.info("Finalizing memory database plug in");
if (database != null) {
try {
database.close();
} catch (Exception e) {
log.error("Closing memory database", e);
}
}
sec.getServletContext().removeAttribute("database");
database = null;
database = null;
}
public void contextInitialized(ServletContextEvent sce) {
log
.info("Initializing memory database plug in from '" pathname
"'");
database = new MemoryUserDatabase();
try {
setPathname(sce.getServletContext().getInitParameter("pathname"));
String path = calculatePath(sce.getServletContext());
if (log.isDebugEnabled()) {
log.debug(" Loading database from '" path "'");
}
database.setPathname(path);
database.open();
} catch (Exception e) {
log.error("Opening memory database", e);
}
sce.getServletContext().setAttribute("database",database);
setupCache(sce.getServletContext());
}
private String calculatePath(ServletContext sc) throws Exception {
// Can we access the database via file I/O?
String path = sc.getRealPath(pathname);
if (path != null) {
return (path);
}
// Does a copy of this file already exist in our temporary directory
File dir = (File)
sc.getAttribute("javax.servlet.context.tempdir");
File file = new File(dir, "struts-example-database.xml");
if (file.exists()) {
return (file.getAbsolutePath());
}
// Copy the static resource to a temporary file and return its path
InputStream is = sc.getResourceAsStream(pathname);
BufferedInputStream bis = new BufferedInputStream(is, 1024);
FileOutputStream os =
new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os, 1024);
byte buffer[] = new byte[1024];
while (true) {
int n = bis.read(buffer);
if (n <= 0) {
break;
}
bos.write(buffer, 0, n);
}
bos.close();
bis.close();
return (file.getAbsolutePath());
}
protected void setupCache(ServletContext sc) {
// Set up list of server types under "serverTypes"
HashMap serverTypes = new HashMap();
serverTypes.put("imap","IMAP Protocol");
serverTypes.put("pop3","POP3 Protocol");
sc.setAttribute("serverTypes", serverTypes);
}
}