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.api;
025:
026: import java.sql.Date;
027: import java.sql.PreparedStatement;
028: import java.sql.SQLException;
029: import java.sql.Time;
030: import java.util.Properties;
031:
032: import org.objectweb.salome_tmf.api.api2db.DataBase;
033: import org.objectweb.salome_tmf.api.api2db.DataSet;
034: import org.objectweb.salome_tmf.api.api2ihm.Utile;
035:
036: public class ChangeListener implements Runnable {
037: private int pid;
038: private boolean stop;
039: private ChangeDispatcher changeDispatcher;
040: private DataBase db;
041: private Properties prop;
042:
043: private int last_pid = -1;
044: private int last_action = -1;
045: private Date last_date = Utile.getCurrentDate();
046: private Time last_time = Utile.getCurrentTime();
047: private Object jeton = new Object();
048: private String projet = null;
049: private Thread prt_thread = null;
050:
051: private int error = 0;
052:
053: public ChangeListener(int _pid, ChangeDispatcher _changeDispatcher,
054: DataBase _db, Properties _prop) {
055: pid = _pid;
056: stop = false;
057: changeDispatcher = _changeDispatcher;
058: db = _db;
059: prop = _prop;
060: //isChange();
061: }
062:
063: public void addChange(int code) {
064: org.objectweb.salome_tmf.api.Api.log("addChange -> projet : "
065: + projet);
066: if (projet == null)
067: return;
068: try {
069: synchronized (jeton) {
070: PreparedStatement prep = db.prepareStatement(prop
071: .getProperty("updateLastChange"));
072: prep.setInt(1, pid);
073: prep.setInt(2, code);
074: prep.setDate(3, Utile.getCurrentDate());
075: prep.setTime(4, Utile.getCurrentTime());
076: prep.setString(5, projet);
077: prep.executeUpdate();
078: }
079: } catch (SQLException e) {
080: e.printStackTrace();
081: }
082: }
083:
084: public boolean isChange() {
085: DataSet stmtRes = null;
086: int _last_pid = -1;
087: int _last_action = -1;
088: Date _last_date = null;
089: Time _last_time = null;
090: boolean ret = false;
091: try {
092: synchronized (jeton) {
093: PreparedStatement prep = db.prepareStatement(prop
094: .getProperty("selectLastChange"));
095: prep.setString(1, projet);
096: stmtRes = new DataSet(prep.executeQuery());
097: org.objectweb.salome_tmf.api.Api
098: .log("Before get change, pid " + last_pid
099: + ",action " + last_action + ", Time "
100: + last_date + " : " + last_time);
101: if (stmtRes.getResults().next()) {
102: _last_pid = stmtRes.getResults().getInt("pid");
103: _last_action = stmtRes.getResults().getInt(
104: "action_type");
105: _last_date = stmtRes.getResults().getDate(
106: "action_date");
107: _last_time = stmtRes.getResults().getTime(
108: "action_time");
109: org.objectweb.salome_tmf.api.Api
110: .log("After get change, pid " + _last_pid
111: + ",action " + _last_action
112: + ", Time " + _last_date + " : "
113: + _last_time);
114: if ((_last_pid != pid)
115: && (_last_pid != last_pid
116: || _last_date.after(last_date) || _last_time
117: .after(last_time))) {
118: last_pid = _last_pid;
119: last_action = _last_action;
120: last_date = _last_date;
121: last_time = _last_time;
122: ret = true;
123: }
124: }
125: error = 0;
126: }
127: } catch (SQLException e) {
128: error++;
129: e.printStackTrace();
130: try {
131: if (error > 2) {
132: org.objectweb.salome_tmf.api.Api
133: .log("Try to resolve error by unlock");
134: PreparedStatement prep = db.prepareStatement(prop
135: .getProperty("unlock"));
136: prep.executeUpdate();
137: }
138: } catch (Exception e1) {
139: e.printStackTrace();
140: }
141: }
142: return ret;
143: }
144:
145: // updateLastChange pid = ?, action_type = ?, action_date = ?, action_time = ? \
146: public void run() {
147: // compute primes larger than minPrime
148: if (db == null)
149: return;
150:
151: while (!stop) {
152: if (projet != null) {
153: if (isChange()) {
154: System.out.println("PID " + last_pid
155: + " do action " + last_action + " at Time "
156: + last_date + " : " + last_time);
157: changeDispatcher.setChange();
158: changeDispatcher.notifyObservers(new Integer(
159: last_action));
160: }
161: if (!stop)
162: waitTime();
163: }
164: }
165: }
166:
167: public synchronized void stop() {
168: stop = true;
169: //notify();
170: }
171:
172: public void setThread(Thread _prt_thread) {
173: prt_thread = _prt_thread;
174: }
175:
176: public void setProjet(String _projet) {
177: projet = _projet;
178: DataSet stmtRes = null;
179: try {
180: PreparedStatement prep = db.prepareStatement(prop
181: .getProperty("countLastChange"));
182: prep.setString(1, projet);
183: stmtRes = new DataSet(prep.executeQuery());
184: if (!stmtRes.hasMoreElements()) {
185: last_pid = pid;
186: last_action = -1;
187: last_date = Utile.getCurrentDate();
188: last_time = Utile.getCurrentTime();
189: prep = db.prepareStatement(prop
190: .getProperty("insertLastChange"));
191: prep.setInt(1, pid);
192: prep.setInt(2, -1);
193: prep.setDate(3, last_date);
194: prep.setTime(4, last_time);
195: prep.setString(5, projet);
196: prep.executeUpdate();
197: } else {
198: isChange();
199: }
200: } catch (SQLException e) {
201: e.printStackTrace();
202: projet = null;
203: }
204: }
205:
206: private synchronized void waitTime() {
207: try {
208: //System.out.println("WAIT");
209: //wait(10000);
210: if (prt_thread != null)
211: prt_thread.sleep(10000);
212: } catch (Exception e) {
213: e.printStackTrace();
214: }
215: }
216: }
|