You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DebugPlugin.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.debug;
  23. import com.dmdirc.addons.debug.commands.*; //NOPMD
  24. import com.dmdirc.commandparser.CommandManager;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.plugins.Plugin;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. /**
  33. * Debug plugin providing commands to aid in debugging the client.
  34. */
  35. public class DebugPlugin extends Plugin {
  36. /** List of build in debug commands to load. */
  37. private static final Class[] CLASSES = {
  38. Benchmark.class, ColourSpam.class, ConfigInfo.class, ConfigStats.class,
  39. com.dmdirc.addons.debug.commands.Error.class, FirstRun.class,
  40. ForceUpdate.class, GlobalConfigInfo.class, Identities.class,
  41. MemInfo.class, Notify.class, RunGC.class, ServerInfo.class,
  42. ServerState.class, Services.class, ShowRaw.class, Threads.class,
  43. Time.class,
  44. };
  45. /** List of registered debug commands. */
  46. private final Map<String, DebugCommand> commands;
  47. /** Debug command. */
  48. private final Debug debugCommand;
  49. /**
  50. * Creates a new debug plugin.
  51. */
  52. public DebugPlugin() {
  53. super();
  54. commands = new HashMap<String, DebugCommand>();
  55. debugCommand = new Debug(this);
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. @SuppressWarnings("unchecked")
  60. public void onLoad() {
  61. CommandManager.registerCommand(debugCommand);
  62. for (Class<DebugCommand> type : CLASSES) {
  63. try {
  64. addCommand(type.getConstructor(Debug.class)
  65. .newInstance(debugCommand));
  66. } catch (LinkageError e) {
  67. Logger.appError(ErrorLevel.HIGH,
  68. "Unable to load debug command", e);
  69. } catch (Exception e) {
  70. Logger.appError(ErrorLevel.HIGH,
  71. "Unable to load debug command", e);
  72. }
  73. }
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public void onUnload() {
  78. commands.clear();
  79. CommandManager.unregisterCommand(debugCommand);
  80. }
  81. /**
  82. * Adds a command to the list of commands.
  83. *
  84. * @param command Command to add
  85. */
  86. public void addCommand(final DebugCommand command) {
  87. commands.put(command.getName(), command);
  88. }
  89. /**
  90. * Removes a command from the list of commands.
  91. *
  92. * @param command Command to remove
  93. */
  94. public void removeCommand(final DebugCommand command) {
  95. commands.remove(command.getName());
  96. }
  97. /**
  98. * Returns a list of command names.
  99. *
  100. * @return List of command names
  101. */
  102. public List<String> getCommandNames() {
  103. final List<String> names = new ArrayList<String>(commands.size());
  104. for (DebugCommand command : commands.values()) {
  105. names.add(command.getName());
  106. }
  107. return names;
  108. }
  109. /**
  110. * Returns the debug command with the specified name.
  111. *
  112. * @param name Name of command to return
  113. *
  114. * @return Command with specified name or null if no matches
  115. */
  116. public DebugCommand getCommand(final String name) {
  117. return commands.get(name);
  118. }
  119. }