Package yalp.modules.grizzly

Source Code of yalp.modules.grizzly.Server

package yalp.modules.grizzly;

import com.sun.grizzly.http.embed.GrizzlyWebServer;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Properties;
import java.util.logging.Level;
import yalp.Logger;
import yalp.Yalp;
import yalp.Yalp.Mode;

public class Server {

    final GrizzlyWebServer ws;

    public Server(File applicationDir, String frameworkId) {
        java.util.logging.Logger.getLogger("").setLevel(Level.OFF);
        Properties p = Yalp.configuration;
        int httpPort = Integer.parseInt(p.getProperty("http.port", "9000"));
        InetAddress address = null;
        if (System.getProperties().containsKey("http.port")) {
            httpPort = Integer.parseInt(System.getProperty("http.port"));
        }
        try {
            if(p.getProperty("http.address") != null) {
                address = InetAddress.getByName(p.getProperty("http.address"));
            }
            if (System.getProperties().containsKey("http.address")) {
                address = InetAddress.getByName(System.getProperty("http.address"));
            }
        } catch(Exception e) {
            Logger.error(e, "Could not understand http.address");
            System.exit(-1);
        }
        ws = new GrizzlyWebServer(httpPort);
        ws.useAsynchronousWrite(true);
        ws.addGrizzlyAdapter(new YalpGrizzlyAdapter(applicationDir, frameworkId, ""), new String[] {"/"});
        try {
            ws.start();
            if (Yalp.mode == Mode.DEV) {
                if(address == null) {
                    Logger.info("Listening for HTTP on port %s (Waiting a first request to start) ...", httpPort);
                } else {
                    Logger.info("Listening for HTTP at %2$s:%1$s (Waiting a first request to start) ...", httpPort, address);
                }
            } else {
                if(address == null) {
                    Logger.info("Listening for HTTP on port %s ...", httpPort);
                } else {
                    Logger.info("Listening for HTTP at %2$s:%1$s  ...", httpPort, address);
                }
            }
        } catch (IOException e) {
            Logger.error("Could not bind on port " + httpPort, e);
            ws.stop();
            System.exit(-1);
        }
    }

    public static void main(String[] args) throws Exception {
        File root = new File(System.getProperty("application.path"));
        new Server(root, System.getProperty("yalp.id", ""));
    }

}
TOP

Related Classes of yalp.modules.grizzly.Server

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.