選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DefaultPluginStatusProvider.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.Files;
  22. import java.nio.file.Path;
  23. import java.util.List;
  24. /**
  25. * The default implementation for {@link PluginStatusProvider}.
  26. * The enabled plugins are read from {@code enabled.txt} file and
  27. * the disabled plugins are read from {@code disabled.txt} file.
  28. *
  29. * @author Decebal Suiu
  30. * @author Mário Franco
  31. */
  32. public class DefaultPluginStatusProvider implements PluginStatusProvider {
  33. private static final Logger log = LoggerFactory.getLogger(DefaultPluginStatusProvider.class);
  34. private final Path pluginsRoot;
  35. private List<String> enabledPlugins;
  36. private List<String> disabledPlugins;
  37. public DefaultPluginStatusProvider(Path pluginsRoot) {
  38. this.pluginsRoot = pluginsRoot;
  39. try {
  40. // create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file)
  41. enabledPlugins = FileUtils.readLines(getEnabledFilePath(), true);
  42. log.info("Enabled plugins: {}", enabledPlugins);
  43. // create a list with plugin identifiers that should not be accepted by this manager (blacklist from plugins/disabled.txt file)
  44. disabledPlugins = FileUtils.readLines(getDisabledFilePath(), true);
  45. log.info("Disabled plugins: {}", disabledPlugins);
  46. } catch (IOException e) {
  47. log.error(e.getMessage(), e);
  48. }
  49. }
  50. @Override
  51. public boolean isPluginDisabled(String pluginId) {
  52. if (disabledPlugins.contains(pluginId)) {
  53. return true;
  54. }
  55. return !enabledPlugins.isEmpty() && !enabledPlugins.contains(pluginId);
  56. }
  57. @Override
  58. public void disablePlugin(String pluginId) {
  59. if (isPluginDisabled(pluginId)) {
  60. // do nothing
  61. return;
  62. }
  63. if (Files.exists(getEnabledFilePath())) {
  64. enabledPlugins.remove(pluginId);
  65. try {
  66. FileUtils.writeLines(enabledPlugins, getEnabledFilePath());
  67. } catch (IOException e) {
  68. throw new PluginRuntimeException(e);
  69. }
  70. } else {
  71. disabledPlugins.add(pluginId);
  72. try {
  73. FileUtils.writeLines(disabledPlugins, getDisabledFilePath());
  74. } catch (IOException e) {
  75. throw new PluginRuntimeException(e);
  76. }
  77. }
  78. }
  79. @Override
  80. public void enablePlugin(String pluginId) {
  81. if (!isPluginDisabled(pluginId)) {
  82. // do nothing
  83. return;
  84. }
  85. if (Files.exists(getEnabledFilePath())) {
  86. enabledPlugins.add(pluginId);
  87. try {
  88. FileUtils.writeLines(enabledPlugins, getEnabledFilePath());
  89. } catch (IOException e) {
  90. throw new PluginRuntimeException(e);
  91. }
  92. } else {
  93. disabledPlugins.remove(pluginId);
  94. try {
  95. FileUtils.writeLines(disabledPlugins, getDisabledFilePath());
  96. } catch (IOException e) {
  97. throw new PluginRuntimeException(e);
  98. }
  99. }
  100. }
  101. public Path getEnabledFilePath() {
  102. return getEnabledFilePath(pluginsRoot);
  103. }
  104. public Path getDisabledFilePath() {
  105. return getDisabledFilePath(pluginsRoot);
  106. }
  107. public static Path getEnabledFilePath(Path pluginsRoot) {
  108. return pluginsRoot.resolve("enabled.txt");
  109. }
  110. public static Path getDisabledFilePath(Path pluginsRoot) {
  111. return pluginsRoot.resolve("disabled.txt");
  112. }
  113. }