comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java @ 263:c0fb96f88ad1

Make used HTTP server exchangeable. artifacts/trunk@1974 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 22 May 2011 11:21:40 +0000
parents 5cab846eb2a3
children fa0d9acea897
comparison
equal deleted inserted replaced
262:5cab846eb2a3 263:c0fb96f88ad1
6 * or visit http://www.gnu.org/licenses/ if it does not exist. 6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */ 7 */
8 8
9 package de.intevation.artifactdatabase.rest; 9 package de.intevation.artifactdatabase.rest;
10 10
11 import de.intevation.artifacts.common.utils.Config; 11 import de.intevation.artifacts.common.utils.XMLUtils;
12 12
13 import de.intevation.artifacts.ArtifactDatabase; 13 import de.intevation.artifacts.ArtifactDatabase;
14 14
15 import org.apache.log4j.Logger; 15 import org.apache.log4j.Logger;
16 16
17 import org.restlet.Component; 17 import org.restlet.Component;
18 import org.restlet.Server; 18 import org.restlet.Server;
19 19
20 import org.restlet.data.Protocol; 20 import org.restlet.data.Protocol;
21 21
22 import org.w3c.dom.Document;
23
22 /** 24 /**
23 * Starts an HTTP server bound to a RestApp. 25 * Starts an HTTP server bound to a RestApp.
24 * The server (binding interface and port) is configure via the 26 * The server (binding interface and port) is configure via the
25 * global configuration. 27 * global configuration.
26 * 28 *
27 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a> 29 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
28 */ 30 */
29 public class Standalone 31 public class Standalone
32 implements HTTPServer
30 { 33 {
31 private static Logger logger = Logger.getLogger(Standalone.class); 34 private static Logger logger = Logger.getLogger(Standalone.class);
32 35
33 /** 36 /**
34 * XPath to figure out the port where to listen from the 37 * XPath to figure out the port where to listen from the
53 "/artifact-database/rest-server/max-threads/text()"; 56 "/artifact-database/rest-server/max-threads/text()";
54 57
55 public static final String MAX_THREADS_DEFAULT = 58 public static final String MAX_THREADS_DEFAULT =
56 "1024"; 59 "1024";
57 60
58 /** 61 protected int port;
59 * Builds a RestApp wrapped around the given artifact database,
60 * and bind this application to HTTP server. The HTTP server
61 * is configured by the global configuration. If no port is
62 * given by the configuration the default port is used. If
63 * no interface is given the HTTP server is reachable from
64 * all interfaces.
65 * @param db The artifact database to be exposed via the
66 * REST application.
67 */
68 public static void startAsServer(ArtifactDatabase db) {
69 String portString = Config.getStringXPath(REST_PORT);
70 String listenString = Config.getStringXPath(LISTEN_INTERFACE);
71 String maxThreads = Config.getStringXPath(MAX_THREADS);
72 62
73 int port = DEFAULT_PORT; 63 protected String listen;
64
65 protected String maxThreads;
66
67 public Standalone() {
68 }
69
70 public void setup(Document document) {
71 String portString = XMLUtils.xpathString(document, REST_PORT, null);
72
73
74 port = DEFAULT_PORT;
74 75
75 if (portString != null) { 76 if (portString != null) {
76 try { 77 try {
77 port = Integer.parseInt(portString); 78 port = Integer.parseInt(portString);
78 if (port < 0) { 79 if (port < 0) {
83 logger.error("rest port is not a positive integer value.", nfe); 84 logger.error("rest port is not a positive integer value.", nfe);
84 return; 85 return;
85 } 86 }
86 } 87 }
87 88
89 listen = XMLUtils.xpathString(document, LISTEN_INTERFACE, null);
90 maxThreads = XMLUtils.xpathString(document, MAX_THREADS, null);
91 }
92
93 /**
94 * Builds a RestApp wrapped around the given artifact database,
95 * and bind this application to HTTP server. The HTTP server
96 * is configured by the global configuration. If no port is
97 * given by the configuration the default port is used. If
98 * no interface is given the HTTP server is reachable from
99 * all interfaces.
100 * @param db The artifact database to be exposed via the
101 * REST application.
102 */
103 public void startAsServer(ArtifactDatabase db) {
104
88 RestApp app = new RestApp(db); 105 RestApp app = new RestApp(db);
89 106
90 Component component = new Component(); 107 Component component = new Component();
91 108
92 Server server = null; 109 Server server = null;
93 110
94 if (listenString != null) { 111 if (listen != null) {
95 server = new Server(Protocol.HTTP, listenString, port); 112 server = new Server(Protocol.HTTP, listen, port);
96 } 113 }
97 else { 114 else {
98 server = new Server(Protocol.HTTP, port); 115 server = new Server(Protocol.HTTP, port);
99 } 116 }
100 117
106 : MAX_THREADS_DEFAULT); 123 : MAX_THREADS_DEFAULT);
107 124
108 component.getDefaultHost().attach(app); 125 component.getDefaultHost().attach(app);
109 126
110 logger.info("Starting rest HTTP server on " 127 logger.info("Starting rest HTTP server on "
111 + (listenString != null ? listenString : "*") 128 + (listen != null ? listen : "*")
112 + ":" + port); 129 + ":" + port);
113 130
114 try { 131 try {
115 component.start(); 132 component.start();
116 } 133 }

http://dive4elements.wald.intevation.org