001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.tools.appletviewer;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024:
025: import java.util.ArrayList;
026: import java.util.Enumeration;
027: import java.util.Iterator;
028: import java.util.Properties;
029:
030: public class Main {
031:
032: static final String propertiesFileName = ".harmony.appletviewer.properties";
033: static final String httpProxyHost = "http.proxyHost";
034: static final String httpProxyPort = "http.proxyPort";
035: static final String httpsProxyHost = "https.proxyHost";
036: static final String httpsProxyPort = "https.proxyPort";
037: static final String ftpProxyHost = "ftp.proxyHost";
038: static final String ftpProxyPort = "ftp.proxyPort";
039: static final Properties properties = new Properties();
040:
041: static File propertiesFile;
042:
043: public static void main(String argv[]) throws Exception {
044:
045: if (argv.length == 0) {
046: printHelp();
047: return;
048: }
049:
050: ArrayList<String> propertiesList = new ArrayList<String>();
051: ArrayList<String> urlsList = new ArrayList<String>();
052:
053: for (int i = 0; i < argv.length; i++) {
054: if (argv[i].startsWith("-D")) {
055: propertiesList.add(argv[i].substring(2));
056: } else {
057: urlsList.add(argv[i]);
058: }
059: }
060:
061: if (urlsList.size() == 0) {
062: printHelp();
063: return;
064: }
065:
066: // Load stored java.properties
067: String userHomeDir = System.getProperty("user.home");
068: propertiesFile = new File(userHomeDir + File.separator
069: + Main.propertiesFileName);
070:
071: boolean needStore = false;
072: if (propertiesFile.exists()) {
073: try {
074: properties.load(new FileInputStream(propertiesFile));
075: } catch (IOException e) {
076: }
077: } else {
078: properties.setProperty(httpProxyHost, "");
079: properties.setProperty(httpProxyPort, "");
080: properties.setProperty(httpsProxyHost, "");
081: properties.setProperty(httpsProxyPort, "");
082: properties.setProperty(ftpProxyHost, "");
083: properties.setProperty(ftpProxyPort, "");
084: needStore = true;
085: }
086:
087: // Parse command line java.properties
088:
089: Iterator<String> iterator = propertiesList.iterator();
090: while (iterator.hasNext()) {
091: String prop = iterator.next();
092: if (prop != null) {
093: String[] pair = prop.split("=");
094: String key = null;
095: String val = null;
096:
097: if (pair[0] != null) {
098: key = pair[0].trim();
099: }
100:
101: if (pair[1] != null) {
102: val = pair[1].trim();
103: }
104:
105: if (key != null && key != "" && val != null
106: && val != "") {
107: if (validatePropertyName(key)) {
108: properties.setProperty(key, val);
109: needStore = true;
110: } else {
111: System.err.println("Unknown proxy property: "
112: + key);
113: System.exit(-1);
114: }
115:
116: if (key.endsWith("Port")) {
117: try {
118: if (Integer.parseInt(val) < 0) {
119: wrongPortMessage(val);
120: System.exit(-1);
121: }
122: } catch (NumberFormatException ex) {
123: wrongPortMessage(key);
124: System.exit(-1);
125: }
126: }
127: }
128: }
129: }
130:
131: if (needStore)
132: storeProxyProperties();
133:
134: Enumeration<?> e = properties.propertyNames();
135:
136: while (e.hasMoreElements()) {
137: String key = (String) e.nextElement();
138: String val = properties.getProperty(key);
139: if (val != null && val != "") {
140: System.setProperty(key, val);
141: }
142: }
143:
144: HTMLParser parser = new HTMLParser();
145: Object[] applets = parser.parse(urlsList
146: .toArray(new String[urlsList.size()]), 0);
147:
148: // Start applets
149: for (int i = 0; i < applets.length; i++)
150: new AppletFrame((AppletInfo) applets[i]);
151: }
152:
153: static void storeProxyProperties() {
154: try {
155: if (!propertiesFile.exists())
156: propertiesFile.createNewFile();
157: properties
158: .store(new FileOutputStream(propertiesFile),
159: "User-specific properties for Harmony AppletViewer");
160: } catch (IOException e) {
161: }
162: }
163:
164: private static boolean validatePropertyName(String name) {
165: if (!name.equals(httpProxyHost) && !name.equals(httpProxyPort)
166: && !name.equals(httpsProxyHost)
167: && !name.equals(httpsProxyPort)
168: && !name.equals(ftpProxyHost)
169: && !name.equals(ftpProxyPort)) {
170:
171: return false;
172: } else {
173: return true;
174: }
175: }
176:
177: private static void wrongPortMessage(String portName) {
178: System.err.println();
179: System.err.println("Proxy parameter error: " + portName
180: + " must be a positive integer value");
181: }
182:
183: private static void printHelp() {
184: System.err.println("AppletViewer");
185: System.err
186: .println("Usage: appletviewer [[-Dproxy.property] ... ] url(s)");
187: System.err.println();
188: System.err.println("Available proxy properties:");
189: System.err.println();
190: System.err.println("\thttp.proxyHost");
191: System.err.println("\thttp.proxyPort");
192: System.err.println("\thttps.proxyHost");
193: System.err.println("\thttps.proxyPort");
194: System.err.println("\tftp.proxyHost");
195: System.err.println("\tftp.proxyPort");
196: }
197: }
|