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;
24 /** the URL where the initial xwar came from. */
25 public static String origin = null;
27 /** true iff the user asked for rendered regions to be shown with a red rectangle */
28 public static boolean showRenders = false;
30 /** don't check if all surfaces are gone (and quit) until this is true */
31 public static boolean doneInitializing = false;
33 /** common entry point */
34 public static void main(String[] args) {
38 if (startargs > args.length - 1) {
39 System.err.println("Usage: xwt [-s] [-v] [-l <port>/<url>] source-location [initial-template]");
40 System.err.println("");
41 System.err.println("Options:");
42 System.err.println(" -s show rendering activity with red rectangles");
43 System.err.println(" -v verbose logging");
44 System.err.println(" -l serve logs via HTTP on 127.0.0.1:<port>/<url>");
45 System.err.println("");
46 System.err.println("Source-location is one of the following:");
47 System.err.println(" - the path to an xwar file");
48 System.err.println(" - the path to a directory to be used as an xwar");
49 System.err.println(" - the http url of an xwar");
50 System.err.println("");
51 System.err.println("Initial-template is the resource name of the template to load; defaults to 'main'");
52 Runtime.getRuntime().exit(-1);
54 else if (args[startargs].equals("-s")) showRenders = true;
55 else if (args[startargs].equals("-v")) Log.verbose = true;
56 else if (args[startargs].equals("-l")) startargs++;
60 final String instancename = args.length > startargs + 1 ? args[startargs + 1] : "main";
63 if (Log.on) for(int i=0; i<args.length; i++) Log.log(Main.class, "argument " + i + ": " + args[i]);
65 InputStream is = Platform.getBuiltinInputStream();
66 if (is == null) Platform.criticalAbort("unable to load builtin.xwar");
67 Resources.loadArchive(is);
69 if (Log.on) Log.log(Main.class, "loading scar image");
70 PNG png = PNG.decode(new ByteArrayInputStream(Resources.getResource("org.xwt.builtin.scar.png")), "bundled scar image");
71 Surface.scarPicture = Platform.createPicture(png.getData(), png.getWidth(), png.getHeight());
73 String initialTemplate = "main";
75 if (args.length > startargs) {
76 if (args[startargs].startsWith("http://")) {
77 if (Log.on) Log.log(Main.class, "downloading xwar");
78 origin = args[startargs];
79 initialTemplate = "org.xwt.builtin.splash";
82 if (Log.on) Log.log(Main.class, "loading xwar from local filesystem");
84 // HACK because MSIE turns \'s into /'s in URLs... argh!!
85 if (Platform.platform.getClass().getName().endsWith("Win32"))
86 args[startargs] = args[startargs].replace('/', '\\');
88 File f = new File(args[startargs]);
89 origin = "file:" + f.getAbsolutePath();
90 if (f.isDirectory()) {
91 Resources.loadDirectory(f);
92 Surface.scarAllSurfacesFromNowOn = true;
94 initialTemplate = "org.xwt.builtin.splash";
97 if (args.length > startargs + 1) initialTemplate = args[startargs + 1];
101 if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate);
102 final String initialTemplate_f = initialTemplate;
103 ThreadMessage.newthread(new JS.Callable() {
104 public Object call(JS.Array args) throws JS.Exn {
105 new Box(initialTemplate_f, null);
106 doneInitializing = true;
107 if (Surface.allSurfaces.size() == 0) {
108 Log.log(this, "exiting because all surfaces are gone");
115 // gcj-win32 exit()'s when the original thread dies, so we have to deadlock ourselves
116 //if (Log.on) Log.log(Main.class, "main thread blocking on new semaphore");
117 //new org.xwt.util.Semaphore().block();
120 } catch (Throwable e) {
122 Platform.criticalAbort("exception in Main.main(): " + e);