01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.core.listener;
15:
16: import java.util.HashMap;
17: import java.util.Map;
18: import org.itsnat.core.ItsNatException;
19: import org.itsnat.impl.core.client.ClientDocumentImpl;
20: import org.itsnat.impl.core.comet.CometNotifierImpl;
21: import org.itsnat.impl.core.js.listener.EventListenerJSRenderImpl;
22:
23: /**
24: *
25: * @author jmarranz
26: */
27: public abstract class CometTaskRegistry {
28: protected Map tasks = new HashMap();
29: protected ClientDocumentImpl clientDoc;
30:
31: /**
32: * Creates a new instance of CometTaskRegistry
33: */
34: public CometTaskRegistry(ClientDocumentImpl clientDoc) {
35: this .clientDoc = clientDoc;
36: }
37:
38: public abstract CometTaskEventListenerWrapper createCometTaskEventListenerWrapper(
39: CometTask taskContainer);
40:
41: public abstract EventListenerJSRenderImpl getJSRender();
42:
43: public void addCometTask(CometNotifierImpl notifier) {
44: // Usar por ejemplo cuando una tarea
45: // va a ser muy larga pero no es deseable que esté bloqueado durante la tarea (lockDoc = false)
46: // y dicha tarea modifica el documento
47: // por lo que hay que recoger los cambios, dichos cambios se recogerán
48: // a través de un evento generado automáticamente al volver al cliente y asíncrono.
49:
50: CometTask taskContainer = new CometTask(notifier);
51: EventListenerWrapperImpl listener = (EventListenerWrapperImpl) createCometTaskEventListenerWrapper(taskContainer);
52:
53: String id = listener.getId();
54:
55: tasks.put(id, listener);
56:
57: getJSRender().addEventListenerCode(listener);
58: }
59:
60: public CometTaskEventListenerWrapper removeCometTask(String id) {
61: // Método no público, es llamado por el framework
62: // En este contexto hay que recordar que el ItsNatDocument está bloqueado
63: // por el hilo actual.
64:
65: CometTaskEventListenerWrapper listener = (CometTaskEventListenerWrapper) tasks
66: .get(id);
67: if (listener == null)
68: throw new ItsNatException("Comet Task with id " + id
69: + " does not exist");
70:
71: CometTask task = listener.getCometTask();
72:
73: if (!task.mustWait()) {
74: task.dispose();
75: tasks.remove(id);
76:
77: CometNotifierImpl notifier = task.getCometNotifier();
78: notifier.setPendingData(false); // El evento va a ser procesado por lo que se enviará el código pendiente
79:
80: if (!notifier.isStopped()) {
81: // Para la siguiente notificación:
82: // Se supone que ItsNatDocument está sincronizado (bloqueado por este hilo)
83:
84: addCometTask(notifier);
85: }
86: }
87:
88: // Ver comentarios en AsyncTaskRegistry.removeAsynchronousTask
89:
90: return listener;
91: }
92: }
|