jdk/src/share/classes/sun/rmi/rmic/BatchEnvironment.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5627 e636ac7a63a4
child 12040 558b0e0d5910
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5627
diff changeset
     2
 * Copyright (c) 1996, 2010, 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
/*****************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*                    Copyright (c) IBM Corporation 1998                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/*                                                                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/* (C) Copyright IBM Corp. 1998                                              */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*                                                                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*****************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package sun.rmi.rmic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.LinkedHashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.tools.java.ClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * BatchEnvironment for rmic extends javac's version in four ways:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 1. It overrides errorString() to handle looking for rmic-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * error messages in rmic's resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * 2. It provides a mechanism for recording intermediate generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * files so that they can be deleted later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * 3. It holds a reference to the Main instance so that generators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * can refer to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * 4. It provides access to the ClassPath passed to the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class BatchEnvironment extends sun.tools.javac.BatchEnvironment {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** instance of Main which created this environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Main main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Create a ClassPath object for rmic from a class path string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static ClassPath createClassPath(String classPathString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ClassPath[] paths = classPaths(null, classPathString, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return paths[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Create a ClassPath object for rmic from the relevant command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * options for class path, boot class path, and extension directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static ClassPath createClassPath(String classPathString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                            String sysClassPathString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                            String extDirsString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * Previously, this method delegated to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * sun.tools.javac.BatchEnvironment.classPaths method in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * to supply default values for paths not specified on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * command line, expand extensions directories into specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * JAR files, and construct the ClassPath object-- but as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         * of the fix for 6473331, which adds support for Class-Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         * manifest entries in JAR files, those steps are now handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * here directly, with the help of a Path utility class copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * from the new javac implementation (see below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Path path = new Path();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (sysClassPathString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            sysClassPathString = System.getProperty("sun.boot.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (sysClassPathString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            path.addFiles(sysClassPathString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * Class-Path manifest entries are supported for JAR files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * everywhere except in the boot class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        path.expandJarClassPaths(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (extDirsString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            extDirsString = System.getProperty("java.ext.dirs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (extDirsString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            path.addDirectories(extDirsString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * In the application class path, an empty element means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * the current working directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        path.emptyPathDefault(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (classPathString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // The env.class.path property is the user's CLASSPATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // environment variable, and it set by the wrapper (ie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // javac.exe).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            classPathString = System.getProperty("env.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (classPathString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                classPathString = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        path.addFiles(classPathString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return new ClassPath(path.toArray(new String[path.size()]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Create a BatchEnvironment for rmic with the given class path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * stream for messages and Main.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public BatchEnvironment(OutputStream out, ClassPath path, Main main) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        super(out, new ClassPath(""), path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                // use empty "sourcePath" (see 4666958)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.main = main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Get the instance of Main which created this environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Main getMain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return main;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Get the ClassPath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public ClassPath getClassPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return binaryPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /** list of generated source files created in this environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private Vector generatedFiles = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Remember a generated source file generated so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * can be removed later, if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void addGeneratedFile(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        generatedFiles.addElement(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Delete all the generated source files made during the execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * of this environment (those that have been registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * "addGeneratedFile" method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public void deleteGeneratedFiles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        synchronized(generatedFiles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            Enumeration enumeration = generatedFiles.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            while (enumeration.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                File file = (File) enumeration.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                file.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            generatedFiles.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Release resources, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        main = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        generatedFiles = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        super.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Return the formatted, localized string for a named error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * and supplied arguments.  For rmic error messages, with names that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * being with "rmic.", look up the error message in rmic's resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * bundle; otherwise, defer to java's superclass method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public String errorString(String err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                              Object arg0, Object arg1, Object arg2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (err.startsWith("rmic.") || err.startsWith("warn.rmic.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            String result =  Main.getText(err,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                          (arg0 != null ? arg0.toString() : null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                          (arg1 != null ? arg1.toString() : null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                          (arg2 != null ? arg2.toString() : null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (err.startsWith("warn.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                result = "warning: " + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return super.errorString(err, arg0, arg1, arg2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Utility for building paths of directories and JAR files.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * class was copied from com.sun.tools.javac.util.Paths as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * the fix for 6473331, which adds support for Class-Path manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * entries in JAR files.  Diagnostic code is simply commented out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * because rmic silently ignored these conditions historically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static class Path extends LinkedHashSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        private static final long serialVersionUID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        private static final boolean warn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        private static class PathIterator implements Collection<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            private int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            private final String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            private final String emptyPathDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            public PathIterator(String path, String emptyPathDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                this.emptyPathDefault = emptyPathDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            public PathIterator(String path) { this(path, null); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            public Iterator<String> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                return new Iterator<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        return pos <= path.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    public String next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        int beg = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        int end = path.indexOf(File.pathSeparator, beg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        if (end == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            end = path.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        pos = end + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        if (beg == end && emptyPathDefault != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            return emptyPathDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            return path.substring(beg, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // required for Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            public boolean add(String o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            public boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            public boolean addAll(Collection<? extends String> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            public boolean removeAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            public boolean retainAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /** Is this the name of a zip file? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        private static boolean isZip(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return new File(name).isFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        private boolean expandJarClassPaths = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        public Path expandJarClassPaths(boolean x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            expandJarClassPaths = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        /** What to use when path element is the empty string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        private String emptyPathDefault = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public Path emptyPathDefault(String x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            emptyPathDefault = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        public Path() { super(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        public Path addDirectories(String dirs, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (dirs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                for (String dir : new PathIterator(dirs))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    addDirectory(dir, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        public Path addDirectories(String dirs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return addDirectories(dirs, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        private void addDirectory(String dir, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (! new File(dir).isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
//              if (warn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
//                  log.warning(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
//                              "dir.path.element.not.found", dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            for (String direntry : new File(dir).list()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                String canonicalized = direntry.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                if (canonicalized.endsWith(".jar") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    canonicalized.endsWith(".zip"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    addFile(dir + File.separator + direntry, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        public Path addFiles(String files, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (files != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                for (String file : new PathIterator(files, emptyPathDefault))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    addFile(file, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        public Path addFiles(String files) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return addFiles(files, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        private void addFile(String file, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (contains(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                /* Discard duplicates and avoid infinite recursion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            File ele = new File(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (! ele.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                /* No such file or directory exist */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                if (warn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
//                      log.warning(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
//                          "path.element.not.found", file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (ele.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                /* File is an ordinay file  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                String arcname = file.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                if (! (arcname.endsWith(".zip") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                       arcname.endsWith(".jar"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    /* File name don't have right extension */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
//                      if (warn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
//                          log.warning(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
//                              "invalid.archive.file", file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            /* Now what we have left is either a directory or a file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
               confirming to archive naming convention */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            super.add(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (expandJarClassPaths && isZip(file))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                addJarClassPath(file, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // Adds referenced classpath elements from a jar's Class-Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // Manifest entry.  In some future release, we may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // update this code to recognize URLs rather than simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // filenames, but if we do, we should redo all path-related code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        private void addJarClassPath(String jarFileName, boolean warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                String jarParent = new File(jarFileName).getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                JarFile jar = new JarFile(jarFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    Manifest man = jar.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (man == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    Attributes attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (attr == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    String path = attr.getValue(Attributes.Name.CLASS_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    if (path == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    for (StringTokenizer st = new StringTokenizer(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        st.hasMoreTokens();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        String elt = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        if (jarParent != null)
5623
5c9695dc013a 6934615: Relative classpaths in jarfile manifests are handled inefficiently by rmic
jjg
parents: 2
diff changeset
   432
                            elt = new File(jarParent, elt).getCanonicalPath();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        addFile(elt, warn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    jar.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
//              log.error(Position.NOPOS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
//                        "error.reading.file", jarFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
//                        e.getLocalizedMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
}