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.0KB

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