comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/Standalone.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java@d52947ce8629
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifactdatabase.rest;
10
11 import de.intevation.artifacts.common.utils.XMLUtils;
12
13 import de.intevation.artifacts.ArtifactDatabase;
14
15 import org.apache.log4j.Logger;
16
17 import org.restlet.Component;
18 import org.restlet.Server;
19
20 import org.restlet.data.Protocol;
21
22 import org.w3c.dom.Document;
23
24 /**
25 * Starts an HTTP server bound to a RestApp.
26 * The server (binding interface and port) is configure via the
27 * global configuration.
28 *
29 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
30 */
31 public class Standalone
32 implements HTTPServer
33 {
34 private static Logger logger = Logger.getLogger(Standalone.class);
35
36 /**
37 * XPath to figure out the port where to listen from the
38 * global configuration.
39 */
40 public static final String REST_PORT =
41 "/artifact-database/rest-server/port/text()";
42
43 /**
44 * XPath to figure out from global configuration
45 * which network interface to use to bind the HTTP server.
46 */
47 public static final String LISTEN_INTERFACE =
48 "/artifact-database/rest-server/listen/text()";
49
50 /**
51 * The default port of the HTTP server: 8181
52 */
53 public static final int DEFAULT_PORT = 8181;
54
55 public static final String MAX_THREADS =
56 "/artifact-database/rest-server/max-threads/text()";
57
58 public static final String MAX_THREADS_DEFAULT =
59 "1024";
60
61 protected int port;
62
63 protected String listen;
64
65 protected String maxThreads;
66
67 public Standalone() {
68 }
69
70 @Override
71 public void setup(Document document) {
72 String portString = XMLUtils.xpathString(document, REST_PORT, null);
73
74 port = DEFAULT_PORT;
75
76 if (portString != null) {
77 try {
78 port = Integer.parseInt(portString);
79 if (port < 0) {
80 throw new NumberFormatException();
81 }
82 }
83 catch (NumberFormatException nfe) {
84 logger.error("rest port is not a positive integer value.", nfe);
85 return;
86 }
87 }
88
89 listen = XMLUtils.xpathString(document, LISTEN_INTERFACE, null);
90 maxThreads = XMLUtils.xpathString(document, MAX_THREADS, null);
91 }
92
93 protected Server createServer() {
94 return listen != null && listen.length() > 0
95 ? new Server(Protocol.HTTP, listen, port)
96 : new Server(Protocol.HTTP, port);
97 }
98
99 protected void logServerStart() {
100 logger.info("Starting " + getClass().getName() + " HTTP server on "
101 + (listen != null ? listen : "*")
102 + ":" + port);
103 }
104
105 /**
106 * Builds a RestApp wrapped around the given artifact database,
107 * and bind this application to HTTP server. The HTTP server
108 * is configured by the global configuration. If no port is
109 * given by the configuration the default port is used. If
110 * no interface is given the HTTP server is reachable from
111 * all interfaces.
112 * @param db The artifact database to be exposed via the
113 * REST application.
114 */
115 @Override
116 public void startAsServer(ArtifactDatabase db) {
117
118 RestApp app = new RestApp(db);
119
120 Component component = new Component();
121
122 Server server = createServer();
123
124 component.getServers().add(server);
125
126 server.getContext().getParameters().add(
127 "maxThreads", maxThreads != null && maxThreads.length() > 0
128 ? maxThreads
129 : MAX_THREADS_DEFAULT);
130
131 component.getDefaultHost().attach(app);
132
133 logServerStart();
134
135 try {
136 component.start();
137 }
138 catch (Exception e) {
139 logger.error(e.getLocalizedMessage(), e);
140 }
141 }
142 }
143 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org