-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathConfiguration.java
More file actions
717 lines (651 loc) · 26.3 KB
/
Configuration.java
File metadata and controls
717 lines (651 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwind;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.util.*;
import org.w3c.dom.*;
import com.jogamp.opengl.*;
import javax.xml.xpath.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
/**
* This class manages the initial WorldWind configuration. It reads WorldWind configuration files and registers their
* contents. Configurations files contain the names of classes to create at run-time, the initial model definition,
* including the globe, elevation model and layers, and various control quantities such as cache sizes and data
* retrieval timeouts.
* <p/>
* The Configuration class is a singleton, but its instance is not exposed publicly. It is addressed only via static
* methods of the class. It is constructed upon first use of any of its static methods.
* <p/>
* When the Configuration class is first instantiated it reads the XML document <code>config/worldwind.xml</code> and
* registers all the information there. The information can subsequently be retrieved via the class' various
* <code>getValue</code> methods. Many WorldWind start-up objects query this information to determine the classes to
* create. For example, the first WorldWind object created by an application is typically a {@link
* gov.nasa.worldwind.awt.WorldWindowGLCanvas}. During construction that class causes WorldWind's internal classes to
* be constructed, using the names of those classes drawn from the Configuration singleton, this class.
* <p/>
* The default WorldWind configuration document is <code>config/worldwind.xml</code>. This can be changed by setting
* the Java property <code>gov.nasa.worldwind.config.file</code> to a different file name or a valid URL prior to
* creating any WorldWind object or invoking any static methods of WorldWind classes, including the Configuration
* class. When an application specifies a different configuration location it typically does so in its main method prior
* to using WorldWind. If a file is specified its location must be on the classpath. (The contents of application and
* WorldWind jar files are typically on the classpath, in which case the configuration file may be in the jar file.)
* <p/>
* Additionally, an application may set another Java property, <code>gov.nasa.worldwind.app.config.document</code>, to a
* file name or URL whose contents contain configuration values to override those of the primary configuration document.
* WorldWind overrides only those values in this application document, it leaves all others to the value specified in
* the primary document. Applications usually specify an override document in order to specify the initial layers in the
* model.
* <p/>
* See <code>config/worldwind.xml</code> for documentation on setting configuration values.
* <p/>
* Configuration values can also be set programatically via {@link Configuration#setValue(String, Object)}, but they are
* not retroactive so affect only Configuration queries made subsequent to setting the value.
* <p/>
* <em>Note:</em> Prior to September of 2009, configuration properties were read from the file
* <code>config/worldwind.properties</code>. An alternate file could be specified via the
* <code>gov.nasa.worldwind.config.file</code> Java property. These mechanisms remain available but are deprecated.
* WorldWind no longer contains a <code>worldwind.properties</code> file. If <code>worldwind.properties</code> or its
* replacement as specified through the Java property exists at run-time and can be found via the classpath,
* configuration values specified by that mechanism are given precedence over values specified by the new mechanism.
*
* @author Tom Gaskins
* @version $Id: Configuration.java 1739 2013-12-04 03:38:19Z dcollins $
*/
public class Configuration // Singleton
{
public static final String DEFAULT_LOGGER_NAME = "gov.nasa.worldwind";
private static final String CONFIG_PROPERTIES_FILE_NAME = "config/worldwind.properties";
private static final String CONFIG_FILE_PROPERTY_KEY = "gov.nasa.worldwind.config.file";
private static final String CONFIG_WW_DOCUMENT_KEY = "gov.nasa.worldwind.config.document";
private static final String CONFIG_WW_DOCUMENT_NAME = "config/worldwind.xml";
private static final String CONFIG_APP_DOCUMENT_KEY = "gov.nasa.worldwind.app.config.document";
private static Configuration ourInstance = new Configuration();
private static Configuration getInstance()
{
return ourInstance;
}
private final Properties properties;
private final ArrayList<Document> configDocs = new ArrayList<Document>();
/** Private constructor invoked only internally. */
private Configuration()
{
this.properties = initializeDefaults();
// Load the app's configuration if there is one
try
{
String appConfigLocation = System.getProperty(CONFIG_APP_DOCUMENT_KEY);
if (appConfigLocation != null)
this.loadConfigDoc(System.getProperty(CONFIG_APP_DOCUMENT_KEY)); // Load app's config first
}
catch (Exception e)
{
Logging.logger(DEFAULT_LOGGER_NAME).log(Level.WARNING, "Configuration.ConfigNotFound",
System.getProperty(CONFIG_APP_DOCUMENT_KEY));
// Don't stop if the app config file can't be found or parsed
}
try
{
// Load the default configuration
this.loadConfigDoc(System.getProperty(CONFIG_WW_DOCUMENT_KEY, CONFIG_WW_DOCUMENT_NAME));
// Load config properties, ensuring that the app's config takes precedence over wwj's
for (int i = this.configDocs.size() - 1; i >= 0; i--)
{
this.loadConfigProperties(this.configDocs.get(i));
}
}
catch (Exception e)
{
Logging.logger(DEFAULT_LOGGER_NAME).log(Level.WARNING, "Configuration.ConfigNotFound",
System.getProperty(CONFIG_WW_DOCUMENT_KEY));
}
// To support old-style configuration, read an existing config properties file and give the properties
// specified there precedence.
this.initializeCustom();
}
private void loadConfigDoc(String configLocation)
{
if (!WWUtil.isEmpty(configLocation))
{
Document doc = WWXML.openDocument(configLocation);
if (doc != null)
{
this.configDocs.add(doc);
// this.loadConfigProperties(doc);
}
}
}
private void insertConfigDoc(String configLocation)
{
if (!WWUtil.isEmpty(configLocation))
{
Document doc = WWXML.openDocument(configLocation);
if (doc != null)
{
this.configDocs.add(0, doc);
this.loadConfigProperties(doc);
}
}
}
private void loadConfigProperties(Document doc)
{
try
{
XPath xpath = WWXML.makeXPath();
NodeList nodes = (NodeList) xpath.evaluate("/WorldWindConfiguration/Property", doc, XPathConstants.NODESET);
if (nodes == null || nodes.getLength() == 0)
return;
for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);
String prop = xpath.evaluate("@name", node);
String value = xpath.evaluate("@value", node);
if (WWUtil.isEmpty(prop))// || WWUtil.isEmpty(value))
continue;
this.properties.setProperty(prop, value);
}
}
catch (XPathExpressionException e)
{
Logging.logger(DEFAULT_LOGGER_NAME).log(Level.WARNING, "XML.ParserConfigurationException");
}
}
private Properties initializeDefaults()
{
Properties defaults = new Properties();
java.util.TimeZone tz = java.util.Calendar.getInstance().getTimeZone();
if (tz != null)
defaults.setProperty(AVKey.INITIAL_LONGITUDE,
Double.toString(
Angle.fromDegrees(180.0 * tz.getOffset(System.currentTimeMillis()) / (12.0 * 3.6e6)).degrees));
return defaults;
}
private void initializeCustom()
{
// IMPORTANT NOTE: Always use the single argument version of Logging.logger in this method because the non-arg
// method assumes an instance of Configuration already exists.
String configFileName = System.getProperty(CONFIG_FILE_PROPERTY_KEY, CONFIG_PROPERTIES_FILE_NAME);
try
{
java.io.InputStream propsStream = null;
File file = new File(configFileName);
if (file.exists())
{
try
{
propsStream = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
Logging.logger(DEFAULT_LOGGER_NAME).log(Level.FINEST, "Configuration.LocalConfigFileNotFound",
configFileName);
}
}
if (propsStream == null)
{
propsStream = this.getClass().getResourceAsStream("/" + configFileName);
}
if (propsStream != null)
this.properties.load(propsStream);
}
// Use a named logger in all the catch statements below to prevent Logger from calling back into
// Configuration when this Configuration instance is not yet fully instantiated.
catch (IOException e)
{
Logging.logger(DEFAULT_LOGGER_NAME).log(Level.SEVERE, "Configuration.ExceptionReadingPropsFile", e);
}
}
public static void insertConfigurationDocument(String fileName)
{
getInstance().insertConfigDoc(fileName);
}
/**
* Return as a string the value associated with a specified key.
*
* @param key the key for the desired value.
* @param defaultValue the value to return if the key does not exist.
*
* @return the value associated with the key, or the specified default value if the key does not exist.
*/
public static synchronized String getStringValue(String key, String defaultValue)
{
String v = getStringValue(key);
return v != null ? v : defaultValue;
}
/**
* Return as a string the value associated with a specified key.
*
* @param key the key for the desired value.
*
* @return the value associated with the key, or null if the key does not exist.
*/
public static synchronized String getStringValue(String key)
{
Object o = getInstance().properties.getProperty(key);
return o != null ? o.toString() : null;
}
/**
* Return as an Integer the value associated with a specified key.
*
* @param key the key for the desired value.
* @param defaultValue the value to return if the key does not exist.
*
* @return the value associated with the key, or the specified default value if the key does not exist or is not an
* Integer or string representation of an Integer.
*/
public static synchronized Integer getIntegerValue(String key, Integer defaultValue)
{
Integer v = getIntegerValue(key);
return v != null ? v : defaultValue;
}
/**
* Return as an Integer the value associated with a specified key.
*
* @param key the key for the desired value.
*
* @return the value associated with the key, or null if the key does not exist or is not an Integer or string
* representation of an Integer.
*/
public static synchronized Integer getIntegerValue(String key)
{
String v = getStringValue(key);
if (v == null)
return null;
try
{
return Integer.parseInt(v);
}
catch (NumberFormatException e)
{
Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
return null;
}
}
/**
* Return as an Long the value associated with a specified key.
*
* @param key the key for the desired value.
* @param defaultValue the value to return if the key does not exist.
*
* @return the value associated with the key, or the specified default value if the key does not exist or is not a
* Long or string representation of a Long.
*/
public static synchronized Long getLongValue(String key, Long defaultValue)
{
Long v = getLongValue(key);
return v != null ? v : defaultValue;
}
/**
* Return as an Long the value associated with a specified key.
*
* @param key the key for the desired value.
*
* @return the value associated with the key, or null if the key does not exist or is not a Long or string
* representation of a Long.
*/
public static synchronized Long getLongValue(String key)
{
String v = getStringValue(key);
if (v == null)
return null;
try
{
return Long.parseLong(v);
}
catch (NumberFormatException e)
{
Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
return null;
}
}
/**
* Return as an Double the value associated with a specified key.
*
* @param key the key for the desired value.
* @param defaultValue the value to return if the key does not exist.
*
* @return the value associated with the key, or the specified default value if the key does not exist or is not an
* Double or string representation of an Double.
*/
public static synchronized Double getDoubleValue(String key, Double defaultValue)
{
Double v = getDoubleValue(key);
return v != null ? v : defaultValue;
}
/**
* Return as an Double the value associated with a specified key.
*
* @param key the key for the desired value.
*
* @return the value associated with the key, or null if the key does not exist or is not an Double or string
* representation of an Double.
*/
public static synchronized Double getDoubleValue(String key)
{
String v = getStringValue(key);
if (v == null)
return null;
try
{
return Double.parseDouble(v);
}
catch (NumberFormatException e)
{
Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
return null;
}
}
/**
* Return as a Boolean the value associated with a specified key.
* <p/>
* Valid values for true are '1' or anything that starts with 't' or 'T'. ie. 'true', 'True', 't' Valid values for
* false are '0' or anything that starts with 'f' or 'F'. ie. 'false', 'False', 'f'
*
* @param key the key for the desired value.
* @param defaultValue the value to return if the key does not exist.
*
* @return the value associated with the key, or the specified default value if the key does not exist or is not a
* Boolean or string representation of an Boolean.
*/
public static synchronized Boolean getBooleanValue(String key, Boolean defaultValue)
{
Boolean v = getBooleanValue(key);
return v != null ? v : defaultValue;
}
/**
* Return as a Boolean the value associated with a specified key.
* <p/>
* Valid values for true are '1' or anything that starts with 't' or 'T'. ie. 'true', 'True', 't' Valid values for
* false are '0' or anything that starts with 'f' or 'F'. ie. 'false', 'False', 'f'
*
* @param key the key for the desired value.
*
* @return the value associated with the key, or null if the key does not exist or is not a Boolean or string
* representation of an Boolean.
*/
public static synchronized Boolean getBooleanValue(String key)
{
String v = getStringValue(key);
if (v == null)
return null;
if (v.trim().toUpperCase().startsWith("T") || v.trim().equals("1"))
{
return true;
}
else if (v.trim().toUpperCase().startsWith("F") || v.trim().equals("0"))
{
return false;
}
else
{
Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
return null;
}
}
/**
* Determines whether a key exists in the configuration.
*
* @param key the key of interest.
*
* @return true if the key exists, otherwise false.
*/
public static synchronized boolean hasKey(String key)
{
return getInstance().properties.contains(key);
}
/**
* Removes a key and its value from the configuration if the configuration contains the key.
*
* @param key the key of interest.
*/
public static synchronized void removeKey(String key)
{
getInstance().properties.remove(key);
}
/**
* Adds a key and value to the configuration, or changes the value associated with the key if the key is already in
* the configuration.
*
* @param key the key to set.
* @param value the value to associate with the key.
*/
public static synchronized void setValue(String key, Object value)
{
getInstance().properties.put(key, value.toString());
}
// OS, user, and run-time specific system properties. //
/**
* Returns the path to the application's current working directory.
*
* @return the absolute path to the application's current working directory.
*/
public static String getCurrentWorkingDirectory()
{
String dir = System.getProperty("user.dir");
return (dir != null) ? dir : ".";
}
/**
* Returns the path to the application user's home directory.
*
* @return the absolute path to the application user's home directory.
*/
public static String getUserHomeDirectory()
{
String dir = System.getProperty("user.home");
return (dir != null) ? dir : ".";
}
/**
* Returns the path to the operating system's temp directory.
*
* @return the absolute path to the operating system's temporary directory.
*/
public static String getSystemTempDirectory()
{
String dir = System.getProperty("java.io.tmpdir");
return (dir != null) ? dir : ".";
}
/**
* Returns the path to the current user's application data directory. The path returned depends on the operating
* system on which the Java Virtual Machine is running. The following table provides the path for all supported
* operating systems:
* <p/>
* <table> <tr><th>Operating System</th><th>Path</th></tr> <tr><td>Mac OS X</td><td>~/Library/Application
* Support</td></tr> <tr><td>Windows</td><td>~\\Application Data</td></tr> <tr><td>Linux, Unix,
* Solaris</td><td>~/</td></tr> </table>
*
* @return the absolute path to the current user's application data directory.
*/
public static String getCurrentUserAppDataDirectory()
{
if (isMacOS())
{
// Return a path that Mac OS X has designated for app-specific data and support files. See the following URL
// for details:
// http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/MacOSXDirectories/MacOSXDirectories.html#//apple_ref/doc/uid/TP40010672-CH10-SW1
return getUserHomeDirectory() + "/Library/Application Support";
}
else if (isWindowsOS())
{
return getUserHomeDirectory() + "\\Application Data";
}
else if (isLinuxOS() || isUnixOS() || isSolarisOS())
{
return getUserHomeDirectory();
}
else
{
String msg = Logging.getMessage("generic.UnknownOperatingSystem");
Logging.logger().fine(msg);
return null;
}
}
/**
* Determines whether the operating system is a Mac operating system.
*
* @return true if the operating system is a Mac operating system, otherwise false.
*/
public static boolean isMacOS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("mac");
}
/**
* Determines whether the operating system is Windows operating system.
*
* @return true if the operating system is a Windows operating system, otherwise false.
*/
public static boolean isWindowsOS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("windows");
}
/**
* Determines whether the operating system is Windows XP operating system.
*
* @return true if the operating system is a Windows XP operating system, otherwise false.
*/
public static boolean isWindowsXPOS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("windows") && osName.contains("xp");
}
/**
* Determines whether the operating system is Windows Vista operating system.
*
* @return true if the operating system is a Windows Vista operating system, otherwise false.
*/
public static boolean isWindowsVistaOS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("windows") && osName.contains("vista");
}
/**
* Determines whether the operating system is Windows 7 operating system.
*
* @return true if the operating system is a Windows Vista operating system, otherwise false.
*/
public static boolean isWindows7OS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("windows") && osName.contains("7");
}
/**
* Determines whether the operating system is Linux operating system.
*
* @return true if the operating system is a Linux operating system, otherwise false.
*/
public static boolean isLinuxOS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("linux");
}
/**
* Determines whether the operating system is Unix operating system.
*
* @return true if the operating system is a Unix operating system, otherwise false.
*/
public static boolean isUnixOS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("unix");
}
/**
* Determines whether the operating system is Solaris operating system.
*
* @return true if the operating system is a Solaris operating system, otherwise false.
*/
public static boolean isSolarisOS()
{
String osName = System.getProperty("os.name");
return osName != null && osName.toLowerCase().contains("solaris");
}
/**
* Returns the version of the Java virtual machine.
*
* @return the Java virtual machine version.
*/
public static float getJavaVersion()
{
float ver = 0f;
String s = System.getProperty("java.specification.version");
if (null == s || s.length() == 0)
s = System.getProperty("java.version");
try
{
ver = Float.parseFloat(s.trim());
}
catch (NumberFormatException ignore)
{
}
return ver;
}
/**
* Returns the highest OpenGL profile available on the current graphics device that is compatible with WorldWind.
* The returned profile favors hardware acceleration over software acceleration. With JOGL version 2.0, this returns
* the highest available profile from the following list:
* <p/>
* <ul> <li>OpenGL compatibility profile 4.x</li> <li>OpenGL compatibility profile 3.x</li> <li>OpenGL profile 1.x
* up to 3.0</li> </ul>
*
* @return the highest compatible OpenGL profile.
*/
public static GLProfile getMaxCompatibleGLProfile()
{
return GLProfile.getMaxFixedFunc(true); // Favor a hardware rasterizer.
}
/**
* Returns a {@link com.jogamp.opengl.GLCapabilities} identifying graphics features required by World Wind. The
* capabilities instance returned requests the maximum OpenGL profile supporting GL fixed function operations, a
* frame buffer with 8 bits each of red, green, blue and alpha, a 24-bit depth buffer, double buffering, and if the
* Java property "gov.nasa.worldwind.stereo.mode" is set to "device", device supported stereo.
*
* @return a new capabilities instance identifying required graphics features.
*/
public static GLCapabilities getRequiredGLCapabilities()
{
GLCapabilities caps = new GLCapabilities(getMaxCompatibleGLProfile());
caps.setAlphaBits(8);
caps.setRedBits(8);
caps.setGreenBits(8);
caps.setBlueBits(8);
caps.setDepthBits(24);
caps.setDoubleBuffered(true);
// Determine whether we should request a stereo canvas
String stereo = System.getProperty(AVKey.STEREO_MODE);
if ("device".equals(stereo))
caps.setStereo(true);
return caps;
}
/**
* Returns a specified element of an XML configuration document.
*
* @param xpathExpression an XPath expression identifying the element of interest.
*
* @return the element of interest if the XPath expression is valid and the element exists, otherwise null.
*
* @throws NullPointerException if the XPath expression is null.
*/
public static Element getElement(String xpathExpression)
{
XPath xpath = WWXML.makeXPath();
for (Document doc : getInstance().configDocs)
{
try
{
Node node = (Node) xpath.evaluate(xpathExpression, doc.getDocumentElement(), XPathConstants.NODE);
if (node != null)
return (Element) node;
}
catch (XPathExpressionException e)
{
return null;
}
}
return null;
}
}