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.

ManifestPluginDescriptorFinder.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.util.FileUtils;
  18. import org.pf4j.util.StringUtils;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.jar.Attributes;
  26. import java.util.jar.JarFile;
  27. import java.util.jar.Manifest;
  28. /**
  29. * Read the plugin descriptor from the manifest file.
  30. *
  31. * @author Decebal Suiu
  32. */
  33. public class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
  34. private static final Logger log = LoggerFactory.getLogger(ManifestPluginDescriptorFinder.class);
  35. @Override
  36. public boolean isApplicable(Path pluginPath) {
  37. return Files.exists(pluginPath) && (Files.isDirectory(pluginPath) || FileUtils.isJarFile(pluginPath));
  38. }
  39. @Override
  40. public PluginDescriptor find(Path pluginPath) throws PluginException {
  41. Manifest manifest = readManifest(pluginPath);
  42. return createPluginDescriptor(manifest);
  43. }
  44. protected Manifest readManifest(Path pluginPath) throws PluginException {
  45. if (FileUtils.isJarFile(pluginPath)) {
  46. try (JarFile jar = new JarFile(pluginPath.toFile())) {
  47. Manifest manifest = jar.getManifest();
  48. if (manifest != null) {
  49. return manifest;
  50. }
  51. } catch (IOException e) {
  52. throw new PluginException(e);
  53. }
  54. }
  55. Path manifestPath = getManifestPath(pluginPath);
  56. if (manifestPath == null) {
  57. throw new PluginException("Cannot find the manifest path");
  58. }
  59. log.debug("Lookup plugin descriptor in '{}'", manifestPath);
  60. if (Files.notExists(manifestPath)) {
  61. throw new PluginException("Cannot find '{}' path", manifestPath);
  62. }
  63. try (InputStream input = Files.newInputStream(manifestPath)) {
  64. return new Manifest(input);
  65. } catch (IOException e) {
  66. throw new PluginException(e);
  67. }
  68. }
  69. protected Path getManifestPath(Path pluginPath) throws PluginException {
  70. if (Files.isDirectory(pluginPath)) {
  71. // legacy (the path is something like "classes/META-INF/MANIFEST.MF")
  72. return FileUtils.findFile(pluginPath,"MANIFEST.MF");
  73. }
  74. return null;
  75. }
  76. protected PluginDescriptor createPluginDescriptor(Manifest manifest) {
  77. DefaultPluginDescriptor pluginDescriptor = createPluginDescriptorInstance();
  78. // TODO validate !!!
  79. Attributes attributes = manifest.getMainAttributes();
  80. String id = attributes.getValue("Plugin-Id");
  81. pluginDescriptor.setPluginId(id);
  82. String description = attributes.getValue("Plugin-Description");
  83. if (StringUtils.isNullOrEmpty(description)) {
  84. pluginDescriptor.setPluginDescription("");
  85. } else {
  86. pluginDescriptor.setPluginDescription(description);
  87. }
  88. String clazz = attributes.getValue("Plugin-Class");
  89. if (StringUtils.isNotNullOrEmpty(clazz)) {
  90. pluginDescriptor.setPluginClass(clazz);
  91. }
  92. String version = attributes.getValue("Plugin-Version");
  93. if (StringUtils.isNotNullOrEmpty(version)) {
  94. pluginDescriptor.setPluginVersion(version);
  95. }
  96. String provider = attributes.getValue("Plugin-Provider");
  97. pluginDescriptor.setProvider(provider);
  98. String dependencies = attributes.getValue("Plugin-Dependencies");
  99. pluginDescriptor.setDependencies(dependencies);
  100. String requires = attributes.getValue("Plugin-Requires");
  101. if (StringUtils.isNotNullOrEmpty(requires)) {
  102. pluginDescriptor.setRequires(requires);
  103. }
  104. pluginDescriptor.setLicense(attributes.getValue("Plugin-License"));
  105. return pluginDescriptor;
  106. }
  107. protected DefaultPluginDescriptor createPluginDescriptorInstance() {
  108. return new DefaultPluginDescriptor();
  109. }
  110. }