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.

PropertiesPluginDescriptorFinder.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.pf4j.util.StringUtils;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.nio.file.Files;
  23. import java.nio.file.Path;
  24. import java.nio.file.Paths;
  25. import java.util.Properties;
  26. /**
  27. * Find a plugin descriptor in a properties file (in plugin repository).
  28. *
  29. * @author Decebal Suiu
  30. */
  31. public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder {
  32. private static final Logger log = LoggerFactory.getLogger(PropertiesPluginDescriptorFinder.class);
  33. private static final String DEFAULT_PROPERTIES_FILE_NAME = "plugin.properties";
  34. protected String propertiesFileName;
  35. public PropertiesPluginDescriptorFinder() {
  36. this(DEFAULT_PROPERTIES_FILE_NAME);
  37. }
  38. public PropertiesPluginDescriptorFinder(String propertiesFileName) {
  39. this.propertiesFileName = propertiesFileName;
  40. }
  41. @Override
  42. public PluginDescriptor find(Path pluginPath) throws PluginException {
  43. Properties properties = readProperties(pluginPath);
  44. return createPluginDescriptor(properties);
  45. }
  46. protected Properties readProperties(Path pluginPath) throws PluginException {
  47. Path propertiesPath = pluginPath.resolve(Paths.get(propertiesFileName));
  48. log.debug("Lookup plugin descriptor in '{}'", propertiesPath);
  49. if (Files.notExists(propertiesPath)) {
  50. throw new PluginException("Cannot find '{}' path", pluginPath);
  51. }
  52. Properties properties = new Properties();
  53. try (InputStream input = Files.newInputStream(propertiesPath)) {
  54. properties.load(input);
  55. } catch (IOException e) {
  56. throw new PluginException(e.getMessage(), e);
  57. }
  58. return properties;
  59. }
  60. protected PluginDescriptor createPluginDescriptor(Properties properties) {
  61. PluginDescriptor pluginDescriptor = createPluginDescriptorInstance();
  62. // TODO validate !!!
  63. String id = properties.getProperty("plugin.id");
  64. pluginDescriptor.setPluginId(id);
  65. String description = properties.getProperty("plugin.description");
  66. if (StringUtils.isEmpty(description)) {
  67. pluginDescriptor.setPluginDescription("");
  68. } else {
  69. pluginDescriptor.setPluginDescription(description);
  70. }
  71. String clazz = properties.getProperty("plugin.class");
  72. pluginDescriptor.setPluginClass(clazz);
  73. String version = properties.getProperty("plugin.version");
  74. if (StringUtils.isNotEmpty(version)) {
  75. pluginDescriptor.setPluginVersion(version);
  76. }
  77. String provider = properties.getProperty("plugin.provider");
  78. pluginDescriptor.setProvider(provider);
  79. String dependencies = properties.getProperty("plugin.dependencies");
  80. pluginDescriptor.setDependencies(dependencies);
  81. String requires = properties.getProperty("plugin.requires");
  82. if (StringUtils.isNotEmpty(requires)) {
  83. pluginDescriptor.setRequires(requires);
  84. }
  85. pluginDescriptor.setLicense(properties.getProperty("plugin.license"));
  86. return pluginDescriptor;
  87. }
  88. protected PluginDescriptor createPluginDescriptorInstance() {
  89. return new PluginDescriptor();
  90. }
  91. }