KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > DlogBaseTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.tags;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.Statement JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.Session;
30 import net.sf.hibernate.SessionFactory;
31
32 import org.apache.struts.Globals;
33
34 import dlog4j.SiteManager;
35 import dlog4j.formbean.SiteForm;
36 import dlog4j.formbean.UserForm;
37
38 /**
39  * 所有用于操作数据的标签库的基类
40  * @author Liudong
41  */

42 public class DlogBaseTag extends TagSupport JavaDoc {
43
44     protected String JavaDoc id = null;
45     
46     public String JavaDoc getParameter(String JavaDoc param) {
47         return pageContext.getRequest().getParameter(param);
48     }
49     
50     public boolean isPost() {
51         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc)pageContext.getRequest();
52         return "POST".equalsIgnoreCase(req.getMethod());
53     }
54
55     public String JavaDoc getId() {
56         return (this.id);
57     }
58
59     public void setId(String JavaDoc id) {
60         this.id = id;
61     }
62     /**
63      * 获取参数,没有没有指定该参数则使用缺省值
64      * @param param
65      * @param defaultValue
66      * @return
67      */

68     protected String JavaDoc getParameter(String JavaDoc param,String JavaDoc defaultValue) {
69         String JavaDoc value = getParameter(param);
70         if(value==null)
71             value = defaultValue;
72         return value;
73     }
74     /**
75      * 获取当前正在访问的子站点
76      * @return
77      */

78     protected SiteForm getCurrentSite(){
79         return SiteManager.getCurrentSite(pageContext.getRequest());
80     }
81     /**
82      * 返回当前的登录用户实例
83      * @param request
84      * @return
85      */

86     protected UserForm getLoginUser(){
87         return UserForm.getLoginUser((HttpServletRequest JavaDoc)pageContext.getRequest());
88     }
89     /**
90      * 提交Hibernate操作
91      * @param session
92      * @throws SQLException
93      * @throws HibernateException
94      */

95     protected void commitSession(Session session, boolean close) throws SQLException JavaDoc, HibernateException{
96         session.flush();
97         session.connection().commit();
98         if(close){
99             session.connection().close();
100             session.close();
101         }
102     }
103     /**
104      * 返回Hibernate操作的实例
105      * @return
106      * @throws SQLException
107      */

108     protected Session getSession() throws SQLException JavaDoc{
109         ServletContext JavaDoc context = pageContext.getServletContext();
110         SessionFactory sessions = (SessionFactory)context.getAttribute(dlog4j.Globals.HIBERNATE_SESSIONS_KEY);
111         return sessions.openSession(getConnection());
112     }
113     /**
114      * 获取数据库连接
115      * @return
116      * @throws SQLException
117      */

118     public Connection JavaDoc getConnection() throws SQLException JavaDoc{
119         ServletContext JavaDoc context = pageContext.getServletContext();
120         DataSource JavaDoc dataSource = (DataSource JavaDoc)context.getAttribute(Globals.DATA_SOURCE_KEY);
121         return dataSource.getConnection();
122     }
123     /**
124      * 关闭session
125      * @param session
126      */

127     protected void close(Session session){
128         try {
129             session.connection().close();
130         }catch(Exception JavaDoc e) {}
131         try {
132             session.close();
133         }catch(Exception JavaDoc e) {}
134     }
135     /**
136      * 关闭session
137      * @param session
138      * @throws SQLException
139      * @throws HibernateException
140      */

141     protected void closeSession(Session session) throws SQLException JavaDoc,HibernateException{
142         session.connection().close();
143         session.close();
144     }
145
146     public static void close(Object JavaDoc obj1,Object JavaDoc obj2,Object JavaDoc obj3){
147         close(obj1);close(obj2);close(obj3);
148     }
149     /**
150      * 资源释放
151      * @param obj
152      */

153     public static void close(Object JavaDoc obj){
154         if(obj==null)
155             return;
156         try{
157             if(obj instanceof Connection JavaDoc)
158                 ((Connection JavaDoc)obj).close();
159             if(obj instanceof Statement JavaDoc)
160                 ((Statement JavaDoc)obj).close();
161             if(obj instanceof ResultSet JavaDoc)
162                 ((ResultSet JavaDoc)obj).close();
163         }catch(Exception JavaDoc e){}
164         obj = null;
165     }
166 }
167
Popular Tags