1 package org.objectweb.celtix.js.rhino; 2 3 import java.io.File ; 4 import java.io.FileFilter ; 5 6 import java.net.MalformedURLException ; 7 import java.net.URL ; 8 9 public class ServerApp { 10 public static final String NO_ADDR_ERR = "error: an endpoint address must be provided"; 11 public static final String NO_FILES_ERR = "error: no JavaScript files specified"; 12 public static final String WRONG_ADDR_ERR = "error: -a requires a URL argument"; 13 public static final String WRONG_BASE_ERR = "error: -b requires a base URL argument"; 14 public static final String ILLEGAL_OPTIONS_ERR = "error: -a and -b cannot be used together"; 15 public static final String UNKNOWN_OPTION = "error: unknown option"; 16 17 private boolean verbose; 18 private boolean bOptSeen; 19 private String epAddr; 20 21 protected void start(String [] args) throws Exception { 22 ProviderFactory ph = createProviderFactory(); 23 FileFilter jsFilter = new JSFilter(); 24 int i = 0; 25 boolean fileSeen = false; 26 boolean msgPrinted = false; 27 for (;;) { 28 if (i == args.length) { 29 break; 30 } 31 if (args[i].startsWith("-")) { 32 i = checkOption(args, i); 33 if (verbose && !msgPrinted) { 34 msgPrinted = true; 35 System.out.println("entering server"); 36 } 37 } else { 38 File f = new File (args[i]); 39 if (f.isFile() && jsFilter.accept(f)) { 40 fileSeen = true; 41 if (verbose) { 42 System.out.println("processing file " + f.getCanonicalPath()); 43 } 44 ph.createAndPublish(f, epAddr, bOptSeen); 45 } else if (f.isDirectory()) { 46 File [] flist = f.listFiles(jsFilter); 47 for (File file : flist) { 48 fileSeen = true; 49 if (verbose) { 50 System.out.println("processing file " + file.getCanonicalPath()); 51 } 52 ph.createAndPublish(file, epAddr, bOptSeen); 53 } 54 } 55 } 56 i++; 57 } 58 if (!fileSeen) { 59 throw new Exception (NO_FILES_ERR); 60 } 61 } 62 63 public static void main(String [] args) throws Exception { 64 ServerApp app = null; 65 try { 66 app = new ServerApp(); 67 app.start(args); 68 } catch (Exception e) { 69 System.err.println("error: " + e.getMessage()); 70 System.exit(1); 71 } 72 if (app.verbose) { 73 System.out.println("server ready..."); 74 } 75 Thread.sleep(5 * 60 * 1000); 76 if (app.verbose) { 77 System.out.println("server timed out, exiting"); 78 } 79 System.exit(0); 80 } 81 82 protected ProviderFactory createProviderFactory() { 83 return new ProviderFactory(); 84 } 85 86 private int checkOption(String [] args, int index) throws Exception { 87 if ("-v".equals(args[index])) { 88 verbose = true; 89 } else if ("-a".equals(args[index])) { 90 bOptSeen = false; 91 if (++index == args.length) { 92 throw new Exception (WRONG_ADDR_ERR); 93 } 94 try { 95 new URL (args[index]); 96 } catch (MalformedURLException m) { 97 throw new Exception (WRONG_ADDR_ERR, m); 98 } 99 epAddr = args[index]; 100 } else if ("-b".equals(args[index])) { 101 bOptSeen = true; 102 if (++index == args.length) { 103 throw new Exception (WRONG_BASE_ERR); 104 } 105 try { 106 new URL (args[index]); 107 } catch (MalformedURLException m) { 108 throw new Exception (WRONG_BASE_ERR, m); 109 } 110 epAddr = args[index]; 111 } else { 112 throw new Exception (UNKNOWN_OPTION + ": " + args[index]); 113 } 114 return index; 115 } 116 117 private static class JSFilter implements FileFilter { 118 public final boolean accept(File f) { 119 if (f.isFile()) { 120 String name = f.getName(); 121 return name.endsWith(".js") || name.endsWith(".jsx"); 122 } else { 123 return false; 124 } 125 } 126 } 127 } 128 | Popular Tags |