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
RevLine 
[3707]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
[5893]4import static org.openstreetmap.josm.tools.I18n.marktr;
[4797]5
[3707]6import java.io.IOException;
[4797]7import java.net.BindException;
[3707]8import java.net.ServerSocket;
9import java.net.Socket;
10import java.net.SocketException;
11
[5893]12import org.openstreetmap.josm.Main;
13
[3707]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
[8126]25 private static volatile RemoteControlHttpServer instance;
[3707]26
27 /**
28 * Starts or restarts the HTTP server
29 */
[4797]30 public static void restartRemoteControlHttpServer() {
[5894]31 int port = Main.pref.getInteger("remote.control.port", 8111);
[4797]32 try {
[5861]33 stopRemoteControlHttpServer();
[3707]34
[5894]35 instance = new RemoteControlHttpServer(port);
[3707]36 instance.start();
[4797]37 } catch (BindException ex) {
[6248]38 Main.warn(marktr("Cannot start remotecontrol server on port {0}: {1}"),
[5894]39 Integer.toString(port), ex.getLocalizedMessage());
[4797]40 } catch (IOException ioe) {
[6643]41 Main.error(ioe);
[3707]42 }
43 }
44
45 /**
[5861]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) {
[6643]55 Main.error(ioe);
[5861]56 }
57 }
58 }
59
60 /**
[3707]61 * Constructor
62 * @param port The port this server will listen on
63 * @throws IOException when connection errors
64 */
[6643]65 public RemoteControlHttpServer(int port) throws IOException {
[3707]66 super("RemoteControl HTTP Server");
67 this.setDaemon(true);
68 // Start the server socket with only 1 connection.
[7800]69 // Also make sure we only listen on the local interface so nobody from the outside can connect!
[5893]70 // NOTE: On a dual stack machine with old Windows OS this may not listen on both interfaces!
[7800]71 this.server = new ServerSocket(port, 1, RemoteControl.getInetAddress());
[3707]72 }
73
74 /**
75 * The main loop, spawns a {@link RequestProcessor} for each connection
76 */
[6084]77 @Override
[6643]78 public void run() {
[7800]79 Main.info(marktr("RemoteControl::Accepting connections on {0}:{1}"),
80 server.getInetAddress(), Integer.toString(server.getLocalPort()));
[6643]81 while (true) {
[7034]82 try {
83 @SuppressWarnings("resource")
84 Socket request = server.accept();
[3707]85 RequestProcessor.processRequest(request);
[7033]86 } catch (SocketException se) {
87 if (!server.isClosed())
[6643]88 Main.error(se);
89 } catch (IOException ioe) {
90 Main.error(ioe);
[3707]91 }
92 }
93 }
94
95 /**
96 * Stops the HTTP server
97 *
98 * @throws IOException
99 */
[6643]100 public void stopServer() throws IOException {
[3707]101 server.close();
[5893]102 Main.info(marktr("RemoteControl::Server stopped."));
[3707]103 }
104}
Note: See TracBrowser for help on using the repository browser.