jdk/test/java/lang/instrument/BootClassPath/Setup.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8543 e5ec12a932da
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8543
diff changeset
     2
 * Copyright (c) 2004, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
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
 * Used by BootClassPath.sh.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Given a "work directory" this class creates a sub-directory with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * name that uses locale specific characters. It the creates a jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * manifest file in the work directory with a Boot-Class-Path that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * encodes the created sub-directory. Finally it creates a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * "boot.dir" in the work directory with the name of the sub-directory.
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.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Setup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        if (args.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            System.err.println("Usage: java Setup <work-dir> <premain-class>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String workDir = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String premainClass = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        String manifestFile = workDir + fileSeparator + "MANIFEST.MF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String bootClassPath = "boot" + suffix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String bootDir = workDir + fileSeparator + bootClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         * Create sub-directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        File f = new File(bootDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        f.mkdir();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         * Create manifest file with Boot-Class-Path encoding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * sub-directory name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         */
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    65
        try (FileOutputStream out = new FileOutputStream(manifestFile)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    66
            out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    68
            byte[] premainBytes =
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    69
                ("Premain-Class: " + premainClass + "\n").getBytes("UTF-8");
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    70
            out.write(premainBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    72
            out.write( "Boot-Class-Path: ".getBytes("UTF-8") );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    74
            byte[] value = bootClassPath.getBytes("UTF-8");
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    75
            for (int i=0; i<value.length; i++) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    76
                int v = (int)value[i];
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    77
                if (v < 0) v += 256;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    78
                byte[] escaped =
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    79
                    ("%" + Integer.toHexString(v)).getBytes("UTF-8");
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    80
                out.write(escaped);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    81
            }
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    82
            out.write( "\n\n".getBytes("UTF-8") );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * Write the name of the boot dir to "boot.dir"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        f = new File(workDir + fileSeparator + "boot.dir");
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    89
        try (FileOutputStream out = new FileOutputStream(f)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    90
            out.write(bootDir.getBytes(defaultEncoding));
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 5506
diff changeset
    91
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /* ported from test/sun/tools/launcher/UnicodeTest.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final String fileSeparator = System.getProperty("file.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final String defaultEncoding = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // language names taken from java.util.Locale.getDisplayLanguage for the respective language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final String s_chinese = "\u4e2d\u6587";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final String t_chinese = "\u4e2d\u6587";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final String japanese = "\u65e5\u672c\u8a9e";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final String korean = "\ud55c\uad6d\uc5b4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final String lithuanian = "Lietuvi\u0173";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final String czech = "\u010de\u0161tina";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final String turkish = "T\u00fcrk\u00e7e";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final String spanish = "espa\u00f1ol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final String thai = "\u0e44\u0e17\u0e22";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static final String unicode = arabic + s_chinese + t_chinese
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            + russian + hindi + greek + hebrew + japanese + korean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            + lithuanian + czech + turkish + spanish + thai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static String suffix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // Mapping from main platform encodings to language names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // for Unix and Windows, respectively. Use empty suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // for Windows encodings where OEM encoding differs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // Use null if encoding isn't used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        String[][] names = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            { "UTF-8",          unicode,        ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            { "windows-1256",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            { "iso-8859-6",     arabic,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            { "GBK",            s_chinese,      s_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            { "GB18030",        s_chinese,      s_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            { "GB2312",         s_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            { "x-windows-950",  null,           t_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            { "x-MS950-HKSCS",  null,           t_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            { "x-euc-tw",       t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            { "Big5",           t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            { "Big5-HKSCS",     t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            { "windows-1251",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            { "iso-8859-5",     russian,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            { "koi8-r",         russian,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            { "windows-1253",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            { "iso-8859-7",     greek,          null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            { "windows-1255",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            { "iso8859-8",      hebrew,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            { "windows-31j",    null,           japanese        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            { "x-eucJP-Open",   japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            { "x-EUC-JP-LINUX", japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            { "x-pck",          japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            { "x-windows-949",  null,           korean          },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            { "euc-kr",         korean,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            { "windows-1257",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            { "iso-8859-13",    lithuanian,     null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            { "windows-1250",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            { "iso-8859-2",     czech,          null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            { "windows-1254",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            { "iso-8859-9",     turkish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            { "windows-1252",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            { "iso-8859-1",     spanish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            { "iso-8859-15",    spanish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            { "x-windows-874",  null,           thai            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            { "tis-620",        thai,           null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (osName.startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            column = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            column = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
             if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                 return names[i][column];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}