All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wings.comet.ServletContextCometConnectionManager Maven / Gradle / Ivy

The newest version!
package org.wings.comet;

import org.wings.session.Session;

import javax.servlet.ServletContext;
import java.util.HashSet;

class ServletContextCometConnectionManager extends CometConnectionManager {

    private final ServletContext servletContext;
    private final HashSet connectionSet;

    public ServletContextCometConnectionManager(Session session) {
        servletContext = session.getServletContext();
        synchronized (servletContext) {
            connectionSet = getSharedObject();
        }
    }

    @Override
    public boolean canAddHangingGet() {
        synchronized (connectionSet) {
            return !connectionSet.contains(getBrowserId());
        }
    }

    @Override
    public boolean addHangingGet() {
        synchronized (connectionSet) {
            final boolean result = !connectionSet.contains(getBrowserId());
            connectionSet.add(getBrowserId());
            return result;
        }
    }

    @Override
    public void removeHangingGet() {
		synchronized (connectionSet) {
			connectionSet.remove(getBrowserId());
		}
	}

    HashSet getSharedObject() {
        HashSet connectionSet = (HashSet) servletContext.getAttribute(NAME);
        if (connectionSet == null) {
            connectionSet = new HashSet<>();
            servletContext.setAttribute(NAME, connectionSet);
        }
        return connectionSet;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy