2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.launcher;
9 import java.awt.Container;
10 import java.awt.Image;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.KeyEvent;
13 import java.awt.event.WindowAdapter;
14 import java.awt.event.WindowEvent;
15 import java.io.BufferedOutputStream;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.PrintStream;
20 import java.util.ArrayList;
21 import java.util.List;
23 import javax.swing.AbstractAction;
24 import javax.swing.JComponent;
25 import javax.swing.JFrame;
26 import javax.swing.KeyStroke;
27 import javax.swing.WindowConstants;
29 import com.samskivert.swing.util.SwingUtil;
30 import com.threerings.getdown.data.Application;
31 import com.threerings.getdown.data.Build;
32 import com.threerings.getdown.data.EnvConfig;
33 import com.threerings.getdown.data.SysProps;
34 import com.threerings.getdown.util.LaunchUtil;
35 import com.threerings.getdown.util.StringUtil;
36 import static com.threerings.getdown.Log.log;
37 import jalview.bin.StartupNotificationListener;
40 * The main application entry point for Getdown.
42 public class GetdownApp
44 public static String startupFilesParameterString = "";
46 * The main entry point of the Getdown launcher application.
48 public static void main (String[] argv) {
51 } catch (Exception e) {
52 log.warning("main() failed.", e);
57 * Runs Getdown as an application, using the arguments supplie as {@code argv}.
58 * @return the {@code Getdown} instance that is running. {@link Getdown#start} will have been
60 * @throws Exception if anything goes wrong starting Getdown.
62 public static Getdown start (String[] argv) throws Exception {
63 List<EnvConfig.Note> notes = new ArrayList<>();
64 EnvConfig envc = EnvConfig.create(argv, notes);
66 if (!notes.isEmpty()) for (EnvConfig.Note n : notes) System.err.println(n.message);
67 else System.err.println("Usage: java -jar getdown.jar [app_dir] [app_id] [app args]");
71 // pipe our output into a file in the application directory
72 if (!SysProps.noLogRedir()) {
73 File logFile = new File(envc.appDir, "launcher.log");
75 PrintStream logOut = new PrintStream(
76 new BufferedOutputStream(new FileOutputStream(logFile)), true);
77 System.setOut(logOut);
78 System.setErr(logOut);
79 } catch (IOException ioe) {
80 log.warning("Unable to redirect output to '" + logFile + "': " + ioe);
84 // report any notes from reading our env config, and abort if necessary
85 boolean abort = false;
86 for (EnvConfig.Note note : notes) {
88 case INFO: log.info(note.message); break;
89 case WARN: log.warning(note.message); break;
90 case ERROR: log.error(note.message); abort = true; break;
93 if (abort) System.exit(-1);
95 log.info("Starting ...");
98 jalview.bin.StartupNotificationListener.setListener();
102 } catch (NoClassDefFoundError e)
104 log.warning("Starting without install4j classes");
105 } catch (Throwable t)
110 // record a few things for posterity
111 log.info("------------------ VM Info ------------------");
112 log.info("-- OS Name: " + System.getProperty("os.name"));
113 log.info("-- OS Arch: " + System.getProperty("os.arch"));
114 log.info("-- OS Vers: " + System.getProperty("os.version"));
115 log.info("-- Java Vers: " + System.getProperty("java.version"));
116 log.info("-- Java Home: " + System.getProperty("java.home"));
117 log.info("-- User Name: " + System.getProperty("user.name"));
118 log.info("-- User Home: " + System.getProperty("user.home"));
119 log.info("-- Cur dir: " + System.getProperty("user.dir"));
120 log.info("-- launcher version: "+Build.version());
121 log.info("-- startupFilesParameterString: " + startupFilesParameterString);
122 log.info("---------------------------------------------");
124 Getdown app = new Getdown(envc) {
126 protected Container createContainer () {
127 // create our user interface, and display it
128 if (_frame == null) {
129 _frame = new JFrame("");
130 _frame.addWindowListener(new WindowAdapter() {
132 public void windowClosing (WindowEvent evt) {
140 } catch (Exception e) {
141 log.warning("Error reading config for keep_on_top");
143 // move window to top, always on top
144 if (_ifc.keepOnTop) {
145 log.info("Keep on top set to ", "keep_on_top", _ifc.keepOnTop);
146 _frame.setAlwaysOnTop(true);
149 // handle close on ESC
150 String cancelId = "Cancel"; // $NON-NLS-1$
151 _frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
152 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelId);
153 _frame.getRootPane().getActionMap().put(cancelId, new AbstractAction() {
154 public void actionPerformed (ActionEvent e) {
158 // this cannot be called in configureContainer as it is only allowed before the
159 // frame has been displayed for the first time
160 _frame.setUndecorated(_ifc.hideDecorations);
161 _frame.setResizable(false);
163 _frame.getContentPane().removeAll();
165 _frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
166 return _frame.getContentPane();
170 protected void configureContainer () {
171 if (_frame == null) return;
173 _frame.setTitle(_ifc.name);
176 _frame.setBackground(new Color(_ifc.background, true));
177 } catch (Exception e) {
178 log.warning("Failed to set background", "bg", _ifc.background, e);
181 if (_ifc.iconImages != null && _ifc.iconImages.size() > 0) {
182 ArrayList<Image> icons = new ArrayList<>();
183 for (String path : _ifc.iconImages) {
184 Image img = loadImage(path);
186 log.warning("Error loading icon image", "path", path);
191 if (icons.isEmpty()) {
192 log.warning("Failed to load any icons", "iconImages", _ifc.iconImages);
194 _frame.setIconImages(icons);
200 protected void showContainer () {
201 if (_frame != null) {
203 SwingUtil.centerWindow(_frame);
204 _frame.setVisible(true);
206 _shownContainer = true;
210 protected void disposeContainer () {
211 if (_frame != null) {
218 protected void showDocument (String url) {
219 if (!StringUtil.couldBeValidUrl(url)) {
220 // command injection would be possible if we allowed e.g. spaces and double quotes
221 log.warning("Invalid document URL.", "url", url);
225 if (LaunchUtil.isWindows()) {
226 String osName = System.getProperty("os.name", "");
227 if (osName.indexOf("9") != -1 || osName.indexOf("Me") != -1) {
228 cmdarray = new String[] {
229 "command.com", "/c", "start", "\"" + url + "\"" };
231 cmdarray = new String[] {
232 "cmd.exe", "/c", "start", "\"\"", "\"" + url + "\"" };
234 } else if (LaunchUtil.isMacOS()) {
235 cmdarray = new String[] { "open", url };
236 } else { // Linux, Solaris, etc.
237 cmdarray = new String[] { "firefox", url };
240 Runtime.getRuntime().exec(cmdarray);
241 } catch (Exception e) {
242 log.warning("Failed to open browser.", "cmdarray", cmdarray, e);
247 protected void exit (int exitCode) {
248 // if we're running the app in the same JVM, don't call System.exit, but do
249 // make double sure that the download window is closed.
250 if (invokeDirect()) {
253 System.exit(exitCode);
258 protected void fail (String message) {
260 // super.fail causes the UI to be created (if needed) on the next UI tick, so we
261 // want to wait until that happens before we attempt to redecorate the window
262 EQinvoke(new Runnable() {
265 // if the frame was set to be undecorated, make window decoration available
266 // to allow the user to close the window
267 if (_frame != null && _frame.isUndecorated()) {
269 Color bg = _frame.getBackground();
270 if (bg != null && bg.getAlpha() < 255) {
271 // decorated windows do not allow alpha backgrounds
272 _frame.setBackground(
273 new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
275 _frame.setUndecorated(false);
282 protected JFrame _frame;
285 String startupFile = getStartupFilesParameterString();
286 if (!StringUtil.isBlank(startupFile)) {
287 Application.setStartupFilesFromParameterString(startupFile);
294 public static void setStartupFilesParameterString(String parameters) {
295 startupFilesParameterString = parameters;
298 public static String getStartupFilesParameterString() {
299 return startupFilesParameterString;