001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: import com.caucho.Version;
032: import com.caucho.es.parser.Parser;
033: import com.caucho.server.util.CauchoSystem;
034: import com.caucho.vfs.MergePath;
035: import com.caucho.vfs.Path;
036: import com.caucho.vfs.ReadStream;
037: import com.caucho.vfs.Vfs;
038: import com.caucho.vfs.VfsStream;
039: import com.caucho.vfs.WriteStream;
040:
041: import java.io.IOException;
042: import java.util.HashMap;
043:
044: /**
045: * Resin is the primary public entry to the JavaScript compiler.
046: *
047: * <p>The script must first be compiled, then executed.
048: *
049: * <p>JavaScript import uses a script path to located the imported
050: * script. The scriptPath is just an array of Paths. If the scriptPath
051: * is null, Resin will default to the script's directory, followed
052: * by $RESIN_HOME/scripts.
053: * <p>Here's the default, where 'is' is the input stream:
054: *
055: * <pre><code>
056: * Path scriptPath[] = new Path[] {
057: * is.getPath().getParent(),
058: * CauchoSystem.getResinHome().lookup("scripts")
059: * }
060: * </code></pre>
061: *
062: * <p>As described in the Script object, programs set global variables
063: * with a hash map. So a simple call might look like:
064: *
065: * <pre><code>
066: * Script script = Resin.parse(Pwd.lookup("foo.js"), scriptPath);
067: *
068: * HashMap props = new HashMap();
069: * props.put("out", System.out);
070: *
071: * script.execute(props, null);
072: * </code></pre>
073: *
074: * <p>Executing the Script object is threadsafe.
075: * The ScriptClosure object, of course, is not threadsafe.
076: */
077: public class Resin {
078: static final String COPYRIGHT = "Copyright (c) 1998-2006 Caucho Technology. All rights reserved.";
079:
080: private static WriteStream dbg;
081:
082: private Resin() {
083: }
084:
085: public static void init(ESFactory factory) {
086: ESBase.init(factory);
087: }
088:
089: public static Parser getParser() throws IOException, ESException {
090: init(null);
091:
092: return new Parser();
093: }
094:
095: public static void main(String[] argv) {
096: String resinConf = CauchoSystem.getResinConfig();
097: boolean verbose = false;
098:
099: try {
100: ReadStream is;
101: String name;
102: int shift = 0;
103:
104: while (argv.length > shift) {
105: if (argv[shift].equals("-v")) {
106: verbose = true;
107: shift++;
108: } else if (shift + 1 < argv.length
109: && argv[shift].equals("-conf")) {
110: resinConf = argv[shift + 1];
111: shift += 2;
112: } else if (argv[shift].equals("--version")) {
113: System.out.println(Version.VERSION);
114: System.exit(0);
115: } else
116: break;
117: }
118:
119: if (argv.length == shift) {
120: is = VfsStream.openRead(System.in);
121: name = "stdin";
122: } else {
123: is = Vfs.lookupNative(argv[shift]).openRead();
124: name = argv[shift++];
125: }
126:
127: Path conf = Vfs.lookup(resinConf);
128: // Registry.setDefault(Registry.parse(conf));
129:
130: String[] args;
131: if (argv.length > shift)
132: args = new String[argv.length - shift];
133: else
134: args = new String[0];
135: for (int i = 0; i < argv.length - shift; i++)
136: args[i] = argv[i + shift];
137:
138: Path scriptPath = null;
139:
140: int p;
141: if ((p = name.lastIndexOf('/')) >= 0) {
142: Path subpath = Vfs.lookupNative(name.substring(0, p));
143:
144: MergePath mergePath = new MergePath();
145: mergePath.addMergePath(Vfs.lookup());
146: mergePath.addMergePath(subpath);
147: mergePath.addMergePath(CauchoSystem.getResinHome()
148: .lookup("scripts"));
149: mergePath.addClassPath(Thread.currentThread()
150: .getContextClassLoader());
151: scriptPath = mergePath;
152: }
153:
154: Parser parser = new Parser();
155: parser.setScriptPath(scriptPath);
156:
157: Script script = parser.parse(is);
158:
159: WriteStream stream = VfsStream.openWrite(System.out);
160: HashMap properties = new HashMap();
161: properties.put("out", stream);
162: properties.put("arguments", args);
163: properties.put("File", Vfs.lookup());
164:
165: script.execute(properties, null);
166:
167: stream.flush();
168: } catch (ESParseException e) {
169: System.err.println(e.getMessage());
170: } catch (ESException e) {
171: System.err.println(e.getMessage());
172: if (verbose)
173: e.printStackTrace();
174: else
175: e.printESStackTrace();
176: } catch (Throwable e) {
177: System.out.println("Exception: " + e);
178: e.printStackTrace();
179: }
180: }
181: }
|