Browse Source

Resolve #335

pull/337/head
Decebal Suiu 5 years ago
parent
commit
f66859f71c
  1. 57
      pf4j/src/main/java/org/pf4j/DefaultPluginStatusProvider.java
  2. 10
      pf4j/src/main/java/org/pf4j/util/FileUtils.java
  3. 49
      pf4j/src/test/java/org/pf4j/DefaultPluginStatusProviderTest.java

57
pf4j/src/main/java/org/pf4j/DefaultPluginStatusProvider.java

@ -20,6 +20,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
@ -45,11 +46,11 @@ public class DefaultPluginStatusProvider implements PluginStatusProvider {
try { try {
// create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file) // create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file)
enabledPlugins = FileUtils.readLines(pluginsRoot.resolve("enabled.txt"), true); enabledPlugins = FileUtils.readLines(getEnabledFilePath(), true);
log.info("Enabled plugins: {}", enabledPlugins); log.info("Enabled plugins: {}", enabledPlugins);
// create a list with plugin identifiers that should not be accepted by this manager (blacklist from plugins/disabled.txt file) // create a list with plugin identifiers that should not be accepted by this manager (blacklist from plugins/disabled.txt file)
disabledPlugins = FileUtils.readLines(pluginsRoot.resolve("disabled.txt"), true); disabledPlugins = FileUtils.readLines(getDisabledFilePath(), true);
log.info("Disabled plugins: {}", disabledPlugins); log.info("Disabled plugins: {}", disabledPlugins);
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@ -67,22 +68,70 @@ public class DefaultPluginStatusProvider implements PluginStatusProvider {
@Override @Override
public void disablePlugin(String pluginId) { public void disablePlugin(String pluginId) {
if (isPluginDisabled(pluginId)) {
// do nothing
return;
}
if (Files.exists(getEnabledFilePath())) {
enabledPlugins.remove(pluginId);
try {
FileUtils.writeLines(enabledPlugins, getEnabledFilePath());
} catch (IOException e) {
throw new PluginRuntimeException(e);
}
} else {
disabledPlugins.add(pluginId); disabledPlugins.add(pluginId);
try { try {
FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile()); FileUtils.writeLines(disabledPlugins, getDisabledFilePath());
} catch (IOException e) { } catch (IOException e) {
throw new PluginRuntimeException(e); throw new PluginRuntimeException(e);
} }
} }
}
@Override @Override
public void enablePlugin(String pluginId) { public void enablePlugin(String pluginId) {
if (!isPluginDisabled(pluginId)) {
// do nothing
return;
}
if (Files.exists(getEnabledFilePath())) {
enabledPlugins.add(pluginId);
try {
FileUtils.writeLines(enabledPlugins, getEnabledFilePath());
} catch (IOException e) {
throw new PluginRuntimeException(e);
}
} else {
disabledPlugins.remove(pluginId); disabledPlugins.remove(pluginId);
try { try {
FileUtils.writeLines(disabledPlugins, pluginsRoot.resolve("disabled.txt").toFile()); FileUtils.writeLines(disabledPlugins, getDisabledFilePath());
} catch (IOException e) { } catch (IOException e) {
throw new PluginRuntimeException(e); throw new PluginRuntimeException(e);
} }
} }
}
public Path getEnabledFilePath() {
return getEnabledFilePath(pluginsRoot);
}
public Path getDisabledFilePath() {
return getDisabledFilePath(pluginsRoot);
}
public static Path getEnabledFilePath(Path pluginsRoot) {
return pluginsRoot.resolve("enabled.txt");
}
public static Path getDisabledFilePath(Path pluginsRoot) {
return pluginsRoot.resolve("disabled.txt");
}
} }

10
pf4j/src/main/java/org/pf4j/util/FileUtils.java

@ -66,8 +66,16 @@ public class FileUtils {
return lines; return lines;
} }
/**
* Use {@link #writeLines(Collection, Path)} instead.
*/
@Deprecated
public static void writeLines(Collection<String> lines, File file) throws IOException { public static void writeLines(Collection<String> lines, File file) throws IOException {
Files.write(file.toPath(), lines, StandardCharsets.UTF_8); writeLines(lines, file.toPath());
}
public static void writeLines(Collection<String> lines, Path path) throws IOException {
Files.write(path, lines, StandardCharsets.UTF_8);
} }
/** /**

49
pf4j/src/test/java/org/pf4j/DefaultPluginStatusProviderTest.java

@ -20,6 +20,7 @@ import org.junit.jupiter.api.io.TempDir;
import org.pf4j.util.FileUtils; import org.pf4j.util.FileUtils;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -74,31 +75,67 @@ public class DefaultPluginStatusProviderTest {
@Test @Test
public void testDisablePluginWithEnableEmpty() throws Exception { public void testDisablePluginWithEnableEmpty() throws Exception {
// scenario with "disabled.txt"
createDisabledFile(); createDisabledFile();
PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath); DefaultPluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
statusProvider.disablePlugin("plugin-1"); statusProvider.disablePlugin("plugin-1");
assertTrue(statusProvider.isPluginDisabled("plugin-1")); assertTrue(statusProvider.isPluginDisabled("plugin-1"));
assertTrue(statusProvider.isPluginDisabled("plugin-2")); assertTrue(statusProvider.isPluginDisabled("plugin-2"));
assertFalse(statusProvider.isPluginDisabled("plugin-3")); assertFalse(statusProvider.isPluginDisabled("plugin-3"));
List<String> disabledPlugins = FileUtils.readLines(pluginsPath.resolve("disabled.txt"), true); List<String> disabledPlugins = FileUtils.readLines(statusProvider.getDisabledFilePath(), true);
assertTrue(disabledPlugins.contains("plugin-1")); assertTrue(disabledPlugins.contains("plugin-1"));
assertTrue(Files.notExists(statusProvider.getEnabledFilePath()));
// scenario with "enabled.txt"
Files.delete(statusProvider.getDisabledFilePath());
assertTrue(Files.notExists(statusProvider.getDisabledFilePath()));
createEnabledFile();
statusProvider = new DefaultPluginStatusProvider(pluginsPath);
statusProvider.disablePlugin("plugin-1");
assertTrue(statusProvider.isPluginDisabled("plugin-1"));
assertFalse(statusProvider.isPluginDisabled("plugin-2"));
List<String> enabledPlugins = FileUtils.readLines(statusProvider.getEnabledFilePath(), true);
assertFalse(enabledPlugins.contains("plugin-1"));
} }
@Test @Test
public void testEnablePlugin() throws Exception { public void testEnablePlugin() throws Exception {
// scenario with "enabled.txt"
createEnabledFile(); createEnabledFile();
PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath); DefaultPluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
statusProvider.enablePlugin("plugin-2"); statusProvider.enablePlugin("plugin-2");
assertFalse(statusProvider.isPluginDisabled("plugin-1")); assertFalse(statusProvider.isPluginDisabled("plugin-1"));
assertFalse(statusProvider.isPluginDisabled("plugin-2")); assertFalse(statusProvider.isPluginDisabled("plugin-2"));
assertTrue(statusProvider.isPluginDisabled("plugin-3")); assertTrue(statusProvider.isPluginDisabled("plugin-3"));
List<String> disabledPlugins = FileUtils.readLines(pluginsPath.resolve("disabled.txt"), true); List<String> enabledPlugins = FileUtils.readLines(statusProvider.getEnabledFilePath(), true);
assertTrue(enabledPlugins.contains("plugin-2"));
assertTrue(Files.notExists(statusProvider.getDisabledFilePath()));
// scenario with "disabled.txt"
Files.delete(statusProvider.getEnabledFilePath());
assertTrue(Files.notExists(statusProvider.getEnabledFilePath()));
createDisabledFile();
statusProvider = new DefaultPluginStatusProvider(pluginsPath);
statusProvider.enablePlugin("plugin-2");
assertFalse(statusProvider.isPluginDisabled("plugin-1"));
assertFalse(statusProvider.isPluginDisabled("plugin-2"));
List<String> disabledPlugins = FileUtils.readLines(statusProvider.getDisabledFilePath(), true);
assertFalse(disabledPlugins.contains("plugin-2")); assertFalse(disabledPlugins.contains("plugin-2"));
} }
@ -126,7 +163,7 @@ public class DefaultPluginStatusProviderTest {
List<String> disabledPlugins = new ArrayList<>(); List<String> disabledPlugins = new ArrayList<>();
disabledPlugins.add("plugin-2"); disabledPlugins.add("plugin-2");
FileUtils.writeLines(disabledPlugins, pluginsPath.resolve("disabled.txt").toFile()); FileUtils.writeLines(disabledPlugins, DefaultPluginStatusProvider.getDisabledFilePath(pluginsPath));
} }
private void createEnabledFile() throws IOException { private void createEnabledFile() throws IOException {
@ -134,7 +171,7 @@ public class DefaultPluginStatusProviderTest {
enabledPlugins.add("plugin-1"); enabledPlugins.add("plugin-1");
enabledPlugins.add("plugin-2"); enabledPlugins.add("plugin-2");
FileUtils.writeLines(enabledPlugins, pluginsPath.resolve("enabled.txt").toFile()); FileUtils.writeLines(enabledPlugins, DefaultPluginStatusProvider.getEnabledFilePath(pluginsPath));
} }
} }

Loading…
Cancel
Save