jdk/src/share/classes/sun/applet/Main.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7498 fded0c3584f8
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The main entry point into AppletViewer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class Main {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The file which contains all of the AppletViewer specific properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static File theUserPropertiesFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The default key/value pairs for the required user-specific properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final String [][] avDefaultUserProps = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // There's a bootstrapping problem here.  If we don't have a proxyHost,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // then we will not be able to connect to a URL outside the firewall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // however, there's no way for us to set the proxyHost without starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // AppletViewer.  This problem existed before the re-write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        {"http.proxyHost", ""},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        {"http.proxyPort", "80"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        {"package.restrict.access.sun", "true"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        File userHome = new File(System.getProperty("user.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // make sure we can write to this location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        userHome.canWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        theUserPropertiesFile = new File(userHome, ".appletviewer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // i18n
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static AppletMessageHandler amh = new AppletMessageHandler("appletviewer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Member variables set according to options passed in to AppletViewer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private boolean debugFlag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean helpFlag  = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private String  encoding  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private boolean noSecurityFlag  = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static boolean cmdLineTestFlag = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * The list of valid URLs passed in to AppletViewer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static Vector urlList = new Vector(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // This is used in init().  Getting rid of this is desirable but depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // on whether the property that uses it is necessary/standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final String theVersion = System.getProperty("java.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The main entry point into AppletViewer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static void main(String [] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Main m = new Main();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int ret = m.run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Exit immediately if we got some sort of error along the way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // For debugging purposes, if we have passed in "-XcmdLineTest" we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // force a premature exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if ((ret != 0) || (cmdLineTestFlag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.exit(ret);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int run(String [] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // DECODE ARGS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            for (int i = 0; i < args.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                int j = decodeArg(args, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (j == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    throw new ParseException(lookup("main.err.unrecognizedarg",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                                    args[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                i += j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (ParseException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.err.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        // CHECK ARGUMENTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (helpFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (urlList.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.err.println(lookup("main.err.inputfile"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (debugFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // START A DEBUG SESSION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // Given the current architecture, we will end up decoding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // arguments again, but at least we are guaranteed to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // arguments which are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return invokeDebugger(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // INSTALL THE SECURITY MANAGER (if necessary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!noSecurityFlag && (System.getSecurityManager() == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // LAUNCH APPLETVIEWER FOR EACH URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i = 0; i < urlList.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // XXX 5/17 this parsing method should be changed/fixed so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                // it doesn't do both parsing of the html file and launching of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // the AppletPanel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                AppletViewer.parse((URL) urlList.elementAt(i), encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                System.err.println(lookup("main.err.io", e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        System.out.println(lookup("usage"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Decode a single argument in an array and return the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param args The array of arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param i    The argument to decode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return     The number of array elements used when the argument was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *             decoded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *             Thrown when there is a problem with something in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *             argument array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private int decodeArg(String [] args, int i) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String arg = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int argc = args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if ("-help".equalsIgnoreCase(arg) || "-?".equals(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            helpFlag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else if ("-encoding".equals(arg) && (i < argc - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (encoding != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new ParseException(lookup("main.err.dupoption", arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            encoding = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else if ("-debug".equals(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            debugFlag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else if ("-Xnosecurity".equals(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // This is an undocumented (and, in the future, unsupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // flag which prevents AppletViewer from installing its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // SecurityManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            System.err.println(lookup("main.warn.nosecmgr"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            noSecurityFlag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else if ("-XcmdLineTest".equals(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // This is an internal flag which should be used for command-line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // testing.  It instructs AppletViewer to force a premature exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // immediately after the applet has been launched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            cmdLineTestFlag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else if (arg.startsWith("-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new ParseException(lookup("main.err.unsupportedopt", arg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // we found what we hope is a url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            URL url = parseURL(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                urlList.addElement(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Following the relevant RFC, construct a valid URL based on the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param url  a string which represents either a relative or absolute URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @return     a URL when the passed in string can be interpreted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *             to the RFC, <code>null</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception  ParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *             Thrown when we are unable to construct a proper URL from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *             passed in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private URL parseURL(String url) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        URL u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // prefix of the urls with 'file' scheme
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        String prefix = "file:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (url.indexOf(':') <= 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // appletviewer accepts only unencoded filesystem paths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                u = ParseUtil.fileToEncodedURL(new File(url));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            } else if (url.startsWith(prefix) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                       url.length() != prefix.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                       !(new File(url.substring(prefix.length())).isAbsolute()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // relative file URL, like this "file:index.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                // ensure that this file URL is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                // ParseUtil.fileToEncodedURL should be done last (see 6329251)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                String path = ParseUtil.fileToEncodedURL(new File(System.getProperty("user.dir"))).getPath() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    url.substring(prefix.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                u = new URL("file", "", path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                // appletviewer accepts only encoded urls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                u = new URL(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new ParseException(lookup("main.err.badurl",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                            url, e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Invoke the debugger with the arguments passed in to appletviewer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param args The arguments passed into the debugger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return     <code>0</code> if the debugger is invoked successfully,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *             <code>1</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private int invokeDebugger(String [] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // CONSTRUCT THE COMMAND LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        String [] newArgs = new String[args.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int current = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // Add a -classpath argument that prevents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // the debugger from launching appletviewer with the default of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // ".". appletviewer's classpath should never contain valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // classes since they will result in security exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // Ideally, the classpath should be set to "", but the VM won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // allow an empty classpath, so a phony directory name is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        String phonyDir = System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                          File.separator + "phony";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        newArgs[current++] = "-Djava.class.path=" + phonyDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // Appletviewer's main class is the debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        newArgs[current++] = "sun.applet.Main";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // Append all the of the original appletviewer arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // leaving out the "-debug" option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (!("-debug".equals(args[i]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                newArgs[current++] = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // LAUNCH THE DEBUGGER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // Reflection is used for two reasons:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // 1) The debugger classes are on classpath and thus must be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // by the application class loader. (Currently, appletviewer are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // loaded through the boot class path out of rt.jar.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // 2) Reflection removes any build dependency between appletviewer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // and jdb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            Class c = Class.forName("com.sun.tools.example.debug.tty.TTY", true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                    ClassLoader.getSystemClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            Method m = c.getDeclaredMethod("main",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                           new Class[] { String[].class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            m.invoke(null, new Object[] { newArgs });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            System.err.println(lookup("main.debug.cantfinddebug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            System.err.println(lookup("main.debug.cantfindmain"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            System.err.println(lookup("main.debug.exceptionindebug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            System.err.println(lookup("main.debug.cantaccess"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // GET APPLETVIEWER USER-SPECIFIC PROPERTIES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Properties avProps = getAVProps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // ADD OTHER RANDOM PROPERTIES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // XXX 5/18 need to revisit why these are here, is there some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // standard for what is available?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // Standard browser properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        avProps.put("browser", "sun.applet.AppletViewer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        avProps.put("browser.version", "1.06");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        avProps.put("browser.vendor", "Sun Microsystems Inc.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        avProps.put("http.agent", "Java(tm) 2 SDK, Standard Edition v" + theVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // Define which packages can be extended by applets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // XXX 5/19 probably not needed, not checked in AppletSecurity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        avProps.put("package.restrict.definition.java", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        avProps.put("package.restrict.definition.sun", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // Define which properties can be read by applets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // A property named by "key" can be read only when its twin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // property "key.applet" is true.  The following ten properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // are open by default.  Any other property can be explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // opened up by the browser user by calling appletviewer with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // -J-Dkey.applet=true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        avProps.put("java.version.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        avProps.put("java.vendor.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        avProps.put("java.vendor.url.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        avProps.put("java.class.version.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        avProps.put("os.name.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        avProps.put("os.version.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        avProps.put("os.arch.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        avProps.put("file.separator.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        avProps.put("path.separator.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        avProps.put("line.separator.applet", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // Read in the System properties.  If something is going to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // over-written, warn about it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        Properties sysProps = System.getProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        for (Enumeration e = sysProps.propertyNames(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            String key = (String) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            String val = (String) sysProps.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            String oldVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if ((oldVal = (String) avProps.setProperty(key, val)) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                System.err.println(lookup("main.warn.prop.overwrite", key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                          oldVal, val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // INSTALL THE PROPERTY LIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        System.setProperties(avProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // Create and install the security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (!noSecurityFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            System.setSecurityManager(new AppletSecurity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            System.err.println(lookup("main.nosecmgr"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // REMIND: Create and install a socket factory!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Read the AppletViewer user-specific properties.  Typically, these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * properties should reside in the file $USER/.appletviewer.  If this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * does not exist, one will be created.  Information for this file will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * be gleaned from $USER/.hotjava/properties.  If that file does not exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * then default values will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @return     A Properties object containing all of the AppletViewer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *             user-specific properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private Properties getAVProps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        Properties avProps = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        File dotAV = theUserPropertiesFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (dotAV.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // we must have already done the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (dotAV.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                // just read the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                avProps = getAVProps(dotAV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                // send out warning and use defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                System.err.println(lookup("main.warn.cantreadprops",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                          dotAV.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                avProps = setDefaultAVProps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // create the $USER/.appletviewer file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            // see if $USER/.hotjava/properties exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            File userHome = new File(System.getProperty("user.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            File dotHJ = new File(userHome, ".hotjava");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            dotHJ = new File(dotHJ, "properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (dotHJ.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                // just read the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                avProps = getAVProps(dotHJ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // send out warning and use defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                System.err.println(lookup("main.warn.cantreadprops",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                          dotHJ.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                avProps = setDefaultAVProps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // SAVE THE FILE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                FileOutputStream out = new FileOutputStream(dotAV);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                avProps.store(out, lookup("main.prop.store"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                System.err.println(lookup("main.err.prop.cantsave",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                                          dotAV.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return avProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Set the AppletViewer user-specific properties to be the default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return     A Properties object containing all of the AppletViewer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *             user-specific properties, set to the default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    private Properties setDefaultAVProps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        Properties avProps = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        for (int i = 0; i < avDefaultUserProps.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            avProps.setProperty(avDefaultUserProps[i][0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                avDefaultUserProps[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return avProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Given a file, find only the properties that are setable by AppletViewer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param inFile A Properties file from which we select the properties of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *             interest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return     A Properties object containing all of the AppletViewer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *             user-specific properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private Properties getAVProps(File inFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        Properties avProps  = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // read the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        Properties tmpProps = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            FileInputStream in = new FileInputStream(inFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            tmpProps.load(new BufferedInputStream(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            System.err.println(lookup("main.err.prop.cantread",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                      inFile.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // pick off the properties we care about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        for (int i = 0; i < avDefaultUserProps.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            String value = tmpProps.getProperty(avDefaultUserProps[i][0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                // the property exists in the file, so replace the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                avProps.setProperty(avDefaultUserProps[i][0], value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                // just use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                avProps.setProperty(avDefaultUserProps[i][0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                    avDefaultUserProps[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return avProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Methods for easier i18n handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private static String lookup(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return amh.getMessage(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private static String lookup(String key, String arg0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return amh.getMessage(key, arg0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    private static String lookup(String key, String arg0, String arg1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return amh.getMessage(key, arg0, arg1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private static String lookup(String key, String arg0, String arg1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                 String arg2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return amh.getMessage(key, arg0, arg1, arg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    class ParseException extends RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        public ParseException(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            super(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        public ParseException(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            super(t.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            this.t = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        Throwable t = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
}