Worldwind public release 0.2.1
[worldwind-tracker.git] / gov / nasa / worldwind / Configuration.java
blob245d847ebfcb1efe6db2c8b7997b68989f69c41f
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 2163 2007-06-27 18:29:50Z tgaskins $
10 @author Tom Gaskins
12 package gov.nasa.worldwind;
14 import java.io.*;
15 import java.util.*;
17 public class Configuration // Singleton
19 private static final String CONFIG_FILE_NAME = "config/worldwind.properties";
20 private static final String CONFIG_FILE_PROPERTY_KEY = "gov.nasa.worldwind.config.file";
22 private static Configuration ourInstance = new Configuration();
24 private static Configuration getInstance()
26 return ourInstance;
29 private Properties properties;
31 private Configuration()
33 Properties defaults = initializeDefaults();
34 initializeCustom(defaults);
37 private static Properties initializeDefaults()
39 Properties defaults = new Properties();
40 defaults.setProperty(AVKey.DATA_FILE_CACHE_CLASS_NAME, "gov.nasa.worldwind.BasicDataFileCache");
41 defaults.setProperty(AVKey.DATA_FILE_CACHE_CONFIGURATION_FILE_NAME, "config/DataFileCache.xml");
42 defaults.setProperty(AVKey.FRAME_CONTROLLER_CLASS_NAME, "gov.nasa.worldwind.BasicFrameController");
43 defaults.setProperty(AVKey.GLOBE_CLASS_NAME, "gov.nasa.worldwind.globes.Earth");
44 defaults.setProperty(AVKey.INPUT_HANDLER_CLASS_NAME, "gov.nasa.worldwind.awt.AWTInputHandler");
45 defaults.setProperty(AVKey.LOGGER_NAME, "gov.nasa.worldwind");
46 defaults.setProperty(AVKey.MEMORY_CACHE_CLASS_NAME, "gov.nasa.worldwind.BasicMemoryCache");
47 defaults.setProperty(AVKey.MODEL_CLASS_NAME, "gov.nasa.worldwind.BasicModel");
48 defaults.setProperty(AVKey.RETRIEVAL_SERVICE_CLASS_NAME, "gov.nasa.worldwind.BasicRetrievalService");
49 defaults.setProperty(AVKey.SCENE_CONTROLLER_CLASS_NAME, "gov.nasa.worldwind.BasicSceneController");
50 defaults.setProperty(AVKey.THREADED_TASK_SERVICE_CLASS_NAME, "gov.nasa.worldwind.ThreadedTaskService");
51 defaults.setProperty(AVKey.VIEW_CLASS_NAME, "gov.nasa.worldwind.BasicOrbitView");
52 defaults.setProperty(AVKey.LAYERS_CLASS_NAMES,
53 "gov.nasa.worldwind.layers.Earth.StarsLayer"
54 + "gov.nasa.worldwind.layers.Earth.BMNGSurfaceLayer"
55 + ",gov.nasa.worldwind.layers.Earth.LandsatI3"
56 + ",gov.nasa.worldwind.layers.Earth.USGSUrbanAreaOrtho"
57 + ",gov.nasa.worldwind.layers.Earth.EarthNASAPlaceNameLayer"
58 + ",gov.nasa.worldwind.layers.CompassLayer"
60 defaults.setProperty(AVKey.INITIAL_LATITUDE, "0");
61 defaults.setProperty(AVKey.RETRIEVAL_POOL_SIZE, "5");
62 defaults.setProperty(AVKey.RETRIEVAL_QUEUE_SIZE, "200");
63 defaults.setProperty(AVKey.RETRIEVAL_QUEUE_STALE_REQUEST_LIMIT, "9000");
64 defaults.setProperty(AVKey.THREADED_TASK_POOL_SIZE, "1");
65 defaults.setProperty(AVKey.THREADED_TASK_QUEUE_SIZE, "5");
66 defaults.setProperty(AVKey.VERTICAL_EXAGGERATION, "1");
67 defaults.setProperty(AVKey.CACHE_SIZE, "2000000000");
68 defaults.setProperty(AVKey.CACHE_LOW_WATER, "140000000");
69 defaults.setProperty(AVKey.URL_CONNECT_TIMEOUT, "8000"); // milliseconds
70 defaults.setProperty(AVKey.URL_READ_TIMEOUT, "5000"); // milliseconds
71 return defaults;
74 private void initializeCustom(Properties defaults)
76 this.properties = new Properties(defaults);
78 String configFileName = System.getProperty(CONFIG_FILE_PROPERTY_KEY, CONFIG_FILE_NAME);
79 try
81 InputStream propsStream = this.getClass().getResourceAsStream("/" + configFileName);
82 if (propsStream == null)
84 File propsFile = new File(configFileName);
85 if (propsFile.exists())
87 propsStream = new FileInputStream(propsFile);
89 else
91 String message = WorldWind.retrieveErrMsg("Configuration.UnavailablePropsFile") + configFileName;
92 WorldWind.logger().log(java.util.logging.Level.FINE, message);
96 if (propsStream != null)
98 this.properties.load(propsStream);
101 catch (FileNotFoundException e)
103 String message = WorldWind.retrieveErrMsg("Configuration.UnavailablePropsFile") + configFileName;
104 WorldWind.logger().log(java.util.logging.Level.FINE, message);
106 catch (IOException e)
108 String message = WorldWind.retrieveErrMsg("Configuration.ExceptionReadingPropsFile") + configFileName;
109 WorldWind.logger().log(java.util.logging.Level.FINE, message);
111 catch (Exception e)
113 String message = WorldWind.retrieveErrMsg("Configuration.ExceptionReadingPropsFile") + configFileName
114 + " " + e.getLocalizedMessage();
115 WorldWind.logger().log(java.util.logging.Level.FINE, message, e);
119 public static String getStringValue(String key, String defaultValue)
121 String v = getStringValue(key);
122 return v != null ? v : defaultValue;
125 public static String getStringValue(String key)
127 return getInstance().properties.getProperty(key);
130 public static Integer getIntegerValue(String key, Integer defaultValue)
132 Integer v = getIntegerValue(key);
133 return v != null ? v : defaultValue;
136 public static Integer getIntegerValue(String key)
138 String v = getStringValue(key);
139 if (v == null)
140 return null;
144 return Integer.parseInt(v);
146 catch (NumberFormatException e)
148 String message = WorldWind.retrieveErrMsg("Configuration.ConversionError") + v;
149 WorldWind.logger().log(java.util.logging.Level.FINE, message);
151 return null;
155 public static Long getLongValue(String key, Long defaultValue)
157 Long v = getLongValue(key);
158 return v != null ? v : defaultValue;
161 public static Long getLongValue(String key)
163 String v = getStringValue(key);
164 if (v == null)
165 return null;
169 return Long.parseLong(v);
171 catch (NumberFormatException e)
173 String message = WorldWind.retrieveErrMsg("Configuration.ConversionError") + v;
174 WorldWind.logger().log(java.util.logging.Level.FINE, message);
176 return null;
180 public static Double getDoubleValue(String key, Double defaultValue)
182 Double v = getDoubleValue(key);
183 return v != null ? v : defaultValue;
186 public static Double getDoubleValue(String key)
188 String v = getStringValue(key);
189 if (v == null)
190 return null;
194 return Double.parseDouble(v);
196 catch (NumberFormatException e)
198 String message = WorldWind.retrieveErrMsg("Configuration.ConversionError") + v;
199 WorldWind.logger().log(java.util.logging.Level.FINE, message);
201 return null;
205 public static boolean hasKey(String key)
207 return getInstance().properties.contains(key);
210 public static void removeKey(String key)
212 getInstance().properties.remove(key);
215 public static void setValue(String key, Object value)
217 getInstance().properties.put(key, value.toString());
220 // OS, user, and run-time specific system properties. //
222 public static String currentWorkingDirectory()
224 String dir = System.getProperty("user.dir");
225 return (dir != null) ? dir : ".";
228 public static String getUserHomeDirectory()
230 String dir = System.getProperty("user.home");
231 return (dir != null) ? dir : ".";
234 public static String systemTempDirectory()
236 String dir = System.getProperty("java.io.tmpdir");
237 return (dir != null) ? dir : ".";
240 public static boolean isMacOS()
242 String osName = System.getProperty("os.name");
243 return osName != null && osName.toLowerCase().contains("mac");
246 public static boolean isWindowsOS()
248 String osName = System.getProperty("os.name");
249 return osName != null && osName.toLowerCase().contains("windows");
252 public static boolean isLinuxOS()
254 String osName = System.getProperty("os.name");
255 return osName != null && osName.toLowerCase().contains("linux");
258 public static boolean isUnixOS()
260 String osName = System.getProperty("os.name");
261 return osName != null && osName.toLowerCase().contains("unix");
264 public static boolean isSolarisOS()
266 String osName = System.getProperty("os.name");
267 return osName != null && osName.toLowerCase().contains("solaris");