1 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 ; 42 import java.util.HashMap ; 43 44 77 public class Resin { 78 static final String 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 , ESException 94 { 95 init(null); 96 97 return new Parser(); 98 } 99 100 public static void main(String []argv) 101 { 102 String resinConf = CauchoSystem.getResinConfig(); 103 boolean verbose = false; 104 105 try { 106 ReadStream is; 107 String 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 136 String []args; 137 if (argv.length > shift) 138 args = new String [argv.length - shift]; 139 else 140 args = new String [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 properties = new HashMap (); 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 e) { 181 System.out.println("Exception: " + e); 182 e.printStackTrace(); 183 } 184 } 185 } 186 | Popular Tags |