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.

ServiceProviderExtensionFinder.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.pf4j.processor.ExtensionStorage;
  18. import org.pf4j.processor.ServiceProviderExtensionStorage;
  19. import org.pf4j.util.FileUtils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import java.io.IOException;
  23. import java.io.Reader;
  24. import java.net.URISyntaxException;
  25. import java.net.URL;
  26. import java.nio.charset.StandardCharsets;
  27. import java.nio.file.FileVisitOption;
  28. import java.nio.file.FileVisitResult;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.nio.file.Paths;
  32. import java.nio.file.SimpleFileVisitor;
  33. import java.nio.file.attribute.BasicFileAttributes;
  34. import java.util.Collections;
  35. import java.util.Enumeration;
  36. import java.util.HashSet;
  37. import java.util.LinkedHashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.Set;
  41. /**
  42. * The {@link java.util.ServiceLoader} base implementation for {@link ExtensionFinder}.
  43. * This class lookup extensions in all extensions index files {@code META-INF/services}.
  44. *
  45. * @author Decebal Suiu
  46. */
  47. public class ServiceProviderExtensionFinder extends AbstractExtensionFinder {
  48. private static final Logger log = LoggerFactory.getLogger(ServiceProviderExtensionFinder.class);
  49. public static final String EXTENSIONS_RESOURCE = ServiceProviderExtensionStorage.EXTENSIONS_RESOURCE;
  50. public ServiceProviderExtensionFinder(PluginManager pluginManager) {
  51. super(pluginManager);
  52. }
  53. @Override
  54. public Map<String, Set<String>> readClasspathStorages() {
  55. log.debug("Reading extensions storages from classpath");
  56. Map<String, Set<String>> result = new LinkedHashMap<>();
  57. final Set<String> bucket = new HashSet<>();
  58. try {
  59. Enumeration<URL> urls = getClass().getClassLoader().getResources(EXTENSIONS_RESOURCE);
  60. if (urls.hasMoreElements()) {
  61. collectExtensions(urls, bucket);
  62. } else {
  63. log.debug("Cannot find '{}'", EXTENSIONS_RESOURCE);
  64. }
  65. debugExtensions(bucket);
  66. result.put(null, bucket);
  67. } catch (IOException | URISyntaxException e) {
  68. log.error(e.getMessage(), e);
  69. }
  70. return result;
  71. }
  72. @Override
  73. public Map<String, Set<String>> readPluginsStorages() {
  74. log.debug("Reading extensions storages from plugins");
  75. Map<String, Set<String>> result = new LinkedHashMap<>();
  76. List<PluginWrapper> plugins = pluginManager.getPlugins();
  77. for (PluginWrapper plugin : plugins) {
  78. String pluginId = plugin.getDescriptor().getPluginId();
  79. log.debug("Reading extensions storages for plugin '{}'", pluginId);
  80. final Set<String> bucket = new HashSet<>();
  81. try {
  82. Enumeration<URL> urls = ((PluginClassLoader) plugin.getPluginClassLoader()).findResources(EXTENSIONS_RESOURCE);
  83. if (urls.hasMoreElements()) {
  84. collectExtensions(urls, bucket);
  85. } else {
  86. log.debug("Cannot find '{}'", EXTENSIONS_RESOURCE);
  87. }
  88. debugExtensions(bucket);
  89. result.put(pluginId, bucket);
  90. } catch (IOException | URISyntaxException e) {
  91. log.error(e.getMessage(), e);
  92. }
  93. }
  94. return result;
  95. }
  96. private void collectExtensions(Enumeration<URL> urls, Set<String> bucket) throws URISyntaxException, IOException {
  97. while (urls.hasMoreElements()) {
  98. URL url = urls.nextElement();
  99. log.debug("Read '{}'", url.getFile());
  100. collectExtensions(url, bucket);
  101. }
  102. }
  103. private void collectExtensions(URL url, Set<String> bucket) throws URISyntaxException, IOException {
  104. Path extensionPath;
  105. if (url.toURI().getScheme().equals("jar")) {
  106. extensionPath = FileUtils.getPath(url.toURI(), EXTENSIONS_RESOURCE);
  107. } else {
  108. extensionPath = Paths.get(url.toURI());
  109. }
  110. try {
  111. bucket.addAll(readExtensions(extensionPath));
  112. } finally {
  113. FileUtils.closePath(extensionPath);
  114. }
  115. }
  116. private Set<String> readExtensions(Path extensionPath) throws IOException {
  117. final Set<String> result = new HashSet<>();
  118. Files.walkFileTree(extensionPath, Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() {
  119. @Override
  120. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  121. log.debug("Read '{}'", file);
  122. try (Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
  123. ExtensionStorage.read(reader, result);
  124. }
  125. return FileVisitResult.CONTINUE;
  126. }
  127. });
  128. return result;
  129. }
  130. }