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.

LegacyExtensionFinder.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.LegacyExtensionStorage;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.io.Reader;
  25. import java.net.URL;
  26. import java.nio.charset.StandardCharsets;
  27. import java.util.Enumeration;
  28. import java.util.HashSet;
  29. import java.util.LinkedHashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.Set;
  33. /**
  34. * All extensions declared in a plugin are indexed in a file {@code META-INF/extensions.idx}.
  35. * This class lookup extensions in all extensions index files {@code META-INF/extensions.idx}.
  36. *
  37. * @author Decebal Suiu
  38. */
  39. public class LegacyExtensionFinder extends AbstractExtensionFinder {
  40. private static final Logger log = LoggerFactory.getLogger(LegacyExtensionFinder.class);
  41. public static final String EXTENSIONS_RESOURCE = LegacyExtensionStorage.EXTENSIONS_RESOURCE;
  42. public LegacyExtensionFinder(PluginManager pluginManager) {
  43. super(pluginManager);
  44. }
  45. @Override
  46. public Map<String, Set<String>> readClasspathStorages() {
  47. log.debug("Reading extensions storages from classpath");
  48. Map<String, Set<String>> result = new LinkedHashMap<>();
  49. Set<String> bucket = new HashSet<>();
  50. try {
  51. Enumeration<URL> urls = getClass().getClassLoader().getResources(EXTENSIONS_RESOURCE);
  52. if (urls.hasMoreElements()) {
  53. collectExtensions(urls, bucket);
  54. } else {
  55. log.debug("Cannot find '{}'", EXTENSIONS_RESOURCE);
  56. }
  57. debugExtensions(bucket);
  58. result.put(null, bucket);
  59. } catch (IOException e) {
  60. log.error(e.getMessage(), e);
  61. }
  62. return result;
  63. }
  64. @Override
  65. public Map<String, Set<String>> readPluginsStorages() {
  66. log.debug("Reading extensions storages from plugins");
  67. Map<String, Set<String>> result = new LinkedHashMap<>();
  68. List<PluginWrapper> plugins = pluginManager.getPlugins();
  69. for (PluginWrapper plugin : plugins) {
  70. String pluginId = plugin.getDescriptor().getPluginId();
  71. log.debug("Reading extensions storage from plugin '{}'", pluginId);
  72. Set<String> bucket = new HashSet<>();
  73. try {
  74. log.debug("Read '{}'", EXTENSIONS_RESOURCE);
  75. ClassLoader pluginClassLoader = plugin.getPluginClassLoader();
  76. try (InputStream resourceStream = pluginClassLoader.getResourceAsStream(EXTENSIONS_RESOURCE)) {
  77. if (resourceStream == null) {
  78. log.debug("Cannot find '{}'", EXTENSIONS_RESOURCE);
  79. } else {
  80. collectExtensions(resourceStream, bucket);
  81. }
  82. }
  83. debugExtensions(bucket);
  84. result.put(pluginId, bucket);
  85. } catch (IOException e) {
  86. log.error(e.getMessage(), e);
  87. }
  88. }
  89. return result;
  90. }
  91. private void collectExtensions(Enumeration<URL> urls, Set<String> bucket) throws IOException {
  92. while (urls.hasMoreElements()) {
  93. URL url = urls.nextElement();
  94. log.debug("Read '{}'", url.getFile());
  95. collectExtensions(url.openStream(), bucket);
  96. }
  97. }
  98. private void collectExtensions(InputStream inputStream, Set<String> bucket) throws IOException {
  99. try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
  100. ExtensionStorage.read(reader, bucket);
  101. }
  102. }
  103. }