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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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;
  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 directory.
  51. *
  52. * @param prefix If non-null, only plugins whose file name starts with this prefix will be
  53. * extracted.
  54. */
  55. public void extractCorePlugins(final String prefix) {
  56. final Map<String, byte[]> resources = ResourceManager.getResourceManager()
  57. .getResourcesStartingWithAsBytes("plugins");
  58. for (Map.Entry<String, byte[]> resource : resources.entrySet()) {
  59. try {
  60. final String resourceName = pluginDir + resource.getKey().substring(7);
  61. if (prefix != null && !resource.getKey().substring(8).startsWith(prefix)) {
  62. continue;
  63. }
  64. final File newDir = new File(resourceName.substring(0,
  65. resourceName.lastIndexOf('/')) + "/");
  66. if (!newDir.exists()) {
  67. newDir.mkdirs();
  68. }
  69. final File newFile = new File(newDir,
  70. resourceName.substring(resourceName.lastIndexOf('/') + 1,
  71. resourceName.length()));
  72. if (!newFile.isDirectory()) {
  73. ResourceManager.getResourceManager().
  74. resourceToFile(resource.getValue(), newFile);
  75. final PluginInfo plugin = pluginManager.getPluginInfo(newFile
  76. .getAbsolutePath().substring(pluginManager.getDirectory().length()));
  77. if (plugin != null) {
  78. plugin.pluginUpdated();
  79. }
  80. }
  81. } catch (IOException ex) {
  82. Logger.userError(ErrorLevel.LOW, "Failed to extract plugins", ex);
  83. }
  84. }
  85. }
  86. }