1 /*
   2  * Copyright (c) 2004, 2018, 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 then 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 
  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          * Environment variable settings ("null" if unset)
  56          */
  57         System.out.println("Env vars:");
  58         System.out.println("  LANG=" + System.getenv("LANG"));
  59         System.out.println("  LC_ALL=" + System.getenv("LC_ALL"));
  60         System.out.println("  LC_CTYPE=" + System.getenv("LC_CTYPE"));
  61 
  62         /*
  63          * Create sub-directory
  64          */
  65         File f = new File(bootDir);
  66         f.mkdir();
  67 
  68         /*
  69          * Create manifest file with Boot-Class-Path encoding the
  70          * sub-directory name.
  71          */
  72         try (FileOutputStream out = new FileOutputStream(manifestFile)) {
  73             out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
  74 
  75             byte[] premainBytes =
  76                 ("Premain-Class: " + premainClass + "\n").getBytes("UTF-8");
  77             out.write(premainBytes);
  78 
  79             out.write( "Boot-Class-Path: ".getBytes("UTF-8") );
  80 
  81             byte[] value = bootClassPath.getBytes("UTF-8");
  82             for (int i=0; i<value.length; i++) {
  83                 int v = (int)value[i];
  84                 if (v < 0) v += 256;
  85                 byte[] escaped =
  86                     ("%" + Integer.toHexString(v)).getBytes("UTF-8");
  87                 out.write(escaped);
  88             }
  89             out.write( "\n\n".getBytes("UTF-8") );
  90         }
  91 
  92         /*
  93          * Write the name of the boot dir to "boot.dir"
  94          */
  95         f = new File(workDir + fileSeparator + "boot.dir");
  96         try (FileOutputStream out = new FileOutputStream(f)) {
  97             out.write(bootDir.getBytes(defaultEncoding));
  98         }
  99     }
 100 
 101     /* ported from test/sun/tools/launcher/UnicodeTest.java */
 102 
 103     private static final String fileSeparator = System.getProperty("file.separator");
 104     private static final String osName = System.getProperty("os.name");
 105     private static final String defaultEncoding = Charset.defaultCharset().name();
 106 
 107     // language names taken from java.util.Locale.getDisplayLanguage for the respective language
 108     private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
 109     private static final String s_chinese = "\u4e2d\u6587";
 110     private static final String t_chinese = "\u4e2d\u6587";
 111     private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
 112     private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
 113     private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
 114     private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
 115     private static final String japanese = "\u65e5\u672c\u8a9e";
 116     private static final String korean = "\ud55c\uad6d\uc5b4";
 117     private static final String lithuanian = "Lietuvi\u0173";
 118     private static final String czech = "\u010de\u0161tina";
 119     private static final String turkish = "T\u00fcrk\u00e7e";
 120     private static final String spanish = "espa\u00f1ol";
 121     private static final String thai = "\u0e44\u0e17\u0e22";
 122     private static final String unicode = arabic + s_chinese + t_chinese
 123             + russian + hindi + greek + hebrew + japanese + korean
 124             + lithuanian + czech + turkish + spanish + thai;
 125 
 126     private static String suffix() {
 127 
 128         // Mapping from main platform encodings to language names
 129         // for Unix and Windows, respectively. Use empty suffix
 130         // for Windows encodings where OEM encoding differs.
 131         // Use null if encoding isn't used.
 132         String[][] names = {
 133             { "UTF-8",          unicode,        ""              },
 134             { "windows-1256",   null,           ""              },
 135             { "iso-8859-6",     arabic,         null            },
 136             { "GBK",            s_chinese,      s_chinese       },
 137             { "GB18030",        s_chinese,      s_chinese       },
 138             { "GB2312",         s_chinese,      null            },
 139             { "x-windows-950",  null,           t_chinese       },
 140             { "x-MS950-HKSCS",  null,           t_chinese       },
 141             { "x-euc-tw",       t_chinese,      null            },
 142             { "Big5",           t_chinese,      null            },
 143             { "Big5-HKSCS",     t_chinese,      null            },
 144             { "windows-1251",   null,           ""              },
 145             { "iso-8859-5",     russian,        null            },
 146             { "koi8-r",         russian,        null            },
 147             { "windows-1253",   null,           ""              },
 148             { "iso-8859-7",     greek,          null            },
 149             { "windows-1255",   null,           ""              },
 150             { "iso8859-8",      hebrew,         null            },
 151             { "windows-31j",    null,           japanese        },
 152             { "x-eucJP-Open",   japanese,       null            },
 153             { "x-EUC-JP-LINUX", japanese,       null            },
 154             { "x-pck",          japanese,       null            },
 155             { "x-windows-949",  null,           korean          },
 156             { "euc-kr",         korean,         null            },
 157             { "windows-1257",   null,           ""              },
 158             { "iso-8859-13",    lithuanian,     null            },
 159             { "windows-1250",   null,           ""              },
 160             { "iso-8859-2",     czech,          null            },
 161             { "windows-1254",   null,           ""              },
 162             { "iso-8859-9",     turkish,        null            },
 163             { "windows-1252",   null,           ""              },
 164             { "iso-8859-1",     spanish,        null            },
 165             { "iso-8859-15",    spanish,        null            },
 166             { "x-windows-874",  null,           thai            },
 167             { "tis-620",        thai,           null            },
 168         };
 169 
 170         int column;
 171         if (osName.startsWith("Windows")) {
 172             column = 2;
 173         } else {
 174             column = 1;
 175         }
 176         for (int i = 0; i < names.length; i++) {
 177              if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
 178                  return names[i][column];
 179              }
 180          }
 181          return "";
 182     }
 183 }