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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.AndFileFilter;
  18. import org.pf4j.util.DirectoryFileFilter;
  19. import org.pf4j.util.FileUtils;
  20. import org.pf4j.util.HiddenFilter;
  21. import org.pf4j.util.NotFileFilter;
  22. import org.pf4j.util.OrFileFilter;
  23. import org.pf4j.util.ZipFileFilter;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.pf4j.util.NameFileFilter;
  27. import java.io.File;
  28. import java.io.FileFilter;
  29. import java.io.IOException;
  30. import java.nio.file.Path;
  31. import java.util.List;
  32. /**
  33. * @author Decebal Suiu
  34. */
  35. public class DefaultPluginRepository extends BasePluginRepository {
  36. private static final Logger log = LoggerFactory.getLogger(DefaultPluginRepository.class);
  37. public DefaultPluginRepository(Path pluginsRoot, boolean development) {
  38. super(pluginsRoot);
  39. AndFileFilter pluginsFilter = new AndFileFilter(new DirectoryFileFilter());
  40. pluginsFilter.addFileFilter(new NotFileFilter(createHiddenPluginFilter(development)));
  41. setFilter(pluginsFilter);
  42. }
  43. @Override
  44. public List<Path> getPluginPaths() {
  45. // expand plugins zip files
  46. File[] pluginZips = pluginsRoot.toFile().listFiles(new ZipFileFilter());
  47. if ((pluginZips != null) && pluginZips.length > 0) {
  48. for (File pluginZip : pluginZips) {
  49. try {
  50. FileUtils.expandIfZip(pluginZip.toPath());
  51. } catch (IOException e) {
  52. log.error("Cannot expand plugin zip '{}'", pluginZip);
  53. log.error(e.getMessage(), e);
  54. }
  55. }
  56. }
  57. return super.getPluginPaths();
  58. }
  59. @Override
  60. public boolean deletePluginPath(Path pluginPath) {
  61. FileUtils.optimisticDelete(FileUtils.findWithEnding(pluginPath, ".zip", ".ZIP", ".Zip"));
  62. return super.deletePluginPath(pluginPath);
  63. }
  64. protected FileFilter createHiddenPluginFilter(boolean development) {
  65. OrFileFilter hiddenPluginFilter = new OrFileFilter(new HiddenFilter());
  66. if (development) {
  67. hiddenPluginFilter.addFileFilter(new NameFileFilter("target"));
  68. }
  69. return hiddenPluginFilter;
  70. }
  71. }