KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > js > rhino > ServerApp


1 package org.objectweb.celtix.js.rhino;
2
3 import java.io.File JavaDoc;
4 import java.io.FileFilter JavaDoc;
5
6 import java.net.MalformedURLException JavaDoc;
7 import java.net.URL JavaDoc;
8
9 public class ServerApp {
10     public static final String JavaDoc NO_ADDR_ERR = "error: an endpoint address must be provided";
11     public static final String JavaDoc NO_FILES_ERR = "error: no JavaScript files specified";
12     public static final String JavaDoc WRONG_ADDR_ERR = "error: -a requires a URL argument";
13     public static final String JavaDoc WRONG_BASE_ERR = "error: -b requires a base URL argument";
14     public static final String JavaDoc ILLEGAL_OPTIONS_ERR = "error: -a and -b cannot be used together";
15     public static final String JavaDoc UNKNOWN_OPTION = "error: unknown option";
16
17     private boolean verbose;
18     private boolean bOptSeen;
19     private String JavaDoc epAddr;
20
21     protected void start(String JavaDoc[] args) throws Exception JavaDoc {
22         ProviderFactory ph = createProviderFactory();
23         FileFilter JavaDoc 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 JavaDoc f = new File JavaDoc(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 JavaDoc[] flist = f.listFiles(jsFilter);
47                     for (File JavaDoc 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 JavaDoc(NO_FILES_ERR);
60         }
61     }
62
63     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
64         ServerApp app = null;
65         try {
66             app = new ServerApp();
67             app.start(args);
68         } catch (Exception JavaDoc 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 JavaDoc[] args, int index) throws Exception JavaDoc {
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 JavaDoc(WRONG_ADDR_ERR);
93             }
94             try {
95                 new URL JavaDoc(args[index]);
96             } catch (MalformedURLException JavaDoc m) {
97                 throw new Exception JavaDoc(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 JavaDoc(WRONG_BASE_ERR);
104             }
105             try {
106                 new URL JavaDoc(args[index]);
107             } catch (MalformedURLException JavaDoc m) {
108                 throw new Exception JavaDoc(WRONG_BASE_ERR, m);
109             }
110             epAddr = args[index];
111         } else {
112             throw new Exception JavaDoc(UNKNOWN_OPTION + ": " + args[index]);
113         }
114         return index;
115     }
116
117     private static class JSFilter implements FileFilter JavaDoc {
118         public final boolean accept(File JavaDoc f) {
119             if (f.isFile()) {
120                 String JavaDoc name = f.getName();
121                 return name.endsWith(".js") || name.endsWith(".jsx");
122             } else {
123                 return false;
124             }
125         }
126     }
127 }
128
Popular Tags