source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpServer.java@ 8126

Last change on this file since 8126 was 8126, checked in by Don-vip, 9 years ago

fix Sonar issue squid:S2444 - Lazy initialization of "static" fields should be "synchronized"

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5
6import java.io.IOException;
7import java.net.BindException;
8import java.net.ServerSocket;
9import java.net.Socket;
10import java.net.SocketException;
11
12import org.openstreetmap.josm.Main;
13
14/**
15 * Simple HTTP server that spawns a {@link RequestProcessor} for every
16 * connection.
17 *
18 * Taken from YWMS plugin by frsantos.
19 */
20public class RemoteControlHttpServer extends Thread {
21
22 /** The server socket */
23 private ServerSocket server;
24
25 private static volatile RemoteControlHttpServer instance;
26
27 /**
28 * Starts or restarts the HTTP server
29 */
30 public static void restartRemoteControlHttpServer() {
31 int port = Main.pref.getInteger("remote.control.port", 8111);
32 try {
33 stopRemoteControlHttpServer();
34
35 instance = new RemoteControlHttpServer(port);
36 instance.start();
37 } catch (BindException ex) {
38 Main.warn(marktr("Cannot start remotecontrol server on port {0}: {1}"),
39 Integer.toString(port), ex.getLocalizedMessage());
40 } catch (IOException ioe) {
41 Main.error(ioe);
42 }
43 }
44
45 /**
46 * Stops the HTTP server
47 * @since 5861
48 */
49 public static void stopRemoteControlHttpServer() {
50 if (instance != null) {
51 try {
52 instance.stopServer();
53 instance = null;
54 } catch (IOException ioe) {
55 Main.error(ioe);
56 }
57 }
58 }
59
60 /**
61 * Constructor
62 * @param port The port this server will listen on
63 * @throws IOException when connection errors
64 */
65 public RemoteControlHttpServer(int port) throws IOException {
66 super("RemoteControl HTTP Server");
67 this.setDaemon(true);
68 // Start the server socket with only 1 connection.
69 // Also make sure we only listen on the local interface so nobody from the outside can connect!
70 // NOTE: On a dual stack machine with old Windows OS this may not listen on both interfaces!
71 this.server = new ServerSocket(port, 1, RemoteControl.getInetAddress());
72 }
73
74 /**
75 * The main loop, spawns a {@link RequestProcessor} for each connection
76 */
77 @Override
78 public void run() {
79 Main.info(marktr("RemoteControl::Accepting connections on {0}:{1}"),
80 server.getInetAddress(), Integer.toString(server.getLocalPort()));
81 while (true) {
82 try {
83 @SuppressWarnings("resource")
84 Socket request = server.accept();
85 RequestProcessor.processRequest(request);
86 } catch (SocketException se) {
87 if (!server.isClosed())
88 Main.error(se);
89 } catch (IOException ioe) {
90 Main.error(ioe);
91 }
92 }
93 }
94
95 /**
96 * Stops the HTTP server
97 *
98 * @throws IOException
99 */
100 public void stopServer() throws IOException {
101 server.close();
102 Main.info(marktr("RemoteControl::Server stopped."));
103 }
104}
Note: See TracBrowser for help on using the repository browser.