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.

DefaultPluginRepository.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2012 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package ro.fortsoft.pf4j;
  14. import ro.fortsoft.pf4j.util.FileUtils;
  15. import java.io.File;
  16. import java.io.FileFilter;
  17. import java.util.Arrays;
  18. import java.util.Collections;
  19. import java.util.List;
  20. /**
  21. * @author Decebal Suiu
  22. * @author Mário Franco
  23. */
  24. public class DefaultPluginRepository implements PluginRepository {
  25. private final File directory;
  26. private final FileFilter filter;
  27. public DefaultPluginRepository(File directory, FileFilter filter) {
  28. this.directory = directory;
  29. this.filter = filter;
  30. }
  31. @Override
  32. public List<File> getPluginArchives() {
  33. File[] files = directory.listFiles(filter);
  34. return (files != null) ? Arrays.asList(files) : Collections.<File>emptyList();
  35. }
  36. @Override
  37. public boolean deletePluginArchive(String pluginPath) {
  38. File[] files = directory.listFiles(filter);
  39. if (files != null) {
  40. File pluginArchive = null;
  41. // strip prepended "/" from the plugin path
  42. String dirName = pluginPath.substring(1);
  43. // find the zip file that matches the plugin path
  44. for (File archive : files) {
  45. String name = archive.getName().substring(0, archive.getName().lastIndexOf('.'));
  46. if (name.equals(dirName)) {
  47. pluginArchive = archive;
  48. break;
  49. }
  50. }
  51. if (pluginArchive != null && pluginArchive.exists()) {
  52. return FileUtils.delete(pluginArchive);
  53. }
  54. }
  55. return false;
  56. }
  57. }