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.comet;
015:
016: import org.itsnat.core.CometNotifier;
017: import org.itsnat.core.ItsNatDocument;
018: import org.itsnat.impl.core.ItsNatDocumentImpl;
019: import org.itsnat.impl.core.client.ClientDocumentImpl;
020:
021: /**
022: *
023: * @author jmarranz
024: */
025: public abstract class CometNotifierImpl implements CometNotifier {
026: protected boolean started = true;
027: protected Object monitor = new Object();
028: protected boolean pendingData = false;
029: protected long expirationDelay = 1 * 60 * 60 * 1000; // Una hora, para que no esté indefinidamente parado el hilo
030: protected ClientDocumentImpl clientDoc;
031:
032: /**
033: * Creates a new instance of CometNotifierImpl
034: */
035: public CometNotifierImpl(ClientDocumentImpl clientDoc) {
036: this .clientDoc = clientDoc;
037: }
038:
039: protected void finalize() {
040: stop(); // Para asegurarnos que el hilo termine, como started es false el ItsNatDocument no se usa (no hay problema de estado "incorrecto" del documento)
041: }
042:
043: public Object getMonitor() {
044: return monitor;
045: }
046:
047: public void notifyClient() {
048: // if (!started) throw new ItsNatException("Notifier was disposed");
049:
050: if (isStopped())
051: return; // Si ya está parado no hace nada
052:
053: weakup(); // Despertar al hilo en espera para que termine y el hilo del evento pendiente de él despierte y haga su trabajo
054: }
055:
056: public void stop() {
057: if (isStopped())
058: return; // Ya parado
059:
060: this .started = false;
061: weakup(); // Despertar el hilo en espera para que se entere que ha de terminar
062: this .pendingData = false;
063: }
064:
065: public boolean isStopped() {
066: return !started;
067: }
068:
069: public void weakup() {
070: synchronized (monitor) {
071: this .pendingData = true;
072: monitor.notifyAll();
073: }
074: }
075:
076: public boolean hasPendingData() {
077: return pendingData;
078: }
079:
080: public void setPendingData(boolean pendingData) {
081: this .pendingData = pendingData;
082: }
083:
084: public ItsNatDocument getItsNatDocument() {
085: return getItsNatDocumentImpl();
086: }
087:
088: public ItsNatDocumentImpl getItsNatDocumentImpl() {
089: return clientDoc.getItsNatDocumentImpl();
090: }
091:
092: public long getExpirationDelay() {
093: return expirationDelay;
094: }
095:
096: public void setExpirationDelay(long expirationDelay) {
097: this.expirationDelay = expirationDelay;
098: }
099:
100: }
|