39e23d2606d51dee1b01c762d6d3d6c9ccb2ed16
[org.ibex.core.git] / src / org / ibex / core / Main.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.core;
6
7 import java.net.*;
8 import java.io.*;
9 import java.util.*;
10 import org.ibex.js.*;
11 import org.ibex.util.*;
12 import org.ibex.plat.*;
13 import org.ibex.graphics.*;
14
15 /** Entry point for the Ibex Engine; handles splash screen, initial xwar loading, and argument processing */
16 public class Main {
17
18     // ugly hack: we have to set these properties before AWT loads
19     static {
20         System.setProperty("apple.awt.showGrowBox", "false");
21         System.setProperty("apple.awt.graphics.EnableLazyDrawing", "40");
22         System.setProperty("apple.awt.graphics.EnableLazyDrawing", "true");
23         System.setProperty("apple.awt.window.position.forceSafeUserPositioning", "true");
24         System.setProperty("apple.awt.window.position.forceSafeCreation", "true");
25         System.setProperty("com.apple.hwaccel", "true");
26         System.setProperty("com.apple.forcehwaccel", "true");
27     }
28
29     /**
30      *  FEATURE: this should be implemented using self-emulation
31      *  Used for security checks. If this is null, it means that only
32      *  scripts originating from the local filesystem are loaded in
33      *  the engine (maximum permissions). If scripts have been loaded
34      *  from multiple locations, this will be 0.0.0.0 (the invalid
35      *  IP).
36      */
37     public static java.net.InetAddress originAddr = null;
38     public static String originHost = null;
39     public static String origin = null;
40     public static String initialTemplate = null;
41     
42     //public static final Fountain builtin = new Fountain.Zip(new Fountain.FromInputStream(Platform.getBuiltinInputStream()));
43     public static final Fountain vera;
44     static {
45         Fountain temp = null;
46         try {
47             temp = new Fountain.FromInputStream(Encode.JavaSourceCode.decode(Vera.data));
48         } catch (Exception e) { Log.error(Main.class, e); }
49         vera = temp;
50     }
51
52     public static void printUsage() {
53         System.err.println("Usage: ibex [-lawp] [ url | file | directory ]");
54         System.err.println("");
55         System.err.println("    -l <level>      set logging level to { debug, info (default), warn, error, silent }");
56         System.err.println("    -l rpc          log all XML-RPC and SOAP conversations");
57         System.err.println("    -l user@host    email log to user@host");
58         System.err.println("    -l host:port    emit log to TCP socket");
59         System.err.println("    -l <file>       write log to a file on disk");
60         //System.err.println("    -a              check assertions");
61         System.err.println("    -w <window-id>  reserved for libibex");
62         System.err.println("    -p              dump profiling information [not yet supported]");
63         Runtime.getRuntime().exit(-1);
64     }
65
66     public static void main(String[] args) throws UnknownHostException, JSExn, IOException {
67         int startargs = 0;
68         while (true) {
69             if (startargs > args.length - 1) printUsage();
70             // FEATURE: This should be enabled at the parser level - there shouldn't even be an assert bytecode
71             /*else if (args[startargs].equals("-a")) JS.checkAssertions = true;*/
72             else if (args[startargs].equals("-l")) {
73                 startargs++;
74                 StringTokenizer opts = new StringTokenizer(args[startargs], ",");
75                 while(opts.hasMoreTokens()) {
76                     String opt = opts.nextToken();
77                     if (opt.indexOf('@') != -1) Log.email(opt);
78                     else if (opt.indexOf(':') != -1)
79                         Log.tcp(opt.substring(0, opt.indexOf(':')),
80                                 Integer.parseInt(opt.substring(opt.indexOf(':') + 1)));
81                     else if (opt.equals("debug")) Log.level = Log.DEBUG;
82                     else if (opt.equals("info")) Log.level = Log.INFO;
83                     else if (opt.equals("warn")) Log.level = Log.WARN;
84                     else if (opt.equals("error")) Log.level = Log.ERROR;
85                     else if (opt.equals("silent")) Log.level = Log.SILENT;
86                     else if (opt.equals("rpc")) Log.rpc = true;
87                     else Log.file(opt);
88                 }
89             }
90             else break;
91             startargs++;
92         }
93
94         org.ibex.plat.Platform.forceLoad();
95         if (Log.on) for(int i=0; i<args.length; i++) Log.info(Main.class, "argument " + i + ": " + args[i]);
96
97         initialTemplate = args.length > startargs + 1 ? args[startargs + 1] : "main";
98         origin = args[startargs];
99
100         Fountain rr;
101         final String startupTemplate;
102         if (origin.startsWith("http://") || origin.startsWith("https://")) {
103             originHost = origin.substring(origin.indexOf('/') + 2);
104             originHost = originHost.substring(0, originHost.indexOf('/') == -1 ? originHost.length() : originHost.indexOf('/'));
105             if (originHost.indexOf('@') != -1) originHost = originHost.substring(originHost.indexOf('@') + 1);
106             originAddr = InetAddress.getByName(originHost);
107             //rr = builtin;
108             //startupTemplate = "org.ibex.builtin.splash";
109             rr = new Fountain.HTTP(origin);
110             startupTemplate = initialTemplate;
111         } else {
112             rr = new Fountain.File(origin);
113             if (!new File(origin).isDirectory()) rr = new Fountain.Zip(rr);
114             startupTemplate = initialTemplate;
115         }
116
117         if (Log.on) Log.info(Main.class, "loading xwar");
118         final Ibex ibex = new Ibex(rr);
119
120         try {
121             JS blessed = ibex.bless(new Fountain.FromInputStream(Encode.JavaSourceCode.decode(org.ibex.core.builtin.Scar.data)));
122             org.ibex.graphics.Surface.scarImage = Platform.createPicture(blessed);
123         } catch (Exception e) {
124             Log.error(Main.class, e);
125         }
126
127         Platform.Scheduler.add(new Callable() {
128             private final JS[] callargs = new JS[1];
129             public Object run(Object o) throws JSExn,UnknownHostException {
130                 if (Log.on) Log.info(Main.class, "invoking initial template");
131                 try {
132                     callargs[0] = new Box(); 
133                     ibex.resolveString(startupTemplate, false).call(null, callargs);
134                 } finally { callargs[0] = null; }
135                 return null;
136             } });
137     
138         Platform.Scheduler.init();
139     }
140 }