0N/A/*
2362N/A * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.applet;
0N/A
0N/Aimport java.io.BufferedInputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.net.URL;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Properties;
0N/Aimport java.util.Vector;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/A/**
0N/A * The main entry point into AppletViewer.
0N/A */
0N/Apublic class Main {
0N/A /**
0N/A * The file which contains all of the AppletViewer specific properties.
0N/A */
0N/A static File theUserPropertiesFile;
0N/A
0N/A /**
0N/A * The default key/value pairs for the required user-specific properties.
0N/A */
0N/A static final String [][] avDefaultUserProps = {
0N/A // There's a bootstrapping problem here. If we don't have a proxyHost,
0N/A // then we will not be able to connect to a URL outside the firewall;
0N/A // however, there's no way for us to set the proxyHost without starting
0N/A // AppletViewer. This problem existed before the re-write.
0N/A {"http.proxyHost", ""},
0N/A {"http.proxyPort", "80"},
0N/A {"package.restrict.access.sun", "true"}
0N/A };
0N/A
0N/A static {
0N/A File userHome = new File(System.getProperty("user.home"));
0N/A // make sure we can write to this location
0N/A userHome.canWrite();
0N/A
0N/A theUserPropertiesFile = new File(userHome, ".appletviewer");
0N/A }
0N/A
0N/A // i18n
0N/A private static AppletMessageHandler amh = new AppletMessageHandler("appletviewer");
0N/A
0N/A /**
0N/A * Member variables set according to options passed in to AppletViewer.
0N/A */
0N/A private boolean debugFlag = false;
0N/A private boolean helpFlag = false;
0N/A private String encoding = null;
0N/A private boolean noSecurityFlag = false;
0N/A private static boolean cmdLineTestFlag = false;
0N/A
0N/A /**
0N/A * The list of valid URLs passed in to AppletViewer.
0N/A */
0N/A private static Vector urlList = new Vector(1);
0N/A
0N/A // This is used in init(). Getting rid of this is desirable but depends
0N/A // on whether the property that uses it is necessary/standard.
0N/A public static final String theVersion = System.getProperty("java.version");
0N/A
0N/A /**
0N/A * The main entry point into AppletViewer.
0N/A */
0N/A public static void main(String [] args) {
0N/A Main m = new Main();
0N/A int ret = m.run(args);
0N/A
0N/A // Exit immediately if we got some sort of error along the way.
0N/A // For debugging purposes, if we have passed in "-XcmdLineTest" we
0N/A // force a premature exit.
0N/A if ((ret != 0) || (cmdLineTestFlag))
0N/A System.exit(ret);
0N/A }
0N/A
0N/A private int run(String [] args) {
0N/A // DECODE ARGS
0N/A try {
0N/A if (args.length == 0) {
0N/A usage();
0N/A return 0;
0N/A }
0N/A for (int i = 0; i < args.length; ) {
0N/A int j = decodeArg(args, i);
0N/A if (j == 0) {
0N/A throw new ParseException(lookup("main.err.unrecognizedarg",
0N/A args[i]));
0N/A }
0N/A i += j;
0N/A }
0N/A } catch (ParseException e) {
0N/A System.err.println(e.getMessage());
0N/A return 1;
0N/A }
0N/A
0N/A // CHECK ARGUMENTS
0N/A if (helpFlag) {
0N/A usage();
0N/A return 0;
0N/A }
0N/A
0N/A if (urlList.size() == 0) {
0N/A System.err.println(lookup("main.err.inputfile"));
0N/A return 1;
0N/A }
0N/A
0N/A if (debugFlag) {
0N/A // START A DEBUG SESSION
0N/A // Given the current architecture, we will end up decoding the
0N/A // arguments again, but at least we are guaranteed to have
0N/A // arguments which are valid.
0N/A return invokeDebugger(args);
0N/A }
0N/A
0N/A // INSTALL THE SECURITY MANAGER (if necessary)
0N/A if (!noSecurityFlag && (System.getSecurityManager() == null))
0N/A init();
0N/A
0N/A // LAUNCH APPLETVIEWER FOR EACH URL
0N/A for (int i = 0; i < urlList.size(); i++) {
0N/A try {
0N/A // XXX 5/17 this parsing method should be changed/fixed so that
0N/A // it doesn't do both parsing of the html file and launching of
0N/A // the AppletPanel
0N/A AppletViewer.parse((URL) urlList.elementAt(i), encoding);
0N/A } catch (IOException e) {
0N/A System.err.println(lookup("main.err.io", e.getMessage()));
0N/A return 1;
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A private static void usage() {
0N/A System.out.println(lookup("usage"));
0N/A }
0N/A
0N/A /**
0N/A * Decode a single argument in an array and return the number of elements
0N/A * used.
0N/A *
0N/A * @param args The array of arguments.
0N/A * @param i The argument to decode.
0N/A * @return The number of array elements used when the argument was
0N/A * decoded.
0N/A * @exception ParseException
0N/A * Thrown when there is a problem with something in the
0N/A * argument array.
0N/A */
0N/A private int decodeArg(String [] args, int i) throws ParseException {
0N/A String arg = args[i];
0N/A int argc = args.length;
0N/A
0N/A if ("-help".equalsIgnoreCase(arg) || "-?".equals(arg)) {
0N/A helpFlag = true;
0N/A return 1;
0N/A } else if ("-encoding".equals(arg) && (i < argc - 1)) {
0N/A if (encoding != null)
0N/A throw new ParseException(lookup("main.err.dupoption", arg));
0N/A encoding = args[++i];
0N/A return 2;
0N/A } else if ("-debug".equals(arg)) {
0N/A debugFlag = true;
0N/A return 1;
0N/A } else if ("-Xnosecurity".equals(arg)) {
0N/A // This is an undocumented (and, in the future, unsupported)
0N/A // flag which prevents AppletViewer from installing its own
0N/A // SecurityManager.
0N/A
0N/A System.err.println();
0N/A System.err.println(lookup("main.warn.nosecmgr"));
0N/A System.err.println();
0N/A
0N/A noSecurityFlag = true;
0N/A return 1;
0N/A } else if ("-XcmdLineTest".equals(arg)) {
0N/A // This is an internal flag which should be used for command-line
0N/A // testing. It instructs AppletViewer to force a premature exit
0N/A // immediately after the applet has been launched.
0N/A cmdLineTestFlag = true;
0N/A return 1;
0N/A } else if (arg.startsWith("-")) {
0N/A throw new ParseException(lookup("main.err.unsupportedopt", arg));
0N/A } else {
0N/A // we found what we hope is a url
0N/A URL url = parseURL(arg);
0N/A if (url != null) {
0N/A urlList.addElement(url);
0N/A return 1;
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Following the relevant RFC, construct a valid URL based on the passed in
0N/A * string.
0N/A *
0N/A * @param url a string which represents either a relative or absolute URL.
0N/A * @return a URL when the passed in string can be interpreted according
0N/A * to the RFC, <code>null</code> otherwise.
0N/A * @exception ParseException
0N/A * Thrown when we are unable to construct a proper URL from the
0N/A * passed in string.
0N/A */
0N/A private URL parseURL(String url) throws ParseException {
0N/A URL u = null;
0N/A // prefix of the urls with 'file' scheme
0N/A String prefix = "file:";
0N/A
0N/A try {
0N/A if (url.indexOf(':') <= 1)
0N/A {
0N/A // appletviewer accepts only unencoded filesystem paths
0N/A u = ParseUtil.fileToEncodedURL(new File(url));
0N/A } else if (url.startsWith(prefix) &&
0N/A url.length() != prefix.length() &&
0N/A !(new File(url.substring(prefix.length())).isAbsolute()))
0N/A {
0N/A // relative file URL, like this "file:index.html"
0N/A // ensure that this file URL is absolute
0N/A // ParseUtil.fileToEncodedURL should be done last (see 6329251)
0N/A String path = ParseUtil.fileToEncodedURL(new File(System.getProperty("user.dir"))).getPath() +
0N/A url.substring(prefix.length());
0N/A u = new URL("file", "", path);
0N/A } else {
0N/A // appletviewer accepts only encoded urls
0N/A u = new URL(url);
0N/A }
0N/A } catch (MalformedURLException e) {
0N/A throw new ParseException(lookup("main.err.badurl",
0N/A url, e.getMessage()));
0N/A }
0N/A
0N/A return u;
0N/A }
0N/A
0N/A /**
0N/A * Invoke the debugger with the arguments passed in to appletviewer.
0N/A *
0N/A * @param args The arguments passed into the debugger.
0N/A * @return <code>0</code> if the debugger is invoked successfully,
0N/A * <code>1</code> otherwise.
0N/A */
0N/A private int invokeDebugger(String [] args) {
0N/A // CONSTRUCT THE COMMAND LINE
0N/A String [] newArgs = new String[args.length + 1];
0N/A int current = 0;
0N/A
0N/A // Add a -classpath argument that prevents
0N/A // the debugger from launching appletviewer with the default of
0N/A // ".". appletviewer's classpath should never contain valid
0N/A // classes since they will result in security exceptions.
0N/A // Ideally, the classpath should be set to "", but the VM won't
0N/A // allow an empty classpath, so a phony directory name is used.
0N/A String phonyDir = System.getProperty("java.home") +
0N/A File.separator + "phony";
0N/A newArgs[current++] = "-Djava.class.path=" + phonyDir;
0N/A
0N/A // Appletviewer's main class is the debuggee
0N/A newArgs[current++] = "sun.applet.Main";
0N/A
0N/A // Append all the of the original appletviewer arguments,
0N/A // leaving out the "-debug" option.
0N/A for (int i = 0; i < args.length; i++) {
0N/A if (!("-debug".equals(args[i]))) {
0N/A newArgs[current++] = args[i];
0N/A }
0N/A }
0N/A
0N/A // LAUNCH THE DEBUGGER
0N/A // Reflection is used for two reasons:
0N/A // 1) The debugger classes are on classpath and thus must be loaded
0N/A // by the application class loader. (Currently, appletviewer are
0N/A // loaded through the boot class path out of rt.jar.)
0N/A // 2) Reflection removes any build dependency between appletviewer
0N/A // and jdb.
0N/A try {
0N/A Class c = Class.forName("com.sun.tools.example.debug.tty.TTY", true,
0N/A ClassLoader.getSystemClassLoader());
0N/A Method m = c.getDeclaredMethod("main",
0N/A new Class[] { String[].class });
0N/A m.invoke(null, new Object[] { newArgs });
0N/A } catch (ClassNotFoundException cnfe) {
0N/A System.err.println(lookup("main.debug.cantfinddebug"));
0N/A return 1;
0N/A } catch (NoSuchMethodException nsme) {
0N/A System.err.println(lookup("main.debug.cantfindmain"));
0N/A return 1;
0N/A } catch (InvocationTargetException ite) {
0N/A System.err.println(lookup("main.debug.exceptionindebug"));
0N/A return 1;
0N/A } catch (IllegalAccessException iae) {
0N/A System.err.println(lookup("main.debug.cantaccess"));
0N/A return 1;
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A private void init() {
0N/A // GET APPLETVIEWER USER-SPECIFIC PROPERTIES
0N/A Properties avProps = getAVProps();
0N/A
0N/A // ADD OTHER RANDOM PROPERTIES
0N/A // XXX 5/18 need to revisit why these are here, is there some
0N/A // standard for what is available?
0N/A
0N/A // Standard browser properties
0N/A avProps.put("browser", "sun.applet.AppletViewer");
0N/A avProps.put("browser.version", "1.06");
3183N/A avProps.put("browser.vendor", "Oracle Corporation");
0N/A avProps.put("http.agent", "Java(tm) 2 SDK, Standard Edition v" + theVersion);
0N/A
0N/A // Define which packages can be extended by applets
0N/A // XXX 5/19 probably not needed, not checked in AppletSecurity
0N/A avProps.put("package.restrict.definition.java", "true");
0N/A avProps.put("package.restrict.definition.sun", "true");
0N/A
0N/A // Define which properties can be read by applets.
0N/A // A property named by "key" can be read only when its twin
0N/A // property "key.applet" is true. The following ten properties
0N/A // are open by default. Any other property can be explicitly
0N/A // opened up by the browser user by calling appletviewer with
0N/A // -J-Dkey.applet=true
0N/A avProps.put("java.version.applet", "true");
0N/A avProps.put("java.vendor.applet", "true");
0N/A avProps.put("java.vendor.url.applet", "true");
0N/A avProps.put("java.class.version.applet", "true");
0N/A avProps.put("os.name.applet", "true");
0N/A avProps.put("os.version.applet", "true");
0N/A avProps.put("os.arch.applet", "true");
0N/A avProps.put("file.separator.applet", "true");
0N/A avProps.put("path.separator.applet", "true");
0N/A avProps.put("line.separator.applet", "true");
0N/A
0N/A // Read in the System properties. If something is going to be
0N/A // over-written, warn about it.
0N/A Properties sysProps = System.getProperties();
0N/A for (Enumeration e = sysProps.propertyNames(); e.hasMoreElements(); ) {
0N/A String key = (String) e.nextElement();
0N/A String val = (String) sysProps.getProperty(key);
0N/A String oldVal;
0N/A if ((oldVal = (String) avProps.setProperty(key, val)) != null)
0N/A System.err.println(lookup("main.warn.prop.overwrite", key,
0N/A oldVal, val));
0N/A }
0N/A
0N/A // INSTALL THE PROPERTY LIST
0N/A System.setProperties(avProps);
0N/A
0N/A // Create and install the security manager
0N/A if (!noSecurityFlag) {
0N/A System.setSecurityManager(new AppletSecurity());
0N/A } else {
0N/A System.err.println(lookup("main.nosecmgr"));
0N/A }
0N/A
0N/A // REMIND: Create and install a socket factory!
0N/A }
0N/A
0N/A /**
0N/A * Read the AppletViewer user-specific properties. Typically, these
0N/A * properties should reside in the file $USER/.appletviewer. If this file
0N/A * does not exist, one will be created. Information for this file will
0N/A * be gleaned from $USER/.hotjava/properties. If that file does not exist,
0N/A * then default values will be used.
0N/A *
0N/A * @return A Properties object containing all of the AppletViewer
0N/A * user-specific properties.
0N/A */
0N/A private Properties getAVProps() {
0N/A Properties avProps = new Properties();
0N/A
0N/A File dotAV = theUserPropertiesFile;
0N/A if (dotAV.exists()) {
0N/A // we must have already done the conversion
0N/A if (dotAV.canRead()) {
0N/A // just read the file
0N/A avProps = getAVProps(dotAV);
0N/A } else {
0N/A // send out warning and use defaults
0N/A System.err.println(lookup("main.warn.cantreadprops",
0N/A dotAV.toString()));
0N/A avProps = setDefaultAVProps();
0N/A }
0N/A } else {
0N/A // create the $USER/.appletviewer file
0N/A
0N/A // see if $USER/.hotjava/properties exists
0N/A File userHome = new File(System.getProperty("user.home"));
0N/A File dotHJ = new File(userHome, ".hotjava");
0N/A dotHJ = new File(dotHJ, "properties");
0N/A if (dotHJ.exists()) {
0N/A // just read the file
0N/A avProps = getAVProps(dotHJ);
0N/A } else {
0N/A // send out warning and use defaults
0N/A System.err.println(lookup("main.warn.cantreadprops",
0N/A dotHJ.toString()));
0N/A avProps = setDefaultAVProps();
0N/A }
0N/A
0N/A // SAVE THE FILE
0N/A try {
0N/A FileOutputStream out = new FileOutputStream(dotAV);
0N/A avProps.store(out, lookup("main.prop.store"));
0N/A out.close();
0N/A } catch (IOException e) {
0N/A System.err.println(lookup("main.err.prop.cantsave",
0N/A dotAV.toString()));
0N/A }
0N/A }
0N/A return avProps;
0N/A }
0N/A
0N/A /**
0N/A * Set the AppletViewer user-specific properties to be the default values.
0N/A *
0N/A * @return A Properties object containing all of the AppletViewer
0N/A * user-specific properties, set to the default values.
0N/A */
0N/A private Properties setDefaultAVProps() {
0N/A Properties avProps = new Properties();
0N/A for (int i = 0; i < avDefaultUserProps.length; i++) {
0N/A avProps.setProperty(avDefaultUserProps[i][0],
0N/A avDefaultUserProps[i][1]);
0N/A }
0N/A return avProps;
0N/A }
0N/A
0N/A /**
0N/A * Given a file, find only the properties that are setable by AppletViewer.
0N/A *
0N/A * @param inFile A Properties file from which we select the properties of
0N/A * interest.
0N/A * @return A Properties object containing all of the AppletViewer
0N/A * user-specific properties.
0N/A */
0N/A private Properties getAVProps(File inFile) {
0N/A Properties avProps = new Properties();
0N/A
0N/A // read the file
0N/A Properties tmpProps = new Properties();
0N/A try {
0N/A FileInputStream in = new FileInputStream(inFile);
0N/A tmpProps.load(new BufferedInputStream(in));
0N/A in.close();
0N/A } catch (IOException e) {
0N/A System.err.println(lookup("main.err.prop.cantread",
0N/A inFile.toString()));
0N/A }
0N/A
0N/A // pick off the properties we care about
0N/A for (int i = 0; i < avDefaultUserProps.length; i++) {
0N/A String value = tmpProps.getProperty(avDefaultUserProps[i][0]);
0N/A if (value != null) {
0N/A // the property exists in the file, so replace the default
0N/A avProps.setProperty(avDefaultUserProps[i][0], value);
0N/A } else {
0N/A // just use the default
0N/A avProps.setProperty(avDefaultUserProps[i][0],
0N/A avDefaultUserProps[i][1]);
0N/A }
0N/A }
0N/A return avProps;
0N/A }
0N/A
0N/A /**
0N/A * Methods for easier i18n handling.
0N/A */
0N/A
0N/A private static String lookup(String key) {
0N/A return amh.getMessage(key);
0N/A }
0N/A
0N/A private static String lookup(String key, String arg0) {
0N/A return amh.getMessage(key, arg0);
0N/A }
0N/A
0N/A private static String lookup(String key, String arg0, String arg1) {
0N/A return amh.getMessage(key, arg0, arg1);
0N/A }
0N/A
0N/A private static String lookup(String key, String arg0, String arg1,
0N/A String arg2) {
0N/A return amh.getMessage(key, arg0, arg1, arg2);
0N/A }
0N/A
0N/A class ParseException extends RuntimeException
0N/A {
0N/A public ParseException(String msg) {
0N/A super(msg);
0N/A }
0N/A
0N/A public ParseException(Throwable t) {
0N/A super(t.getMessage());
0N/A this.t = t;
0N/A }
0N/A
0N/A Throwable t = null;
0N/A }
0N/A}