File: Setup.java

package info (click to toggle)
openjdk-24 24.0.2%2B12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 831,900 kB
  • sloc: java: 5,677,020; cpp: 1,323,154; xml: 1,320,524; ansic: 486,889; asm: 405,131; objc: 21,025; sh: 15,221; javascript: 11,049; python: 8,222; makefile: 2,504; perl: 357; awk: 351; sed: 172; pascal: 103; exp: 54; jsp: 24; csh: 3
file content (192 lines) | stat: -rw-r--r-- 8,500 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 *
 *
 * Used by BootClassPath.sh.
 *
 * Given a "work directory" this class creates a sub-directory with a
 * name that uses locale specific characters. It then creates a jar
 * manifest file in the work directory with a Boot-Class-Path that
 * encodes the created sub-directory. Finally it creates a file
 * "boot.dir" in the work directory with the name of the sub-directory.
 */
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.Charset;

public class Setup {

    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.err.println("Usage: java Setup <work-dir> <premain-class>");
            return;
        }
        String workDir = args[0];
        String premainClass = args[1];
        boolean isCygwin = false;
        if (args.length == 3 && args[2].equals("CYGWIN")) {
            isCygwin = true;
        }

        String manifestFile = workDir + fileSeparator + "MANIFEST.MF";
        String bootClassPath = "boot" + suffix();

        String bootDir = workDir + fileSeparator + bootClassPath;

        /*
         * Environment variable settings ("null" if unset)
         */
        System.out.println("Env vars:");
        System.out.println("  LANG=" + System.getenv("LANG"));
        System.out.println("  LC_ALL=" + System.getenv("LC_ALL"));
        System.out.println("  LC_CTYPE=" + System.getenv("LC_CTYPE"));

        /*
         * Create sub-directory
         */
        File f = new File(bootDir);
        f.mkdir();

        /*
         * Create manifest file with Boot-Class-Path encoding the
         * sub-directory name.
         */
        try (FileOutputStream out = new FileOutputStream(manifestFile)) {
            out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));

            byte[] premainBytes =
                ("Premain-Class: " + premainClass + "\n").getBytes("UTF-8");
            out.write(premainBytes);

            out.write( "Boot-Class-Path: ".getBytes("UTF-8") );

            byte[] value = bootClassPath.getBytes("UTF-8");
            for (int i=0; i<value.length; i++) {
                int v = (int)value[i];
                if (v < 0) v += 256;
                byte[] escaped =
                    ("%" + Integer.toHexString(v)).getBytes("UTF-8");
                out.write(escaped);
            }
            out.write( "\n\n".getBytes("UTF-8") );
        }

        /*
         * Write the name of the boot dir to "boot.dir"
         */
        f = new File(workDir + fileSeparator + "boot.dir");
        try (FileOutputStream out = new FileOutputStream(f)) {
            if (osName.startsWith("Windows") && isCygwin) {
                out.write(bootDir.getBytes("UTF-8"));
            }
            else {
                out.write(bootDir.getBytes(filePathEncoding));
            }
        }
    }

    /* ported from test/sun/tools/launcher/UnicodeTest.java */

    private static final String fileSeparator = System.getProperty("file.separator");
    private static final String osName = System.getProperty("os.name");
    private static final String filePathEncoding = System.getProperty("sun.jnu.encoding");

    // language names taken from java.util.Locale.getDisplayLanguage for the respective language
    private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
    private static final String s_chinese = "\u4e2d\u6587";
    private static final String t_chinese = "\u4e2d\u6587";
    private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
    private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
    private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
    private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
    private static final String japanese = "\u65e5\u672c\u8a9e";
    private static final String korean = "\ud55c\uad6d\uc5b4";
    private static final String lithuanian = "Lietuvi\u0173";
    private static final String czech = "\u010de\u0161tina";
    private static final String turkish = "T\u00fcrk\u00e7e";
    private static final String spanish = "espa\u00f1ol";
    private static final String thai = "\u0e44\u0e17\u0e22";
    private static final String unicode = arabic + s_chinese + t_chinese
            + russian + hindi + greek + hebrew + japanese + korean
            + lithuanian + czech + turkish + spanish + thai;

    private static String suffix() {

        // Mapping from main platform encodings to language names
        // for Unix and Windows, respectively. Use empty suffix
        // for Windows encodings where OEM encoding differs.
        // Use null if encoding isn't used.
        String[][] names = {
            { "UTF-8",          unicode,        ""              },
            { "Cp1256",         null,           ""              },
            { "iso-8859-6",     arabic,         null            },
            { "GBK",            s_chinese,      s_chinese       },
            { "GB18030",        s_chinese,      s_chinese       },
            { "GB2312",         s_chinese,      null            },
            { "MS950",          null,           t_chinese       },
            { "MS950_HKSCS_XP", null,           t_chinese       },
            { "EUC-TW",         t_chinese,      null            },
            { "Big5",           t_chinese,      null            },
            { "Big5-HKSCS",     t_chinese,      null            },
            { "windows-1251",   null,           ""              },
            { "iso-8859-5",     russian,        null            },
            { "koi8-r",         russian,        null            },
            { "Cp1253",         null,           ""              },
            { "iso-8859-7",     greek,          null            },
            { "Cp1255",         null,           ""              },
            { "iso-8859-8",     hebrew,         null            },
            { "MS932",          null,           japanese        },
            { "x-eucJP-Open",   japanese,       null            },
            { "EUC-JP-LINUX",   japanese,       null            },
            { "x-pck",          japanese,       null            },
            { "MS949",          null,           korean          },
            { "euc-kr",         korean,         null            },
            { "Cp1257",         null,           ""              },
            { "iso-8859-13",    lithuanian,     null            },
            { "Cp1250",         null,           ""              },
            { "iso-8859-2",     czech,          null            },
            { "Cp1254",         null,           ""              },
            { "iso-8859-9",     turkish,        null            },
            { "Cp1252",         null,           ""              },
            { "iso-8859-1",     spanish,        null            },
            { "iso-8859-15",    spanish,        null            },
            { "MS874",          null,           thai            },
            { "tis-620",        thai,           null            },
        };

        int column;
        if (osName.startsWith("Windows")) {
            column = 2;
        } else {
            column = 1;
        }
        for (int i = 0; i < names.length; i++) {
             if (names[i][0].equalsIgnoreCase(filePathEncoding)) {
                 return names[i][column];
             }
         }
         return "";
    }
}