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

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