annotate artifact-database/src/main/java/de/intevation/artifactdatabase/rest/Standalone.java @ 90:68285f7bc476

More javadoc. artifacts/trunk@846 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 26 Mar 2010 17:59:50 +0000
parents 69c84cf7c5d7
children e27cf9c84eb8
rev   line source
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.artifactdatabase.rest;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import org.restlet.Component;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import org.restlet.data.Protocol;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import de.intevation.artifacts.ArtifactDatabase;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import de.intevation.artifactdatabase.Config;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import org.apache.log4j.Logger;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12
24
d5dc2900392f * Added callback parameter to Artifact.setup()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
13 /**
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
14 * Starts an HTTP server bound to a RestApp.
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
15 * The server (binding interface and port) is configure via the
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
16 * global configuration.
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
17 *
77
48d1a9a082c2 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
18 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
24
d5dc2900392f * Added callback parameter to Artifact.setup()
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
19 */
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 public class Standalone
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 {
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 private static Logger logger = Logger.getLogger(Standalone.class);
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
24 /**
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
25 * XPath to figure out the port where to listen from the
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
26 * global configuration.
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
27 */
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 public static final String REST_PORT =
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 "/artifact-database/rest-server/port/text()";
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
31 /**
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
32 * XPath to figure out from global configuration
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
33 * which network interface to use to bind the HTTP server.
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
34 */
44
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
35 public static final String LISTEN_INTERFACE =
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
36 "/artifact-database/rest-server/listen/text()";
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
37
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
38 /**
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
39 * The default port of the HTTP server: 8181
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
40 */
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 public static final int DEFAULT_PORT = 8181;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
43 /**
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
44 * Builds a RestApp wrapped around the given artifact database,
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
45 * and bind this application to HTTP server. The HTTP server
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
46 * is configured by the global configuration. If no port is
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
47 * given by the configuration the default port is used. If
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
48 * no interface is given the HTTP server is reachable from
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
49 * all interfaces.
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
50 * @param db The artifact database to be exposed via the
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
51 * REST application.
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
52 */
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 public static void startAsServer(ArtifactDatabase db) {
44
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
54 String portString = Config.getStringXPath(REST_PORT);
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
55 String listenString = Config.getStringXPath(LISTEN_INTERFACE);
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 int port = DEFAULT_PORT;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 if (portString != null) {
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 try {
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 port = Integer.parseInt(portString);
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 if (port < 0) {
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 throw new NumberFormatException();
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 }
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 catch (NumberFormatException nfe) {
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 logger.error("rest port is not a positive integer value.", nfe);
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 return;
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 }
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 }
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 RestApp app = new RestApp(db);
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 Component component = new Component();
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75
44
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
76 if (listenString != null) {
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
77 component.getServers().add(Protocol.HTTP, listenString, port);
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
78 }
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
79 else {
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
80 component.getServers().add(Protocol.HTTP, port);
86d2a843e588 Allow REST server to bind to configurable interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 24
diff changeset
81 }
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 component.getDefaultHost().attach(app);
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84
47
4ae4dc99127d Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 46
diff changeset
85 logger.info("Starting rest HTTP server on "
46
f2648672c9c4 Removed needless imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 44
diff changeset
86 + (listenString != null ? listenString : "*")
f2648672c9c4 Removed needless imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 44
diff changeset
87 + ":" + port);
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 19
diff changeset
88
19
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 try {
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 component.start();
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 }
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 catch (Exception e) {
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 logger.error(e.getLocalizedMessage(), e);
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 }
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 }
1259d192e3c3 * New configuration based on config directory
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
97 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org