1 16 package dlog4j; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.util.Date ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletException ; 28 import javax.sql.DataSource ; 29 30 import dlog4j.formbean.ParamForm; 31 import dlog4j.formbean.SiteForm; 32 import dlog4j.formbean.UserForm; 33 import dlog4j.security.DlogRole; 34 35 import web.struts.ActionServletExtend; 36 37 import net.sf.hibernate.Criteria; 38 import net.sf.hibernate.HibernateException; 39 import net.sf.hibernate.Session; 40 import net.sf.hibernate.SessionFactory; 41 import net.sf.hibernate.cfg.Configuration; 42 43 47 public class DlogActionServlet extends ActionServletExtend{ 48 49 52 public void init() throws ServletException { 53 if(getServletContext().getAttribute(Globals.HIBERNATE_SESSIONS_KEY)==null){ 55 URL hibernate_cfg_url = null; 56 String hibernate_cfg = getInitParameter("hibernate-config"); 57 if(hibernate_cfg!=null&&hibernate_cfg.trim().length()>0){ 58 String webapp_path = getServletContext().getRealPath(ROOT_PATH); 59 if(!webapp_path.endsWith(File.separator)) 60 webapp_path += File.separator; 61 if(hibernate_cfg.startsWith(File.separator)) 62 hibernate_cfg = hibernate_cfg.substring(1); 63 File f = new File (webapp_path + hibernate_cfg); 64 try{ 65 hibernate_cfg_url = f.toURL(); 66 }catch(MalformedURLException e){} 67 } 68 else 69 hibernate_cfg_url = DlogActionServlet.class.getResource("/hibernate.cfg.xml"); 70 try{ 72 Configuration cfg = new Configuration().configure(hibernate_cfg_url); 73 SessionFactory sessions = cfg.buildSessionFactory(); 74 getServletContext().setAttribute(Globals.HIBERNATE_SESSIONS_KEY,sessions); 75 ManagerBase.init(getServletContext()); 77 }catch(Exception e){ 78 log.fatal("initialize hibernate failed, cfg is " + hibernate_cfg_url, e); 79 } 80 } 81 super.init(); 83 try{ 85 buildDlogData(); 86 }catch(Exception e){ 87 log.fatal("Initialize DLOG data failed.",e); 88 } 89 } 90 91 98 protected boolean buildDlogData() throws HibernateException, SQLException , IOException { 99 Session ssn = getSession(); 100 try{ 101 Criteria crit = ssn.createCriteria(SiteForm.class); 102 if(crit.list().size()>0) 103 return false; 104 SiteForm site = new SiteForm(); 106 site.setName("dlog"); 107 site.setDisplayName("MY BLOG..."); 108 site.setDetail("MY BLOG..."); 109 site.setCreateTime(new Date ()); 110 ssn.save(site); 111 UserForm user = new UserForm(); 113 user.setDisplayName("ADMIN"); 114 user.setLoginName("admin"); 115 user.setPassword("admin"); 116 user.setPortrait("faces/face1.gif"); 117 user.setSite(site); 118 user.setUserRole(DlogRole.ROLE_MANAGER); 119 user.setRegTime(new Date ()); 120 ssn.save(user); 121 ParamForm param = new ParamForm(); 123 param.setSite(site); 124 param.setName("LOGS_PER_PAGE"); 125 param.setType(ParamForm.TYPE_INTEGER); 126 param.setValue("5"); 127 ssn.save(param); 128 ParamForm param1 = new ParamForm(); 129 param1.setSite(site); 130 param1.setName("USERS_PER_PAGE"); 131 param1.setType(ParamForm.TYPE_INTEGER); 132 param1.setValue("30"); 133 ssn.save(param1); 134 ParamForm param2 = new ParamForm(); 135 param2.setSite(site); 136 param2.setName("TOP_COMMENT_COUNT"); 137 param2.setType(ParamForm.TYPE_INTEGER); 138 param2.setValue("5"); 139 ssn.save(param2); 140 ParamForm param3 = new ParamForm(); 141 param3.setSite(site); 142 param3.setName("REPLIES_PER_PAGE"); 143 param3.setType(ParamForm.TYPE_INTEGER); 144 param3.setValue("20"); 145 ssn.save(param3); 146 ParamForm param4 = new ParamForm(); 147 param4.setSite(site); 148 param4.setName("SHOW_TOP_INFO"); 149 param4.setType(ParamForm.TYPE_BOOLEAN); 150 param4.setValue("0"); 151 ssn.save(param4); 152 ManagerBase.commitSession(ssn, false); 153 log.info("DLOG data initialized."); 154 }finally{ 155 ManagerBase.closeSession(ssn); 156 } 157 return true; 158 } 159 160 163 protected Session getSession() throws SQLException { 164 SessionFactory sessions = (SessionFactory)( 165 getServletContext().getAttribute(Globals.HIBERNATE_SESSIONS_KEY)); 166 return sessions.openSession(getConnection()); 167 } 168 173 protected Connection getConnection() throws SQLException { 174 ServletContext context = this.getServletContext(); 175 DataSource dataSource = (DataSource )context.getAttribute( 176 org.apache.struts.Globals.DATA_SOURCE_KEY); 177 return dataSource.getConnection(); 178 } 179 180 } 181 | Popular Tags |