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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.JarURLConnection;
  26. import java.net.URI;
  27. import java.net.URISyntaxException;
  28. import java.net.URL;
  29. import java.net.URLConnection;
  30. import java.nio.charset.StandardCharsets;
  31. import java.nio.file.Path;
  32. import java.util.Collections;
  33. import java.util.Enumeration;
  34. import java.util.HashSet;
  35. import java.util.LinkedHashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Set;
  39. import java.util.jar.JarFile;
  40. /**
  41. * All extensions declared in a plugin are indexed in a file {@code META-INF/extensions.idx}.
  42. * This class lookup extensions in all extensions index files {@code META-INF/extensions.idx}.
  43. *
  44. * @author Decebal Suiu
  45. */
  46. public class LegacyExtensionFinder extends AbstractExtensionFinder {
  47. private static final Logger log = LoggerFactory.getLogger(LegacyExtensionFinder.class);
  48. public static final String EXTENSIONS_RESOURCE = LegacyExtensionStorage.EXTENSIONS_RESOURCE;
  49. public LegacyExtensionFinder(PluginManager pluginManager) {
  50. super(pluginManager);
  51. }
  52. @Override
  53. public Map<String, Set<String>> readClasspathStorages() {
  54. log.debug("Reading extensions storages from classpath");
  55. Map<String, Set<String>> result = new LinkedHashMap<>();
  56. Set<String> bucket = new HashSet<>();
  57. try {
  58. Enumeration<URL> urls = getClass().getClassLoader().getResources(EXTENSIONS_RESOURCE);
  59. if (urls.hasMoreElements()) {
  60. collectExtensions(urls, bucket);
  61. } else {
  62. log.debug("Cannot find '{}'", EXTENSIONS_RESOURCE);
  63. }
  64. debugExtensions(bucket);
  65. result.put(null, bucket);
  66. } catch (IOException e) {
  67. log.error(e.getMessage(), e);
  68. }
  69. return result;
  70. }
  71. @Override
  72. public Map<String, Set<String>> readPluginsStorages() {
  73. log.debug("Reading extensions storages from plugins");
  74. Map<String, Set<String>> result = new LinkedHashMap<>();
  75. List<PluginWrapper> plugins = pluginManager.getPlugins();
  76. for (PluginWrapper plugin : plugins) {
  77. String pluginId = plugin.getDescriptor().getPluginId();
  78. log.debug("Reading extensions storage from plugin '{}'", pluginId);
  79. Set<String> bucket = new HashSet<>();
  80. try {
  81. log.debug("Read '{}'", EXTENSIONS_RESOURCE);
  82. ClassLoader pluginClassLoader = plugin.getPluginClassLoader();
  83. List<URL> extensionFiles = Collections.list(pluginClassLoader.getResources(EXTENSIONS_RESOURCE));
  84. URL targetFile = null;
  85. if (extensionFiles.size() == 1) {
  86. targetFile = extensionFiles.get(0);
  87. } else if (extensionFiles.size() > 1) {
  88. targetFile = extensionFiles.get(0);
  89. //Detect the correct extension file. in the past, getResourceAsStream was used but this fails
  90. //if ClassLoadingStrategy.APD is used and the application also contains a META-INF/extensions.idx file
  91. Path pluginPath = plugin.getPluginPath();
  92. for (URL url : extensionFiles) {
  93. URI extensionUri = "jar".equals(url.getProtocol()) ? new URI(url.getFile()) : url.toURI();
  94. Path extensionPath = java.nio.file.Paths.get(extensionUri);
  95. if (extensionPath.toString().startsWith(pluginPath.toString())) {
  96. targetFile = url;
  97. break;
  98. }
  99. }
  100. }
  101. if (targetFile != null) {pluginClassLoader.getResourceAsStream(EXTENSIONS_RESOURCE);
  102. URLConnection urlc = targetFile.openConnection();
  103. try (InputStream resourceStream = urlc.getInputStream()) {
  104. if (resourceStream == null) {
  105. log.debug("Cannot find '{}'", EXTENSIONS_RESOURCE);
  106. } else {
  107. collectExtensions(resourceStream, bucket);
  108. }
  109. } finally {
  110. //close jar file of jar url connection - otherwise the file cannot be deleted
  111. if (urlc instanceof JarURLConnection) {
  112. JarURLConnection juc = (JarURLConnection)urlc;
  113. JarFile jar = juc.getJarFile();
  114. jar.close();
  115. }
  116. }
  117. }
  118. debugExtensions(bucket);
  119. result.put(pluginId, bucket);
  120. } catch (IOException e) {
  121. log.error(e.getMessage(), e);
  122. } catch (URISyntaxException e) {
  123. log.error(e.getMessage(), e);
  124. }
  125. }
  126. return result;
  127. }
  128. private void collectExtensions(Enumeration<URL> urls, Set<String> bucket) throws IOException {
  129. while (urls.hasMoreElements()) {
  130. URL url = urls.nextElement();
  131. log.debug("Read '{}'", url.getFile());
  132. collectExtensions(url.openStream(), bucket);
  133. }
  134. }
  135. private void collectExtensions(InputStream inputStream, Set<String> bucket) throws IOException {
  136. try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
  137. ExtensionStorage.read(reader, bucket);
  138. }
  139. }
  140. }