KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > Resin


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 import com.caucho.Version;
32 import com.caucho.es.parser.Parser;
33 import com.caucho.server.util.CauchoSystem;
34 import com.caucho.vfs.MergePath;
35 import com.caucho.vfs.Path;
36 import com.caucho.vfs.ReadStream;
37 import com.caucho.vfs.Vfs;
38 import com.caucho.vfs.VfsStream;
39 import com.caucho.vfs.WriteStream;
40
41 import java.io.IOException JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * Resin is the primary public entry to the JavaScript compiler.
46  *
47  * <p>The script must first be compiled, then executed.
48  *
49  * <p>JavaScript import uses a script path to located the imported
50  * script. The scriptPath is just an array of Paths. If the scriptPath
51  * is null, Resin will default to the script's directory, followed
52  * by $RESIN_HOME/scripts.
53  * <p>Here's the default, where 'is' is the input stream:
54  *
55  * <pre><code>
56  * Path scriptPath[] = new Path[] {
57  * is.getPath().getParent(),
58  * CauchoSystem.getResinHome().lookup("scripts")
59  * }
60  * </code></pre>
61  *
62  * <p>As described in the Script object, programs set global variables
63  * with a hash map. So a simple call might look like:
64  *
65  * <pre><code>
66  * Script script = Resin.parse(Pwd.lookup("foo.js"), scriptPath);
67  *
68  * HashMap props = new HashMap();
69  * props.put("out", System.out);
70  *
71  * script.execute(props, null);
72  * </code></pre>
73  *
74  * <p>Executing the Script object is threadsafe.
75  * The ScriptClosure object, of course, is not threadsafe.
76  */

77 public class Resin {
78   static final String JavaDoc COPYRIGHT =
79     "Copyright (c) 1998-2006 Caucho Technology. All rights reserved.";
80       
81   private static WriteStream dbg;
82
83   private Resin()
84   {
85   }
86
87   public static void init(ESFactory factory)
88   {
89     ESBase.init(factory);
90   }
91   
92   public static Parser getParser()
93     throws IOException JavaDoc, ESException
94   {
95     init(null);
96
97     return new Parser();
98   }
99
100   public static void main(String JavaDoc []argv)
101   {
102     String JavaDoc resinConf = CauchoSystem.getResinConfig();
103     boolean verbose = false;
104
105     try {
106       ReadStream is;
107       String JavaDoc name;
108       int shift = 0;
109
110       while (argv.length > shift) {
111     if (argv[shift].equals("-v")) {
112       verbose = true;
113       shift++;
114     } else if (shift + 1 < argv.length && argv[shift].equals("-conf")) {
115       resinConf = argv[shift + 1];
116       shift += 2;
117     } else if (argv[shift].equals("--version")) {
118       System.out.println(Version.VERSION);
119       System.exit(0);
120     } else
121       break;
122       }
123
124       if (argv.length == shift) {
125     is = VfsStream.openRead(System.in);
126     name = "stdin";
127       }
128       else {
129     is = Vfs.lookupNative(argv[shift]).openRead();
130     name = argv[shift++];
131       }
132
133       Path conf = Vfs.lookup(resinConf);
134       // Registry.setDefault(Registry.parse(conf));
135

136       String JavaDoc []args;
137       if (argv.length > shift)
138     args = new String JavaDoc[argv.length - shift];
139       else
140     args = new String JavaDoc[0];
141       for (int i = 0; i < argv.length - shift; i++)
142     args[i] = argv[i + shift];
143
144       Path scriptPath = null;
145
146       int p;
147       if ((p = name.lastIndexOf('/')) >= 0) {
148     Path subpath = Vfs.lookupNative(name.substring(0, p));
149
150         MergePath mergePath = new MergePath();
151         mergePath.addMergePath(Vfs.lookup());
152         mergePath.addMergePath(subpath);
153         mergePath.addMergePath(CauchoSystem.getResinHome().lookup("scripts"));
154         mergePath.addClassPath(Thread.currentThread().getContextClassLoader());
155         scriptPath = mergePath;
156       }
157
158       Parser parser = new Parser();
159       parser.setScriptPath(scriptPath);
160       
161       Script script = parser.parse(is);
162
163       WriteStream stream = VfsStream.openWrite(System.out);
164       HashMap JavaDoc properties = new HashMap JavaDoc();
165       properties.put("out", stream);
166       properties.put("arguments", args);
167       properties.put("File", Vfs.lookup());
168
169       script.execute(properties, null);
170
171       stream.flush();
172     } catch (ESParseException e) {
173       System.err.println(e.getMessage());
174     } catch (ESException e) {
175       System.err.println(e.getMessage());
176       if (verbose)
177     e.printStackTrace();
178       else
179     e.printESStackTrace();
180     } catch (Throwable JavaDoc e) {
181       System.out.println("Exception: " + e);
182       e.printStackTrace();
183     }
184   }
185 }
186
Popular Tags