KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > CLIOptions


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 import java.io.File JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import org.netbeans.CLIHandler;
27 import org.netbeans.TopSecurityManager;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.util.NbBundle;
30
31 /**
32  * Handler for core options.
33  * @author Jaroslav Tulach
34  */

35 public class CLIOptions extends CLIHandler {
36
37     /** directory for modules */
38     static final String JavaDoc DIR_MODULES = "modules"; // NOI18N
39

40     static boolean defaultsLoaded = false; // package private for testing
41

42     /** The flag whether to create the log - can be set via -nologging
43     * command line option */

44     protected static boolean noLogging = false;
45
46     /** The flag whether to show the Splash screen on the startup */
47     private static boolean noSplash;
48     
49     /* The class of the UIManager to be used for netbeans - can be set by command-line argument -ui <class name> */
50     protected static Class JavaDoc uiClass;
51     /* The size of the fonts in the UI - 0 pt, the default value is set in NbTheme (for Metal L&F), for other L&Fs is set
52        in the class Main. The value can be changed in Themes.xml in system directory or by command-line argument -fontsize <size> */

53     private static int uiFontSize = 0;
54
55     /** The netbeans home dir - acquired from property netbeans.home */
56     private static String JavaDoc homeDir;
57     /** The netbeans user dir - acquired from property netbeans.user */
58     private static String JavaDoc userDir;
59     /** The netbeans system dir - ${netbeans.user}/system */
60     private static String JavaDoc systemDir;
61
62     /**
63      * Create a default handler.
64      */

65     public CLIOptions() {
66         super(WHEN_BOOT);
67     }
68     
69     protected int cli(Args arguments) {
70         return cli(arguments.getArguments());
71     }
72     
73     
74     /** Checks whether we are supposed to use GUI features or not.
75      */

76     public static boolean isGui () {
77         return "true".equals (System.getProperty ("org.openide.TopManager.GUI")); // NOI18N
78
}
79     
80     private static boolean isOption (String JavaDoc value, String JavaDoc 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 JavaDoc[] args) {
92         // let's go through the command line
93
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")) { // NOI18N
99
System.getProperties().put("org.openide.TopManager", "org.netbeans.core.NonGui"); // NOI18N
100
System.setProperty ("org.openide.TopManager.GUI", "false"); // NOI18N
101
} else if (isOption (args[i], "nosplash")) { // NOI18N
102
noSplash = true;
103             } else if (isOption (args[i], "noinfo")) { // NOI18N
104
// obsolete switch, ignore
105
} else if (isOption (args[i], "nologging")) { // NOI18N
106
noLogging = true;
107             } else if (isOption (args[i], "userdir")) { // NOI18N
108
args[i] = null;
109                 try {
110                     System.setProperty ("netbeans.user", args[++i]);
111                 } catch(ArrayIndexOutOfBoundsException JavaDoc e) {
112                     System.err.println(getString("ERR_UserDirExpected"));
113                     return 2;
114                 }
115             } else if (isOption (args[i], "ui") || isOption (args[i], "laf")) { // NOI18N
116
args[i] = null;
117                 try {
118                     String JavaDoc ui = args[++i];
119                     uiClass = Class.forName(ui);
120                 } catch(ArrayIndexOutOfBoundsException JavaDoc e) {
121                     System.err.println(getString("ERR_UIExpected"));
122                     return 2;
123                 } catch (ClassNotFoundException JavaDoc e2) {
124                     System.err.println(getString("ERR_UINotFound"));
125                 }
126             } else if (isOption (args[i], "fontsize")) { // NOI18N
127
args[i] = null;
128                 try {
129                     uiFontSize = Integer.parseInt(args[++i]);
130                 } catch(ArrayIndexOutOfBoundsException JavaDoc e) {
131                     System.err.println(getString("ERR_FontSizeExpected"));
132                     return 2;
133                 } catch (NumberFormatException JavaDoc e2) {
134                     System.err.println(getString("ERR_BadFontSize"));
135                     return 1;
136                 }
137             } else if (isOption (args[i], "locale")) { // NOI18N
138
args[i] = null;
139                 String JavaDoc localeParam = args[++i];
140                 String JavaDoc language;
141                 String JavaDoc country = ""; // NOI18N
142
String JavaDoc variant = ""; // NOI18N
143
int index1 = localeParam.indexOf(":"); // NOI18N
144
if (index1 == -1)
145                     language = localeParam;
146                 else {
147                     language = localeParam.substring(0, index1);
148                     int index2 = localeParam.indexOf(":", index1+1); // NOI18N
149
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 JavaDoc(language, country, variant));
157             } else if (isOption (args[i], "branding")) { // NOI18N
158
args[i] = null;
159                 if (++i == args.length) {
160                     System.err.println(getString("ERR_BrandingNeedsArgument"));
161                     return 2;
162                 }
163                 String JavaDoc branding = args[i];
164                 if (branding.equals("-")) branding = null; // NOI18N
165
try {
166                     NbBundle.setBranding(branding);
167                 } catch (IllegalArgumentException JavaDoc 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     /** Initializes logging etc.
183      */

184     public static void initialize() {
185         TopLogging.initialize();
186         StartLog.logProgress("TopLogging initialized"); // NOI18N
187
}
188     
189     protected void usage(PrintWriter JavaDoc 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 // \ --branding <token> use specified branding (- for default)
197
//
198
// \ --nologging do not create the log file\n\
199
// \ --nosplash do not show the splash screen\n\
200
// \ --nogui just start up internals, do not show GUI
201
}
202     
203     private static String JavaDoc getString (String JavaDoc key) {
204         return NbBundle.getMessage (CLIOptions.class, key);
205     }
206     
207     //
208
// Directory functions
209
//
210

211
212     /** Directory to place logs into logging.
213     */

214     public static String JavaDoc getLogDir () {
215         return new File JavaDoc (new File JavaDoc (getUserDir (), "var"), "log").toString ();
216     }
217
218     /** Tests need to clear some static variables.
219      */

220     static final void clearForTests () {
221         homeDir = null;
222         userDir = null;
223     }
224
225     /** Getter for home directory. */
226     public static String JavaDoc getHomeDir () {
227         if (homeDir == null) {
228             homeDir = System.getProperty ("netbeans.home");
229         }
230         return homeDir;
231     }
232
233     /** Getter for user home directory. */
234     public static String JavaDoc getUserDir () {
235         if (userDir == null) {
236             userDir = System.getProperty ("netbeans.user");
237             
238             if ("memory".equals (userDir)) { // NOI18N
239
return "memory"; // NOI18N
240
}
241             
242             if (userDir == null) {
243                 if (homeDir == null) {
244                     return "memory"; // NOI18N
245
}
246                 System.err.println(NbBundle.getMessage(CLIOptions.class, "ERR_no_user_directory"));
247                 Thread.dumpStack(); // likely to happen from misbehaving unit tests, etc.
248
TopSecurityManager.exit(1);
249             }
250
251             // #11735, #21085: avoid relative user dirs, or ../ seqs
252
File JavaDoc userDirF = FileUtil.normalizeFile(new File JavaDoc(userDir));
253
254             String JavaDoc homeDir = getHomeDir();
255             if (homeDir != null) {
256                 File JavaDoc homeDirF = FileUtil.normalizeFile(new File JavaDoc(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); // NOI18N
265

266             File JavaDoc systemDirFile = new File JavaDoc(userDirF, NbRepository.SYSTEM_FOLDER);
267             makedir (systemDirFile);
268             systemDir = systemDirFile.getAbsolutePath ();
269             makedir(new File JavaDoc(userDirF, DIR_MODULES)); // NOI18N
270
}
271         return userDir;
272     }
273
274     private static void makedir (File JavaDoc f) {
275         if (f.isFile ()) {
276             Object JavaDoc[] arg = new Object JavaDoc[] {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 JavaDoc[] arg = new Object JavaDoc[] {f};
283                 System.err.println (NbBundle.getMessage (CLIOptions.class, "CTL_CannotCreateSysDir_text", arg));
284                 org.netbeans.TopSecurityManager.exit (7);
285             }
286         }
287     }
288     
289     /** System directory getter.
290     */

291     protected static String JavaDoc getSystemDir () {
292         getUserDir ();
293         return systemDir;
294     }
295
296     //
297
// other getters
298
//
299

300     private static void initDefaults() {
301     if (!defaultsLoaded) {
302         if (CLIOptions.uiFontSize == 0) {
303         String JavaDoc key = "";
304         try {
305             key = NbBundle.getMessage (Main.class, "CTL_globalFontSize"); //NOI18N
306
} catch (MissingResourceException JavaDoc mre) {
307             //Key not found, nothing to do
308
}
309         if (key.length() > 0) {
310             try {
311             CLIOptions.uiFontSize = Integer.parseInt(key);
312             } catch (NumberFormatException JavaDoc exc) {
313             //Incorrect value, nothing to do
314
}
315         }
316         }
317         if (!noSplash) {
318         // was not overriden from command line - read brandable setting
319
String JavaDoc 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