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

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