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@263: import de.intevation.artifacts.common.utils.XMLUtils; 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@263: import org.w3c.dom.Document; sascha@263: 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@263: implements HTTPServer 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@263: protected int port; sascha@19: sascha@263: protected String listen; sascha@263: sascha@263: protected String maxThreads; sascha@263: sascha@263: public Standalone() { sascha@263: } sascha@263: sascha@264: @Override sascha@263: public void setup(Document document) { sascha@263: String portString = XMLUtils.xpathString(document, REST_PORT, null); sascha@263: sascha@263: 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@263: listen = XMLUtils.xpathString(document, LISTEN_INTERFACE, null); sascha@263: maxThreads = XMLUtils.xpathString(document, MAX_THREADS, null); sascha@263: } sascha@263: sascha@264: protected Server createServer() { sascha@265: return listen != null && listen.length() > 0 sascha@264: ? new Server(Protocol.HTTP, listen, port) sascha@264: : new Server(Protocol.HTTP, port); sascha@264: } sascha@264: sascha@264: protected void logServerStart() { sascha@264: logger.info("Starting " + getClass().getName() + " HTTP server on " sascha@264: + (listen != null ? listen : "*") sascha@264: + ":" + port); sascha@264: } sascha@264: sascha@263: /** sascha@263: * Builds a RestApp wrapped around the given artifact database, sascha@263: * and bind this application to HTTP server. The HTTP server sascha@263: * is configured by the global configuration. If no port is sascha@263: * given by the configuration the default port is used. If sascha@263: * no interface is given the HTTP server is reachable from sascha@263: * all interfaces. sascha@263: * @param db The artifact database to be exposed via the sascha@263: * REST application. sascha@263: */ sascha@264: @Override sascha@263: public void startAsServer(ArtifactDatabase db) { sascha@263: sascha@19: RestApp app = new RestApp(db); sascha@19: sascha@19: Component component = new Component(); sascha@19: sascha@264: Server server = createServer(); sascha@19: ingo@101: component.getServers().add(server); ingo@101: sascha@262: server.getContext().getParameters().add( sascha@265: "maxThreads", maxThreads != null && maxThreads.length() > 0 sascha@262: ? maxThreads sascha@262: : MAX_THREADS_DEFAULT); ingo@101: sascha@19: component.getDefaultHost().attach(app); sascha@19: sascha@264: logServerStart(); 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 :