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