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.

CompoundPluginDescriptorFinder.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.nio.file.Path;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. /**
  23. * @author Decebal Suiu
  24. */
  25. public class CompoundPluginDescriptorFinder implements PluginDescriptorFinder {
  26. private static final Logger log = LoggerFactory.getLogger(CompoundPluginDescriptorFinder.class);
  27. private List<PluginDescriptorFinder> finders = new ArrayList<>();
  28. public CompoundPluginDescriptorFinder add(PluginDescriptorFinder finder) {
  29. if (finder == null) {
  30. throw new IllegalArgumentException("null not allowed");
  31. }
  32. finders.add(finder);
  33. return this;
  34. }
  35. public int size() {
  36. return finders.size();
  37. }
  38. @Override
  39. public boolean isApplicable(Path pluginPath) {
  40. for (PluginDescriptorFinder finder : finders) {
  41. if (finder.isApplicable(pluginPath)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. @Override
  48. public PluginDescriptor find(Path pluginPath) {
  49. for (PluginDescriptorFinder finder : finders) {
  50. if (finder.isApplicable(pluginPath)) {
  51. log.debug("'{}' is applicable for plugin '{}'", finder, pluginPath);
  52. try {
  53. PluginDescriptor pluginDescriptor = finder.find(pluginPath);
  54. if (pluginDescriptor != null) {
  55. return pluginDescriptor;
  56. }
  57. } catch (Exception e) {
  58. if (finders.indexOf(finder) == finders.size() - 1) {
  59. // it's the last finder
  60. log.error(e.getMessage(), e);
  61. } else {
  62. // log the exception and continue with the next finder
  63. log.debug(e.getMessage());
  64. log.debug("Try to continue with the next finder");
  65. }
  66. }
  67. } else {
  68. log.debug("'{}' is not applicable for plugin '{}'", finder, pluginPath);
  69. }
  70. }
  71. throw new PluginRuntimeException("No PluginDescriptorFinder for plugin '{}'", pluginPath);
  72. }
  73. }