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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  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.config.IdentityManager;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.logger.Logger;
  28. import com.dmdirc.plugins.PluginManager;
  29. import com.dmdirc.plugins.implementations.BaseCommandPlugin;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import lombok.Getter;
  35. /**
  36. * Debug plugin providing commands to aid in debugging the client.
  37. */
  38. @SuppressWarnings("unused")
  39. public class DebugPlugin extends BaseCommandPlugin {
  40. /** List of build in debug commands to load. */
  41. private static final Class[] CLASSES = {
  42. Benchmark.class, ColourSpam.class, ConfigInfo.class, ConfigStats.class,
  43. com.dmdirc.addons.debug.commands.Error.class, FakeUpdates.class,
  44. FirstRun.class, ForceUpdate.class, GlobalConfigInfo.class,
  45. Identities.class, MemInfo.class, Notify.class, RunGC.class,
  46. ServerInfo.class, ServerState.class, Services.class, ShowRaw.class,
  47. Threads.class, Time.class, StatusbarMessage.class,
  48. };
  49. /** List of registered debug commands. */
  50. private final Map<String, DebugCommand> commands;
  51. /** Debug command. */
  52. private final Debug debugCommand;
  53. /** Plugin manager instance. */
  54. @Getter
  55. private final PluginManager pluginManager;
  56. /** Identity manager instance. */
  57. @Getter
  58. private final IdentityManager identityManager;
  59. /**
  60. * Creates a new debug plugin.
  61. *
  62. * @param identityManager Identity manager instance
  63. * @param pluginManager Plugin manager instance.
  64. * @param commandController Command controller to register commands
  65. */
  66. public DebugPlugin(final IdentityManager identityManager,
  67. final PluginManager pluginManager,
  68. final CommandController commandController) {
  69. super(commandController);
  70. this.identityManager = identityManager;
  71. this.pluginManager = pluginManager;
  72. commands = new HashMap<>();
  73. debugCommand = new Debug(this, commandController);
  74. registerCommand(debugCommand, Debug.INFO);
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. @SuppressWarnings("unchecked")
  79. public void onLoad() {
  80. for (Class<DebugCommand> type : CLASSES) {
  81. try {
  82. addCommand(type.getConstructor(DebugPlugin.class, Debug.class)
  83. .newInstance(this, debugCommand));
  84. } catch (LinkageError | Exception e) {
  85. Logger.appError(ErrorLevel.HIGH,
  86. "Unable to load debug command", e);
  87. }
  88. }
  89. super.onLoad();
  90. }
  91. /** {@inheritDoc} */
  92. @Override
  93. public void onUnload() {
  94. commands.clear();
  95. super.onUnload();
  96. }
  97. /**
  98. * Adds a command to the list of commands.
  99. *
  100. * @param command Command to add
  101. */
  102. public void addCommand(final DebugCommand command) {
  103. commands.put(command.getName(), command);
  104. }
  105. /**
  106. * Removes a command from the list of commands.
  107. *
  108. * @param command Command to remove
  109. */
  110. public void removeCommand(final DebugCommand command) {
  111. commands.remove(command.getName());
  112. }
  113. /**
  114. * Returns a list of command names.
  115. *
  116. * @return List of command names
  117. */
  118. public List<String> getCommandNames() {
  119. final List<String> names = new ArrayList<>(commands.size());
  120. for (DebugCommand command : commands.values()) {
  121. names.add(command.getName());
  122. }
  123. return names;
  124. }
  125. /**
  126. * Returns the debug command with the specified name.
  127. *
  128. * @param name Name of command to return
  129. *
  130. * @return Command with specified name or null if no matches
  131. */
  132. public DebugCommand getCommand(final String name) {
  133. return commands.get(name);
  134. }
  135. }