Mercurial > 510Connectbot
annotate src/org/tn5250j/GlobalConfigure.java @ 62:aac8e0496ef9 tn5250
add 5250 config entries
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 13 Jun 2014 15:33:16 -0700 |
parents | b29b39f386a4 |
children | 8f23b05a51f7 |
rev | line source |
---|---|
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; |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
42 import android.util.Log; |
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
43 |
20 | 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 { | |
27
b29b39f386a4
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
52 private static final String TAG = "GlobalConfigure"; |
20 | 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 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
67 |
20 | 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 { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
115 Log.i(TAG,"Settings folder '"+settingsfolder+"' doesn't exist. Will created now."); |
20 | 116 f.mkdir(); |
117 } catch (Exception e) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
118 Log.w(TAG,"Couldn't create settings folder '"+settingsfolder+"'", e); |
20 | 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() { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
153 Log.i(TAG,"reloading settings"); |
20 | 154 loadSettings(); |
155 loadSessions(); | |
156 loadMacros(); | |
157 loadKeyStrokes(); | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
158 Log.i(TAG,"Done (reloading settings)."); |
20 | 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) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
174 Log.i(TAG," Information Message: " |
20 | 175 + fnfea.getMessage() + ". The file " + settingsFile() |
176 + " will be created for first time use."); | |
177 saveSettings(); | |
178 } | |
179 catch (IOException ioea) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
180 Log.w(TAG,"IO Exception accessing File " |
20 | 181 + settingsFile() + " for the following reason : " |
182 + ioea.getMessage()); | |
183 } | |
184 catch (SecurityException sea) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
185 Log.w(TAG,"Security Exception for file " |
20 | 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) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
250 Log.w(TAG,"File not found : writing file " |
20 | 251 + fileName + ". Description of error is " |
252 + fnfe.getMessage()); | |
253 } | |
254 catch (IOException ioe) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
255 Log.w(TAG,"IO Exception : writing file " |
20 | 256 + fileName + ". Description of error is " |
257 + ioe.getMessage()); | |
258 } | |
259 catch (SecurityException se) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
260 Log.w(TAG,"Security Exception : writing file " |
20 | 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) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
318 Log.i(TAG," Information Message: " + fnfe.getMessage() |
20 | 319 + ". The file " + fileName + " will" |
320 + " be created for first time use."); | |
321 | |
322 saveSettings(regKey,header); | |
323 | |
324 } | |
325 else { | |
326 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
327 Log.i(TAG," Information Message: " + fnfe.getMessage() |
20 | 328 + "."); |
329 | |
330 } | |
331 } | |
332 catch (IOException ioe) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
333 Log.w(TAG,"IO Exception accessing File "+ fileName + |
20 | 334 " for the following reason : " |
335 + ioe.getMessage()); | |
336 } | |
337 catch (SecurityException se) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
338 Log.w(TAG,"Security Exception for file "+ fileName |
20 | 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 | |
22 | 378 public Properties getProperties(String regKey, |
379 String fileName, | |
380 boolean createFile, | |
381 String header, | |
382 boolean reloadIfLoaded) { | |
20 | 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) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
399 Log.i(TAG," Information Message: " + fnfe.getMessage() |
20 | 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 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
412 Log.i(TAG," Information Message: " + fnfe.getMessage() |
20 | 413 + "."); |
414 | |
415 } | |
416 } | |
417 catch (IOException ioe) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
418 Log.w(TAG,"IO Exception accessing File "+ fileName + |
20 | 419 " for the following reason : " |
420 + ioe.getMessage()); | |
421 } | |
422 catch (SecurityException se) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
22
diff
changeset
|
423 Log.w(TAG,"Security Exception for file "+ fileName |
20 | 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 } |