sascha@19: package de.intevation.artifactdatabase.rest; sascha@19: sascha@93: import de.intevation.artifactdatabase.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; 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@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@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: sascha@44: if (listenString != null) { sascha@44: component.getServers().add(Protocol.HTTP, listenString, port); sascha@44: } sascha@44: else { sascha@44: component.getServers().add(Protocol.HTTP, port); sascha@44: } sascha@19: 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 :