CometNotifierImpl.java :  » Ajax » ItsNat » org » itsnat » impl » core » comet » Java Open Source

Java Open Source » Ajax » ItsNat 
ItsNat » org » itsnat » impl » core » comet » CometNotifierImpl.java
/*
  ItsNat Java Web Application Framework
  Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
  Author: Jose Maria Arranz Santamaria

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version. See the GNU Affero General Public 
  License for more details. See the copy of the GNU Affero General Public License
  included in this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.itsnat.impl.core.comet;

import org.itsnat.core.CometNotifier;
import org.itsnat.core.ItsNatDocument;
import org.itsnat.impl.core.ItsNatDocumentImpl;
import org.itsnat.impl.core.client.ClientDocumentImpl;

/**
 *
 * @author jmarranz
 */
public abstract class CometNotifierImpl implements CometNotifier
{
    protected boolean started = true;
    protected Object monitor = new Object();
    protected boolean pendingData = false;
    protected long expirationDelay = 1 * 60 * 60 * 1000; // Una hora, para que no est indefinidamente parado el hilo
    protected ClientDocumentImpl clientDoc;    
    
    /**
     * Creates a new instance of CometNotifierImpl
     */
    public CometNotifierImpl(ClientDocumentImpl clientDoc)
    {
        this.clientDoc = clientDoc;
    }

    protected void finalize()
    {
        stop(); // Para asegurarnos que el hilo termine, como started es false el ItsNatDocument no se usa (no hay problema de estado "incorrecto" del documento)
    }
    
    public Object getMonitor()
    {
        return monitor;
    }
    
    public void notifyClient()
    {
        // if (!started) throw new ItsNatException("Notifier was disposed");
        
        if (isStopped()) return; // Si ya est parado no hace nada
        
        weakup();  // Despertar al hilo en espera para que termine y el hilo del evento pendiente de l despierte y haga su trabajo
    }

    public void stop()
    {
        if (isStopped()) return; // Ya parado

        this.started = false;
        weakup(); // Despertar el hilo en espera para que se entere que ha de terminar
        this.pendingData = false;
    }
    
    public boolean isStopped()
    {
        return !started;
    }
    
    public void weakup()
    {
        synchronized(monitor)
        {
            this.pendingData = true;
            monitor.notifyAll();
        }             
    }        

    public boolean hasPendingData()
    {
        return pendingData;
    }

    public void setPendingData(boolean pendingData)
    {
        this.pendingData = pendingData;
    }    
 
    public ItsNatDocument getItsNatDocument()
    {
        return getItsNatDocumentImpl();
    } 
    
    public ItsNatDocumentImpl getItsNatDocumentImpl()
    {
        return clientDoc.getItsNatDocumentImpl();
    }

    public long getExpirationDelay()
    {
        return expirationDelay;
    }

    public void setExpirationDelay(long expirationDelay)
    {
        this.expirationDelay = expirationDelay;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.