Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DefaultPluginStatusProvider.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j;
  17. import org.pf4j.util.FileUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import java.io.IOException;
  21. import java.nio.file.Path;
  22. import java.util.List;
  23. /**
  24. * The default implementation for {@link PluginStatusProvider}.
  25. * The enabled plugins are read from {@code enabled.txt} file and
  26. * the disabled plugins are read from {@code disabled.txt} file.
  27. *
  28. * @author Decebal Suiu
  29. * @author Mário Franco
  30. */
  31. public class DefaultPluginStatusProvider implements PluginStatusProvider {
  32. private static final Logger log = LoggerFactory.getLogger(DefaultPluginStatusProvider.class);
  33. private final Path pluginsRoot;
  34. private List<String> enabledPlugins;
  35. private List<String> disabledPlugins;
  36. public DefaultPluginStatusProvider(Path pluginsRoot) {
  37. this.pluginsRoot = pluginsRoot;
  38. try {
  39. // create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file)
  40. enabledPlugins = FileUtils.readLines(pluginsRoot.resolve("enabled.txt"), true);
  41. log.info("Enabled plugins: {}", enabledPlugins);
  42. // create a list with plugin identifiers that should not be accepted by this manager (blacklist from plugins/disabled.txt file)
  43. disabledPlugins = FileUtils.readLines(pluginsRoot.resolve("disabled.txt"), true);
  44. log.info("Disabled plugins: {}", disabledPlugins);
  45. } catch (IOException e) {
  46. log.error(e.getMessage(), e);
  47. }
  48. }
  49. @Override
  50. public boolean isPluginDisabled(String pluginId) {
  51. if (disabledPlugins.contains(pluginId)) {
  52. return true;
  53. }
  54. return !enabledPlugins.isEmpty() && !enabledPlugins.contains(pluginId);
  55. }
  56. @Override
  57. public boolean disablePlugin(String pluginId) {
  58. if (disabledPlugins.add(pluginId)) {
  59. try {
  60. FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile());
  61. } catch (IOException e) {
  62. log.error("Failed to disable plugin {}", pluginId, e);
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. @Override
  69. public boolean enablePlugin(String pluginId) {
  70. if (disabledPlugins.remove(pluginId)) {
  71. try {
  72. FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile());
  73. } catch (IOException e) {
  74. log.error("Failed to enable plugin {}", pluginId, e);
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. }