20
|
1 /**
|
|
2 * Title: GlobalConfigure.java
|
|
3 * Copyright: Copyright (c) 2001, 2002, 2003
|
|
4 * Company:
|
|
5 * @author Kenneth J. Pouncey
|
|
6 * @version 0.1
|
|
7 *
|
|
8 * Description:
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2, or (at your option)
|
|
13 * any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this software; see the file COPYING. If not, write to
|
|
22 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|
23 * Boston, MA 02111-1307 USA
|
|
24 *
|
|
25 */
|
|
26 package org.tn5250j;
|
|
27
|
|
28 import java.io.BufferedReader;
|
|
29 import java.io.File;
|
|
30 import java.io.FileInputStream;
|
|
31 import java.io.FileNotFoundException;
|
|
32 import java.io.FileOutputStream;
|
|
33 import java.io.FileReader;
|
|
34 import java.io.FileWriter;
|
|
35 import java.io.IOException;
|
|
36 import java.io.PrintWriter;
|
|
37 import java.util.Hashtable;
|
|
38 import java.util.Properties;
|
|
39
|
|
40 import javax.swing.JOptionPane;
|
|
41
|
|
42 import com.five-ten-sg.connectbot.TerminalView;
|
|
43 import org.tn5250j.interfaces.ConfigureFactory;
|
|
44 import org.tn5250j.tools.logging.TN5250jLogFactory;
|
|
45 import org.tn5250j.tools.logging.TN5250jLogger;
|
|
46
|
|
47 /**
|
|
48 * Utility class for referencing global settings and functions of which at most
|
|
49 * one instance can exist per VM.
|
|
50 *
|
|
51 * Use GlobalConfigure.instance() to access this instance.
|
|
52 */
|
|
53 public class GlobalConfigure extends ConfigureFactory {
|
|
54
|
|
55 public static final String TN5250J_FOLDER = ".tn5250j";
|
|
56
|
|
57 /**
|
|
58 * A handle to the unique GlobalConfigure class
|
|
59 */
|
|
60 static private GlobalConfigure _instance;
|
|
61
|
|
62 /**
|
|
63 * A handle to the the Global Properties
|
|
64 */
|
|
65 static private Properties settings;
|
|
66
|
|
67 static private Hashtable registry = new Hashtable();
|
|
68 static private Hashtable headers = new Hashtable(); //LUC GORRENS
|
|
69
|
|
70 // Moved to ConfigureFactory
|
|
71 // static final public String SESSIONS = "sessions";
|
|
72 static final public File ses = new File(SESSIONS);
|
|
73 // static final public String MACROS = "macros";
|
|
74 // static final public String KEYMAP = "keymap";
|
|
75
|
|
76 private final TN5250jLogger log = TN5250jLogFactory.getLogger (this.getClass());
|
|
77
|
|
78 /**
|
|
79 * The constructor is made protected to allow overriding.
|
|
80 */
|
|
81 public GlobalConfigure() {
|
|
82 if (_instance == null) {
|
|
83 // initialize the settings information
|
|
84 initialize();
|
|
85 // set our instance to this one.
|
|
86 _instance = this;
|
|
87 }
|
|
88 }
|
|
89
|
|
90 /**
|
|
91 *
|
|
92 * @return The unique instance of this class.
|
|
93 */
|
|
94 static public GlobalConfigure instance() {
|
|
95
|
|
96 if (_instance == null) {
|
|
97 _instance = new GlobalConfigure();
|
|
98 }
|
|
99 return _instance;
|
|
100
|
|
101 }
|
|
102
|
|
103 /**
|
|
104 * Initialize the properties registry for use later.
|
|
105 *
|
|
106 */
|
|
107 private void initialize() {
|
|
108 verifySettingsFolder();
|
|
109 loadSettings();
|
|
110 loadSessions();
|
|
111 loadMacros();
|
|
112 loadKeyStrokes();
|
|
113 }
|
|
114
|
|
115 /**
|
|
116 * check if folder %USERPROFILE%/.tn5250j exists
|
|
117 * and create if necessary
|
|
118 */
|
|
119 private void verifySettingsFolder() {
|
|
120 final String settingsfolder = settingsFolder();
|
|
121 final File f = new File (settingsfolder);
|
|
122 if (!f.exists()) {
|
|
123 try {
|
|
124 if (log.isInfoEnabled()) {
|
|
125 log.info("Settings folder '"+settingsfolder+"' doesn't exist. Will created now.");
|
|
126 }
|
|
127 f.mkdir();
|
|
128 } catch (Exception e) {
|
|
129 if (log.isWarnEnabled()) {
|
|
130 log.warn("Couldn't create settings folder '"+settingsfolder+"'", e);
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Load the sessions properties
|
|
138 */
|
|
139 private void loadSessions() {
|
|
140
|
|
141 setProperties(SESSIONS,SESSIONS,"------ Sessions --------",true);
|
|
142 }
|
|
143
|
|
144 /**
|
|
145 * Load the macros
|
|
146 */
|
|
147 private void loadMacros() {
|
|
148
|
|
149 setProperties(MACROS,MACROS,"------ Macros --------",true);
|
|
150
|
|
151 }
|
|
152
|
|
153 private void loadKeyStrokes() {
|
|
154
|
|
155 setProperties(KEYMAP,KEYMAP,
|
|
156 "------ Key Map key=keycode,isShiftDown,isControlDown,isAltDown,isAltGrDown --------",
|
|
157 true);
|
|
158
|
|
159 }
|
|
160
|
|
161 /**
|
|
162 * Reload the environment settings.
|
|
163 */
|
|
164 @Override
|
|
165 public void reloadSettings() {
|
|
166 if (log.isInfoEnabled()) {
|
|
167 log.info("reloading settings");
|
|
168 }
|
|
169 loadSettings();
|
|
170 loadSessions();
|
|
171 loadMacros();
|
|
172 loadKeyStrokes();
|
|
173 if (log.isInfoEnabled()) {
|
|
174 log.info("Done (reloading settings).");
|
|
175 }
|
|
176 }
|
|
177
|
|
178 /**
|
|
179 * Loads the emulator setting from the setting(s) file
|
|
180 */
|
|
181 private void loadSettings() {
|
|
182
|
|
183 FileInputStream in = null;
|
|
184 settings = new Properties();
|
|
185
|
|
186 try {
|
|
187 in = new FileInputStream(settingsFile());
|
|
188 settings.load(in);
|
|
189 }
|
|
190 catch (FileNotFoundException fnfea) {
|
|
191 log.info(" Information Message: "
|
|
192 + fnfea.getMessage() + ". The file " + settingsFile()
|
|
193 + " will be created for first time use.");
|
|
194 checkLegacy();
|
|
195 saveSettings();
|
|
196 }
|
|
197 catch (IOException ioea) {
|
|
198 log.warn("IO Exception accessing File "
|
|
199 + settingsFile() + " for the following reason : "
|
|
200 + ioea.getMessage());
|
|
201 }
|
|
202 catch (SecurityException sea) {
|
|
203 log.warn("Security Exception for file "
|
|
204 + settingsFile() + " This file can not be "
|
|
205 + "accessed because : " + sea.getMessage());
|
|
206 }
|
|
207 }
|
|
208
|
|
209 private void checkDirs() {
|
|
210 // we now check to see if the settings directory is a directory. If not then we create it
|
|
211 File sd = new File(settings.getProperty("emulator.settingsDirectory"));
|
|
212 if (!sd.isDirectory())
|
|
213 sd.mkdirs();
|
|
214 }
|
|
215
|
|
216 private void checkLegacy() {
|
|
217 // we check if the sessions file already exists in the directory
|
|
218 // if it does exist we are working with an old install so we
|
|
219 // need to set the settings directory to the users directory
|
|
220 // SESSIONS is declared as a string, so we just can use the keyword here.
|
|
221 if(ses.exists()) {
|
|
222 int cfc;
|
|
223 cfc = JOptionPane.showConfirmDialog(null,
|
|
224 "Dear User,\n\n" +
|
|
225 "Seems you are using an old version of tn5250j.\n" +
|
|
226 "In meanwhile the application became multi-user capable,\n" +
|
|
227 "which means ALL the config- and settings-files are\n" +
|
|
228 "placed in your home-dir to avoid further problems in\n" +
|
|
229 "the near future.\n\n" +
|
|
230 "You have the choice to choose if you want the files\n" +
|
|
231 "to be copied or not, please make your choice !\n\n" +
|
|
232 "Shall we copy the files to the new location ?",
|
|
233 "Old install detected", JOptionPane.WARNING_MESSAGE,
|
|
234 JOptionPane.YES_NO_OPTION);
|
|
235 if (cfc == 0) {
|
|
236 // Here we do a checkdir so we know the destination-dir exists
|
|
237 checkDirs();
|
|
238 copyConfigs(SESSIONS);
|
|
239 copyConfigs(MACROS);
|
|
240 copyConfigs(KEYMAP);
|
|
241 }
|
|
242 else {
|
|
243 JOptionPane.showMessageDialog(null,
|
|
244 "Dear User,\n\n" +
|
|
245 "You choosed not to copy the file.\n" +
|
|
246 "This means the program will end here.\n\n" +
|
|
247 "To use this NON-STANDARD behaviour start tn5250j\n" +
|
|
248 "with -Demulator.settingsDirectory=<settings-dir> \n" +
|
|
249 "as a parameter to avoid this question all the time.",
|
|
250 "Using NON-STANDARD behaviour", JOptionPane.WARNING_MESSAGE);
|
|
251 System.exit(0);
|
|
252 }
|
|
253 }
|
|
254 }
|
|
255
|
|
256 private void copyConfigs(String sesFile) {
|
|
257 /** Copy the config-files to the user's home-dir */
|
|
258 String srcFile = System.getProperty("user.dir") + File.separator + sesFile;
|
|
259 String dest = System.getProperty("user.home") +
|
|
260 File.separator + TN5250J_FOLDER + File.separator + sesFile;
|
|
261 File rmvFile = new File(sesFile);
|
|
262 try {
|
|
263 FileReader r = new FileReader(srcFile);
|
|
264 BufferedReader b = new BufferedReader(r);
|
|
265
|
|
266 FileWriter w = new FileWriter(dest);
|
|
267 PrintWriter p = new PrintWriter(w);
|
|
268 String regel = b.readLine();
|
|
269 while (regel != null) {
|
|
270 p.println(regel);
|
|
271 regel = b.readLine();
|
|
272 }
|
|
273 b.close();
|
|
274 p.close();
|
|
275 rmvFile.delete();
|
|
276 }
|
|
277 catch (FileNotFoundException e) {
|
|
278 log.warn(srcFile + " not found !");
|
|
279 }
|
|
280 catch (IOException e) {
|
|
281 log.warn("Global io-error !");
|
|
282 }
|
|
283 catch (ArrayIndexOutOfBoundsException e) {
|
|
284 }
|
|
285 }
|
|
286
|
|
287 /**
|
|
288 * Save the settings for the global configuration
|
|
289 */
|
|
290 @Override
|
|
291 public void saveSettings() {
|
|
292
|
|
293 try {
|
|
294 FileOutputStream out = new FileOutputStream(settingsFile());
|
|
295 settings.store(out,"----------------- tn5250j Global Settings --------------");
|
|
296 }
|
|
297 catch (FileNotFoundException fnfe) {}
|
|
298 catch (IOException ioe) {}
|
|
299 }
|
|
300
|
|
301 /**
|
|
302 * Save the setting in the registry using the key passed in with no header
|
|
303 * output.
|
|
304 *
|
|
305 * @param regKey
|
|
306 */
|
|
307 @Override
|
|
308 public void saveSettings(String regKey) {
|
|
309
|
|
310 saveSettings(regKey,"");
|
|
311 }
|
|
312
|
|
313 /**
|
|
314 * Save the settings in the registry using the key passed with a header
|
|
315 * in the output.
|
|
316 *
|
|
317 * @param regKey
|
|
318 * @param Header
|
|
319 */
|
|
320 @Override
|
|
321 public void saveSettings(String regKey, String header) {
|
|
322
|
|
323 saveSettings(regKey,regKey,header);
|
|
324 }
|
|
325
|
|
326 /**
|
|
327 * Save the settings in the registry using the key passed with a header
|
|
328 * in the output.
|
|
329 *
|
|
330 * @param regKey
|
|
331 * @param Header
|
|
332 */
|
|
333 @Override
|
|
334 public void saveSettings(String regKey, String fileName, String header) {
|
|
335
|
|
336 if (registry.containsKey(regKey)) {
|
|
337 try {
|
|
338 FileOutputStream out = new FileOutputStream(
|
|
339 settingsDirectory() + fileName);
|
|
340 Properties props = (Properties)registry.get(regKey);
|
|
341 props.store(out,header);
|
|
342 out.flush();
|
|
343 out.close();
|
|
344 }
|
|
345 catch (FileNotFoundException fnfe) {
|
|
346 log.warn("File not found : writing file "
|
|
347 + fileName + ". Description of error is "
|
|
348 + fnfe.getMessage());
|
|
349 }
|
|
350 catch (IOException ioe) {
|
|
351 log.warn("IO Exception : writing file "
|
|
352 + fileName + ". Description of error is "
|
|
353 + ioe.getMessage());
|
|
354 }
|
|
355 catch (SecurityException se) {
|
|
356 log.warn("Security Exception : writing file "
|
|
357 + fileName + ". Description of error is "
|
|
358 + se.getMessage());
|
|
359 }
|
|
360
|
|
361 }
|
|
362
|
|
363 }
|
|
364
|
|
365 /**
|
|
366 * Place the Properties in the registry under a given registry name
|
|
367 *
|
|
368 * @param regKey
|
|
369 * @param regProps
|
|
370 */
|
|
371 @Override
|
|
372 public void setProperties(String regKey, Properties regProps) {
|
|
373
|
|
374 registry.put(regKey, regProps);
|
|
375
|
|
376 }
|
|
377
|
|
378 /**
|
|
379 * Set the properties for the given registry key.
|
|
380 *
|
|
381 * @param regKey
|
|
382 * @param fileName
|
|
383 * @param header
|
|
384 */
|
|
385 @Override
|
|
386 public void setProperties(String regKey, String fileName, String header) { //LG NEW
|
|
387 setProperties(regKey, fileName, header, false);
|
|
388 }
|
|
389
|
|
390 /**
|
|
391 * Set the properties for the given registry key.
|
|
392 *
|
|
393 * @param regKey
|
|
394 * @param fileName
|
|
395 * @param header
|
|
396 * @param createFile
|
|
397 */
|
|
398 @Override
|
|
399 public void setProperties(String regKey, String fileName, String header,
|
|
400 boolean createFile) {
|
|
401
|
|
402 FileInputStream in = null;
|
|
403 Properties props = new Properties();
|
|
404 headers.put(regKey, header);
|
|
405
|
|
406 try {
|
|
407 in = new FileInputStream(settingsDirectory() + fileName);
|
|
408 props.load(in);
|
|
409
|
|
410 }
|
|
411 catch (FileNotFoundException fnfe) {
|
|
412
|
|
413 if (createFile) {
|
|
414 log.info(" Information Message: " + fnfe.getMessage()
|
|
415 + ". The file " + fileName + " will"
|
|
416 + " be created for first time use.");
|
|
417
|
|
418 saveSettings(regKey,header);
|
|
419
|
|
420 }
|
|
421 else {
|
|
422
|
|
423 log.info(" Information Message: " + fnfe.getMessage()
|
|
424 + ".");
|
|
425
|
|
426 }
|
|
427 }
|
|
428 catch (IOException ioe) {
|
|
429 log.warn("IO Exception accessing File "+ fileName +
|
|
430 " for the following reason : "
|
|
431 + ioe.getMessage());
|
|
432 }
|
|
433 catch (SecurityException se) {
|
|
434 log.warn("Security Exception for file "+ fileName
|
|
435 + ". This file can not be accessed because : "
|
|
436 + se.getMessage());
|
|
437 }
|
|
438
|
|
439 registry.put(regKey,props);
|
|
440
|
|
441 }
|
|
442
|
|
443 /**
|
|
444 * Returns the properties associated with a given registry key.
|
|
445 *
|
|
446 * @param regKey
|
|
447 * @return
|
|
448 */
|
|
449 @Override
|
|
450 public Properties getProperties(String regKey) {
|
|
451
|
|
452 if (registry.containsKey(regKey)) {
|
|
453 return (Properties)registry.get(regKey);
|
|
454 }
|
|
455 return null;
|
|
456 }
|
|
457
|
|
458 public Properties getProperties() {
|
|
459 return settings;
|
|
460 }
|
|
461
|
|
462 @Override
|
|
463 public Properties getProperties(String regKey,String fileName) {
|
|
464 return getProperties(regKey,fileName,false,"",false);
|
|
465 }
|
|
466
|
|
467 @Override
|
|
468 public Properties getProperties(String regKey,String fileName,
|
|
469 boolean createFile, String header) {
|
|
470 return getProperties(regKey,fileName,false,"",false);
|
|
471 }
|
|
472
|
|
473 @Override
|
|
474 public Properties getProperties(String regKey,String fileName,
|
|
475 boolean createFile,String header,
|
|
476 boolean reloadIfLoaded) {
|
|
477
|
|
478 if (!registry.containsKey(regKey) || reloadIfLoaded) {
|
|
479
|
|
480 FileInputStream in = null;
|
|
481 Properties props = new Properties();
|
|
482 headers.put(regKey, header);
|
|
483
|
|
484 try {
|
|
485 in = new FileInputStream(settingsDirectory()
|
|
486 + fileName);
|
|
487 props.load(in);
|
|
488
|
|
489 }
|
|
490 catch (FileNotFoundException fnfe) {
|
|
491
|
|
492 if (createFile) {
|
|
493 log.info(" Information Message: " + fnfe.getMessage()
|
|
494 + ". The file " + fileName + " will"
|
|
495 + " be created for first time use.");
|
|
496
|
|
497 registry.put(regKey,props);
|
|
498
|
|
499 saveSettings(regKey,header);
|
|
500
|
|
501 return props;
|
|
502
|
|
503 }
|
|
504 else {
|
|
505
|
|
506 log.info(" Information Message: " + fnfe.getMessage()
|
|
507 + ".");
|
|
508
|
|
509 }
|
|
510 }
|
|
511 catch (IOException ioe) {
|
|
512 log.warn("IO Exception accessing File "+ fileName +
|
|
513 " for the following reason : "
|
|
514 + ioe.getMessage());
|
|
515 }
|
|
516 catch (SecurityException se) {
|
|
517 log.warn("Security Exception for file "+ fileName
|
|
518 + ". This file can not be accessed because : "
|
|
519 + se.getMessage());
|
|
520 }
|
|
521
|
|
522 registry.put(regKey,props);
|
|
523
|
|
524 return props;
|
|
525 }
|
|
526 else {
|
|
527 return (Properties)registry.get(regKey);
|
|
528 }
|
|
529 }
|
|
530
|
|
531 /**
|
|
532 * Returns the setting from the given key of the global properties or the
|
|
533 * default passed if the property does not exist.
|
|
534 *
|
|
535 * @param key
|
|
536 * @param def
|
|
537 * @return
|
|
538 */
|
|
539 @Override
|
|
540 public String getProperty(String key, String def) {
|
|
541 if (settings.containsKey(key))
|
|
542 return settings.getProperty(key);
|
|
543 else
|
|
544 return def;
|
|
545 }
|
|
546
|
|
547 /**
|
|
548 * Returns the setting from the given key of the global properties.
|
|
549 *
|
|
550 * @param key
|
|
551 * @return
|
|
552 */
|
|
553 @Override
|
|
554 public String getProperty(String key) {
|
|
555 return settings.getProperty(key);
|
|
556 }
|
|
557
|
|
558 /**
|
|
559 * Private helper to return the full path to the settings file
|
|
560 *
|
|
561 * @return
|
|
562 */
|
|
563 private String settingsFile() {
|
|
564 return settingsDirectory() + "tn5250jstartup.cfg";
|
|
565
|
|
566 }
|
|
567
|
|
568 /**
|
|
569 * Private helper to return the settings directory
|
|
570 *
|
|
571 * @return
|
|
572 */
|
|
573 private String settingsDirectory() {
|
|
574 return settingsFolder() + File.separator;
|
|
575
|
|
576 }
|
|
577
|
|
578 /**
|
|
579 * Private helper to return the settings folder path
|
|
580 *
|
|
581 * @return
|
|
582 */
|
|
583 private String settingsFolder() {
|
|
584 return TerminalView.android_home_directory + File.separator + TN5250J_FOLDER;
|
|
585
|
|
586 }
|
|
587
|
|
588 /**
|
|
589 * Not sure yet so be careful using this.
|
|
590 *
|
|
591 * @return
|
|
592 */
|
|
593 public ClassLoader getClassLoader() {
|
|
594
|
|
595 ClassLoader loader = GlobalConfigure.class.getClassLoader();
|
|
596 if (loader == null)
|
|
597 loader = ClassLoader.getSystemClassLoader();
|
|
598
|
|
599 return loader;
|
|
600 }
|
|
601
|
|
602 }
|