PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/java/org/javasimon/SimonManager.java

http://javasimon.googlecode.com/
Java | 251 lines | 98 code | 27 blank | 126 comment | 6 complexity | d268d83926af70414c97c50eb576bd96 MD5 | raw file
Possible License(s): LGPL-2.1
  1. package org.javasimon;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.Collection;
  7. import org.javasimon.callback.Callback;
  8. import org.javasimon.callback.CompositeCallback;
  9. import org.javasimon.utils.SimonUtils;
  10. import org.javasimon.utils.SystemDebugCallback;
  11. /**
  12. * SimonManager is static utility class providing so called "default {@link org.javasimon.Manager}.
  13. * It is possible to create separate Manager, but it cannot be accessed via this convenient
  14. * utility-like class. This option may be usefull in Java EE environmant when it's required to
  15. * separate Simon trees accross different applications. For majority of Java SE applications this
  16. * class is recommended.
  17. * <p/>
  18. * SimonManager also provides configuration initialization via properties. To initialize configuration
  19. * with the configuration file following option can be added to JVM executable:
  20. * <pre>-Djavasimon.config.file=some-path/simon.config.xml</pre>
  21. * To configure the SimonManager via resource that can be found on classpath:
  22. * <pre>-Djavasimon.config.resource=org/javasimon/example/wannabe-config.xml</pre>
  23. *
  24. * @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a>
  25. */
  26. public final class SimonManager {
  27. /**
  28. * Property name for the Simon configuration file is "javasimon.config.file".
  29. */
  30. public static final String PROPERTY_CONFIG_FILE_NAME = "javasimon.config.file";
  31. /**
  32. * Property name for the Simon configuration resource is "javasimon.config.resource".
  33. */
  34. public static final String PROPERTY_CONFIG_RESOURCE_NAME = "javasimon.config.resource";
  35. private static final long INIT_NANOS;
  36. private static final long INIT_MILLIS;
  37. private static Manager manager = new SwitchingManager();
  38. /**
  39. * Calls {@link #init()}.
  40. */
  41. static {
  42. init();
  43. // for conversion between nano and millis - see method millisForNano(long)
  44. INIT_NANOS = System.nanoTime();
  45. INIT_MILLIS = System.currentTimeMillis();
  46. }
  47. /**
  48. * Initilizes the configuration facility for the default Simon Manager. Fetches exception
  49. * if configuration resource or file is not found. This method does NOT clear the manager
  50. * itself, only the configuration is reloaded. Method also preserves Callback setup.
  51. */
  52. static void init() {
  53. Callback tempraryCallback = new SystemDebugCallback();
  54. manager.callback().addCallback(tempraryCallback); // just for reporting warnings, will be removed
  55. try {
  56. manager.configuration().clear();
  57. String fileName = System.getProperty(PROPERTY_CONFIG_FILE_NAME);
  58. if (fileName != null) {
  59. manager.configuration().readConfig(new FileReader(fileName));
  60. }
  61. String resourceName = System.getProperty(PROPERTY_CONFIG_RESOURCE_NAME);
  62. if (resourceName != null) {
  63. InputStream is = SimonManager.class.getClassLoader().getResourceAsStream(resourceName);
  64. if (is == null) {
  65. throw new FileNotFoundException(resourceName);
  66. }
  67. manager.configuration().readConfig(new InputStreamReader(is));
  68. }
  69. } catch (Exception e) {
  70. manager.callback().onManagerWarning("SimonManager initialization error", e);
  71. }
  72. manager.callback().removeCallback(tempraryCallback);
  73. }
  74. private SimonManager() {
  75. throw new UnsupportedOperationException();
  76. }
  77. /**
  78. * Returns Simon by its name if it exists.
  79. *
  80. * @param name name of the Simon
  81. * @return Simon object
  82. */
  83. public static Simon getSimon(String name) {
  84. return manager.getSimon(name);
  85. }
  86. /**
  87. * Destroys Simon or replaces it with UnknownSimon if it's necessary to preserve the hierarchy.
  88. *
  89. * @param name name of the Simon
  90. */
  91. public static void destroySimon(String name) {
  92. manager.destroySimon(name);
  93. }
  94. /**
  95. * Returns existing Counter or creates new if necessary.
  96. *
  97. * @param name name of the Counter
  98. * @return counter object
  99. */
  100. public static Counter getCounter(String name) {
  101. return manager.getCounter(name);
  102. }
  103. /**
  104. * Returns existing Stopwatch or creates new if necessary.
  105. *
  106. * @param name name of the Stopwatch
  107. * @return stopwatch object
  108. */
  109. public static Stopwatch getStopwatch(String name) {
  110. return manager.getStopwatch(name);
  111. }
  112. /**
  113. * Enables the Simon Manager. Enabled manager provides real Simons.
  114. */
  115. public static void enable() {
  116. manager.enable();
  117. }
  118. /**
  119. * Disables the Simon Manager. Disabled manager provides null Simons that actually do nothing.
  120. */
  121. public static void disable() {
  122. manager.disable();
  123. }
  124. /**
  125. * Returns true if the Simon Manager is enabled.
  126. *
  127. * @return true if the Simon Manager is enabled
  128. */
  129. public static boolean isEnabled() {
  130. return manager.isEnabled();
  131. }
  132. /**
  133. * Returns root Simon. Type of the Simon is unknown at the start but it can be replaced
  134. * by the real Simon later. Specific get method with root Simon name constant can be used
  135. * in that case.
  136. *
  137. * @return root Simon
  138. */
  139. public static Simon getRootSimon() {
  140. return manager.getRootSimon();
  141. }
  142. /**
  143. * Returns unmodifiable collection containing names of all existing Simons.
  144. *
  145. * @return collection of all Simon names
  146. * @since 3.1
  147. */
  148. public static Collection<String> getSimonNames() {
  149. return manager.getSimonNames();
  150. }
  151. /**
  152. * Returns collection containing all existing Simons matching the pattern (can be {@code null}).
  153. * Collection is unmodifiable if {@code null} pattern is provided and all Simons are returned,
  154. * otherwise new collection with matching Simons is returned.
  155. *
  156. * @param pattern Simon name pattern (see {@link SimonPattern}
  157. * @return collection of all Simons matching the pattern
  158. * @see SimonPattern to find out more about possible patterns
  159. * @since 3.1
  160. */
  161. public static Collection<Simon> getSimons(SimonPattern pattern) {
  162. return manager.getSimons(pattern);
  163. }
  164. /**
  165. * Clears the SimonManager (ignored if manager is disabled). All Simons are lost,
  166. * but configuration is preserved.
  167. */
  168. public static void clear() {
  169. manager.clear();
  170. }
  171. /**
  172. * Accesses Simon callback.
  173. *
  174. * @return Simon callback
  175. */
  176. public static CompositeCallback callback() {
  177. return manager.callback();
  178. }
  179. /**
  180. * Accesses configuration of this manager.
  181. *
  182. * @return configuration of this manager
  183. */
  184. public static ManagerConfiguration configuration() {
  185. return manager.configuration();
  186. }
  187. /**
  188. * Accesses default Simon Manager which is the switching manager.
  189. *
  190. * @return default Simon Manager
  191. */
  192. public static Manager manager() {
  193. return manager;
  194. }
  195. /**
  196. * Method propagates message to manager's {@link org.javasimon.callback.Callback}. This allows user to report any
  197. * message if they implement {@link org.javasimon.callback.Callback#onManagerMessage(String)}.
  198. *
  199. * @param message message text
  200. */
  201. public static void message(String message) {
  202. manager.message(message);
  203. }
  204. /**
  205. * Method propagates warning to manager's {@link org.javasimon.callback.Callback}. This allows user to report any
  206. * warning and/or exception if they implement {@link org.javasimon.callback.Callback#onManagerWarning(String, Exception)}.
  207. *
  208. * @param warning arbitrary warning message
  209. * @param cause exception causing this warning
  210. */
  211. public static void warning(String warning, Exception cause) {
  212. manager.warning(warning, cause);
  213. }
  214. /**
  215. * Converts nano timer value into millis timestamp compatible with {@link System#currentTimeMillis()}.
  216. *
  217. * @param nanos nano timer value
  218. * @return ms timestamp
  219. * @since 3.1
  220. */
  221. public static long millisForNano(long nanos) {
  222. return INIT_MILLIS + (nanos - INIT_NANOS) / SimonUtils.NANOS_IN_MILLIS;
  223. }
  224. }