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

com.sun.grizzly.standalone.Cometd Maven / Gradle / Ivy

/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (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/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */

package com.sun.grizzly.standalone;

import com.sun.grizzly.comet.CometAsyncFilter;
import com.sun.grizzly.cometd.standalone.CometdAdapter;
import com.sun.grizzly.http.AsyncHandler;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.arp.DefaultAsyncHandler;
import com.sun.grizzly.tcp.Adapter;
import java.util.logging.Level;

/**
 * Basic startup class used when Grizzly standalone is used
 *
 * @author Jeanfrancois Arcand
 */
public class Cometd extends StandaloneMainUtil {
    static int port = 8080;
    
    private String applicationLoc;
    
    public Cometd() {
    }
    
    
    public static void main( String args[] ) throws Exception {       
        Cometd main = new Cometd();        
        main.start(args);
    }

    
    @Override
    public SelectorThread createSelectorThread(String[] args) throws Exception{
        SelectorThread st = super.createSelectorThread(args);
        st.setEnableAsyncExecution(true);
        st.setBufferResponse(false);    
        st.setFileCacheIsEnabled(false);
        st.setLargeFileCacheEnabled(false);
        AsyncHandler asyncHandler = new DefaultAsyncHandler();
        asyncHandler.addAsyncFilter(new CometAsyncFilter()); 
        st.setAsyncHandler(asyncHandler);
            
        SelectorThread.logger()
            .log(Level.INFO,"Enabling Grizzly Bayeux support.");
        return st;
    }   

    @Override
    public void printHelpAndExit() {
        System.err.println("Usage: " + Main.class.getCanonicalName() + " [options]");
        System.err.println();
        System.err.println("    -p, --port=port                  Runs Servlet on the specified port.");
        System.err.println("                                     Default: 8080");
        System.err.println("    -a, --apps=application path      The static resourde folder or jar or war location.");
        System.err.println("                                     Default: .");
        System.err.println("    -h, --help                       Show this help message.");
        System.exit(1);  
    }

    
    @Override
    public boolean parseOptions(String[] args) {
        // parse options
        for (int i = 0; i < args.length - 1; i++) {
            String arg = args[i];
            
            if("-h".equals(arg) || "--help".equals(arg)) {
                printHelpAndExit();
            } else if("-a".equals(arg)) {
                i ++;
                applicationLoc = args[i];
            } else if(arg.startsWith("--application=")) {
                applicationLoc = arg.substring("--application=".length(), arg.length());
            } else if ("-p".equals(arg)) {
                i ++;
                setPort(args[i]);
            } else if (arg.startsWith("--port=")) {
                String num = arg.substring("--port=".length(), arg.length());
                setPort(num);
            }
        }
        
        if(applicationLoc == null) {
            System.err.println("Illegal War file.");
            printHelpAndExit();
        }
        return true;
    }

    @Override
    public Adapter configureAdapter(SelectorThread st) {
        String adapterClass =  System.getProperty(ADAPTER);
        Adapter adapter;
        if (adapterClass == null){
            adapter = new CometdAdapter();
            ((CometdAdapter)adapter).setRootFolder(SelectorThread.getWebAppRootPath());
        } else {
            adapter = (Adapter)loadClass(adapterClass);
        }
        return adapter;
    }

    @Override
    public String parseApplicationLocation(String[] args) {
       return applicationLoc;
    }
   
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy