/*
* Created on May 9, 2006
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package sample.zk;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.webapp.example.memory.*;
import javax.servlet.ServletContext;
import java.util.HashMap;
/**
* @author michael lee it
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public final class MemoryDatabasePlugIn implements ServletContextListener {
/**
* The {@link MemoryUserDatabase} object we construct and make available.
*/
private MemoryUserDatabase database = null;
/**
* Logging output for this plug in instance.
*/
private Log log = LogFactory.getLog(this.getClass());
// ------------------------------------------------------------- Properties
/**
* The web application resource path of our persistent database
* storage file.
*/
private String pathname = "/WEB-INF/database.xml";
public String getPathname() {
return (this.pathname);
}
public void setPathname(String pathname) {
this.pathname = pathname;
}
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
log.info("Initializing memory database plug in from '" +
pathname + "'");
// Remember our associated configuration and servlet
// Construct a new database and make it available
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);
}
// Make the initialized database available
sce.getServletContext().setAttribute("database",database);
// Setup and cache other required data
//setupCache(servlet, config);
setupCache(sce.getServletContext());
}
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent sec) {
// TODO Auto-generated method stub
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;
}
/**
* Calculate and return an absolute pathname to the XML file to contain
* our persistent storage information.
*
* @exception Exception if an input/output error occurs
*/
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());
}
/**
* <p>Cache commonly required data as servlet context attributes.</p>
*
* @param servlet The <code>ActionServlet</code> instance running
* this webapp
* @param config The <code>ModuleConfig</code> for this application module
*/
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);
}
}
zk简单实例(适合初学者)
需积分: 9 137 浏览量
2008-11-01
22:54:07
上传
评论
收藏 444KB ZIP 举报
student1406
- 粉丝: 2
- 资源: 6
最新资源
- 全卷积网络基于voc2012数据集简单pytorch实现
- pycharm的一些介绍-用于更好的学习python
- 基于C++的程序设计大赛天梯赛L2答案(天梯赛)
- 基于python实现的三次样条插值和均值插值法实现
- Python语言教程2-python批量图片大小处理-多文件夹
- Python语言教程1-python批量图片重命名,将后缀某几个不想要的字去除
- Space Combat Kit 太空战斗套件Unity游戏开发插件资源unitypackage C#
- Universal Device Preview 通用设备预览Unity游戏开发插件资源unitypackage
- Paladin Anim Set 圣骑士动画集Unity游戏动作动画插件资源unitypackage
- 计算机财务管理期末考报表部分题目及答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈