1 19 20 package org.netbeans.core.startup; 21 22 import java.io.File ; 23 import java.io.PrintWriter ; 24 import java.util.Locale ; 25 import java.util.MissingResourceException ; 26 import org.netbeans.CLIHandler; 27 import org.netbeans.TopSecurityManager; 28 import org.openide.filesystems.FileUtil; 29 import org.openide.util.NbBundle; 30 31 35 public class CLIOptions extends CLIHandler { 36 37 38 static final String DIR_MODULES = "modules"; 40 static boolean defaultsLoaded = false; 42 44 protected static boolean noLogging = false; 45 46 47 private static boolean noSplash; 48 49 50 protected static Class uiClass; 51 53 private static int uiFontSize = 0; 54 55 56 private static String homeDir; 57 58 private static String userDir; 59 60 private static String systemDir; 61 62 65 public CLIOptions() { 66 super(WHEN_BOOT); 67 } 68 69 protected int cli(Args arguments) { 70 return cli(arguments.getArguments()); 71 } 72 73 74 76 public static boolean isGui () { 77 return "true".equals (System.getProperty ("org.openide.TopManager.GUI")); } 79 80 private static boolean isOption (String value, String optionName) { 81 if (value == null) return false; 82 83 if (value.startsWith ("--")) { 84 return value.substring (2).equals (optionName); 85 } else if (value.startsWith ("-")) { 86 return value.substring (1).equals (optionName); 87 } 88 return false; 89 } 90 91 public final int cli(String [] args) { 92 for (int i = 0; i < args.length; i++) { 94 if (args[i] == null) { 95 continue; 96 } 97 boolean used = true; 98 if (isOption (args[i], "nogui")) { System.getProperties().put("org.openide.TopManager", "org.netbeans.core.NonGui"); System.setProperty ("org.openide.TopManager.GUI", "false"); } else if (isOption (args[i], "nosplash")) { noSplash = true; 103 } else if (isOption (args[i], "noinfo")) { } else if (isOption (args[i], "nologging")) { noLogging = true; 107 } else if (isOption (args[i], "userdir")) { args[i] = null; 109 try { 110 System.setProperty ("netbeans.user", args[++i]); 111 } catch(ArrayIndexOutOfBoundsException e) { 112 System.err.println(getString("ERR_UserDirExpected")); 113 return 2; 114 } 115 } else if (isOption (args[i], "ui") || isOption (args[i], "laf")) { args[i] = null; 117 try { 118 String ui = args[++i]; 119 uiClass = Class.forName(ui); 120 } catch(ArrayIndexOutOfBoundsException e) { 121 System.err.println(getString("ERR_UIExpected")); 122 return 2; 123 } catch (ClassNotFoundException e2) { 124 System.err.println(getString("ERR_UINotFound")); 125 } 126 } else if (isOption (args[i], "fontsize")) { args[i] = null; 128 try { 129 uiFontSize = Integer.parseInt(args[++i]); 130 } catch(ArrayIndexOutOfBoundsException e) { 131 System.err.println(getString("ERR_FontSizeExpected")); 132 return 2; 133 } catch (NumberFormatException e2) { 134 System.err.println(getString("ERR_BadFontSize")); 135 return 1; 136 } 137 } else if (isOption (args[i], "locale")) { args[i] = null; 139 String localeParam = args[++i]; 140 String language; 141 String country = ""; String variant = ""; int index1 = localeParam.indexOf(":"); if (index1 == -1) 145 language = localeParam; 146 else { 147 language = localeParam.substring(0, index1); 148 int index2 = localeParam.indexOf(":", index1+1); if (index2 != -1) { 150 country = localeParam.substring(index1+1, index2); 151 variant = localeParam.substring(index2+1); 152 } 153 else 154 country = localeParam.substring(index1+1); 155 } 156 Locale.setDefault(new Locale (language, country, variant)); 157 } else if (isOption (args[i], "branding")) { args[i] = null; 159 if (++i == args.length) { 160 System.err.println(getString("ERR_BrandingNeedsArgument")); 161 return 2; 162 } 163 String branding = args[i]; 164 if (branding.equals("-")) branding = null; try { 166 NbBundle.setBranding(branding); 167 } catch (IllegalArgumentException iae) { 168 iae.printStackTrace(); 169 return 1; 170 } 171 } else { 172 used = false; 173 } 174 if (used) { 175 args[i] = null; 176 } 177 } 178 179 return 0; 180 } 181 182 184 public static void initialize() { 185 TopLogging.initialize(); 186 StartLog.logProgress("TopLogging initialized"); } 188 189 protected void usage(PrintWriter w) { 190 w.println("Core options:"); 191 w.println(" --laf <LaF classname> use given LookAndFeel class instead of the default"); 192 w.println(" --fontsize <size> set the base font size of the user interface, in points"); 193 w.println(" --locale <language[:country[:variant]]> use specified locale"); 194 w.println(" --userdir <path> use specified directory to store user settings"); 195 w.println(""); 196 } 202 203 private static String getString (String key) { 204 return NbBundle.getMessage (CLIOptions.class, key); 205 } 206 207 211 212 214 public static String getLogDir () { 215 return new File (new File (getUserDir (), "var"), "log").toString (); 216 } 217 218 220 static final void clearForTests () { 221 homeDir = null; 222 userDir = null; 223 } 224 225 226 public static String getHomeDir () { 227 if (homeDir == null) { 228 homeDir = System.getProperty ("netbeans.home"); 229 } 230 return homeDir; 231 } 232 233 234 public static String getUserDir () { 235 if (userDir == null) { 236 userDir = System.getProperty ("netbeans.user"); 237 238 if ("memory".equals (userDir)) { return "memory"; } 241 242 if (userDir == null) { 243 if (homeDir == null) { 244 return "memory"; } 246 System.err.println(NbBundle.getMessage(CLIOptions.class, "ERR_no_user_directory")); 247 Thread.dumpStack(); TopSecurityManager.exit(1); 249 } 250 251 File userDirF = FileUtil.normalizeFile(new File (userDir)); 253 254 String homeDir = getHomeDir(); 255 if (homeDir != null) { 256 File homeDirF = FileUtil.normalizeFile(new File (homeDir)); 257 if ((userDirF.getAbsolutePath() + File.separatorChar).startsWith(homeDirF.getParentFile().getAbsolutePath() + File.separatorChar)) { 258 System.err.println(NbBundle.getMessage(CLIOptions.class, "ERR_user_directory_is_inside_home")); 259 TopSecurityManager.exit(1); 260 } 261 } 262 263 userDir = userDirF.getPath(); 264 System.setProperty("netbeans.user", userDir); 266 File systemDirFile = new File (userDirF, NbRepository.SYSTEM_FOLDER); 267 makedir (systemDirFile); 268 systemDir = systemDirFile.getAbsolutePath (); 269 makedir(new File (userDirF, DIR_MODULES)); } 271 return userDir; 272 } 273 274 private static void makedir (File f) { 275 if (f.isFile ()) { 276 Object [] arg = new Object [] {f}; 277 System.err.println (NbBundle.getMessage (CLIOptions.class, "CTL_CannotCreate_text", arg)); 278 org.netbeans.TopSecurityManager.exit (6); 279 } 280 if (! f.exists ()) { 281 if (! f.mkdirs ()) { 282 Object [] arg = new Object [] {f}; 283 System.err.println (NbBundle.getMessage (CLIOptions.class, "CTL_CannotCreateSysDir_text", arg)); 284 org.netbeans.TopSecurityManager.exit (7); 285 } 286 } 287 } 288 289 291 protected static String getSystemDir () { 292 getUserDir (); 293 return systemDir; 294 } 295 296 300 private static void initDefaults() { 301 if (!defaultsLoaded) { 302 if (CLIOptions.uiFontSize == 0) { 303 String key = ""; 304 try { 305 key = NbBundle.getMessage (Main.class, "CTL_globalFontSize"); } catch (MissingResourceException mre) { 307 } 309 if (key.length() > 0) { 310 try { 311 CLIOptions.uiFontSize = Integer.parseInt(key); 312 } catch (NumberFormatException exc) { 313 } 315 } 316 } 317 if (!noSplash) { 318 String value = NbBundle.getMessage(CLIOptions.class, "SplashOnByDefault"); 320 noSplash = !Boolean.parseBoolean(value); 321 } 322 323 defaultsLoaded = true; 324 } 325 } 326 public static int getFontSize () { 327 initDefaults(); 328 return uiFontSize; 329 } 330 331 static boolean isNoSplash() { 332 initDefaults(); 333 return noSplash; 334 } 335 } 336 | Popular Tags |