1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
8 import org.bouncycastle.util.encoders.Base64;
10 import org.xwt.util.*;
12 /** Entry point for the XWT Engine; handles splash screen, initial xwar loading, and argument processing */
16 * Used for security checks. If this is null, it means that only
17 * scripts originating from the local filesystem are loaded in
18 * the engine (maximum permissions). If scripts have been loaded
19 * from multiple locations, this will be 0.0.0.0 (the invalid
22 public static java.net.InetAddress originAddr = null;
23 public static String originHost = null;
25 /** the URL where the initial xwar came from. */
26 public static String origin = null;
28 /** true iff the user asked for rendered regions to be shown with a red rectangle */
29 public static boolean showRenders = false;
31 /** don't check if all surfaces are gone (and quit) until this is true */
32 public static boolean doneInitializing = false;
34 /** common entry point */
35 public static void main(String[] args) {
39 if (startargs > args.length - 1) {
40 System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
41 System.err.println("");
42 System.err.println("Options:");
43 System.err.println(" -s show rendering activity with red rectangles");
44 System.err.println(" -v verbose logging");
45 System.err.println(" -l serve logs via HTTP on 127.0.0.1:<port>/<url>");
46 System.err.println("");
47 System.err.println("Source-location is one of the following:");
48 System.err.println(" - the path to an xwar file");
49 System.err.println(" - the path to a directory to be used as an xwar");
50 System.err.println(" - the http url of an xwar");
51 System.err.println("");
52 System.err.println("Initial-template is the resource name of the template to load; defaults to 'main'");
53 Runtime.getRuntime().exit(-1);
55 else if (args[startargs].equals("-s")) showRenders = true;
56 else if (args[startargs].equals("-v")) Log.verbose = true;
57 else if (args[startargs].equals("-l")) startargs++;
61 final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
64 if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
66 InputStream is = Platform.getBuiltinInputStream();
67 if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
68 Resources.loadArchive(is);
70 if (Log.on) Log.log(Main.class, "loading scar image");
71 PNG png = PNG.decode(new ByteArrayInputStream(Resources.getResource("org.xwt.builtin.scar.png")), "bundled scar image");
72 Surface.scarPicture = Platform.createPicture(png.getData(), png.getWidth(), png.getHeight());
74 String initialTemplate = "main";
76 if (args.length > startargs) {
77 if (args[startargs].startsWith("http://")) {
78 if (Log.on) Log.log(Main.class, "downloading xwar");
79 origin = args[startargs];
80 initialTemplate = "org.xwt.builtin.splash";
83 if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
85 // HACK because MSIE turns \'s into /'s in URLs... argh!!
86 if (Platform.platform.getClass().getName().endsWith("Win32"))
87 args[startargs] = args[startargs].replace('/', '\\');
89 File f = new File(args[startargs]);
90 origin = "file:" + f.getAbsolutePath();
91 if (f.isDirectory()) {
92 Resources.loadDirectory(f);
93 Surface.scarAllSurfacesFromNowOn = true;
95 initialTemplate = "org.xwt.builtin.splash";
98 if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
102 if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
103 final String initialTemplate_f = initialTemplate;
104 ThreadMessage.newthread(new JS.Callable() {
105 public Object call(JS.Array args) throws JS.Exn {
106 new Box(initialTemplate_f, null);
107 doneInitializing = true;
108 if (Surface.allSurfaces.size() == 0) {
109 Log.log(this, "exiting because all surfaces are gone");
116 // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
117 //if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
118 //new org.xwt.util.Semaphore().block();
121 } catch (Throwable e) {
123 Platform.criticalAbort("exception in Main.main(): " + e);