jdk/test/java/lang/instrument/BootClassPath/Setup.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        FileOutputStream out = new FileOutputStream(manifestFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        byte[] premainBytes = ("Premain-Class: " + premainClass + "\n").getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        out.write(premainBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        out.write( "Boot-Class-Path: ".getBytes("UTF-8") );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        byte[] value = bootClassPath.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        for (int i=0; i<value.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            int v = (int)value[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (v < 0) v += 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            byte[] escaped =  ("%" + Integer.toHexString(v)).getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            out.write(escaped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        out.write( "\n\n".getBytes("UTF-8") );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
         * Write the name of the boot dir to "boot.dir"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        f = new File(workDir + fileSeparator + "boot.dir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        out = new FileOutputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        out.write(bootDir.getBytes(defaultEncoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /* ported from test/sun/tools/launcher/UnicodeTest.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final String fileSeparator = System.getProperty("file.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final String defaultEncoding = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // language names taken from java.util.Locale.getDisplayLanguage for the respective language
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final String s_chinese = "\u4e2d\u6587";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final String t_chinese = "\u4e2d\u6587";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final String japanese = "\u65e5\u672c\u8a9e";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final String korean = "\ud55c\uad6d\uc5b4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final String lithuanian = "Lietuvi\u0173";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final String czech = "\u010de\u0161tina";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final String turkish = "T\u00fcrk\u00e7e";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final String spanish = "espa\u00f1ol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final String thai = "\u0e44\u0e17\u0e22";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final String unicode = arabic + s_chinese + t_chinese
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            + russian + hindi + greek + hebrew + japanese + korean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            + lithuanian + czech + turkish + spanish + thai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static String suffix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // Mapping from main platform encodings to language names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // for Unix and Windows, respectively. Use empty suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // for Windows encodings where OEM encoding differs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // Use null if encoding isn't used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String[][] names = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            { "UTF-8",          unicode,        ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            { "windows-1256",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            { "iso-8859-6",     arabic,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            { "GBK",            s_chinese,      s_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            { "GB18030",        s_chinese,      s_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            { "GB2312",         s_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            { "x-windows-950",  null,           t_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            { "x-MS950-HKSCS",  null,           t_chinese       },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            { "x-euc-tw",       t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            { "Big5",           t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            { "Big5-HKSCS",     t_chinese,      null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            { "windows-1251",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            { "iso-8859-5",     russian,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            { "koi8-r",         russian,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            { "windows-1253",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            { "iso-8859-7",     greek,          null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            { "windows-1255",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            { "iso8859-8",      hebrew,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            { "windows-31j",    null,           japanese        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            { "x-eucJP-Open",   japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            { "x-EUC-JP-LINUX", japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            { "x-pck",          japanese,       null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            { "x-windows-949",  null,           korean          },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            { "euc-kr",         korean,         null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            { "windows-1257",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            { "iso-8859-13",    lithuanian,     null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            { "windows-1250",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            { "iso-8859-2",     czech,          null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            { "windows-1254",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            { "iso-8859-9",     turkish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            { "windows-1252",   null,           ""              },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            { "iso-8859-1",     spanish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            { "iso-8859-15",    spanish,        null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            { "x-windows-874",  null,           thai            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            { "tis-620",        thai,           null            },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (osName.startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            column = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            column = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                 return names[i][column];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}