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.EventQueue;
11 import java.awt.Image;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.KeyEvent;
14 import java.awt.event.WindowAdapter;
15 import java.awt.event.WindowEvent;
16 import java.io.BufferedOutputStream;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.io.PrintStream;
21 import java.util.ArrayList;
22 import java.util.List;
24 import javax.swing.AbstractAction;
25 import javax.swing.JComponent;
26 import javax.swing.JFrame;
27 import javax.swing.KeyStroke;
28 import javax.swing.WindowConstants;
30 import com.samskivert.swing.util.SwingUtil;
31 import com.threerings.getdown.data.Application;
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);
97 jalview.bin.StartupNotificationListener.setListener();
101 } catch (NoClassDefFoundError e)
103 log.warning("Starting without install4j classes");
104 } catch (Throwable t)
109 // record a few things for posterity
110 log.info("------------------ VM Info ------------------");
111 log.info("-- OS Name: " + System.getProperty("os.name"));
112 log.info("-- OS Arch: " + System.getProperty("os.arch"));
113 log.info("-- OS Vers: " + System.getProperty("os.version"));
114 log.info("-- Java Vers: " + System.getProperty("java.version"));
115 log.info("-- Java Home: " + System.getProperty("java.home"));
116 log.info("-- User Name: " + System.getProperty("user.name"));
117 log.info("-- User Home: " + System.getProperty("user.home"));
118 log.info("-- Cur dir: " + System.getProperty("user.dir"));
119 log.info("-- startupFilesParameterString: " + startupFilesParameterString);
120 log.info("---------------------------------------------");
122 Getdown app = new Getdown(envc) {
124 protected Container createContainer () {
125 // create our user interface, and display it
126 if (_frame == null) {
127 _frame = new JFrame("");
128 _frame.addWindowListener(new WindowAdapter() {
130 public void windowClosing (WindowEvent evt) {
134 // handle close on ESC
135 String cancelId = "Cancel"; // $NON-NLS-1$
136 _frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
137 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelId);
138 _frame.getRootPane().getActionMap().put(cancelId, new AbstractAction() {
139 public void actionPerformed (ActionEvent e) {
143 // this cannot be called in configureContainer as it is only allowed before the
144 // frame has been displayed for the first time
145 _frame.setUndecorated(_ifc.hideDecorations);
146 _frame.setResizable(false);
148 _frame.getContentPane().removeAll();
150 _frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
151 return _frame.getContentPane();
155 protected void configureContainer () {
156 if (_frame == null) return;
158 _frame.setTitle(_ifc.name);
161 _frame.setBackground(new Color(_ifc.background, true));
162 } catch (Exception e) {
163 log.warning("Failed to set background", "bg", _ifc.background, e);
166 if (_ifc.iconImages != null) {
167 ArrayList<Image> icons = new ArrayList<>();
168 for (String path : _ifc.iconImages) {
169 Image img = loadImage(path);
171 log.warning("Error loading icon image", "path", path);
176 if (icons.isEmpty()) {
177 log.warning("Failed to load any icons", "iconImages", _ifc.iconImages);
179 _frame.setIconImages(icons);
185 protected void showContainer () {
186 if (_frame != null) {
188 SwingUtil.centerWindow(_frame);
189 _frame.setVisible(true);
194 protected void disposeContainer () {
195 if (_frame != null) {
202 protected void showDocument (String url) {
203 if (!StringUtil.couldBeValidUrl(url)) {
204 // command injection would be possible if we allowed e.g. spaces and double quotes
205 log.warning("Invalid document URL.", "url", url);
209 if (LaunchUtil.isWindows()) {
210 String osName = System.getProperty("os.name", "");
211 if (osName.indexOf("9") != -1 || osName.indexOf("Me") != -1) {
212 cmdarray = new String[] {
213 "command.com", "/c", "start", "\"" + url + "\"" };
215 cmdarray = new String[] {
216 "cmd.exe", "/c", "start", "\"\"", "\"" + url + "\"" };
218 } else if (LaunchUtil.isMacOS()) {
219 cmdarray = new String[] { "open", url };
220 } else { // Linux, Solaris, etc.
221 cmdarray = new String[] { "firefox", url };
224 Runtime.getRuntime().exec(cmdarray);
225 } catch (Exception e) {
226 log.warning("Failed to open browser.", "cmdarray", cmdarray, e);
231 protected void exit (int exitCode) {
232 // if we're running the app in the same JVM, don't call System.exit, but do
233 // make double sure that the download window is closed.
234 if (invokeDirect()) {
237 System.exit(exitCode);
242 protected void fail (String message) {
244 // super.fail causes the UI to be created (if needed) on the next UI tick, so we
245 // want to wait until that happens before we attempt to redecorate the window
246 EventQueue.invokeLater(new Runnable() {
249 // if the frame was set to be undecorated, make window decoration available
250 // to allow the user to close the window
251 if (_frame != null && _frame.isUndecorated()) {
253 Color bg = _frame.getBackground();
254 if (bg != null && bg.getAlpha() < 255) {
255 // decorated windows do not allow alpha backgrounds
256 _frame.setBackground(
257 new Color(bg.getRed(), bg.getGreen(), bg.getBlue()));
259 _frame.setUndecorated(false);
266 protected JFrame _frame;
269 String startupFile = getStartupFilesParameterString();
270 if (!StringUtil.isBlank(startupFile)) {
271 Application.setStartupFilesFromParameterString(startupFile);
278 public static void setStartupFilesParameterString(String parameters) {
279 startupFilesParameterString = parameters;
282 public static String getStartupFilesParameterString() {
283 return startupFilesParameterString;