comparison src/org/tn5250j/GlobalConfigure.java @ 79:01d939969b10

merge tn5250 branch into default
author Carl Byington <carl@five-ten-sg.com>
date Mon, 16 Jun 2014 08:24:00 -0700
parents b29b39f386a4
children 8f23b05a51f7
comparison
equal deleted inserted replaced
19:b3d0d806cbe2 79:01d939969b10
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 com.five_ten_sg.connectbot.TerminalView;
41 import org.tn5250j.interfaces.ConfigureFactory;
42 import android.util.Log;
43
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 private static final String TAG = "GlobalConfigure";
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
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 Log.i(TAG,"Settings folder '"+settingsfolder+"' doesn't exist. Will created now.");
116 f.mkdir();
117 } catch (Exception e) {
118 Log.w(TAG,"Couldn't create settings folder '"+settingsfolder+"'", e);
119 }
120 }
121 }
122
123 /**
124 * Load the sessions properties
125 */
126 private void loadSessions() {
127
128 setProperties(SESSIONS,SESSIONS,"------ Sessions --------",true);
129 }
130
131 /**
132 * Load the macros
133 */
134 private void loadMacros() {
135
136 setProperties(MACROS,MACROS,"------ Macros --------",true);
137
138 }
139
140 private void loadKeyStrokes() {
141
142 setProperties(KEYMAP,KEYMAP,
143 "------ Key Map key=keycode,isShiftDown,isControlDown,isAltDown,isAltGrDown --------",
144 true);
145
146 }
147
148 /**
149 * Reload the environment settings.
150 */
151 @Override
152 public void reloadSettings() {
153 Log.i(TAG,"reloading settings");
154 loadSettings();
155 loadSessions();
156 loadMacros();
157 loadKeyStrokes();
158 Log.i(TAG,"Done (reloading settings).");
159 }
160
161 /**
162 * Loads the emulator setting from the setting(s) file
163 */
164 private void loadSettings() {
165
166 FileInputStream in = null;
167 settings = new Properties();
168
169 try {
170 in = new FileInputStream(settingsFile());
171 settings.load(in);
172 }
173 catch (FileNotFoundException fnfea) {
174 Log.i(TAG," Information Message: "
175 + fnfea.getMessage() + ". The file " + settingsFile()
176 + " will be created for first time use.");
177 saveSettings();
178 }
179 catch (IOException ioea) {
180 Log.w(TAG,"IO Exception accessing File "
181 + settingsFile() + " for the following reason : "
182 + ioea.getMessage());
183 }
184 catch (SecurityException sea) {
185 Log.w(TAG,"Security Exception for file "
186 + settingsFile() + " This file can not be "
187 + "accessed because : " + sea.getMessage());
188 }
189 }
190
191 /**
192 * Save the settings for the global configuration
193 */
194 @Override
195 public void saveSettings() {
196
197 try {
198 FileOutputStream out = new FileOutputStream(settingsFile());
199 settings.store(out,"----------------- tn5250j Global Settings --------------");
200 }
201 catch (FileNotFoundException fnfe) {}
202 catch (IOException ioe) {}
203 }
204
205 /**
206 * Save the setting in the registry using the key passed in with no header
207 * output.
208 *
209 * @param regKey
210 */
211 @Override
212 public void saveSettings(String regKey) {
213
214 saveSettings(regKey,"");
215 }
216
217 /**
218 * Save the settings in the registry using the key passed with a header
219 * in the output.
220 *
221 * @param regKey
222 * @param Header
223 */
224 @Override
225 public void saveSettings(String regKey, String header) {
226
227 saveSettings(regKey,regKey,header);
228 }
229
230 /**
231 * Save the settings in the registry using the key passed with a header
232 * in the output.
233 *
234 * @param regKey
235 * @param Header
236 */
237 @Override
238 public void saveSettings(String regKey, String fileName, String header) {
239
240 if (registry.containsKey(regKey)) {
241 try {
242 FileOutputStream out = new FileOutputStream(
243 settingsDirectory() + fileName);
244 Properties props = (Properties)registry.get(regKey);
245 props.store(out,header);
246 out.flush();
247 out.close();
248 }
249 catch (FileNotFoundException fnfe) {
250 Log.w(TAG,"File not found : writing file "
251 + fileName + ". Description of error is "
252 + fnfe.getMessage());
253 }
254 catch (IOException ioe) {
255 Log.w(TAG,"IO Exception : writing file "
256 + fileName + ". Description of error is "
257 + ioe.getMessage());
258 }
259 catch (SecurityException se) {
260 Log.w(TAG,"Security Exception : writing file "
261 + fileName + ". Description of error is "
262 + se.getMessage());
263 }
264
265 }
266
267 }
268
269 /**
270 * Place the Properties in the registry under a given registry name
271 *
272 * @param regKey
273 * @param regProps
274 */
275 @Override
276 public void setProperties(String regKey, Properties regProps) {
277
278 registry.put(regKey, regProps);
279
280 }
281
282 /**
283 * Set the properties for the given registry key.
284 *
285 * @param regKey
286 * @param fileName
287 * @param header
288 */
289 @Override
290 public void setProperties(String regKey, String fileName, String header) { //LG NEW
291 setProperties(regKey, fileName, header, false);
292 }
293
294 /**
295 * Set the properties for the given registry key.
296 *
297 * @param regKey
298 * @param fileName
299 * @param header
300 * @param createFile
301 */
302 @Override
303 public void setProperties(String regKey, String fileName, String header,
304 boolean createFile) {
305
306 FileInputStream in = null;
307 Properties props = new Properties();
308 headers.put(regKey, header);
309
310 try {
311 in = new FileInputStream(settingsDirectory() + fileName);
312 props.load(in);
313
314 }
315 catch (FileNotFoundException fnfe) {
316
317 if (createFile) {
318 Log.i(TAG," Information Message: " + fnfe.getMessage()
319 + ". The file " + fileName + " will"
320 + " be created for first time use.");
321
322 saveSettings(regKey,header);
323
324 }
325 else {
326
327 Log.i(TAG," Information Message: " + fnfe.getMessage()
328 + ".");
329
330 }
331 }
332 catch (IOException ioe) {
333 Log.w(TAG,"IO Exception accessing File "+ fileName +
334 " for the following reason : "
335 + ioe.getMessage());
336 }
337 catch (SecurityException se) {
338 Log.w(TAG,"Security Exception for file "+ fileName
339 + ". This file can not be accessed because : "
340 + se.getMessage());
341 }
342
343 registry.put(regKey,props);
344
345 }
346
347 /**
348 * Returns the properties associated with a given registry key.
349 *
350 * @param regKey
351 * @return
352 */
353 @Override
354 public Properties getProperties(String regKey) {
355
356 if (registry.containsKey(regKey)) {
357 return (Properties)registry.get(regKey);
358 }
359 return null;
360 }
361
362 public Properties getProperties() {
363 return settings;
364 }
365
366 @Override
367 public Properties getProperties(String regKey,String fileName) {
368 return getProperties(regKey,fileName,false,"",false);
369 }
370
371 @Override
372 public Properties getProperties(String regKey,String fileName,
373 boolean createFile, String header) {
374 return getProperties(regKey,fileName,false,"",false);
375 }
376
377 @Override
378 public Properties getProperties(String regKey,
379 String fileName,
380 boolean createFile,
381 String header,
382 boolean reloadIfLoaded) {
383
384 if (!registry.containsKey(regKey) || reloadIfLoaded) {
385
386 FileInputStream in = null;
387 Properties props = new Properties();
388 headers.put(regKey, header);
389
390 try {
391 in = new FileInputStream(settingsDirectory()
392 + fileName);
393 props.load(in);
394
395 }
396 catch (FileNotFoundException fnfe) {
397
398 if (createFile) {
399 Log.i(TAG," Information Message: " + fnfe.getMessage()
400 + ". The file " + fileName + " will"
401 + " be created for first time use.");
402
403 registry.put(regKey,props);
404
405 saveSettings(regKey,header);
406
407 return props;
408
409 }
410 else {
411
412 Log.i(TAG," Information Message: " + fnfe.getMessage()
413 + ".");
414
415 }
416 }
417 catch (IOException ioe) {
418 Log.w(TAG,"IO Exception accessing File "+ fileName +
419 " for the following reason : "
420 + ioe.getMessage());
421 }
422 catch (SecurityException se) {
423 Log.w(TAG,"Security Exception for file "+ fileName
424 + ". This file can not be accessed because : "
425 + se.getMessage());
426 }
427
428 registry.put(regKey,props);
429
430 return props;
431 }
432 else {
433 return (Properties)registry.get(regKey);
434 }
435 }
436
437 /**
438 * Returns the setting from the given key of the global properties or the
439 * default passed if the property does not exist.
440 *
441 * @param key
442 * @param def
443 * @return
444 */
445 @Override
446 public String getProperty(String key, String def) {
447 if (settings.containsKey(key))
448 return settings.getProperty(key);
449 else
450 return def;
451 }
452
453 /**
454 * Returns the setting from the given key of the global properties.
455 *
456 * @param key
457 * @return
458 */
459 @Override
460 public String getProperty(String key) {
461 return settings.getProperty(key);
462 }
463
464 /**
465 * Private helper to return the full path to the settings file
466 *
467 * @return
468 */
469 private String settingsFile() {
470 return settingsDirectory() + "tn5250jstartup.cfg";
471
472 }
473
474 /**
475 * Private helper to return the settings directory
476 *
477 * @return
478 */
479 private String settingsDirectory() {
480 return settingsFolder() + File.separator;
481
482 }
483
484 /**
485 * Private helper to return the settings folder path
486 *
487 * @return
488 */
489 private String settingsFolder() {
490 return TerminalView.android_home_directory + File.separator + TN5250J_FOLDER;
491
492 }
493
494 /**
495 * Not sure yet so be careful using this.
496 *
497 * @return
498 */
499 public ClassLoader getClassLoader() {
500
501 ClassLoader loader = GlobalConfigure.class.getClassLoader();
502 if (loader == null)
503 loader = ClassLoader.getSystemClassLoader();
504
505 return loader;
506 }
507
508 }