view artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java @ 237:23ec48ce717e

Possible fix for flys/issue8 artifacts/trunk@1650 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 06 Apr 2011 16:49:14 +0000
parents b2115f484edb
children 5cab846eb2a3
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.artifactdatabase.rest;

import de.intevation.artifacts.common.utils.Config;

import de.intevation.artifacts.ArtifactDatabase;

import org.apache.log4j.Logger;

import org.restlet.Component;
import org.restlet.Server;

import org.restlet.data.Protocol;

/**
 * Starts an HTTP server bound to a RestApp.
 * The server (binding interface and port) is configure via the
 * global configuration.
 *
 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
 */
public class Standalone
{
    private static Logger logger = Logger.getLogger(Standalone.class);

    /**
     * XPath to figure out the port where to listen from the
     * global configuration.
     */
    public static final String REST_PORT =
        "/artifact-database/rest-server/port/text()";

    /**
     * XPath to figure out from global configuration
     * which network interface to use to bind the HTTP server.
     */
    public static final String LISTEN_INTERFACE =
        "/artifact-database/rest-server/listen/text()";

    /**
     * The default port of the HTTP server: 8181
     */
    public static final int DEFAULT_PORT = 8181;

    /**
     * Builds a RestApp wrapped around the given artifact database,
     * and bind this application to HTTP server. The HTTP server
     * is configured by the global configuration. If no port is
     * given by the configuration the default port is used. If
     * no interface is given the HTTP server is reachable from
     * all interfaces.
     * @param db The artifact database to be exposed via the
     * REST application.
     */
    public static void startAsServer(ArtifactDatabase db) {
        String portString   = Config.getStringXPath(REST_PORT);
        String listenString = Config.getStringXPath(LISTEN_INTERFACE);

        int port = DEFAULT_PORT;

        if (portString != null) {
            try {
                port = Integer.parseInt(portString);
                if (port < 0) {
                    throw new NumberFormatException();
                }
            }
            catch (NumberFormatException nfe) {
                logger.error("rest port is not a positive integer value.", nfe);
                return;
            }
        }

        RestApp app = new RestApp(db);

        Component component = new Component();

        Server server = null;

        if (listenString != null) {
            server = new Server(Protocol.HTTP, listenString, port);
        }
        else {
            server = new Server(Protocol.HTTP, port);
        }

        component.getServers().add(server);

        server.getContext().getParameters().add("maxThreads", "512");

        component.getDefaultHost().attach(app);

        logger.info("Starting rest HTTP server on "
            + (listenString != null ? listenString : "*")
            + ":" + port);

        try {
            component.start();
        }
        catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org