Update to Worldwind release 20070920
[worldwind-tracker.git] / gov / nasa / worldwind / Configuration.java
blob24a28772fa98a87e85e6675d5f9e19e2d6a01039
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 /**
9 @version $Id: Configuration.java 2915 2007-09-19 06:01:49Z tgaskins $
10 @author Tom Gaskins
12 package gov.nasa.worldwind;
14 import gov.nasa.worldwind.avlist.AVKey;
15 import gov.nasa.worldwind.awt.AWTInputHandler;
16 import gov.nasa.worldwind.cache.*;
17 import gov.nasa.worldwind.geom.Angle;
18 import gov.nasa.worldwind.globes.Earth;
19 import gov.nasa.worldwind.retrieve.BasicRetrievalService;
20 import gov.nasa.worldwind.util.*;
21 import gov.nasa.worldwind.view.BasicOrbitView;
23 import java.io.*;
24 import java.util.Properties;
25 import java.util.logging.Level;
27 public class Configuration // Singleton
29 private static final String DEFAULT_LOGGER_NAME = "gov.nasa.worldwind";
30 private static final String CONFIG_FILE_NAME = "config/worldwind.properties";
31 private static final String CONFIG_FILE_PROPERTY_KEY = "gov.nasa.worldwind.config.file";
33 private static Configuration ourInstance = new Configuration();
35 private static Configuration getInstance()
37 return ourInstance;
40 private final Properties properties;
42 private Configuration()
44 this.properties = new Properties(initializeDefaults());
45 this.initializeCustom();
48 private Properties initializeDefaults()
50 Properties defaults = new Properties();
51 defaults.setProperty(AVKey.LOGGER_NAME, "gov.nasa.worldwind");
52 defaults.setProperty(AVKey.DATA_FILE_CACHE_CONFIGURATION_FILE_NAME, "config/DataFileCache.xml");
53 defaults.setProperty(AVKey.DATA_FILE_CACHE_CLASS_NAME, BasicDataFileCache.class.getName());
54 defaults.setProperty(AVKey.GLOBE_CLASS_NAME, Earth.class.getName());
55 defaults.setProperty(AVKey.INPUT_HANDLER_CLASS_NAME, AWTInputHandler.class.getName());
56 defaults.setProperty(AVKey.MEMORY_CACHE_SET_CLASS_NAME, BasicMemoryCacheSet.class.getName());
57 defaults.setProperty(AVKey.WORLD_WINDOW_CLASS_NAME, WorldWindowGLAutoDrawable.class.getName());
58 defaults.setProperty(AVKey.MODEL_CLASS_NAME, BasicModel.class.getName());
59 defaults.setProperty(AVKey.RETRIEVAL_SERVICE_CLASS_NAME, BasicRetrievalService.class.getName());
60 defaults.setProperty(AVKey.SCENE_CONTROLLER_CLASS_NAME, BasicSceneController.class.getName());
61 defaults.setProperty(AVKey.TASK_SERVICE_CLASS_NAME, ThreadedTaskService.class.getName());
62 defaults.setProperty(AVKey.VIEW_CLASS_NAME, BasicOrbitView.class.getName());
63 defaults.setProperty(AVKey.LAYERS_CLASS_NAMES,
64 gov.nasa.worldwind.layers.Earth.StarsLayer.class.getName()
65 + "," + gov.nasa.worldwind.layers.Earth.BMNGSurfaceLayer.class.getName()
66 + "," + gov.nasa.worldwind.layers.Earth.LandsatI3.class.getName()
67 + "," + gov.nasa.worldwind.layers.Earth.USGSUrbanAreaOrtho.class.getName()
68 + "," + gov.nasa.worldwind.layers.Earth.EarthNASAPlaceNameLayer.class.getName()
69 + "," + gov.nasa.worldwind.layers.CompassLayer.class.getName()
71 defaults.setProperty(AVKey.BMNG_ONE_IMAGE_PATH, "images/BMNG_world.topo.bathy.200405.3.2048x1024.jpg");
73 java.util.TimeZone tz = java.util.Calendar.getInstance().getTimeZone();
74 if (tz != null)
75 defaults.setProperty(AVKey.INITIAL_LONGITUDE,
76 Double.toString(
77 Angle.fromDegrees(180.0 * tz.getOffset(System.currentTimeMillis()) / (12.0 * 3.6e6)).degrees));
78 return defaults;
81 private void initializeCustom()
83 String configFileName = System.getProperty(CONFIG_FILE_PROPERTY_KEY, CONFIG_FILE_NAME);
84 try
86 InputStream propsStream = this.getClass().getResourceAsStream("/" + configFileName);
87 if (propsStream == null)
89 File propsFile = new File(configFileName);
90 if (propsFile.exists())
91 propsStream = new FileInputStream(propsFile);
92 else
93 Logging.logger().log(Level.WARNING, "Configuration.UnavailablePropsFile", configFileName);
96 if (propsStream != null)
97 this.properties.load(propsStream);
99 // Use a named logger in all the catch statements below to prevent Logger from calling back into
100 // Configuration when this Configuration instance is not yet fully instantiated.
101 catch (FileNotFoundException e)
103 Logging.logger(DEFAULT_LOGGER_NAME).log(Level.WARNING, "Configuration.UnavailablePropsFile",
104 configFileName);
106 catch (IOException e)
108 Logging.logger(DEFAULT_LOGGER_NAME).log(Level.SEVERE, "Configuration.ExceptionReadingPropsFile", e);
110 catch (Exception e)
112 Logging.logger(DEFAULT_LOGGER_NAME).log(Level.SEVERE, "Configuration.ExceptionReadingPropsFile", e);
116 public static synchronized String getStringValue(String key, String defaultValue)
118 String v = getStringValue(key);
119 return v != null ? v : defaultValue;
122 public static synchronized String getStringValue(String key)
124 return getInstance().properties.getProperty(key);
127 public static synchronized Integer getIntegerValue(String key, Integer defaultValue)
129 Integer v = getIntegerValue(key);
130 return v != null ? v : defaultValue;
133 public static synchronized Integer getIntegerValue(String key)
135 String v = getStringValue(key);
136 if (v == null)
137 return null;
141 return Integer.parseInt(v);
143 catch (NumberFormatException e)
145 Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
146 return null;
150 public static synchronized Long getLongValue(String key, Long defaultValue)
152 Long v = getLongValue(key);
153 return v != null ? v : defaultValue;
156 public static synchronized Long getLongValue(String key)
158 String v = getStringValue(key);
159 if (v == null)
160 return null;
164 return Long.parseLong(v);
166 catch (NumberFormatException e)
168 Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
169 return null;
173 public static synchronized Double getDoubleValue(String key, Double defaultValue)
175 Double v = getDoubleValue(key);
176 return v != null ? v : defaultValue;
179 public static synchronized Double getDoubleValue(String key)
181 String v = getStringValue(key);
182 if (v == null)
183 return null;
187 return Double.parseDouble(v);
189 catch (NumberFormatException e)
191 Logging.logger().log(Level.SEVERE, "Configuration.ConversionError", v);
192 return null;
196 public static synchronized boolean hasKey(String key)
198 return getInstance().properties.contains(key);
201 public static synchronized void removeKey(String key)
203 getInstance().properties.remove(key);
206 public static synchronized void setValue(String key, Object value)
208 getInstance().properties.put(key, value.toString());
211 // OS, user, and run-time specific system properties. //
213 public static String currentWorkingDirectory()
215 String dir = System.getProperty("user.dir");
216 return (dir != null) ? dir : ".";
219 public static String getUserHomeDirectory()
221 String dir = System.getProperty("user.home");
222 return (dir != null) ? dir : ".";
225 public static String systemTempDirectory()
227 String dir = System.getProperty("java.io.tmpdir");
228 return (dir != null) ? dir : ".";
231 public static boolean isMacOS()
233 String osName = System.getProperty("os.name");
234 return osName != null && osName.toLowerCase().contains("mac");
237 public static boolean isWindowsOS()
239 String osName = System.getProperty("os.name");
240 return osName != null && osName.toLowerCase().contains("windows");
243 public static boolean isLinuxOS()
245 String osName = System.getProperty("os.name");
246 return osName != null && osName.toLowerCase().contains("linux");
249 public static boolean isUnixOS()
251 String osName = System.getProperty("os.name");
252 return osName != null && osName.toLowerCase().contains("unix");
255 public static boolean isSolarisOS()
257 String osName = System.getProperty("os.name");
258 return osName != null && osName.toLowerCase().contains("solaris");