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.

CorePluginExtractor.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.plugins;
  23. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  24. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  25. import com.dmdirc.events.UserErrorEvent;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.util.resourcemanager.ResourceManager;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.util.Map;
  31. import javax.inject.Inject;
  32. import javax.inject.Singleton;
  33. import com.dmdirc.DMDircMBassador;
  34. /**
  35. * Utility class that can extract bundled plugins.
  36. */
  37. @Singleton
  38. public class CorePluginExtractor {
  39. /** The plugin manager to inform when plugins are updated. */
  40. private final PluginManager pluginManager;
  41. /** The directory to extract plugins to. */
  42. private final String pluginDir;
  43. /** The event bus to post events to. */
  44. private final DMDircMBassador eventBus;
  45. /**
  46. * Creates a new instance of {@link CorePluginExtractor}.
  47. *
  48. * @param pluginManager The plugin manager to inform when plugins are updated.
  49. * @param pluginDir The directory to extract plugins to.
  50. * @param eventBus The event bus to post events to.
  51. */
  52. @Inject
  53. public CorePluginExtractor(
  54. final PluginManager pluginManager,
  55. @Directory(DirectoryType.PLUGINS) final String pluginDir,
  56. final DMDircMBassador eventBus) {
  57. this.pluginManager = pluginManager;
  58. this.pluginDir = pluginDir;
  59. this.eventBus = eventBus;
  60. }
  61. /**
  62. * Extracts plugins bundled with DMDirc to the user's profile's plugin directory.
  63. *
  64. * @param prefix If non-null, only plugins whose file name starts with this prefix will be
  65. * extracted.
  66. */
  67. public void extractCorePlugins(final String prefix) {
  68. final Map<String, byte[]> resources = ResourceManager.getResourceManager()
  69. .getResourcesStartingWithAsBytes("plugins");
  70. for (Map.Entry<String, byte[]> resource : resources.entrySet()) {
  71. try {
  72. final String resourceName = pluginDir + resource.getKey().substring(7);
  73. if (prefix != null && !resource.getKey().substring(8).startsWith(prefix)) {
  74. continue;
  75. }
  76. final File newDir = new File(resourceName.substring(0,
  77. resourceName.lastIndexOf('/')) + "/");
  78. if (!newDir.exists()) {
  79. newDir.mkdirs();
  80. }
  81. final File newFile = new File(newDir,
  82. resourceName.substring(resourceName.lastIndexOf('/') + 1,
  83. resourceName.length()));
  84. if (!newFile.isDirectory()) {
  85. ResourceManager.getResourceManager().
  86. resourceToFile(resource.getValue(), newFile);
  87. final PluginInfo plugin = pluginManager.getPluginInfo(newFile
  88. .getAbsolutePath().substring(pluginDir.length()));
  89. if (plugin != null) {
  90. plugin.pluginUpdated();
  91. }
  92. }
  93. } catch (IOException ex) {
  94. eventBus.publish(new UserErrorEvent(ErrorLevel.LOW, ex,
  95. "Failed to extract plugins", ""));
  96. }
  97. }
  98. }
  99. }