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.action;
017:
018: import java.sql.Connection;
019: import java.sql.ResultSet;
020: import java.sql.SQLException;
021: import java.sql.Statement;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.struts.action.ActionErrors;
028: import org.apache.struts.action.ActionForm;
029: import org.apache.struts.action.ActionForward;
030: import org.apache.struts.action.ActionMapping;
031:
032: import dlog4j.Globals;
033: import dlog4j.formbean.UserForm;
034:
035: import web.struts.ActionExtend;
036: import net.sf.hibernate.HibernateException;
037: import net.sf.hibernate.Session;
038: import net.sf.hibernate.SessionFactory;
039:
040: /**
041: * DLOG项目的基础Action类
042: * @author Liudong
043: */
044: public abstract class DlogActionBase extends ActionExtend {
045:
046: /**
047: * 该方法覆盖了父类的功能,主要是为了resin 3.0.x的兼容性做了改进
048: * 对于其他应用服务器并没有影响,同时统一了formbean的自动validate功能
049: * 要求所有的action都必须关闭自动validate功能,而在此处进行统一处理
050: * Action类执行入口
051: */
052: public ActionForward execute(ActionMapping mapping,
053: ActionForm form, HttpServletRequest req,
054: HttpServletResponse res) throws Exception {
055: ActionForward forward = null;
056: //统一validate
057: if (form != null) {
058: ActionErrors errors = form.validate(mapping, req);
059: if (errors != null && !errors.isEmpty()) {
060: saveErrors(req, errors);
061: forward = mapping.getInputForward();
062: }
063: }
064: if (forward == null)
065: forward = super .execute(mapping, form, req, res);
066: //处理resin 3.0.x 的兼容性问题 -> jspe小服务程序
067: if (!forward.getRedirect()) {
068: //传递要forward的路径给jspe小服务程序
069: req
070: .setAttribute(Globals.ACTION_PATH_KEY, forward
071: .getPath());
072: }
073: return forward;
074: }
075:
076: /**
077: * 获取当前操作用户的信息
078: * @param req
079: * @return
080: */
081: public static UserForm getLoginUser(HttpServletRequest req) {
082: return UserForm.getLoginUser(req);
083: }
084:
085: /**
086: * 获取Hibernate持久层的操作实例
087: * @return
088: */
089: protected Session getSession() throws SQLException {
090: ServletContext context = servlet.getServletContext();
091: SessionFactory sessions = (SessionFactory) context
092: .getAttribute(dlog4j.Globals.HIBERNATE_SESSIONS_KEY);
093: return sessions.openSession(getConnection());
094: }
095:
096: /**
097: * 提交Hibernate操作
098: * @param session
099: * @throws SQLException
100: * @throws HibernateException
101: */
102: protected void commitSession(Session session, boolean close)
103: throws SQLException, HibernateException {
104: session.flush();
105: session.connection().commit();
106: if (close) {
107: session.connection().close();
108: session.close();
109: }
110: }
111:
112: /**
113: * 关闭session
114: * @param session
115: * @throws SQLException
116: * @throws HibernateException
117: */
118: protected void closeSession(Session session) throws SQLException,
119: HibernateException {
120: session.connection().close();
121: session.close();
122: }
123:
124: /**
125: * 一般用于释放数据库资源,例如 close(ps,conn);
126: * @param obj1
127: * @param obj2
128: */
129: public static void close(Object obj1, Object obj2) {
130: close(obj1);
131: close(obj2);
132: }
133:
134: /**
135: * 一般用于释放数据库资源,例如 close(rs,ps,conn);
136: * @param obj1
137: * @param obj2
138: * @param obj3
139: */
140: public static void close(Object obj1, Object obj2, Object obj3) {
141: close(obj1);
142: close(obj2);
143: close(obj3);
144: }
145:
146: /**
147: * 资源释放
148: * @param obj
149: */
150: public static void close(Object obj) {
151: if (obj == null)
152: return;
153: try {
154: if (obj instanceof Connection)
155: ((Connection) obj).close();
156: else if (obj instanceof Statement)
157: ((Statement) obj).close();
158: else if (obj instanceof ResultSet)
159: ((ResultSet) obj).close();
160: else if (obj instanceof Session) {
161: Session snn = (Session) obj;
162: snn.connection().close();
163: snn.close();
164: }
165: } catch (Exception e) {
166: }
167: obj = null;
168: }
169:
170: }
|