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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2012 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.pf4j.util.StringUtils;
  18. import java.nio.file.Path;
  19. import java.util.jar.Attributes;
  20. import java.util.jar.Manifest;
  21. /**
  22. * Read the plugin descriptor from the manifest file.
  23. *
  24. * @author Decebal Suiu
  25. */
  26. public abstract class ManifestPluginDescriptorFinder implements PluginDescriptorFinder {
  27. @Override
  28. public PluginDescriptor find(Path pluginPath) throws PluginException {
  29. Manifest manifest = readManifest(pluginPath);
  30. return createPluginDescriptor(manifest);
  31. }
  32. public abstract Manifest readManifest(Path pluginPath) throws PluginException;
  33. protected PluginDescriptor createPluginDescriptor(Manifest manifest) {
  34. PluginDescriptor pluginDescriptor = createPluginDescriptorInstance();
  35. // TODO validate !!!
  36. Attributes attributes = manifest.getMainAttributes();
  37. String id = attributes.getValue("Plugin-Id");
  38. pluginDescriptor.setPluginId(id);
  39. String description = attributes.getValue("Plugin-Description");
  40. if (StringUtils.isEmpty(description)) {
  41. pluginDescriptor.setPluginDescription("");
  42. } else {
  43. pluginDescriptor.setPluginDescription(description);
  44. }
  45. String clazz = attributes.getValue("Plugin-Class");
  46. pluginDescriptor.setPluginClass(clazz);
  47. String version = attributes.getValue("Plugin-Version");
  48. if (StringUtils.isNotEmpty(version)) {
  49. pluginDescriptor.setPluginVersion(version);
  50. }
  51. String provider = attributes.getValue("Plugin-Provider");
  52. pluginDescriptor.setProvider(provider);
  53. String dependencies = attributes.getValue("Plugin-Dependencies");
  54. pluginDescriptor.setDependencies(dependencies);
  55. String requires = attributes.getValue("Plugin-Requires");
  56. if (StringUtils.isNotEmpty(requires)) {
  57. pluginDescriptor.setRequires(requires);
  58. }
  59. pluginDescriptor.setLicense(attributes.getValue("Plugin-License"));
  60. return pluginDescriptor;
  61. }
  62. protected PluginDescriptor createPluginDescriptorInstance() {
  63. return new PluginDescriptor();
  64. }
  65. }