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

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