001: package org.objectweb.celtix.js.rhino;
002:
003: import java.io.File;
004: import java.io.FileFilter;
005:
006: import java.net.MalformedURLException;
007: import java.net.URL;
008:
009: public class ServerApp {
010: public static final String NO_ADDR_ERR = "error: an endpoint address must be provided";
011: public static final String NO_FILES_ERR = "error: no JavaScript files specified";
012: public static final String WRONG_ADDR_ERR = "error: -a requires a URL argument";
013: public static final String WRONG_BASE_ERR = "error: -b requires a base URL argument";
014: public static final String ILLEGAL_OPTIONS_ERR = "error: -a and -b cannot be used together";
015: public static final String UNKNOWN_OPTION = "error: unknown option";
016:
017: private boolean verbose;
018: private boolean bOptSeen;
019: private String epAddr;
020:
021: protected void start(String[] args) throws Exception {
022: ProviderFactory ph = createProviderFactory();
023: FileFilter jsFilter = new JSFilter();
024: int i = 0;
025: boolean fileSeen = false;
026: boolean msgPrinted = false;
027: for (;;) {
028: if (i == args.length) {
029: break;
030: }
031: if (args[i].startsWith("-")) {
032: i = checkOption(args, i);
033: if (verbose && !msgPrinted) {
034: msgPrinted = true;
035: System.out.println("entering server");
036: }
037: } else {
038: File f = new File(args[i]);
039: if (f.isFile() && jsFilter.accept(f)) {
040: fileSeen = true;
041: if (verbose) {
042: System.out.println("processing file "
043: + f.getCanonicalPath());
044: }
045: ph.createAndPublish(f, epAddr, bOptSeen);
046: } else if (f.isDirectory()) {
047: File[] flist = f.listFiles(jsFilter);
048: for (File file : flist) {
049: fileSeen = true;
050: if (verbose) {
051: System.out.println("processing file "
052: + file.getCanonicalPath());
053: }
054: ph.createAndPublish(file, epAddr, bOptSeen);
055: }
056: }
057: }
058: i++;
059: }
060: if (!fileSeen) {
061: throw new Exception(NO_FILES_ERR);
062: }
063: }
064:
065: public static void main(String[] args) throws Exception {
066: ServerApp app = null;
067: try {
068: app = new ServerApp();
069: app.start(args);
070: } catch (Exception e) {
071: System.err.println("error: " + e.getMessage());
072: System.exit(1);
073: }
074: if (app.verbose) {
075: System.out.println("server ready...");
076: }
077: Thread.sleep(5 * 60 * 1000);
078: if (app.verbose) {
079: System.out.println("server timed out, exiting");
080: }
081: System.exit(0);
082: }
083:
084: protected ProviderFactory createProviderFactory() {
085: return new ProviderFactory();
086: }
087:
088: private int checkOption(String[] args, int index) throws Exception {
089: if ("-v".equals(args[index])) {
090: verbose = true;
091: } else if ("-a".equals(args[index])) {
092: bOptSeen = false;
093: if (++index == args.length) {
094: throw new Exception(WRONG_ADDR_ERR);
095: }
096: try {
097: new URL(args[index]);
098: } catch (MalformedURLException m) {
099: throw new Exception(WRONG_ADDR_ERR, m);
100: }
101: epAddr = args[index];
102: } else if ("-b".equals(args[index])) {
103: bOptSeen = true;
104: if (++index == args.length) {
105: throw new Exception(WRONG_BASE_ERR);
106: }
107: try {
108: new URL(args[index]);
109: } catch (MalformedURLException m) {
110: throw new Exception(WRONG_BASE_ERR, m);
111: }
112: epAddr = args[index];
113: } else {
114: throw new Exception(UNKNOWN_OPTION + ": " + args[index]);
115: }
116: return index;
117: }
118:
119: private static class JSFilter implements FileFilter {
120: public final boolean accept(File f) {
121: if (f.isFile()) {
122: String name = f.getName();
123: return name.endsWith(".js") || name.endsWith(".jsx");
124: } else {
125: return false;
126: }
127: }
128: }
129: }
|