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.

BasePluginRepository.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 java.io.File;
  19. import java.io.FileFilter;
  20. import java.io.IOException;
  21. import java.nio.file.NoSuchFileException;
  22. import java.nio.file.Path;
  23. import java.util.Arrays;
  24. import java.util.Comparator;
  25. import java.util.List;
  26. import java.util.stream.Collectors;
  27. import java.util.stream.Stream;
  28. /**
  29. * @author Decebal Suiu
  30. * @author Mário Franco
  31. */
  32. public class BasePluginRepository implements PluginRepository {
  33. protected final List<Path> pluginsRoots;
  34. protected FileFilter filter;
  35. protected Comparator<File> comparator;
  36. public BasePluginRepository(Path... pluginsRoots) {
  37. this(Arrays.asList(pluginsRoots));
  38. }
  39. public BasePluginRepository(List<Path> pluginsRoots) {
  40. this(pluginsRoots, null);
  41. }
  42. public BasePluginRepository(List<Path> pluginsRoots, FileFilter filter) {
  43. this.pluginsRoots = pluginsRoots;
  44. this.filter = filter;
  45. // last modified file is first
  46. this.comparator = Comparator.comparingLong(File::lastModified);
  47. }
  48. public void setFilter(FileFilter filter) {
  49. this.filter = filter;
  50. }
  51. /**
  52. * Set a {@link File} {@link Comparator} used to sort the listed files from {@code pluginsRoot}.
  53. * This comparator is used in {@link #getPluginPaths()} method.
  54. * By default is used a file comparator that returns the last modified files first.
  55. * If you don't want a file comparator, then call this method with {@code null}.
  56. */
  57. public void setComparator(Comparator<File> comparator) {
  58. this.comparator = comparator;
  59. }
  60. @Override
  61. public List<Path> getPluginPaths() {
  62. return pluginsRoots.stream()
  63. .flatMap(path -> streamFiles(path, filter))
  64. .sorted(comparator)
  65. .map(File::toPath)
  66. .collect(Collectors.toList());
  67. }
  68. @Override
  69. public boolean deletePluginPath(Path pluginPath) {
  70. if (!filter.accept(pluginPath.toFile())) {
  71. return false;
  72. }
  73. try {
  74. FileUtils.delete(pluginPath);
  75. return true;
  76. } catch (NoSuchFileException e) {
  77. return false; // Return false on not found to be compatible with previous API (#135)
  78. } catch (IOException e) {
  79. throw new PluginRuntimeException(e);
  80. }
  81. }
  82. protected Stream<File> streamFiles(Path directory, FileFilter filter) {
  83. File[] files = directory.toFile().listFiles(filter);
  84. return files != null
  85. ? Arrays.stream(files)
  86. : Stream.empty();
  87. }
  88. }