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.

DefaultPluginStatusProvider.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2014 Decebal Suiu
  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. *
  26. * @author Decebal Suiu
  27. * @author Mário Franco
  28. */
  29. public class DefaultPluginStatusProvider implements PluginStatusProvider {
  30. private static final Logger log = LoggerFactory.getLogger(DefaultPluginStatusProvider.class);
  31. private final Path pluginsRoot;
  32. private List<String> enabledPlugins;
  33. private List<String> disabledPlugins;
  34. public DefaultPluginStatusProvider(Path pluginsRoot) {
  35. this.pluginsRoot = pluginsRoot;
  36. initialize();
  37. }
  38. private void initialize() {
  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(pluginsRoot.resolve("enabled.txt"), 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(pluginsRoot.resolve("disabled.txt"), 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 boolean disablePlugin(String pluginId) {
  59. if (disabledPlugins.add(pluginId)) {
  60. try {
  61. FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile());
  62. } catch (IOException e) {
  63. log.error("Failed to disable plugin {}", pluginId, e);
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. @Override
  70. public boolean enablePlugin(String pluginId) {
  71. try {
  72. if (disabledPlugins.remove(pluginId)) {
  73. FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile());
  74. }
  75. } catch (IOException e) {
  76. log.error("Failed to enable plugin {}", pluginId, e);
  77. return false;
  78. }
  79. return true;
  80. }
  81. }