Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CompoundPluginLoader.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import java.util.function.BooleanSupplier;
  23. /**
  24. * @author Decebal Suiu
  25. */
  26. public class CompoundPluginLoader implements PluginLoader {
  27. private static final Logger log = LoggerFactory.getLogger(CompoundPluginLoader.class);
  28. private List<PluginLoader> loaders = new ArrayList<>();
  29. public CompoundPluginLoader add(PluginLoader loader) {
  30. if (loader == null) {
  31. throw new IllegalArgumentException("null not allowed");
  32. }
  33. loaders.add(loader);
  34. return this;
  35. }
  36. /**
  37. * Add a {@link PluginLoader} only if the {@code condition} is satisfied.
  38. *
  39. * @param loader
  40. * @param condition
  41. * @return
  42. */
  43. public CompoundPluginLoader add(PluginLoader loader, BooleanSupplier condition) {
  44. if (condition.getAsBoolean()) {
  45. return add(loader);
  46. }
  47. return this;
  48. }
  49. public int size() {
  50. return loaders.size();
  51. }
  52. @Override
  53. public boolean isApplicable(Path pluginPath) {
  54. for (PluginLoader loader : loaders) {
  55. if (loader.isApplicable(pluginPath)) {
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. @Override
  62. public ClassLoader loadPlugin(Path pluginPath, PluginDescriptor pluginDescriptor) {
  63. for (PluginLoader loader : loaders) {
  64. if (loader.isApplicable(pluginPath)) {
  65. log.debug("'{}' is applicable for plugin '{}'", loader, pluginPath);
  66. try {
  67. ClassLoader classLoader = loader.loadPlugin(pluginPath, pluginDescriptor);
  68. if (classLoader != null) {
  69. return classLoader;
  70. }
  71. } catch (Exception e) {
  72. // log the exception and continue with the next loader
  73. log.error(e.getMessage()); // ?!
  74. }
  75. } else {
  76. log.debug("'{}' is not applicable for plugin '{}'", loader, pluginPath);
  77. }
  78. }
  79. throw new RuntimeException("No PluginLoader for plugin '" + pluginPath + "' and descriptor '" + pluginDescriptor + "'");
  80. }
  81. }