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 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2012 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 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.Collections;
  25. import java.util.List;
  26. /**
  27. * @author Decebal Suiu
  28. * @author Mário Franco
  29. */
  30. public class BasePluginRepository implements PluginRepository {
  31. protected final Path pluginsRoot;
  32. protected FileFilter filter;
  33. public BasePluginRepository(Path pluginsRoot) {
  34. this.pluginsRoot = pluginsRoot;
  35. }
  36. public BasePluginRepository(Path pluginsRoot, FileFilter filter) {
  37. this.pluginsRoot = pluginsRoot;
  38. this.filter = filter;
  39. }
  40. public void setFilter(FileFilter filter) {
  41. this.filter = filter;
  42. }
  43. @Override
  44. public List<Path> getPluginPaths() {
  45. File[] files = pluginsRoot.toFile().listFiles(filter);
  46. if ((files == null) || files.length == 0) {
  47. return Collections.emptyList();
  48. }
  49. List<Path> paths = new ArrayList<>(files.length);
  50. for (File file : files) {
  51. paths.add(file.toPath());
  52. }
  53. return paths;
  54. }
  55. @Override
  56. public boolean deletePluginPath(Path pluginPath) {
  57. try {
  58. FileUtils.delete(pluginPath);
  59. return true;
  60. } catch (NoSuchFileException nsf) {
  61. return false; // Return false on not found to be compatible with previous API
  62. } catch (IOException e) {
  63. throw new RuntimeException(e);
  64. }
  65. }
  66. }