001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Mikael MARCHE
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.databaseSQL;
025:
026: import java.sql.Date;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.Time;
030:
031: import org.objectweb.salome_tmf.api.Api;
032: import org.objectweb.salome_tmf.api.IChangeDispatcher;
033: import org.objectweb.salome_tmf.api.ISafeThread;
034: import org.objectweb.salome_tmf.api.Util;
035:
036: public class ChangeListener extends Thread implements ISafeThread {
037: private int pid;
038: private boolean stop;
039: private IChangeDispatcher changeDispatcher;
040:
041: private int last_pid = -1;
042: private int last_action = -1;
043: private Date last_date = Util.getCurrentDate();
044: private Time last_time = Util.getCurrentTime();
045: private Object jeton = new Object();
046: private String projet = null;
047:
048: private int error = 0;
049:
050: public ChangeListener(int _pid, IChangeDispatcher _changeDispatcher) {
051: pid = _pid;
052: stop = false;
053: changeDispatcher = _changeDispatcher;
054: }
055:
056: public void addChange(int code) {
057: Util.log("addChange -> projet : " + projet);
058: if (projet == null)
059: return;
060: try {
061: synchronized (jeton) {
062: //int transNumber = -1;
063: try {
064: //transNumber = SQLEngine.beginTransaction(0, ApiConstants.COMMON_REQ);
065: PreparedStatement prep;
066: prep = SQLEngine.getStatement("BEGIN");
067: prep.execute();
068:
069: prep = SQLEngine
070: .getSQLCommonQuery("updateLastChange");
071: prep.setInt(1, pid);
072: prep.setInt(2, code);
073: prep.setDate(3, Util.getCurrentDate());
074: prep.setTime(4, Util.getCurrentTime());
075: prep.setString(5, projet);
076: SQLEngine.runUpdateQuery(prep);
077:
078: //SQLEngine.commitTrans(transNumber);
079: prep = SQLEngine.getStatement("COMMIT");
080: prep.execute();
081: } catch (Exception e) {
082: //SQLEngine.rollBackTrans(transNumber);
083: try {
084: PreparedStatement prep = SQLEngine
085: .getStatement("ROLLBACK");
086: prep.execute();
087: } catch (Exception e2) {
088: }
089: throw e;
090: }
091: }
092: } catch (Exception e) {
093: e.printStackTrace();
094: }
095: }
096:
097: public boolean isChange() {
098: int _last_pid = -1;
099: int _last_action = -1;
100: Date _last_date = null;
101: Time _last_time = null;
102: boolean ret = false;
103: try {
104: if (SQLEngine.isLock) {
105: return false;
106: }
107: synchronized (jeton) {
108: PreparedStatement prep = SQLEngine
109: .getSQLCommonQuery("selectLastChange");
110: prep.setString(1, projet);
111: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
112: Util.log("Before get change, pid " + last_pid
113: + ",action " + last_action + ", Time "
114: + last_date + " : " + last_time);
115: if (stmtRes.next()) {
116: _last_pid = stmtRes.getInt("pid");
117: _last_action = stmtRes.getInt("action_type");
118: _last_date = stmtRes.getDate("action_date");
119: _last_time = stmtRes.getTime("action_time");
120: Util.log("After get change, pid " + _last_pid
121: + ",action " + _last_action + ", Time "
122: + _last_date + " : " + _last_time);
123: if ((_last_pid != pid)
124: && (_last_pid != last_pid
125: || _last_date.after(last_date) || _last_time
126: .after(last_time))) {
127: last_pid = _last_pid;
128: last_action = _last_action;
129: last_date = _last_date;
130: last_time = _last_time;
131: ret = true;
132: }
133: }
134: error = 0;
135: }
136: } catch (Exception e) {
137: error++;
138: e.printStackTrace();
139: try {
140: if (error > 2) {
141: Util.log("Try to resolve error by unlock");
142: if (Api.getLockMeth() == 1) {
143: PreparedStatement prep = SQLEngine
144: .getSQLCommonQuery("unlock");
145: SQLEngine.runUpdateQuery(prep);
146: }
147: }
148: } catch (Exception e1) {
149: e.printStackTrace();
150: }
151: }
152: return ret;
153: }
154:
155: // updateLastChange pid = ?, action_type = ?, action_date = ?, action_time = ? \
156: public void run() {
157: while (!stop) {
158: if (projet != null) {
159: if (isChange()) {
160: Util.log("PID " + last_pid + " do action "
161: + last_action + " at Time " + last_date
162: + " : " + last_time);
163: changeDispatcher.setChange();
164: changeDispatcher.notifyObservers(new Integer(
165: last_action));
166: }
167: if (!stop)
168: waitTime();
169: }
170: }
171: }
172:
173: public synchronized void safe_stop() {
174: stop = true;
175: //notify();
176: }
177:
178: public synchronized void safe_restart() {
179: stop = false;
180: }
181:
182: public void setProjet(String _projet) {
183: projet = _projet;
184: try {
185: PreparedStatement prep;
186: prep = SQLEngine.getStatement("BEGIN");
187: prep.execute();
188:
189: prep = SQLEngine.getSQLCommonQuery("countLastChange");
190: prep.setString(1, projet);
191: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
192:
193: if (!stmtRes.next()) {
194: last_pid = pid;
195: last_action = -1;
196: last_date = Util.getCurrentDate();
197: last_time = Util.getCurrentTime();
198:
199: prep = SQLEngine.getSQLCommonQuery("insertLastChange");
200: prep.setInt(1, pid);
201: prep.setInt(2, -1);
202: prep.setDate(3, last_date);
203: prep.setTime(4, last_time);
204: prep.setString(5, projet);
205: SQLEngine.runUpdateQuery(prep);
206:
207: } else {
208: isChange();
209: }
210: prep = SQLEngine.getStatement("COMMIT");
211: prep.execute();
212: } catch (Exception e) {
213: try {
214: PreparedStatement prep = SQLEngine
215: .getStatement("ROLLBACK");
216: prep.execute();
217: } catch (Exception e2) {
218: }
219: e.printStackTrace();
220: projet = null;
221: }
222: }
223:
224: private synchronized void waitTime() {
225: try {
226: for (int i = 0; i < 6; i++) {
227: yield();
228: Thread.sleep(10000);
229: }
230: } catch (Exception e) {
231: e.printStackTrace();
232: }
233: }
234: }
|