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
|
21
|
40 import com.five_ten_sg.connectbot.TerminalView;
|
20
|
41 import org.tn5250j.interfaces.ConfigureFactory;
|
|
42 import org.tn5250j.tools.logging.TN5250jLogFactory;
|
|
43 import org.tn5250j.tools.logging.TN5250jLogger;
|
|
44
|
|
45 /**
|
|
46 * Utility class for referencing global settings and functions of which at most
|
|
47 * one instance can exist per VM.
|
|
48 *
|
|
49 * Use GlobalConfigure.instance() to access this instance.
|
|
50 */
|
|
51 public class GlobalConfigure extends ConfigureFactory {
|
|
52
|
|
53 public static final String TN5250J_FOLDER = ".tn5250j";
|
|
54
|
|
55 /**
|
|
56 * A handle to the unique GlobalConfigure class
|
|
57 */
|
|
58 static private GlobalConfigure _instance;
|
|
59
|
|
60 /**
|
|
61 * A handle to the the Global Properties
|
|
62 */
|
|
63 static private Properties settings;
|
|
64
|
|
65 static private Hashtable registry = new Hashtable();
|
|
66 static private Hashtable headers = new Hashtable(); //LUC GORRENS
|
|
67 private final TN5250jLogger log = TN5250jLogFactory.getLogger (this.getClass());
|
|
68
|
|
69 /**
|
|
70 * The constructor is made protected to allow overriding.
|
|
71 */
|
|
72 public GlobalConfigure() {
|
|
73 if (_instance == null) {
|
|
74 // initialize the settings information
|
|
75 initialize();
|
|
76 // set our instance to this one.
|
|
77 _instance = this;
|
|
78 }
|
|
79 }
|
|
80
|
|
81 /**
|
|
82 *
|
|
83 * @return The unique instance of this class.
|
|
84 */
|
|
85 static public GlobalConfigure instance() {
|
|
86
|
|
87 if (_instance == null) {
|
|
88 _instance = new GlobalConfigure();
|
|
89 }
|
|
90 return _instance;
|
|
91
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Initialize the properties registry for use later.
|
|
96 *
|
|
97 */
|
|
98 private void initialize() {
|
|
99 verifySettingsFolder();
|
|
100 loadSettings();
|
|
101 loadSessions();
|
|
102 loadMacros();
|
|
103 loadKeyStrokes();
|
|
104 }
|
|
105
|
|
106 /**
|
|
107 * check if folder %USERPROFILE%/.tn5250j exists
|
|
108 * and create if necessary
|
|
109 */
|
|
110 private void verifySettingsFolder() {
|
|
111 final String settingsfolder = settingsFolder();
|
|
112 final File f = new File (settingsfolder);
|
|
113 if (!f.exists()) {
|
|
114 try {
|
|
115 if (log.isInfoEnabled()) {
|
|
116 log.info("Settings folder '"+settingsfolder+"' doesn't exist. Will created now.");
|
|
117 }
|
|
118 f.mkdir();
|
|
119 } catch (Exception e) {
|
|
120 if (log.isWarnEnabled()) {
|
|
121 log.warn("Couldn't create settings folder '"+settingsfolder+"'", e);
|
|
122 }
|
|
123 }
|
|
124 }
|
|
125 }
|
|
126
|
|
127 /**
|
|
128 * Load the sessions properties
|
|
129 */
|
|
130 private void loadSessions() {
|
|
131
|
|
132 setProperties(SESSIONS,SESSIONS,"------ Sessions --------",true);
|
|
133 }
|
|
134
|
|
135 /**
|
|
136 * Load the macros
|
|
137 */
|
|
138 private void loadMacros() {
|
|
139
|
|
140 setProperties(MACROS,MACROS,"------ Macros --------",true);
|
|
141
|
|
142 }
|
|
143
|
|
144 private void loadKeyStrokes() {
|
|
145
|
|
146 setProperties(KEYMAP,KEYMAP,
|
|
147 "------ Key Map key=keycode,isShiftDown,isControlDown,isAltDown,isAltGrDown --------",
|
|
148 true);
|
|
149
|
|
150 }
|
|
151
|
|
152 /**
|
|
153 * Reload the environment settings.
|
|
154 */
|
|
155 @Override
|
|
156 public void reloadSettings() {
|
|
157 if (log.isInfoEnabled()) {
|
|
158 log.info("reloading settings");
|
|
159 }
|
|
160 loadSettings();
|
|
161 loadSessions();
|
|
162 loadMacros();
|
|
163 loadKeyStrokes();
|
|
164 if (log.isInfoEnabled()) {
|
|
165 log.info("Done (reloading settings).");
|
|
166 }
|
|
167 }
|
|
168
|
|
169 /**
|
|
170 * Loads the emulator setting from the setting(s) file
|
|
171 */
|
|
172 private void loadSettings() {
|
|
173
|
|
174 FileInputStream in = null;
|
|
175 settings = new Properties();
|
|
176
|
|
177 try {
|
|
178 in = new FileInputStream(settingsFile());
|
|
179 settings.load(in);
|
|
180 }
|
|
181 catch (FileNotFoundException fnfea) {
|
|
182 log.info(" Information Message: "
|
|
183 + fnfea.getMessage() + ". The file " + settingsFile()
|
|
184 + " will be created for first time use.");
|
|
185 saveSettings();
|
|
186 }
|
|
187 catch (IOException ioea) {
|
|
188 log.warn("IO Exception accessing File "
|
|
189 + settingsFile() + " for the following reason : "
|
|
190 + ioea.getMessage());
|
|
191 }
|
|
192 catch (SecurityException sea) {
|
|
193 log.warn("Security Exception for file "
|
|
194 + settingsFile() + " This file can not be "
|
|
195 + "accessed because : " + sea.getMessage());
|
|
196 }
|
|
197 }
|
|
198
|
|
199 /**
|
|
200 * Save the settings for the global configuration
|
|
201 */
|
|
202 @Override
|
|
203 public void saveSettings() {
|
|
204
|
|
205 try {
|
|
206 FileOutputStream out = new FileOutputStream(settingsFile());
|
|
207 settings.store(out,"----------------- tn5250j Global Settings --------------");
|
|
208 }
|
|
209 catch (FileNotFoundException fnfe) {}
|
|
210 catch (IOException ioe) {}
|
|
211 }
|
|
212
|
|
213 /**
|
|
214 * Save the setting in the registry using the key passed in with no header
|
|
215 * output.
|
|
216 *
|
|
217 * @param regKey
|
|
218 */
|
|
219 @Override
|
|
220 public void saveSettings(String regKey) {
|
|
221
|
|
222 saveSettings(regKey,"");
|
|
223 }
|
|
224
|
|
225 /**
|
|
226 * Save the settings in the registry using the key passed with a header
|
|
227 * in the output.
|
|
228 *
|
|
229 * @param regKey
|
|
230 * @param Header
|
|
231 */
|
|
232 @Override
|
|
233 public void saveSettings(String regKey, String header) {
|
|
234
|
|
235 saveSettings(regKey,regKey,header);
|
|
236 }
|
|
237
|
|
238 /**
|
|
239 * Save the settings in the registry using the key passed with a header
|
|
240 * in the output.
|
|
241 *
|
|
242 * @param regKey
|
|
243 * @param Header
|
|
244 */
|
|
245 @Override
|
|
246 public void saveSettings(String regKey, String fileName, String header) {
|
|
247
|
|
248 if (registry.containsKey(regKey)) {
|
|
249 try {
|
|
250 FileOutputStream out = new FileOutputStream(
|
|
251 settingsDirectory() + fileName);
|
|
252 Properties props = (Properties)registry.get(regKey);
|
|
253 props.store(out,header);
|
|
254 out.flush();
|
|
255 out.close();
|
|
256 }
|
|
257 catch (FileNotFoundException fnfe) {
|
|
258 log.warn("File not found : writing file "
|
|
259 + fileName + ". Description of error is "
|
|
260 + fnfe.getMessage());
|
|
261 }
|
|
262 catch (IOException ioe) {
|
|
263 log.warn("IO Exception : writing file "
|
|
264 + fileName + ". Description of error is "
|
|
265 + ioe.getMessage());
|
|
266 }
|
|
267 catch (SecurityException se) {
|
|
268 log.warn("Security Exception : writing file "
|
|
269 + fileName + ". Description of error is "
|
|
270 + se.getMessage());
|
|
271 }
|
|
272
|
|
273 }
|
|
274
|
|
275 }
|
|
276
|
|
277 /**
|
|
278 * Place the Properties in the registry under a given registry name
|
|
279 *
|
|
280 * @param regKey
|
|
281 * @param regProps
|
|
282 */
|
|
283 @Override
|
|
284 public void setProperties(String regKey, Properties regProps) {
|
|
285
|
|
286 registry.put(regKey, regProps);
|
|
287
|
|
288 }
|
|
289
|
|
290 /**
|
|
291 * Set the properties for the given registry key.
|
|
292 *
|
|
293 * @param regKey
|
|
294 * @param fileName
|
|
295 * @param header
|
|
296 */
|
|
297 @Override
|
|
298 public void setProperties(String regKey, String fileName, String header) { //LG NEW
|
|
299 setProperties(regKey, fileName, header, false);
|
|
300 }
|
|
301
|
|
302 /**
|
|
303 * Set the properties for the given registry key.
|
|
304 *
|
|
305 * @param regKey
|
|
306 * @param fileName
|
|
307 * @param header
|
|
308 * @param createFile
|
|
309 */
|
|
310 @Override
|
|
311 public void setProperties(String regKey, String fileName, String header,
|
|
312 boolean createFile) {
|
|
313
|
|
314 FileInputStream in = null;
|
|
315 Properties props = new Properties();
|
|
316 headers.put(regKey, header);
|
|
317
|
|
318 try {
|
|
319 in = new FileInputStream(settingsDirectory() + fileName);
|
|
320 props.load(in);
|
|
321
|
|
322 }
|
|
323 catch (FileNotFoundException fnfe) {
|
|
324
|
|
325 if (createFile) {
|
|
326 log.info(" Information Message: " + fnfe.getMessage()
|
|
327 + ". The file " + fileName + " will"
|
|
328 + " be created for first time use.");
|
|
329
|
|
330 saveSettings(regKey,header);
|
|
331
|
|
332 }
|
|
333 else {
|
|
334
|
|
335 log.info(" Information Message: " + fnfe.getMessage()
|
|
336 + ".");
|
|
337
|
|
338 }
|
|
339 }
|
|
340 catch (IOException ioe) {
|
|
341 log.warn("IO Exception accessing File "+ fileName +
|
|
342 " for the following reason : "
|
|
343 + ioe.getMessage());
|
|
344 }
|
|
345 catch (SecurityException se) {
|
|
346 log.warn("Security Exception for file "+ fileName
|
|
347 + ". This file can not be accessed because : "
|
|
348 + se.getMessage());
|
|
349 }
|
|
350
|
|
351 registry.put(regKey,props);
|
|
352
|
|
353 }
|
|
354
|
|
355 /**
|
|
356 * Returns the properties associated with a given registry key.
|
|
357 *
|
|
358 * @param regKey
|
|
359 * @return
|
|
360 */
|
|
361 @Override
|
|
362 public Properties getProperties(String regKey) {
|
|
363
|
|
364 if (registry.containsKey(regKey)) {
|
|
365 return (Properties)registry.get(regKey);
|
|
366 }
|
|
367 return null;
|
|
368 }
|
|
369
|
|
370 public Properties getProperties() {
|
|
371 return settings;
|
|
372 }
|
|
373
|
|
374 @Override
|
|
375 public Properties getProperties(String regKey,String fileName) {
|
|
376 return getProperties(regKey,fileName,false,"",false);
|
|
377 }
|
|
378
|
|
379 @Override
|
|
380 public Properties getProperties(String regKey,String fileName,
|
|
381 boolean createFile, String header) {
|
|
382 return getProperties(regKey,fileName,false,"",false);
|
|
383 }
|
|
384
|
|
385 @Override
|
22
|
386 public Properties getProperties(String regKey,
|
|
387 String fileName,
|
|
388 boolean createFile,
|
|
389 String header,
|
|
390 boolean reloadIfLoaded) {
|
20
|
391
|
|
392 if (!registry.containsKey(regKey) || reloadIfLoaded) {
|
|
393
|
|
394 FileInputStream in = null;
|
|
395 Properties props = new Properties();
|
|
396 headers.put(regKey, header);
|
|
397
|
|
398 try {
|
|
399 in = new FileInputStream(settingsDirectory()
|
|
400 + fileName);
|
|
401 props.load(in);
|
|
402
|
|
403 }
|
|
404 catch (FileNotFoundException fnfe) {
|
|
405
|
|
406 if (createFile) {
|
|
407 log.info(" Information Message: " + fnfe.getMessage()
|
|
408 + ". The file " + fileName + " will"
|
|
409 + " be created for first time use.");
|
|
410
|
|
411 registry.put(regKey,props);
|
|
412
|
|
413 saveSettings(regKey,header);
|
|
414
|
|
415 return props;
|
|
416
|
|
417 }
|
|
418 else {
|
|
419
|
|
420 log.info(" Information Message: " + fnfe.getMessage()
|
|
421 + ".");
|
|
422
|
|
423 }
|
|
424 }
|
|
425 catch (IOException ioe) {
|
|
426 log.warn("IO Exception accessing File "+ fileName +
|
|
427 " for the following reason : "
|
|
428 + ioe.getMessage());
|
|
429 }
|
|
430 catch (SecurityException se) {
|
|
431 log.warn("Security Exception for file "+ fileName
|
|
432 + ". This file can not be accessed because : "
|
|
433 + se.getMessage());
|
|
434 }
|
|
435
|
|
436 registry.put(regKey,props);
|
|
437
|
|
438 return props;
|
|
439 }
|
|
440 else {
|
|
441 return (Properties)registry.get(regKey);
|
|
442 }
|
|
443 }
|
|
444
|
|
445 /**
|
|
446 * Returns the setting from the given key of the global properties or the
|
|
447 * default passed if the property does not exist.
|
|
448 *
|
|
449 * @param key
|
|
450 * @param def
|
|
451 * @return
|
|
452 */
|
|
453 @Override
|
|
454 public String getProperty(String key, String def) {
|
|
455 if (settings.containsKey(key))
|
|
456 return settings.getProperty(key);
|
|
457 else
|
|
458 return def;
|
|
459 }
|
|
460
|
|
461 /**
|
|
462 * Returns the setting from the given key of the global properties.
|
|
463 *
|
|
464 * @param key
|
|
465 * @return
|
|
466 */
|
|
467 @Override
|
|
468 public String getProperty(String key) {
|
|
469 return settings.getProperty(key);
|
|
470 }
|
|
471
|
|
472 /**
|
|
473 * Private helper to return the full path to the settings file
|
|
474 *
|
|
475 * @return
|
|
476 */
|
|
477 private String settingsFile() {
|
|
478 return settingsDirectory() + "tn5250jstartup.cfg";
|
|
479
|
|
480 }
|
|
481
|
|
482 /**
|
|
483 * Private helper to return the settings directory
|
|
484 *
|
|
485 * @return
|
|
486 */
|
|
487 private String settingsDirectory() {
|
|
488 return settingsFolder() + File.separator;
|
|
489
|
|
490 }
|
|
491
|
|
492 /**
|
|
493 * Private helper to return the settings folder path
|
|
494 *
|
|
495 * @return
|
|
496 */
|
|
497 private String settingsFolder() {
|
|
498 return TerminalView.android_home_directory + File.separator + TN5250J_FOLDER;
|
|
499
|
|
500 }
|
|
501
|
|
502 /**
|
|
503 * Not sure yet so be careful using this.
|
|
504 *
|
|
505 * @return
|
|
506 */
|
|
507 public ClassLoader getClassLoader() {
|
|
508
|
|
509 ClassLoader loader = GlobalConfigure.class.getClassLoader();
|
|
510 if (loader == null)
|
|
511 loader = ClassLoader.getSystemClassLoader();
|
|
512
|
|
513 return loader;
|
|
514 }
|
|
515
|
|
516 }
|