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 3.7KB

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