DlogActionServlet.java :  » Portal » DLOG4J » dlog4j » Java Open Source

Java Open Source » Portal » DLOG4J 
DLOG4J » dlog4j » DlogActionServlet.java
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package dlog4j;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.sql.DataSource;

import dlog4j.formbean.ParamForm;
import dlog4j.formbean.SiteForm;
import dlog4j.formbean.UserForm;
import dlog4j.security.DlogRole;

import web.struts.ActionServletExtend;

import net.sf.hibernate.Criteria;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

/**
 * DLOG4JActionServlet
 * @author Liudong
 */
public class DlogActionServlet extends ActionServletExtend{

  /* (non-Javadoc)
   * @see javax.servlet.GenericServlet#init()
   */
  public void init() throws ServletException {
    //Hibernate,HibernatePlugIn
    if(getServletContext().getAttribute(Globals.HIBERNATE_SESSIONS_KEY)==null){
      URL hibernate_cfg_url = null;
      String hibernate_cfg = getInitParameter("hibernate-config");
      if(hibernate_cfg!=null&&hibernate_cfg.trim().length()>0){
        String webapp_path = getServletContext().getRealPath(ROOT_PATH);
        if(!webapp_path.endsWith(File.separator))
          webapp_path += File.separator;
        if(hibernate_cfg.startsWith(File.separator))
          hibernate_cfg = hibernate_cfg.substring(1);
        File f = new File(webapp_path + hibernate_cfg);
        try{      
          hibernate_cfg_url = f.toURL();
        }catch(MalformedURLException e){}
      }
      else
        hibernate_cfg_url = DlogActionServlet.class.getResource("/hibernate.cfg.xml");
      //Hibernate
      try{
        Configuration cfg = new Configuration().configure(hibernate_cfg_url);
        SessionFactory sessions = cfg.buildSessionFactory();
        getServletContext().setAttribute(Globals.HIBERNATE_SESSIONS_KEY,sessions);
        //ManagerBase
        ManagerBase.init(getServletContext());
      }catch(Exception e){
        log.fatal("initialize hibernate failed, cfg is " + hibernate_cfg_url, e);
      }
    }
    //Struts
    super.init(); 
    //
    try{
      buildDlogData();
    }catch(Exception e){
      log.fatal("Initialize DLOG data failed.",e);
    }
  }

  /**
   * DLOG
   * @return false
   * @throws SQLException
   * @throws HibernateException
   * @throws IOException
   */
  protected boolean buildDlogData() throws HibernateException, SQLException, IOException{
    Session ssn = getSession();
    try{
      Criteria crit = ssn.createCriteria(SiteForm.class);
      if(crit.list().size()>0)
        return false;
      //1. dlog_site
      SiteForm site = new SiteForm();
      site.setName("dlog");
      site.setDisplayName("MY BLOG...");
      site.setDetail("MY BLOG...");
      site.setCreateTime(new Date());
      ssn.save(site);
      //2. dlog_user
      UserForm user = new UserForm();
      user.setDisplayName("ADMIN");
      user.setLoginName("admin");
      user.setPassword("admin");
      user.setPortrait("faces/face1.gif");
      user.setSite(site);
      user.setUserRole(DlogRole.ROLE_MANAGER);
      user.setRegTime(new Date());
      ssn.save(user);
      //3. dlog_param
      ParamForm param = new ParamForm();
      param.setSite(site);
      param.setName("LOGS_PER_PAGE");
      param.setType(ParamForm.TYPE_INTEGER);
      param.setValue("5");
      ssn.save(param);
      ParamForm param1 = new ParamForm();
      param1.setSite(site);
      param1.setName("USERS_PER_PAGE");
      param1.setType(ParamForm.TYPE_INTEGER);
      param1.setValue("30");
      ssn.save(param1);
      ParamForm param2 = new ParamForm();
      param2.setSite(site);
      param2.setName("TOP_COMMENT_COUNT");
      param2.setType(ParamForm.TYPE_INTEGER);
      param2.setValue("5");
      ssn.save(param2);
      ParamForm param3 = new ParamForm();
      param3.setSite(site);
      param3.setName("REPLIES_PER_PAGE");
      param3.setType(ParamForm.TYPE_INTEGER);
      param3.setValue("20");
      ssn.save(param3);
      ParamForm param4 = new ParamForm();
      param4.setSite(site);
      param4.setName("SHOW_TOP_INFO");
      param4.setType(ParamForm.TYPE_BOOLEAN);
      param4.setValue("0");
      ssn.save(param4);
      ManagerBase.commitSession(ssn, false);
      log.info("DLOG data initialized.");
    }finally{
      ManagerBase.closeSession(ssn);
    }
    return true;
  }
    
    /**
     * HibernateSession
     */
    protected Session getSession() throws SQLException {
      SessionFactory sessions = (SessionFactory)(
          getServletContext().getAttribute(Globals.HIBERNATE_SESSIONS_KEY));
        return sessions.openSession(getConnection());
    }
  /**
   * 
   * @return
   * @throws SQLException
   */
  protected Connection getConnection() throws SQLException{
    ServletContext context = this.getServletContext();
    DataSource dataSource = (DataSource)context.getAttribute(
        org.apache.struts.Globals.DATA_SOURCE_KEY);
    return dataSource.getConnection();    
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.