Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / WorldWind.java
blob771b32e69173bdb6d327d2748009f06a30341826
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 package gov.nasa.worldwind;
9 import java.util.*;
11 /**
12 * @author Tom Gaskins
13 * @version $Id: WorldWind.java 1792 2007-05-08 21:28:37Z tgaskins $
15 //TODO: prevent circular exception loops when Resources unavailable (internationalisation)
16 // until that problem is solved, don't create international support for this class.
17 public final class WorldWind
19 private static WorldWind instance = new WorldWind();
21 private WWObjectImpl wwo = new WWObjectImpl();
22 private MemoryCache memoryCache;
23 private FileCache dataFileCache;
24 private BasicRetrievalService retrievalService;
25 private ThreadedTaskService threadedTaskService = new ThreadedTaskService();
27 private WorldWind() // Singleton, prevent instantiation.
29 this.retrievalService = (BasicRetrievalService) createConfigurationComponent(
30 AVKey.RETRIEVAL_SERVICE_CLASS_NAME);
31 this.threadedTaskService = (ThreadedTaskService) createConfigurationComponent(
32 AVKey.THREADED_TASK_SERVICE_CLASS_NAME);
33 this.dataFileCache = (FileCache) createConfigurationComponent(AVKey.DATA_FILE_CACHE_CLASS_NAME);
34 this.memoryCache = (MemoryCache) createConfigurationComponent(AVKey.MEMORY_CACHE_CLASS_NAME);
37 public static java.util.logging.Logger logger()
39 String loggerName = Configuration.getStringValue(AVKey.LOGGER_NAME);
40 if (loggerName == null)
41 return java.util.logging.Logger.global;
43 java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggerName);
44 return logger != null ? logger : java.util.logging.Logger.global;
47 public static MemoryCache memoryCache()
49 return instance.memoryCache;
52 public static FileCache dataFileCache()
54 return instance.dataFileCache;
57 public static RetrievalService retrievalService()
59 return instance.retrievalService;
62 public static ThreadedTaskService threadedTaskService()
64 return instance.threadedTaskService;
67 /**
68 * @param className
69 * @return
70 * @throws WWRuntimeException if the <code>Object</code> could not be created
71 * @throws IllegalArgumentException if <code>className</code> is null or zero length
73 public static Object createComponent(String className) throws WWRuntimeException
75 if (className == null || className.length() == 0)
76 { //WorldWind.ClassNameKeyNulZero
77 String message = "Class name key is null or zero length";
78 WorldWind.logger().log(java.util.logging.Level.FINE, message);
79 throw new IllegalArgumentException(message);
82 try
84 Class c = Class.forName(className);
85 return c.newInstance();
87 catch (Exception e)
88 { //WorldWind.ExceptionCreatingComponent
89 String message = "Exception while creating World Wind component " + className;
90 WorldWind.logger().log(java.util.logging.Level.FINE, message);
91 throw new WWRuntimeException(message, e);
93 catch (Throwable t)
94 { //WorldWind.ErrorCreatingComponent
95 String message = "Error while creating World Wind component " + className;
96 WorldWind.logger().log(java.util.logging.Level.FINE, message);
97 throw new WWRuntimeException(message, t);
102 * @param classNameKey
103 * @return
104 * @throws IllegalStateException if no name could be found which corresponds to <code>classNameKey</code>
105 * @throws IllegalArgumentException if <code>classNameKey<code> is null
106 * @throws WWRuntimeException if the component could not be created
108 public static Object createConfigurationComponent(String classNameKey)
109 throws IllegalStateException, IllegalArgumentException
111 if (classNameKey == null)
112 { //nullValue.ClassNameKeyNull
113 String message = "Class name key is null";
114 WorldWind.logger().log(java.util.logging.Level.FINE, message);
115 throw new IllegalArgumentException(message);
118 String name = Configuration.getStringValue(classNameKey);
119 if (name == null)
120 { //WorldWind.NoClassNameInConfigurationForKey
121 String message = "No class name in configuration for key " + classNameKey;
122 WorldWind.logger().log(java.util.logging.Level.FINE, message);
123 throw new IllegalStateException(message);
128 return WorldWind.createComponent(name);
130 catch (Throwable e)
131 { //WorldWind.UnableToCreateClassForConfigurationKey
132 String message = "Unable to create class for configuration key " + classNameKey;
133 WorldWind.logger().log(java.util.logging.Level.FINE, message);
134 throw new WWRuntimeException(message, e);
138 public static void setValue(String key, String value)
140 instance.wwo.setValue(key, value);
143 public static Object getValue(String key)
145 return instance.wwo.getValue(key);
148 public static String getStringValue(String key)
150 return instance.wwo.getStringValue(key);
153 public static boolean hasKey(String key)
155 return instance.wwo.hasKey(key);
158 public static void removeKey(String key)
160 instance.wwo.removeKey(key);
164 * Retrieve a String resource from the "error" bundle.
166 * @param property The property used to identify which String to return.
167 * @return the requested String.
169 public static String retrieveErrMsg(String property)
171 ResourceBundle res = ResourceBundle.getBundle("ErrorStrings", Locale.getDefault());
172 return (String) res.getObject(property);
175 public static String retrieveMessage(String property, String bundle)
177 ResourceBundle res = ResourceBundle.getBundle(bundle, Locale.getDefault());
178 return (String) res.getObject(property);