ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@19: package de.intevation.artifactdatabase.rest; sascha@19: sascha@207: import de.intevation.artifacts.common.utils.Config; sascha@93: sascha@93: import de.intevation.artifacts.ArtifactDatabase; sascha@93: sascha@93: import org.apache.log4j.Logger; sascha@93: sascha@19: import org.restlet.Component; ingo@101: import org.restlet.Server; sascha@19: sascha@19: import org.restlet.data.Protocol; sascha@19: sascha@24: /** sascha@88: * Starts an HTTP server bound to a RestApp. sascha@88: * The server (binding interface and port) is configure via the sascha@88: * global configuration. sascha@88: * sascha@77: * @author Sascha L. Teichmann sascha@24: */ sascha@19: public class Standalone sascha@19: { sascha@19: private static Logger logger = Logger.getLogger(Standalone.class); sascha@19: sascha@88: /** sascha@88: * XPath to figure out the port where to listen from the sascha@88: * global configuration. sascha@88: */ sascha@19: public static final String REST_PORT = sascha@19: "/artifact-database/rest-server/port/text()"; sascha@19: sascha@88: /** sascha@88: * XPath to figure out from global configuration sascha@88: * which network interface to use to bind the HTTP server. sascha@88: */ sascha@44: public static final String LISTEN_INTERFACE = sascha@44: "/artifact-database/rest-server/listen/text()"; sascha@44: sascha@88: /** sascha@88: * The default port of the HTTP server: 8181 sascha@88: */ sascha@19: public static final int DEFAULT_PORT = 8181; sascha@19: sascha@262: public static final String MAX_THREADS = sascha@262: "/artifact-database/rest-server/max-threads/text()"; sascha@262: sascha@262: public static final String MAX_THREADS_DEFAULT = sascha@262: "1024"; sascha@262: sascha@88: /** sascha@88: * Builds a RestApp wrapped around the given artifact database, sascha@88: * and bind this application to HTTP server. The HTTP server sascha@88: * is configured by the global configuration. If no port is sascha@88: * given by the configuration the default port is used. If sascha@88: * no interface is given the HTTP server is reachable from sascha@88: * all interfaces. sascha@88: * @param db The artifact database to be exposed via the sascha@88: * REST application. sascha@88: */ sascha@19: public static void startAsServer(ArtifactDatabase db) { sascha@44: String portString = Config.getStringXPath(REST_PORT); sascha@44: String listenString = Config.getStringXPath(LISTEN_INTERFACE); sascha@262: String maxThreads = Config.getStringXPath(MAX_THREADS); sascha@19: sascha@19: int port = DEFAULT_PORT; sascha@19: sascha@19: if (portString != null) { sascha@19: try { sascha@19: port = Integer.parseInt(portString); sascha@19: if (port < 0) { sascha@19: throw new NumberFormatException(); sascha@19: } sascha@19: } sascha@19: catch (NumberFormatException nfe) { sascha@19: logger.error("rest port is not a positive integer value.", nfe); sascha@19: return; sascha@19: } sascha@19: } sascha@19: sascha@19: RestApp app = new RestApp(db); sascha@19: sascha@19: Component component = new Component(); sascha@19: ingo@101: Server server = null; ingo@101: sascha@44: if (listenString != null) { ingo@101: server = new Server(Protocol.HTTP, listenString, port); sascha@44: } sascha@44: else { ingo@101: server = new Server(Protocol.HTTP, port); sascha@44: } sascha@19: ingo@101: component.getServers().add(server); ingo@101: sascha@262: server.getContext().getParameters().add( sascha@262: "maxThreads", maxThreads != null sascha@262: ? maxThreads sascha@262: : MAX_THREADS_DEFAULT); ingo@101: sascha@19: component.getDefaultHost().attach(app); sascha@19: sascha@47: logger.info("Starting rest HTTP server on " sascha@46: + (listenString != null ? listenString : "*") sascha@46: + ":" + port); sascha@20: sascha@19: try { sascha@19: component.start(); sascha@19: } sascha@19: catch (Exception e) { sascha@19: logger.error(e.getLocalizedMessage(), e); sascha@19: } sascha@19: } sascha@19: } sascha@93: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :