PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/WorldWindX/src/main/java/gov/nasa/worldwindx/applications/worldwindow/core/Registry.java

http://geoforge.googlecode.com/
Java | 217 lines | 170 code | 25 blank | 22 comment | 29 complexity | 39d343a9d65cb2e51766a4ddafb6e8ee MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 2001, 2010 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.worldwindx.applications.worldwindow.core;
  8. import gov.nasa.worldwind.util.WWUtil;
  9. import gov.nasa.worldwindx.applications.worldwindow.util.Util;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.util.*;
  12. import java.util.concurrent.ConcurrentHashMap;
  13. import java.util.logging.Level;
  14. /**
  15. * @author tag
  16. * @version $Id: Registry.java 1 2011-07-16 23:22:47Z dcollins $
  17. */
  18. public class Registry
  19. {
  20. private ConcurrentHashMap<String, Object> registeredObjects = new ConcurrentHashMap<String, Object>();
  21. /**
  22. * @param className the full name, including package names, of the component to create
  23. *
  24. * @return the new component
  25. *
  26. * @throws RuntimeException if the <code>Object</code> could not be created
  27. * @throws IllegalArgumentException if <code>className</code> is null or zero length
  28. */
  29. public Object createObject(String className)
  30. {
  31. if (className == null || className.length() == 0)
  32. {
  33. String msg = "Class name is null or zero length";
  34. Util.getLogger().severe(msg);
  35. throw new IllegalArgumentException(msg);
  36. }
  37. try
  38. {
  39. return Class.forName(className.trim()).newInstance();
  40. }
  41. catch (Exception e)
  42. {
  43. String msg = "Exception creating object " + className;
  44. Util.getLogger().log(java.util.logging.Level.SEVERE, msg, e);
  45. throw new RuntimeException(msg, e);
  46. }
  47. catch (Throwable t)
  48. {
  49. String msg = "Error creating object " + className;
  50. Util.getLogger().log(java.util.logging.Level.SEVERE, msg, t);
  51. throw new RuntimeException(msg, t);
  52. }
  53. }
  54. public Object createRegistryObject(Object classOrName)
  55. throws ClassNotFoundException, IllegalAccessException, InstantiationException
  56. {
  57. if (classOrName == null)
  58. {
  59. String msg = "Class or class name is null";
  60. Util.getLogger().severe(msg);
  61. throw new IllegalArgumentException(msg);
  62. }
  63. if (!(classOrName instanceof Class || classOrName instanceof String))
  64. {
  65. String msg = "Class or class name is not Class or String type";
  66. Util.getLogger().severe(msg);
  67. throw new IllegalArgumentException(msg);
  68. }
  69. if (classOrName instanceof String && ((String) classOrName).length() == 0)
  70. {
  71. String msg = "Class name is null or zero length";
  72. Util.getLogger().severe(msg);
  73. throw new IllegalArgumentException(msg);
  74. }
  75. Class<?> c = classOrName instanceof Class ? (Class) classOrName : Class.forName(((String) classOrName).trim());
  76. String className = c.getName();
  77. try
  78. {
  79. // Create self-registering object, else non-self-registering object
  80. return c.getConstructor(this.getClass()).newInstance(this);
  81. }
  82. catch (NoSuchMethodException e)
  83. {
  84. return createObject(className);
  85. }
  86. catch (InstantiationException e)
  87. {
  88. return createObject(className);
  89. }
  90. catch (IllegalAccessException e)
  91. {
  92. return createObject(className);
  93. }
  94. catch (InvocationTargetException e)
  95. {
  96. return createObject(className);
  97. }
  98. catch (Exception e)
  99. {
  100. String msg = "Exception creating object " + className;
  101. Util.getLogger().log(java.util.logging.Level.SEVERE, msg, className);
  102. throw new RuntimeException(msg, e);
  103. }
  104. catch (Throwable t)
  105. {
  106. String msg = "Error creating object " + className;
  107. Util.getLogger().log(java.util.logging.Level.SEVERE, msg, className);
  108. throw new RuntimeException(msg, t);
  109. }
  110. }
  111. public Object createAndRegisterObject(String objectID, Object classOrName)
  112. throws IllegalAccessException, InstantiationException, ClassNotFoundException
  113. {
  114. if (WWUtil.isEmpty(objectID))
  115. {
  116. String msg = String.format("Object ID %s is null or zero length", objectID);
  117. Util.getLogger().severe(msg);
  118. throw new IllegalArgumentException(msg);
  119. }
  120. if (classOrName == null || (classOrName instanceof String && WWUtil.isEmpty(classOrName)))
  121. {
  122. String msg = String.format("Class name %s for feature %s is zero length", classOrName, objectID);
  123. Util.getLogger().severe(msg);
  124. throw new IllegalArgumentException(msg);
  125. }
  126. registerObject(objectID, createRegistryObject(classOrName));
  127. return getRegisteredObject(objectID);
  128. }
  129. public synchronized Object getRegisteredObject(String objectID)
  130. {
  131. return this.registeredObjects.get(objectID);
  132. }
  133. public synchronized Object registerObject(String objectID, Object o)
  134. {
  135. if (objectID != null)
  136. this.registeredObjects.put(objectID, o);
  137. return o;
  138. }
  139. public Collection<Object> getObjects()
  140. {
  141. return this.registeredObjects.values();
  142. }
  143. public Object[] getObjectsOfType(String className)
  144. {
  145. ArrayList<Object> list = new ArrayList<Object>();
  146. try
  147. {
  148. Class classClass = Class.forName(className);
  149. for (Map.Entry<String, Object> entry : this.registeredObjects.entrySet())
  150. {
  151. if (entry.getValue() == null)
  152. continue;
  153. if (classClass.isInstance(entry.getValue()))
  154. {
  155. list.add(entry.getValue());
  156. }
  157. else if (entry.getValue() instanceof Class)
  158. {
  159. // TODO: also check superclasses for instance of type. See java.lang.Class.getEnclosingClass and
  160. // search recursively.
  161. if (implementsInterface(classClass, (Class) entry.getValue()))
  162. {
  163. try
  164. {
  165. list.add(this.createAndRegisterObject(entry.getKey(), entry.getValue()));
  166. }
  167. catch (Exception e)
  168. {
  169. // continue
  170. }
  171. }
  172. }
  173. }
  174. }
  175. catch (ClassNotFoundException e)
  176. {
  177. Util.getLogger().log(Level.SEVERE,
  178. "No class found for class name " + (className != null ? className : null), e);
  179. }
  180. return list.toArray();
  181. }
  182. protected boolean implementsInterface(Class interfaceClass, Class compareClass)
  183. {
  184. Class<?>[] interfaces = compareClass.getInterfaces();
  185. for (Class i : interfaces)
  186. {
  187. if (i == interfaceClass)
  188. return true;
  189. }
  190. return false;
  191. }
  192. }