001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core;
015:
016: import org.itsnat.core.ItsNatDocument;
017: import org.itsnat.core.ItsNatException;
018: import org.itsnat.core.ItsNatTimer;
019: import org.itsnat.core.event.TimerHandle;
020: import org.itsnat.impl.core.listener.domext.TimerEventListenerWrapperImpl;
021: import java.util.Date;
022: import java.util.HashMap;
023: import java.util.Map;
024: import org.itsnat.core.event.ParamTransport;
025: import org.itsnat.impl.core.js.listener.TimerEventListenerJSRenderImpl;
026: import org.w3c.dom.events.EventListener;
027: import org.w3c.dom.events.EventTarget;
028:
029: /**
030: *
031: * @author jmarranz
032: */
033: public class ItsNatTimerImpl implements ItsNatTimer {
034: protected Map listeners;
035: protected ItsNatDocumentImpl itsNatDoc;
036: protected boolean canceled = false;
037:
038: /**
039: * Creates a new instance of ItsNatTimerImpl
040: */
041: public ItsNatTimerImpl(ItsNatDocumentImpl itsNatDoc) {
042: this .itsNatDoc = itsNatDoc;
043: }
044:
045: public ItsNatDocument getItsNatDocument() {
046: return itsNatDoc;
047: }
048:
049: public ItsNatDocumentImpl getItsNatDocumentImpl() {
050: return itsNatDoc;
051: }
052:
053: public Map getListeners() {
054: if (listeners == null)
055: this .listeners = new HashMap();
056: return listeners;
057: }
058:
059: public void cancel() {
060: if (canceled)
061: return;
062:
063: Map listeners = getListeners();
064: if (!listeners.isEmpty()) {
065: // Convertimos en array porque al cancelar cada timer task se modifica el propio Map (un iterador daría error)
066: TimerEventListenerWrapperImpl[] listenerArray = (TimerEventListenerWrapperImpl[]) listeners
067: .values().toArray(
068: new TimerEventListenerWrapperImpl[listeners
069: .size()]);
070: for (int i = 0; i < listenerArray.length; i++) {
071: TimerEventListenerWrapperImpl listener = (TimerEventListenerWrapperImpl) listenerArray[i];
072: listener.cancel(); // Automáticamente desregistra de este timer y en el documento. Si en este momento está ejecutándose el método TimerEventListener.handleEvent(Event) asegura que no se genera código JavaScript para enviar más
073: }
074: listeners.clear(); // No hace falta pero por si acaso
075: }
076:
077: this .canceled = true;
078: }
079:
080: public TimerEventListenerWrapperImpl getTimerEventListenerWrapper(
081: String id) {
082: Map listeners = getListeners();
083: return (TimerEventListenerWrapperImpl) listeners.get(id);
084: }
085:
086: public void removeListener(TimerEventListenerWrapperImpl listener) {
087: removeListenerById(listener.getId());
088: }
089:
090: public void removeListenerById(String id) {
091: // Desregistro local
092: Map listeners = getListeners();
093: TimerEventListenerWrapperImpl listener = (TimerEventListenerWrapperImpl) listeners
094: .remove(id);
095:
096: if (listener == null)
097: return; // Ya se eliminó o nunca se añadió (raro)
098:
099: getItsNatDocumentImpl().getTimerEventListenerRegistry()
100: .removeTimerEventListenerByIdOnly(id);
101: }
102:
103: private TimerHandle scheduleGeneric(EventTarget target,
104: EventListener listener, long time, long period,
105: boolean fixedRate, int syncMode,
106: ParamTransport[] extraParams, String preSendCode,
107: long ajaxTimeout) {
108: if (canceled)
109: throw new ItsNatException("This timer is cancelled");
110:
111: if (!itsNatDoc.isAJAXEnabled())
112: return null;
113:
114: TimerEventListenerWrapperImpl listenerWrapper = getItsNatDocumentImpl()
115: .getTimerEventListenerRegistry().addTimerEventListener(
116: target, listener, time, period, fixedRate,
117: syncMode, extraParams, preSendCode,
118: ajaxTimeout, this );
119:
120: // Registro local
121: Map listeners = getListeners();
122: listeners.put(listenerWrapper.getId(), listenerWrapper);
123:
124: return listenerWrapper;
125: }
126:
127: public TimerHandle schedule(EventTarget target, EventListener task,
128: Date time) {
129: int syncMode = getItsNatDocument().getDefaultSyncMode();
130: long ajaxTimeout = getItsNatDocument().getAJAXTimeout();
131: return schedule(target, task, time, syncMode, null, null,
132: ajaxTimeout);
133: }
134:
135: public TimerHandle schedule(EventTarget target, EventListener task,
136: Date time, int syncMode, ParamTransport[] extraParams,
137: String preSendCode, long ajaxTimeout) {
138: return scheduleGeneric(target, task, time.getTime(), 0, false,
139: syncMode, extraParams, preSendCode, ajaxTimeout);
140: }
141:
142: public TimerHandle schedule(EventTarget target, EventListener task,
143: Date firstTime, long period) {
144: int syncMode = getItsNatDocument().getDefaultSyncMode();
145: long ajaxTimeout = getItsNatDocument().getAJAXTimeout();
146: return schedule(target, task, firstTime, period, syncMode,
147: null, null, ajaxTimeout);
148: }
149:
150: public TimerHandle schedule(EventTarget target, EventListener task,
151: Date firstTime, long period, int syncMode,
152: ParamTransport[] extraParams, String preSendCode,
153: long ajaxTimeout) {
154: return scheduleGeneric(target, task, firstTime.getTime(),
155: period, false, syncMode, extraParams, preSendCode,
156: ajaxTimeout);
157: }
158:
159: public TimerHandle schedule(EventTarget target, EventListener task,
160: long delay) {
161: int syncMode = getItsNatDocument().getDefaultSyncMode();
162: long ajaxTimeout = getItsNatDocument().getAJAXTimeout();
163: return schedule(target, task, delay, syncMode, null, null,
164: ajaxTimeout);
165: }
166:
167: public TimerHandle schedule(EventTarget target, EventListener task,
168: long delay, int syncMode, ParamTransport[] extraParams,
169: String preSendCode, long ajaxTimeout) {
170: if (delay < 0)
171: throw new IllegalArgumentException("Negative delay");
172:
173: return scheduleGeneric(target, task, System.currentTimeMillis()
174: + delay, 0, false, syncMode, extraParams, preSendCode,
175: ajaxTimeout);
176: }
177:
178: public TimerHandle schedule(EventTarget target, EventListener task,
179: long delay, long period) {
180: int syncMode = getItsNatDocument().getDefaultSyncMode();
181: long ajaxTimeout = getItsNatDocument().getAJAXTimeout();
182: return schedule(target, task, delay, period, syncMode, null,
183: null, ajaxTimeout);
184: }
185:
186: public TimerHandle schedule(EventTarget target, EventListener task,
187: long delay, long period, int syncMode,
188: ParamTransport[] extraParams, String preSendCode,
189: long ajaxTimeout) {
190: if (delay < 0)
191: throw new IllegalArgumentException("Negative delay");
192:
193: return scheduleGeneric(target, task, System.currentTimeMillis()
194: + delay, period, false, syncMode, extraParams,
195: preSendCode, ajaxTimeout);
196: }
197:
198: public TimerHandle scheduleAtFixedRate(EventTarget target,
199: EventListener task, Date firstTime, long period) {
200: int syncMode = getItsNatDocument().getDefaultSyncMode();
201: long ajaxTimeout = getItsNatDocument().getAJAXTimeout();
202: return scheduleAtFixedRate(target, task, firstTime, period,
203: syncMode, null, null, ajaxTimeout);
204: }
205:
206: public TimerHandle scheduleAtFixedRate(EventTarget target,
207: EventListener task, Date firstTime, long period,
208: int syncMode, ParamTransport[] extraParams,
209: String preSendCode, long ajaxTimeout) {
210: return scheduleGeneric(target, task, firstTime.getTime(),
211: period, true, syncMode, extraParams, preSendCode,
212: ajaxTimeout);
213: }
214:
215: public TimerHandle scheduleAtFixedRate(EventTarget target,
216: EventListener task, long delay, long period) {
217: int syncMode = getItsNatDocument().getDefaultSyncMode();
218: long ajaxTimeout = getItsNatDocument().getAJAXTimeout();
219: return scheduleAtFixedRate(target, task, delay, period,
220: syncMode, null, null, ajaxTimeout);
221: }
222:
223: public TimerHandle scheduleAtFixedRate(EventTarget target,
224: EventListener task, long delay, long period, int syncMode,
225: ParamTransport[] extraParams, String preSendCode,
226: long ajaxTimeout) {
227: if (delay < 0)
228: throw new IllegalArgumentException("Negative delay");
229:
230: return scheduleGeneric(target, task, System.currentTimeMillis()
231: + delay, period, true, syncMode, extraParams,
232: preSendCode, ajaxTimeout);
233: }
234: }
|