Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007-2015 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.grizzly.comet;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Closeable;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.CloseType;
import org.glassfish.grizzly.GenericCloseListener;
import org.glassfish.grizzly.ReadHandler;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.http.server.TimeoutHandler;
import org.glassfish.grizzly.http.io.NIOInputStream;
import org.glassfish.grizzly.http.util.Header;
import org.glassfish.grizzly.utils.DataStructures;
/**
* The main object used by {@link CometHandler} and Servlet to push information amongst suspended request/response. The
* {@link CometContext} is always available for {@link CometHandler} and can be used to {@link #notify}, or share
* information with other {@link CometHandler}. This is the equivalent of server push as the CometContext will invoke
* all registered CometHandler ({@link #addCometHandler}) sequentially.
A CometContext can be considered as a
* topic where CometHandler register for information. A CometContext can be shared amongst Servlet of the same
* application, or globally across all deployed web applications. Normally, a CometContext is created using a topic's
* name like:
*
*
* CometEngine ce = CometEngine.getEngine();
* CometContext cc = ce.registerContext("MyTopic");
*
* and then inside a Servlet.service() method, you just need to call:
*
* cc.addCometListener(myNewCometListener());
* cc.notify("I'm pushing data to all registered CometHandler");
*
*
*
As soon as {@link #addCometHandler} is invoked, Grizzly will automatically suspend the
* request/response (will not commit the response). A response can be resumed by invoking {@link
* #resumeCometHandler}, which will automatically commit the response and remove the associated CometHandler from the
* CometContext.
A CometContext uses a {@link NotificationHandler} to invoke, using the calling thread or a
* Grizzly thread pool, all CometHandler than have been added using the {@link #addCometHandler}. A {@link
* NotificationHandler} can be used to filter or transform the content that will eventually be pushed back to all
* connected clients. You can also use a {@link NotificationHandler} to throttle push like invoking only a subset of the
* CometHandler, etc.
Idle suspended connection can be timed out by configuring the {@link
* #setExpirationDelay(long)}. The value needs to be in milliseconds. If there is no I/O operations and no invocation of
* {@link #notify} during the expiration delay, Grizzly will resume all suspended connection. An application will have a
* chance to send back data using the connection as Grizzly will invoke the {@link CometHandler#onInterrupt} before
* resuming the connection. Note that setting the expiration delay to -1 disable the above mechanism, e.g. idle
* connection will never get resumed by Grizzly.
Attributes can be added/removed the same way HttpServletSession
* is doing. It is not recommended to use attributes if this {@link CometContext} is not shared amongst multiple context
* path (uses HttpServletSession instead).
*/
public class CometContext {
/**
* Generic error message
*/
protected final static String INVALID_COMET_HANDLER = "CometHandler cannot be null. "
+ "This CometHandler was probably resumed and an invalid reference was made to it.";
protected final static String ALREADY_REMOVED = "CometHandler already been removed or invalid.";
private final static String COMET_NOT_ENABLED = "Make sure you have enabled Comet or make sure the thread"
+ " invoking that method is the same as the Servlet.service() thread.";
protected final static Logger LOGGER = Logger.getLogger(CometContext.class.getName());
private final Map