001: package hero.session;
002:
003: /*
004: * 02/01/2002 - 15:24:07
005: *
006: * AllProjectsSessionEJB.java -
007: * Copyright (C) 2002 Ecoo Team
008: * valdes@loria.fr
009: *
010: *
011: * This program is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public License
013: * as published by the Free Software Foundation; either version 2
014: * of the License, or (at your option) any later version.
015: *
016: * This program is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License
022: * along with this program; if not, write to the Free Software
023: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
024: */
025:
026: import hero.interfaces.BnNodeLocal;
027: import hero.interfaces.BnNodeLocalHome;
028: import hero.interfaces.BnNodeUtil;
029: import hero.interfaces.DeadlineSessionLocal;
030: import hero.interfaces.DeadlineSessionLocalHome;
031: import hero.interfaces.DeadlineSessionUtil;
032: import hero.util.TimerData;
033:
034: import hero.util.HeroException;
035: import java.util.Date;
036: import java.io.Serializable;
037: import java.sql.Timestamp;
038: import java.util.ArrayList;
039: import java.util.Collection;
040: import java.util.Iterator;
041:
042: import javax.ejb.CreateException;
043: import javax.ejb.SessionBean;
044: import javax.ejb.SessionContext;
045: import javax.ejb.TimedObject;
046: import javax.ejb.Timer;
047: import javax.ejb.TimerService;
048:
049: /**
050: * Session Bean Template
051: *
052: * @ejb:bean name="DeadlineEjbTimerSession"
053: * display-name="DeadlineEjbTimerSession Bean"
054: * type="Stateless"
055: * transaction-type="Container"
056: * jndi-name="ejb/hero/DeadlineEjbTimerSession"
057: * local-jndi-name="ejb/hero/DeadlineEjbTimerSession_L"
058: *
059: * @ejb:transaction type="Supports"
060: * @ejb.permission unchecked="yes"
061: * @ejb.security-identity run-as="SuperAdmin"
062: * @jonas.bean
063: * ejb-name="DeadlineEjbTimerSession"
064: * jndi-name="ejb/hero/DeadlineEjbTimerSession"
065: *
066: * @jboss.container-configuration name="Standard Stateless SessionBean for Bonita"
067: **/
068:
069: public class DeadlineEjbTimerSessionBean implements SessionBean,
070: TimedObject {
071:
072: private SessionContext mContext;
073:
074: /**
075: * Start the EJB timers for node deadlines .
076: *
077: * @param name the name of the node
078: * @param p_id id of the project
079: * @ejb:interface-method view-type="both"
080: * @ejb:transaction type="Required"
081: * @throws HeroException
082: *
083: **/
084: public void startDeadlineEjbTimers(String nodeName, String p_id)
085: throws HeroException {
086:
087: try {
088: BnNodeLocalHome lh = BnNodeUtil.getLocalHome();
089: BnNodeLocal node = lh.findByName(nodeName, p_id);
090:
091: String type = null;
092: Timestamp lNext = null;
093:
094: // For absolute deadline
095: if (node.getDeadlines().size() != 0) {
096: //debug
097: //System.out.println("startDeadlines : Nb of absolute deadlines = " + node.getDeadlines().size());
098: Collection co = node.getDeadlines();
099: Iterator it = co.iterator();
100:
101: while (it.hasNext()) {
102: // the co contains Date elements
103: Timestamp date = (java.sql.Timestamp) it.next();
104: type = node.getName() + ":"
105: + node.getBnProject().getName() + ":"
106: + date.getTime();
107: lNext = date;
108: // add notification for each deadline in the collection
109:
110: TimerService ts = mContext.getTimerService();
111: // addNotification(lNext, type, node);
112: // Create the timer with the date
113: TimerData info = new TimerData(type, lNext
114: .getTime());
115: Timer timer = ts.createTimer(date, info);
116: //info.onStart(timer);
117: //debug
118: //System.out.println("startDeadlines for absolute deadlines: lnext = " + lNext.toString() + " type = " + type + " node = " + node.getName());
119: }
120: }
121: // For relative deadline
122: if (node.getRelativeDeadlines().size() != 0) {
123: //debug
124: //System.out.println("startDeadlines : Nb of relative deadlines = " + node.getRelativeDeadlines().size());
125: Collection co = node.getRelativeDeadlines();
126: Iterator it = co.iterator();
127:
128: while (it.hasNext()) {
129: // the co contains Date elements
130: java.sql.Timestamp date = (java.sql.Timestamp) it
131: .next();
132: type = node.getName() + ":"
133: + node.getBnProject().getName() + ":"
134: + date.getTime();
135: //create EJB timers
136: lNext = new Timestamp(new Date().getTime()
137: + date.getTime());
138: TimerService ts = mContext.getTimerService();
139: TimerData info = new TimerData(type, lNext
140: .getTime());
141: Timer timer = ts.createTimer(lNext, info);
142: //call method onStart: not used !
143: //info.onStart(timer);
144: //debug
145: //System.out.println("startDeadlines for relative deadlines: lnext = " + lNext.toString() + " type = " + type + " node = " + node.getName());
146: }
147: }
148:
149: } catch (Exception e) {
150: throw new HeroException(
151: "ProjectSessionBean.startDeadlinesEjbTimers: "
152: + e.getMessage());
153: }
154: }
155:
156: /**
157: * Stop the EJB timer for node deadline .
158: *
159: * @param projectName the name of the project
160: * @param nodeName the name of the node
161: * @param date date
162: * @ejb:interface-method view-type="both"
163: * @ejb:transaction type="Required"
164: * @throws HeroException
165: *
166: **/
167: public void stopDeadlineEjbTimer(String projectName,
168: String nodeName, long date) throws HeroException {
169: try {
170: TimerService ts = mContext.getTimerService();
171: Collection timers = ts.getTimers();
172: Iterator it = timers.iterator();
173: String type = nodeName + ":" + projectName + ":" + date;
174: while (it.hasNext()) {
175: Timer t = (Timer) it.next();
176:
177: if (isCalled(t, type)) {
178: TimerData td = (TimerData) t.getInfo();
179: td.onStop(t);
180: }
181: }
182: } catch (Exception e) {
183: throw new HeroException(
184: "ProjectSessionBean.stopDeadlineEjbTimer: "
185: + e.getMessage());
186: }
187: }
188:
189: /**
190: * timeout is called after time duration within timer
191: *
192: * @param Timer timer from the EJB container-managed timer service
193: * @ejb:interface-method view-type="both"
194: * @ejb:transaction type="Required"
195: * @throws HeroException
196: *
197: **/
198: public void ejbTimeout(Timer timer) {
199: DeadlineSessionLocalHome dlHome = null;
200: DeadlineSessionLocal dlSession = null;
201: try {
202: dlHome = DeadlineSessionUtil.getLocalHome();
203: } catch (javax.naming.NamingException ne) {
204: System.out
205: .println("Error: DeadlineEjbTimerSessionBean: getting DeadlineSessionLocalHome");
206: }
207: try {
208: dlSession = dlHome.create();
209: } catch (CreateException he) {
210: System.out
211: .println("Error: DeadlineEjbTimerSessionBean: creating DeadlineSessionLocal");
212: }
213: Object info = timer.getInfo();
214: //debug
215: //System.out.println("====> timeoutDeadlineEjbTimer: has been called !!!!");
216: if (info != null && info instanceof TimerData) {
217: TimerData td = (TimerData) info;
218: /*
219: try {
220: td.onTimeout(timer);
221: } catch (HeroException e) {
222: e.printStackTrace();
223: }
224: */
225: try {
226: String type = (String) td.getType();
227: String projectName = type
228: .substring(type.indexOf(":") + 1);
229: projectName = projectName.substring(0, projectName
230: .indexOf(":"));
231: String nodeName = type.substring(0, type.indexOf(":"));
232: dlSession.executeHookDeadline(type, projectName,
233: nodeName);
234: } catch (Exception hh) {
235: hh.printStackTrace();
236: }
237: }
238: }
239:
240: protected boolean isCalled(Timer t, String timerType)
241: throws HeroException {
242: try {
243: Object info = t.getInfo();
244: if (info != null && info instanceof TimerData) {
245: TimerData td = (TimerData) info;
246: if (td.getType().equals(timerType)) {
247: return true;
248: }
249: }
250: } catch (Exception e) {
251: throw new HeroException(
252: "DeadlineEjbTimerSession: Error in isCalled: "
253: + e.getMessage());
254: }
255: return false;
256: }
257:
258: /**
259: * Retrieves current timers
260: *
261: * @return Collection of <tt>TimerData</tt> objects
262: * @ejb:interface-method view-type="both"
263: **/
264: public Collection getTimers() {
265: Collection ret = new ArrayList();
266:
267: // Iterates through all timers
268: Collection timers = mContext.getTimerService().getTimers();
269: for (Iterator iterator = timers.iterator(); iterator.hasNext();) {
270: Serializable info = ((Timer) iterator.next()).getInfo();
271:
272: // Check the Timer info data to increase robustness
273: if (info instanceof TimerData) {
274: ret.add(info);
275: }
276: }
277:
278: return ret;
279: }
280:
281: /**
282: * Remove on going timers for a particular process
283: *
284: * @ejb:interface-method view-type="both"
285: **/
286: public void removeProjectTimers(String projectName)
287: throws HeroException {
288: Collection timers = mContext.getTimerService().getTimers();
289: for (Iterator iterator = timers.iterator(); iterator.hasNext();) {
290: Timer t = (Timer) iterator.next();
291: TimerData td = (TimerData) t.getInfo();
292: // Check the Timer info data to increase robustness
293: if (td.getType().matches(
294: ".*" + ":" + projectName + ":" + ".*")) {
295: td.onStop(t);
296: }
297: }
298: }
299:
300: public void ejbCreate() throws CreateException {
301: }
302:
303: public void setSessionContext(final javax.ejb.SessionContext context) {
304: mContext = context;
305: }
306:
307: public void ejbRemove() {
308: }
309:
310: public void ejbActivate() {
311: }
312:
313: public void ejbPassivate() {
314: }
315:
316: }
|