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.

DefaultPluginDescriptorFinder.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2012 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package org.pf4j;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.IOException;
  18. import java.util.jar.Attributes;
  19. import java.util.jar.Manifest;
  20. import org.apache.commons.lang.StringUtils;
  21. /**
  22. * Read the plugin descriptor from the manifest file.
  23. *
  24. * @author Decebal Suiu
  25. */
  26. public class DefaultPluginDescriptorFinder implements PluginDescriptorFinder {
  27. @Override
  28. public PluginDescriptor find(File pluginRepository) throws PluginException {
  29. // TODO it's ok with classes/ ?
  30. File manifestFile = new File(pluginRepository, "classes/META-INF/MANIFEST.MF");
  31. if (!manifestFile.exists()) {
  32. // not found a 'plugin.xml' file for this plugin
  33. throw new PluginException("Cannot find '" + manifestFile + "' file");
  34. }
  35. FileInputStream input = null;
  36. try {
  37. input = new FileInputStream(manifestFile);
  38. } catch (FileNotFoundException e) {
  39. // not happening
  40. }
  41. Manifest manifest = null;
  42. try {
  43. manifest = new Manifest(input);
  44. } catch (IOException e) {
  45. throw new PluginException(e.getMessage(), e);
  46. } finally {
  47. try {
  48. input.close();
  49. } catch (IOException e) {
  50. throw new PluginException(e.getMessage(), e);
  51. }
  52. }
  53. PluginDescriptor pluginDescriptor = new PluginDescriptor();
  54. // TODO validate !!!
  55. Attributes attrs = manifest.getMainAttributes();
  56. String id = attrs.getValue("Plugin-Id");
  57. if (StringUtils.isEmpty(id)) {
  58. throw new PluginException("Plugin-Id cannot be empty");
  59. }
  60. pluginDescriptor.setPluginId(id);
  61. String clazz = attrs.getValue("Plugin-Class");
  62. if (StringUtils.isEmpty(clazz)) {
  63. throw new PluginException("Plugin-Class cannot be empty");
  64. }
  65. pluginDescriptor.setPluginClass(clazz);
  66. String version = attrs.getValue("Plugin-Version");
  67. if (StringUtils.isEmpty(version)) {
  68. throw new PluginException("Plugin-Version cannot be empty");
  69. }
  70. pluginDescriptor.setPluginVersion(PluginVersion.createVersion(version));
  71. String provider = attrs.getValue("Plugin-Provider");
  72. pluginDescriptor.setProvider(provider);
  73. String dependencies = attrs.getValue("Plugin-Dependencies");
  74. pluginDescriptor.setDependencies(dependencies);
  75. return pluginDescriptor;
  76. }
  77. }