1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  package com.gridsystems.config.app;
25  
26  import java.io.BufferedReader;
27  import java.io.IOException;
28  import java.io.InputStreamReader;
29  import java.lang.reflect.Method;
30  import java.net.URL;
31  import java.text.MessageFormat;
32  import java.util.Collection;
33  import java.util.Enumeration;
34  import java.util.HashMap;
35  import java.util.Locale;
36  import java.util.Map;
37  import java.util.ResourceBundle;
38  import java.util.StringTokenizer;
39  
40  import javax.swing.UIManager;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  import com.gridsystems.config.Configurator;
46  import com.gridsystems.config.modules.jvm.JVMConfigurator;
47  import com.gridsystems.config.tools.console.ConsoleTools;
48  import com.gridsystems.utils.AWTUtils;
49  
50  
51  
52  
53  
54  
55  
56  
57  public abstract class UI {
58  
59    
60  
61  
62    private static final String[] HELP_TEXT = {
63      "Valid options:",
64      "-swing      Executes in GUI mode",
65      "-console    Executes in text mode",
66      "-silent     Executes with no user interface",
67      "-locale:xx  Sets the locale to xx (en, es_ES, ...). ",
68      "            Defaults to the user environment settings",
69      "-username:xx The name of the user that will execute the configured server",
70      "",
71      "Text mode flags:",
72      "-ansi       If the terminal supports ANSI escape sequences",
73      "-noacs      If line characters are not correctly displayed",
74      "",
75      "Swing mode flags:",
76      "-laf:x      Sets the look and feel to use",
77      "            Valid values are: system, cross, metal, gtk, motif and windows",
78      "",
79      "Other flags:",
80      "-expert     Turns on the advanced configuration mode. Ignored in silent mode"
81    };
82  
83    
84  
85  
86    private static Log log = LogFactory.getLog(UI.class);
87  
88    
89  
90  
91    private static ResourceBundle bundle = ResourceBundle.getBundle("config");
92  
93    
94  
95  
96    public static final int NONE = 0;
97  
98    
99  
100 
101   public static final int SWING = 1;
102 
103   
104 
105 
106   public static final int CONSOLE = 2;
107 
108   
109 
110 
111   private static final String[] CLASS_NAMES = {
112     "com.gridsystems.config.app.NullUI",
113     "com.gridsystems.config.app.SwingUI",
114     "com.gridsystems.config.app.ConsoleUI"
115   };
116 
117   
118 
119 
120   private static boolean expertMode = false;
121 
122   
123 
124 
125   protected static Configurator[] configurators = null;
126 
127   
128 
129 
130   public UI() { }
131 
132   
133 
134 
135 
136   public abstract boolean execute();
137 
138   
139 
140 
141 
142 
143 
144 
145 
146 
147   public static String getString(String key) {
148     return bundle.getString(key);
149   }
150 
151   
152 
153 
154 
155 
156   public static boolean isExpertMode() {
157     return expertMode;
158   }
159 
160   
161 
162 
163 
164 
165 
166 
167 
168 
169   public static Configurator[] getConfigurators() {
170     if (configurators == null) {
171       JVMConfigurator jvmc = JVMConfigurator.getInstance();
172 
173       HashMap<String, Configurator> map = new HashMap<String, Configurator>();
174       ClassLoader cl = Thread.currentThread().getContextClassLoader();
175       try {
176         
177         register(map, jvmc);
178 
179         
180         jvmc.getModel().load();
181 
182         Enumeration enumeration = cl.getResources("configurators.txt");
183         while (enumeration.hasMoreElements()) {
184           URL url = (URL)enumeration.nextElement();
185 
186           BufferedReader reader = null;
187           try {
188             reader = new BufferedReader(new InputStreamReader(url.openStream()));
189             String line = reader.readLine();
190             while (line != null) {
191               line = line.trim();
192               if (line.charAt(0) != '#') {
193                 Configurator config = newConfigurator(line);
194                 if (config != null) {
195                   config.getModel().load();
196                   register(map, config);
197                 }
198               }
199               line = reader.readLine();
200             }
201           } finally {
202             ReflectionTools.close(reader);
203           }
204         }
205       } catch (IOException e) { }
206 
207       Collection<Configurator> col = map.values();
208       Configurator[] configs = new Configurator[col.size()];
209       col.toArray(configs);
210 
211       configurators = configs;
212     }
213     return configurators;
214   }
215 
216   
217 
218 
219   private static void showHelp() {
220     for (int i = 0; i < HELP_TEXT.length; i++) {
221       System.out.println(HELP_TEXT[i]);
222     }
223   }
224   
225 
226 
227 
228 
229 
230 
231 
232   private static int prepareMode(int mode, boolean ansi, boolean acs) {
233     
234     if (mode == Configurator.MODE_SWING && !AWTUtils.existDisplay()) {
235       mode = Configurator.MODE_CONSOLE;
236     }
237 
238     if (mode == Configurator.MODE_CONSOLE) {
239       ConsoleTools.setup(ansi, acs);
240     }
241 
242     return mode;
243   }
244 
245   
246 
247 
248 
249 
250   private static void parseLocale(String s) {
251     String locale = s.substring("-locale:".length());
252     setDefaultLocale(locale);
253   }
254 
255   
256 
257 
258 
259 
260 
261 
262   public static void main(String[] args) throws Exception {
263     int mode = Configurator.MODE_SILENT;
264     boolean ansi = false;
265     boolean acs = true;
266 
267     for (int i = 0; i < args.length; i++) {
268       if ("-help".equals(args[i])) {
269         showHelp();
270         System.exit(0);
271       } else if ("-expert".equals(args[i])) {
272         expertMode = true;
273       } else if ("-swing".equals(args[i])) {
274         mode = Configurator.MODE_SWING;
275       } else if ("-console".equals(args[i])) {
276         mode = Configurator.MODE_CONSOLE;
277       } else if ("-silent".equals(args[i])) {
278         mode = Configurator.MODE_SILENT;
279       } else if ("-ansi".equals(args[i])) {
280         ansi = true;
281       } else if ("-noacs".equals(args[i])) {
282         acs = false;
283       } else if (args[i].startsWith("-locale:")) {
284         
285         parseLocale(args[i]);
286       } else if (args[i].startsWith("-laf:")) {
287         parseLaf(args[i].substring("-laf:".length()));
288       } else if (args[i].startsWith("-username:")) {
289         Configurator.setUsername(args[i].substring("-username:".length()));
290       } else {
291         log.warn("Invalid " + args[i] + " option. Ignored");
292       }
293     }
294 
295     mode = prepareMode(mode, ansi, acs);
296 
297     boolean quitConfigurator = false;
298     do {
299       boolean correctExecution = execute(mode);
300       quitConfigurator = quitConfigurator(correctExecution);
301     } while (!quitConfigurator);
302   }
303 
304   
305 
306 
307 
308   private static boolean quitConfigurator(boolean correctExecution) {
309     if (correctExecution) {
310       return true;
311     }
312     
313     String line;
314 
315     do {
316       try {
317         line =
318           ConsoleTools.readLine(UI.getString("console.readLine.applyErrors"));
319       } catch (IOException e) {
320         log.error("Error while reading information");
321         e.printStackTrace();
322         return false;
323       }
324     } while (wrongValue(line));
325 
326     if (line.equals("y") || line.equals("Y")) {
327       return true;
328     }
329 
330     return false;
331 
332   }
333 
334   
335 
336 
337 
338   private static boolean wrongValue(String line) {
339     return line.length() != 1
340             || (!line.equals("y") && !line.equals("n")
341                 && !line.equals("Y") && !line.equals("N"));
342   }
343 
344   
345 
346 
347 
348 
349 
350 
351   private static boolean execute(int mode) throws Exception {
352     if (mode >= 0 && mode <= 2) {
353       try {
354         Configurator.setViewMode(mode);
355         Class c = Class.forName(CLASS_NAMES[mode]);
356         if (c != null) {
357           UI ui = (UI)c.newInstance();
358           UI.getConfigurators();
359           return ui.execute();
360         }
361       } catch (Exception e) {
362         e.printStackTrace();
363         System.exit(1);
364         return false;
365       }
366     } else {
367       throw new Exception("No UI implemented for this options");
368     }
369     return false;
370   }
371 
372   
373 
374 
375 
376 
377 
378   private static String parseLaf(String laf) {
379     if (laf.equalsIgnoreCase("system")) {
380       laf = UIManager.getSystemLookAndFeelClassName();
381     } else if (laf.equalsIgnoreCase("cross")) {
382       laf = UIManager.getCrossPlatformLookAndFeelClassName();
383     } else if (laf.equalsIgnoreCase("windows")) {
384       laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
385     } else if (laf.equalsIgnoreCase("gtk")) {
386       laf = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
387     } else if (laf.equalsIgnoreCase("metal")) {
388       laf = "javax.swing.plaf.metal.MetalLookAndFeel";
389     } else if (laf.equalsIgnoreCase("motif")) {
390       laf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
391     }
392 
393     try {
394       
395       Class.forName(laf);
396       System.setProperty("swing.defaultlaf", laf);
397     } catch (Exception e) {
398       System.err.println("WARNING: Invalid LAF specified");
399     }
400 
401     return laf;
402   }
403 
404   
405 
406 
407 
408 
409 
410   private static void register(Map<String, Configurator> map, Configurator c) {
411     if (map.containsKey(c.getPath())) {
412       (new InternalError("Configurator overriden!!!")).printStackTrace();
413       System.exit(1);
414     }
415     map.put(c.getPath(), c);
416   }
417 
418   
419 
420 
421 
422 
423 
424 
425 
426 
427 
428   private static Configurator newConfigurator(String className) {
429     try {
430       Class c = Class.forName(className);
431       if (Configurator.class.isAssignableFrom(c)) {
432         try {
433           Method m = c.getMethod("getInstance");
434           return (Configurator)m.invoke(null);
435         } catch (Exception e) {
436           return (Configurator)c.newInstance();
437         }
438       }
439     } catch (Exception e) {
440       String pattern = UI.getString("messages.classload");
441       String msg = MessageFormat.format(pattern, new Object[] { className });
442       log.warn(msg, e);
443     }
444     return null;
445   }
446 
447   
448 
449 
450 
451 
452 
453 
454 
455 
456   private static void setDefaultLocale(String slocale) {
457     StringTokenizer st = new StringTokenizer(slocale, "_");
458     Locale locale = null;
459     if (st.hasMoreTokens()) {
460       String lang = st.nextToken().trim().toLowerCase();
461       if (st.hasMoreTokens()) {
462         String country = st.nextToken().trim().toUpperCase();
463         if (st.hasMoreTokens()) {
464           String variant = st.nextToken().trim();
465           locale = new Locale(lang, country, variant);
466         } else {
467           locale = new Locale(lang, country);
468         }
469       } else {
470         locale = new Locale(lang);
471       }
472     }
473 
474     if (locale != null) {
475       Locale.setDefault(locale);
476       bundle = ResourceBundle.getBundle("config", locale);
477     }
478   }
479 }