001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.sql.Connection;
023: import java.sql.SQLException;
024: import java.util.Date;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028: import javax.sql.DataSource;
029:
030: import dlog4j.formbean.ParamForm;
031: import dlog4j.formbean.SiteForm;
032: import dlog4j.formbean.UserForm;
033: import dlog4j.security.DlogRole;
034:
035: import web.struts.ActionServletExtend;
036:
037: import net.sf.hibernate.Criteria;
038: import net.sf.hibernate.HibernateException;
039: import net.sf.hibernate.Session;
040: import net.sf.hibernate.SessionFactory;
041: import net.sf.hibernate.cfg.Configuration;
042:
043: /**
044: * DLOG4J系统扩展的ActionServlet
045: * @author Liudong
046: */
047: public class DlogActionServlet extends ActionServletExtend {
048:
049: /* (non-Javadoc)
050: * @see javax.servlet.GenericServlet#init()
051: */
052: public void init() throws ServletException {
053: //初始化Hibernate,先初始化Hibernate的目的是为了让PlugIn类可以访问数据库
054: if (getServletContext().getAttribute(
055: Globals.HIBERNATE_SESSIONS_KEY) == null) {
056: URL hibernate_cfg_url = null;
057: String hibernate_cfg = getInitParameter("hibernate-config");
058: if (hibernate_cfg != null
059: && hibernate_cfg.trim().length() > 0) {
060: String webapp_path = getServletContext().getRealPath(
061: ROOT_PATH);
062: if (!webapp_path.endsWith(File.separator))
063: webapp_path += File.separator;
064: if (hibernate_cfg.startsWith(File.separator))
065: hibernate_cfg = hibernate_cfg.substring(1);
066: File f = new File(webapp_path + hibernate_cfg);
067: try {
068: hibernate_cfg_url = f.toURL();
069: } catch (MalformedURLException e) {
070: }
071: } else
072: hibernate_cfg_url = DlogActionServlet.class
073: .getResource("/hibernate.cfg.xml");
074: //初始化Hibernate
075: try {
076: Configuration cfg = new Configuration()
077: .configure(hibernate_cfg_url);
078: SessionFactory sessions = cfg.buildSessionFactory();
079: getServletContext().setAttribute(
080: Globals.HIBERNATE_SESSIONS_KEY, sessions);
081: //初始化ManagerBase类
082: ManagerBase.init(getServletContext());
083: } catch (Exception e) {
084: log.fatal("initialize hibernate failed, cfg is "
085: + hibernate_cfg_url, e);
086: }
087: }
088: //执行Struts的初始化
089: super .init();
090: //初始化数据
091: try {
092: buildDlogData();
093: } catch (Exception e) {
094: log.fatal("Initialize DLOG data failed.", e);
095: }
096: }
097:
098: /**
099: * 初始化DLOG数据
100: * @return 如果已经初始化则返回false
101: * @throws SQLException
102: * @throws HibernateException
103: * @throws IOException
104: */
105: protected boolean buildDlogData() throws HibernateException,
106: SQLException, IOException {
107: Session ssn = getSession();
108: try {
109: Criteria crit = ssn.createCriteria(SiteForm.class);
110: if (crit.list().size() > 0)
111: return false;
112: //1. 初始化dlog_site
113: SiteForm site = new SiteForm();
114: site.setName("dlog");
115: site.setDisplayName("MY BLOG...");
116: site.setDetail("MY BLOG...");
117: site.setCreateTime(new Date());
118: ssn.save(site);
119: //2. 初始化dlog_user
120: UserForm user = new UserForm();
121: user.setDisplayName("ADMIN");
122: user.setLoginName("admin");
123: user.setPassword("admin");
124: user.setPortrait("faces/face1.gif");
125: user.setSite(site);
126: user.setUserRole(DlogRole.ROLE_MANAGER);
127: user.setRegTime(new Date());
128: ssn.save(user);
129: //3. 初始化dlog_param
130: ParamForm param = new ParamForm();
131: param.setSite(site);
132: param.setName("LOGS_PER_PAGE");
133: param.setType(ParamForm.TYPE_INTEGER);
134: param.setValue("5");
135: ssn.save(param);
136: ParamForm param1 = new ParamForm();
137: param1.setSite(site);
138: param1.setName("USERS_PER_PAGE");
139: param1.setType(ParamForm.TYPE_INTEGER);
140: param1.setValue("30");
141: ssn.save(param1);
142: ParamForm param2 = new ParamForm();
143: param2.setSite(site);
144: param2.setName("TOP_COMMENT_COUNT");
145: param2.setType(ParamForm.TYPE_INTEGER);
146: param2.setValue("5");
147: ssn.save(param2);
148: ParamForm param3 = new ParamForm();
149: param3.setSite(site);
150: param3.setName("REPLIES_PER_PAGE");
151: param3.setType(ParamForm.TYPE_INTEGER);
152: param3.setValue("20");
153: ssn.save(param3);
154: ParamForm param4 = new ParamForm();
155: param4.setSite(site);
156: param4.setName("SHOW_TOP_INFO");
157: param4.setType(ParamForm.TYPE_BOOLEAN);
158: param4.setValue("0");
159: ssn.save(param4);
160: ManagerBase.commitSession(ssn, false);
161: log.info("DLOG data initialized.");
162: } finally {
163: ManagerBase.closeSession(ssn);
164: }
165: return true;
166: }
167:
168: /**
169: * 获取Hibernate的Session实例
170: */
171: protected Session getSession() throws SQLException {
172: SessionFactory sessions = (SessionFactory) (getServletContext()
173: .getAttribute(Globals.HIBERNATE_SESSIONS_KEY));
174: return sessions.openSession(getConnection());
175: }
176:
177: /**
178: * 获取到数据库的连接句柄
179: * @return
180: * @throws SQLException
181: */
182: protected Connection getConnection() throws SQLException {
183: ServletContext context = this .getServletContext();
184: DataSource dataSource = (DataSource) context
185: .getAttribute(org.apache.struts.Globals.DATA_SOURCE_KEY);
186: return dataSource.getConnection();
187: }
188:
189: }
|