001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.js.rhino;
019:
020: import java.io.File;
021: import java.io.FileFilter;
022:
023: import java.net.MalformedURLException;
024: import java.net.URL;
025:
026: public class ServerApp {
027: public static final String NO_ADDR_ERR = "error: an endpoint address must be provided";
028: public static final String NO_FILES_ERR = "error: no JavaScript files specified";
029: public static final String WRONG_ADDR_ERR = "error: -a requires a URL argument";
030: public static final String WRONG_BASE_ERR = "error: -b requires a base URL argument";
031: public static final String ILLEGAL_OPTIONS_ERR = "error: -a and -b cannot be used together";
032: public static final String UNKNOWN_OPTION = "error: unknown option";
033:
034: private boolean verbose;
035: private boolean bOptSeen;
036: private String epAddr;
037:
038: protected void start(String[] args) throws Exception {
039: ProviderFactory ph = createProviderFactory();
040: FileFilter jsFilter = new JSFilter();
041: int i = 0;
042: boolean fileSeen = false;
043: boolean msgPrinted = false;
044: for (;;) {
045: if (i == args.length) {
046: break;
047: }
048: if (args[i].startsWith("-")) {
049: i = checkOption(args, i);
050: if (verbose && !msgPrinted) {
051: msgPrinted = true;
052: if (verbose) {
053: System.out.println("entering server");
054: }
055: }
056: } else {
057: File f = new File(args[i]);
058: if (f.isFile() && jsFilter.accept(f)) {
059: fileSeen = true;
060: if (verbose) {
061: System.out.println("processing file "
062: + f.getCanonicalPath());
063: }
064: ph.createAndPublish(f, epAddr, bOptSeen);
065: } else if (f.isDirectory()) {
066: File[] flist = f.listFiles(jsFilter);
067: for (File file : flist) {
068: fileSeen = true;
069: if (verbose) {
070: System.out.println("processing file "
071: + file.getCanonicalPath());
072: }
073: ph.createAndPublish(file, epAddr, bOptSeen);
074: }
075: }
076: }
077: i++;
078: }
079: if (!fileSeen) {
080: throw new Exception(NO_FILES_ERR);
081: }
082: }
083:
084: public static void main(String[] args) throws Exception {
085: ServerApp app = null;
086: try {
087: app = new ServerApp();
088: app.start(args);
089: } catch (Exception e) {
090: System.err.println("error: " + e.getMessage());
091: System.exit(1);
092: }
093: if (app.verbose) {
094: System.out.println("server ready...");
095: }
096: Thread.sleep(5 * 60 * 1000);
097: if (app.verbose) {
098: System.out.println("server timed out, exiting");
099: }
100: System.exit(0);
101: }
102:
103: protected ProviderFactory createProviderFactory() {
104: return new ProviderFactory();
105: }
106:
107: private int checkOption(String[] args, int index) throws Exception {
108: if ("-v".equals(args[index])) {
109: verbose = true;
110: } else if ("-a".equals(args[index])) {
111: bOptSeen = false;
112: if (++index == args.length) {
113: throw new Exception(WRONG_ADDR_ERR);
114: }
115: try {
116: new URL(args[index]);
117: } catch (MalformedURLException m) {
118: throw new Exception(WRONG_ADDR_ERR, m);
119: }
120: epAddr = args[index];
121: } else if ("-b".equals(args[index])) {
122: bOptSeen = true;
123: if (++index == args.length) {
124: throw new Exception(WRONG_BASE_ERR);
125: }
126: try {
127: new URL(args[index]);
128: } catch (MalformedURLException m) {
129: throw new Exception(WRONG_BASE_ERR, m);
130: }
131: epAddr = args[index];
132: } else {
133: throw new Exception(UNKNOWN_OPTION + ": " + args[index]);
134: }
135: return index;
136: }
137:
138: private static class JSFilter implements FileFilter {
139: public final boolean accept(File f) {
140: if (f.isFile()) {
141: String name = f.getName();
142: return name.endsWith(".js") || name.endsWith(".jsx");
143: } else {
144: return false;
145: }
146: }
147: }
148: }
|