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.

DcopPlugin.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2006-2014 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.dcop;
  23. import com.dmdirc.interfaces.CommandController;
  24. import com.dmdirc.plugins.Exported;
  25. import com.dmdirc.plugins.implementations.BaseCommandPlugin;
  26. import java.io.BufferedReader;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. /**
  32. * Allows the user to execute dcop commands (and read the results).
  33. */
  34. public final class DcopPlugin extends BaseCommandPlugin {
  35. /**
  36. * Creates a new instance of this plugin.
  37. *
  38. * @param commandController Command controller to register commands
  39. */
  40. public DcopPlugin(final CommandController commandController) {
  41. super(commandController);
  42. registerCommand(new DcopCommand(commandController), DcopCommand.INFO);
  43. }
  44. /**
  45. * Retrieves the result from executing the specified command.
  46. *
  47. * @param command The command to be executed
  48. * @return The output of the specified command
  49. */
  50. @Exported
  51. public static List<String> getDcopResult(final String command) {
  52. final ArrayList<String> result = new ArrayList<>();
  53. InputStreamReader reader;
  54. BufferedReader input;
  55. Process process;
  56. try {
  57. process = Runtime.getRuntime().exec(command);
  58. reader = new InputStreamReader(process.getInputStream());
  59. input = new BufferedReader(reader);
  60. String line = "";
  61. while ((line = input.readLine()) != null) {
  62. result.add(line);
  63. }
  64. reader.close();
  65. input.close();
  66. process.destroy();
  67. } catch (IOException ex) {
  68. // Do nothing
  69. }
  70. return result;
  71. }
  72. }