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