Source Code Cross Referenced for DlogActionServlet.java in  » Portal » DLOG4J » dlog4j » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » DLOG4J » dlog4j 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.