001package jmri.web.server;
002
003import java.awt.event.ActionEvent;
004import javax.swing.Icon;
005import jmri.util.swing.JmriAbstractAction;
006import jmri.util.swing.WindowInterface;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Action to start a web server. Doesn't show a panel.
012 *
013 * @author Randall Wood Copyright (C) 2012
014 */
015public class WebServerAction extends JmriAbstractAction {
016
017    private static ServerThread serverThread = null;
018    private static final Object lock = new Object();
019    private final static Logger log = LoggerFactory.getLogger(WebServerAction.class);
020
021    public WebServerAction(String s, WindowInterface wi) {
022        super(s, wi);
023    }
024
025    public WebServerAction(String s, Icon i, WindowInterface wi) {
026        super(s, i, wi);
027    }
028
029    public WebServerAction() {
030        super(Bundle.getMessage("MenuWebServerAction"));
031    }
032
033    @Override
034    public void actionPerformed(ActionEvent ev) {
035        synchronized (lock) {
036            if (serverThread == null) {
037                serverThread = new ServerThread();
038                serverThread.setName("WebServerAction action");
039                serverThread.start();
040            } else {
041                log.info("Web Server already running");
042            }
043        }
044    }
045
046    @Override
047    public jmri.util.swing.JmriPanel makePanel() { return null; } // not used by this classes actionPerformed, as it doesn't show anything
048    
049    private static class ServerThread extends Thread {
050
051        @Override
052        public void run() {
053            try {
054                WebServer.getDefault().start();
055            } catch (Exception ex) {
056                log.error("Unable to start web server.", ex);
057            }
058        }
059    }
060}