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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. public static final String PLUGIN_ID = "Plugin-Id";
  36. public static final String PLUGIN_DESCRIPTION = "Plugin-Description";
  37. public static final String PLUGIN_CLASS = "Plugin-Class";
  38. public static final String PLUGIN_VERSION = "Plugin-Version";
  39. public static final String PLUGIN_PROVIDER = "Plugin-Provider";
  40. public static final String PLUGIN_DEPENDENCIES = "Plugin-Dependencies";
  41. public static final String PLUGIN_REQUIRES = "Plugin-Requires";
  42. public static final String PLUGIN_LICENSE = "Plugin-License";
  43. @Override
  44. public boolean isApplicable(Path pluginPath) {
  45. return Files.exists(pluginPath) && (Files.isDirectory(pluginPath) || FileUtils.isJarFile(pluginPath));
  46. }
  47. @Override
  48. public PluginDescriptor find(Path pluginPath) throws PluginException {
  49. Manifest manifest = readManifest(pluginPath);
  50. return createPluginDescriptor(manifest);
  51. }
  52. protected Manifest readManifest(Path pluginPath) throws PluginException {
  53. if (FileUtils.isJarFile(pluginPath)) {
  54. try (JarFile jar = new JarFile(pluginPath.toFile())) {
  55. Manifest manifest = jar.getManifest();
  56. if (manifest != null) {
  57. return manifest;
  58. }
  59. } catch (IOException e) {
  60. throw new PluginException(e);
  61. }
  62. }
  63. Path manifestPath = getManifestPath(pluginPath);
  64. if (manifestPath == null) {
  65. throw new PluginException("Cannot find the manifest path");
  66. }
  67. log.debug("Lookup plugin descriptor in '{}'", manifestPath);
  68. if (Files.notExists(manifestPath)) {
  69. throw new PluginException("Cannot find '{}' path", manifestPath);
  70. }
  71. try (InputStream input = Files.newInputStream(manifestPath)) {
  72. return new Manifest(input);
  73. } catch (IOException e) {
  74. throw new PluginException(e);
  75. }
  76. }
  77. protected Path getManifestPath(Path pluginPath) {
  78. if (Files.isDirectory(pluginPath)) {
  79. // legacy (the path is something like "classes/META-INF/MANIFEST.MF")
  80. return FileUtils.findFile(pluginPath,"MANIFEST.MF");
  81. }
  82. return null;
  83. }
  84. protected PluginDescriptor createPluginDescriptor(Manifest manifest) {
  85. DefaultPluginDescriptor pluginDescriptor = createPluginDescriptorInstance();
  86. // TODO validate !!!
  87. Attributes attributes = manifest.getMainAttributes();
  88. String id = attributes.getValue(PLUGIN_ID);
  89. pluginDescriptor.setPluginId(id);
  90. String description = attributes.getValue(PLUGIN_DESCRIPTION);
  91. if (StringUtils.isNullOrEmpty(description)) {
  92. pluginDescriptor.setPluginDescription("");
  93. } else {
  94. pluginDescriptor.setPluginDescription(description);
  95. }
  96. String clazz = attributes.getValue(PLUGIN_CLASS);
  97. if (StringUtils.isNotNullOrEmpty(clazz)) {
  98. pluginDescriptor.setPluginClass(clazz);
  99. }
  100. String version = attributes.getValue(PLUGIN_VERSION);
  101. if (StringUtils.isNotNullOrEmpty(version)) {
  102. pluginDescriptor.setPluginVersion(version);
  103. }
  104. String provider = attributes.getValue(PLUGIN_PROVIDER);
  105. pluginDescriptor.setProvider(provider);
  106. String dependencies = attributes.getValue(PLUGIN_DEPENDENCIES);
  107. pluginDescriptor.setDependencies(dependencies);
  108. String requires = attributes.getValue(PLUGIN_REQUIRES);
  109. if (StringUtils.isNotNullOrEmpty(requires)) {
  110. pluginDescriptor.setRequires(requires);
  111. }
  112. pluginDescriptor.setLicense(attributes.getValue(PLUGIN_LICENSE));
  113. return pluginDescriptor;
  114. }
  115. protected DefaultPluginDescriptor createPluginDescriptorInstance() {
  116. return new DefaultPluginDescriptor();
  117. }
  118. }