Launcher.java 12.2 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21
package org.jivesoftware.messenger.launcher;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.*;
22 23
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.util.XMLProperties;
24 25
import org.jdesktop.jdic.tray.TrayIcon;
import org.jdesktop.jdic.tray.SystemTray;
Matt Tucker's avatar
Matt Tucker committed
26 27 28 29 30 31 32

/**
 * Launcher for Jive Messenger.
 *
 * @author Matt Tucker
 */
public class Launcher {
Matt Tucker's avatar
Matt Tucker committed
33

Matt Tucker's avatar
Matt Tucker committed
34
    private Process messengerd = null;
35
    private String configFile = JiveGlobals.getMessengerHome() + File.separator + "conf" + File.separator + "jive-messenger.xml";
Matt Tucker's avatar
Matt Tucker committed
36 37 38 39 40 41
    private JPanel toolbar = new JPanel();

    /**
     * Creates a new Launcher object.
     */
    public Launcher() {
42 43
        // Initialize the SystemTray now (to avoid a bug!)
        SystemTray tray = SystemTray.getDefaultSystemTray();
Matt Tucker's avatar
Matt Tucker committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        // Use the native look and feel.
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        String title = "Jive Messenger Server Launcher";
        final JFrame frame = new JFrame(title);

        ImageIcon splash = null;
        JLabel splashLabel = null;

        // Set the icon.
        try {
            ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("messenger-16x16.gif"));
            splash = new ImageIcon(getClass().getClassLoader().getResource("splash.gif"));
            splashLabel = new JLabel("", splash, JLabel.LEFT);
            frame.setIconImage(icon.getImage());
        }
        catch (Exception e) {
        }

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        // Add buttons
        final JButton startButton = new JButton("Start");
        startButton.setActionCommand("Start");

        final JButton stopButton = new JButton("Stop");
        stopButton.setActionCommand("Stop");

        final JButton browserButton = new JButton("Launch Admin");
        browserButton.setActionCommand("Launch Admin");

        final JButton quitButton = new JButton("Quit");
        quitButton.setActionCommand("Quit");

        toolbar.setLayout(new GridBagLayout());
        toolbar.add(startButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        toolbar.add(stopButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        toolbar.add(browserButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        toolbar.add(quitButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
89

Matt Tucker's avatar
Matt Tucker committed
90 91 92 93 94 95
        if (splashLabel != null) {
            mainPanel.add(splashLabel, BorderLayout.CENTER);
        }

        mainPanel.add(toolbar, BorderLayout.SOUTH);

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        // create the main menu of the system tray icon
        JPopupMenu menu = new JPopupMenu("Messenger Menu");

        menu.addSeparator();

        final JMenuItem showMenuItem = new JMenuItem("Hide");
        showMenuItem.setActionCommand("Hide/Show");
        menu.add(showMenuItem);

        final JMenuItem startMenuItem = new JMenuItem("Start");
        startMenuItem.setActionCommand("Start");
        menu.add(startMenuItem);

        final JMenuItem stopMenuItem = new JMenuItem("Stop");
        stopMenuItem.setActionCommand("Stop");
        menu.add(stopMenuItem);

        final JMenuItem browserMenuItem = new JMenuItem("Launch Admin");
        browserMenuItem.setActionCommand("Launch Admin");
        menu.add(browserMenuItem);

        menu.addSeparator();

        final JMenuItem quitMenuItem = new JMenuItem("Quit");
        quitMenuItem.setActionCommand("Quit");
        menu.add(quitMenuItem);

        browserButton.setEnabled(false);
        stopButton.setEnabled(false);
        browserMenuItem.setEnabled(false);
        stopMenuItem.setEnabled(false);
Matt Tucker's avatar
Matt Tucker committed
127 128 129 130 131 132 133 134

        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals("Start")) {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    startApplication();
                    startButton.setEnabled(false);
                    stopButton.setEnabled(true);
135 136
                    startMenuItem.setEnabled(false);
                    stopMenuItem.setEnabled(true);
Matt Tucker's avatar
Matt Tucker committed
137 138 139 140 141 142 143 144
                    Thread thread = new Thread() {
                        public void run() {
                            try {
                                sleep(8000);
                            }
                            catch (Exception e) {
                            }

145 146 147 148 149
                            // Enable the Launch Admin button/menu item only if the server has started
                            if (stopButton.isEnabled()) {
                                browserButton.setEnabled(true);
                                browserMenuItem.setEnabled(true);
                            }
Matt Tucker's avatar
Matt Tucker committed
150 151 152 153 154 155 156 157 158 159 160

                            frame.setCursor(Cursor.getDefaultCursor());
                        }
                    };

                    thread.start();
                }
                else if (e.getActionCommand().equals("Stop")) {
                    stopApplication();
                    browserButton.setEnabled(false);
                    startButton.setEnabled(true);
161 162 163 164
                    stopButton.setEnabled(false);
                    browserMenuItem.setEnabled(false);
                    startMenuItem.setEnabled(true);
                    stopMenuItem.setEnabled(false);
Matt Tucker's avatar
Matt Tucker committed
165 166 167 168 169 170 171 172
                }
                else if (e.getActionCommand().equals("Launch Admin")) {
                    launchBrowser();
                }
                else if (e.getActionCommand().equals("Quit")) {
                    stopApplication();
                    System.exit(0);
                }
173 174 175 176 177 178 179 180 181 182 183 184 185 186
                else if (e.getActionCommand().equals("Hide/Show") || e.getActionCommand().equals("PressAction")) {
                    // Hide/Unhide the window if the user clicked in the system tray icon or
                    // selected the menu option
                    if (frame.isVisible()) {
                        frame.setVisible(false);
                        frame.setState(Frame.ICONIFIED);
                        showMenuItem.setText("Show");
                    }
                    else {
                        frame.setVisible(true);
                        frame.setState(Frame.NORMAL);
                        showMenuItem.setText("Hide");
                    }
                }
Matt Tucker's avatar
Matt Tucker committed
187 188 189 190 191 192 193 194 195
            }
        };

        // Register a listener for the radio buttons.
        startButton.addActionListener(actionListener);
        stopButton.addActionListener(actionListener);
        browserButton.addActionListener(actionListener);
        quitButton.addActionListener(actionListener);

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        // Register a listener for the menu items.
        quitMenuItem.addActionListener(actionListener);
        browserMenuItem.addActionListener(actionListener);
        stopMenuItem.addActionListener(actionListener);
        startMenuItem.addActionListener(actionListener);
        showMenuItem.addActionListener(actionListener);

        // Set the system tray icon with the menu
        ImageIcon i = new ImageIcon(getClass().getClassLoader().getResource("messenger-16x16.gif"));
        TrayIcon ti = new TrayIcon(i, "Jive Messenger", menu);
        ti.setIconAutoSize(true);
        ti.addActionListener(actionListener);
        tray.addTrayIcon(ti);

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                stopApplication();
                System.exit(0);
            }

            public void windowIconified(WindowEvent e) {
                // Make the window disappear when minimized
                frame.setVisible(false);
                showMenuItem.setText("Show");
            }
        });

Matt Tucker's avatar
Matt Tucker committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        frame.getContentPane().add(mainPanel);
        frame.pack();
        // frame.setSize(539,418);
        frame.setResizable(false);

        GraphicUtils.centerWindowOnScreen(frame);

        frame.setVisible(true);
    }

    /**
     * DOCUMENT ME!
     *
     * @param args DOCUMENT ME!
     */
    public static void main(String[] args) {
        new Launcher();
    }

    private synchronized void startApplication() {
        if (messengerd == null) {
244
            File binDir = null;
245
            File libDir = null;
246
            File homeDir = null;
Matt Tucker's avatar
Matt Tucker committed
247 248 249
            File exe = null;

            try {
250
                // Aliases keep their cwd rather than the aliased binDir's cwd on MacOS X
Matt Tucker's avatar
Matt Tucker committed
251 252
                // so we'll do a search for messengerd rather than relying on it being where
                // we think it will be...
253 254 255
                binDir = new File("").getAbsoluteFile();
                libDir = new File("../lib").getAbsoluteFile();
                homeDir = binDir.getParentFile();
Matt Tucker's avatar
Matt Tucker committed
256 257


258 259 260 261 262 263
                if (libDir.exists()) {
                    messengerd = Runtime.getRuntime().exec(new String[]{"java", "-jar", new File(libDir, "startup.jar").toString()});
                }
                else {
                    // MacOS X
                    exe = new File(homeDir, "messenger.app");
Matt Tucker's avatar
Matt Tucker committed
264 265

                    if (exe.exists()) {
266 267 268
                        messengerd = Runtime.getRuntime().exec(new String[]{
                            "open", exe.toString()
                        });
Matt Tucker's avatar
Matt Tucker committed
269 270
                    }
                    else {
271 272
                        // Unix
                        exe = new File(homeDir, "messenger");
Matt Tucker's avatar
Matt Tucker committed
273 274

                        if (exe.exists()) {
275
                            messengerd = Runtime.getRuntime().exec(new String[]{exe.toString()});
Matt Tucker's avatar
Matt Tucker committed
276 277
                        }
                        else {
278
                            throw new FileNotFoundException();
Matt Tucker's avatar
Matt Tucker committed
279 280 281 282 283 284 285
                        }
                    }
                }
            }
            catch (Exception e) {
                // Try one more time using the jar and hope java is on the path
                try {
286
                    if (binDir != null) {
Matt Tucker's avatar
Matt Tucker committed
287
                        messengerd = Runtime.getRuntime().exec(new String[]{
288
                            "java", "-jar", new File(libDir, "startup.jar").toString()
Matt Tucker's avatar
Matt Tucker committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
                        });
                    }
                    else {
                        e.printStackTrace();
                        JOptionPane.showMessageDialog(null,
                                "Launcher could not locate messengerd,\nthe Jive Messenger server daemon executable",
                                "File not found", JOptionPane.ERROR_MESSAGE);
                    }
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null,
                            "Launcher could not locate messengerd,\nthe Jive Messenger server daemon executable",
                            "File not found", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

    private synchronized void stopApplication() {
        if (messengerd != null) {
            try {
                messengerd.destroy();
                messengerd.waitFor();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        messengerd = null;
    }

    private synchronized void launchBrowser() {
        try {
            XMLProperties props = new XMLProperties(configFile);
            String port = props.getProperty("embedded-web.port");
326
            BrowserLauncher.openURL("http://127.0.0.1:" + port + "/index.html");
Matt Tucker's avatar
Matt Tucker committed
327 328
        }
        catch (Exception e) {
329
            JOptionPane.showMessageDialog(new JFrame(), configFile + " " + e.getMessage());
Matt Tucker's avatar
Matt Tucker committed
330 331
        }
    }
332 333
}