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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.nio.file.Paths;
  26. import java.util.Properties;
  27. /**
  28. * Find a plugin descriptor in a properties file (in plugin repository).
  29. *
  30. * @author Decebal Suiu
  31. */
  32. public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder {
  33. private static final Logger log = LoggerFactory.getLogger(PropertiesPluginDescriptorFinder.class);
  34. private static final String DEFAULT_PROPERTIES_FILE_NAME = "plugin.properties";
  35. protected String propertiesFileName;
  36. public PropertiesPluginDescriptorFinder() {
  37. this(DEFAULT_PROPERTIES_FILE_NAME);
  38. }
  39. public PropertiesPluginDescriptorFinder(String propertiesFileName) {
  40. this.propertiesFileName = propertiesFileName;
  41. }
  42. @Override
  43. public boolean isApplicable(Path pluginPath) {
  44. return Files.exists(pluginPath) && (Files.isDirectory(pluginPath) || FileUtils.isJarFile(pluginPath));
  45. }
  46. @Override
  47. public PluginDescriptor find(Path pluginPath) throws PluginException {
  48. Properties properties = readProperties(pluginPath);
  49. return createPluginDescriptor(properties);
  50. }
  51. protected Properties readProperties(Path pluginPath) throws PluginException {
  52. Path propertiesPath = getPropertiesPath(pluginPath, propertiesFileName);
  53. if (propertiesPath == null) {
  54. throw new PluginException("Cannot find the properties path");
  55. }
  56. log.debug("Lookup plugin descriptor in '{}'", propertiesPath);
  57. if (Files.notExists(propertiesPath)) {
  58. throw new PluginException("Cannot find '{}' path", propertiesPath);
  59. }
  60. Properties properties = new Properties();
  61. try (InputStream input = Files.newInputStream(propertiesPath)) {
  62. properties.load(input);
  63. } catch (IOException e) {
  64. throw new PluginException(e);
  65. }
  66. return properties;
  67. }
  68. protected Path getPropertiesPath(Path pluginPath, String propertiesFileName) throws PluginException {
  69. if (Files.isDirectory(pluginPath)) {
  70. return pluginPath.resolve(Paths.get(propertiesFileName));
  71. } else {
  72. // it's a jar file
  73. try {
  74. return FileUtils.getPath(pluginPath, propertiesFileName);
  75. } catch (IOException e) {
  76. throw new PluginException(e);
  77. }
  78. }
  79. }
  80. protected PluginDescriptor createPluginDescriptor(Properties properties) {
  81. DefaultPluginDescriptor pluginDescriptor = createPluginDescriptorInstance();
  82. // TODO validate !!!
  83. String id = properties.getProperty("plugin.id");
  84. pluginDescriptor.setPluginId(id);
  85. String description = properties.getProperty("plugin.description");
  86. if (StringUtils.isNullOrEmpty(description)) {
  87. pluginDescriptor.setPluginDescription("");
  88. } else {
  89. pluginDescriptor.setPluginDescription(description);
  90. }
  91. String clazz = properties.getProperty("plugin.class");
  92. if (StringUtils.isNotNullOrEmpty(clazz)) {
  93. pluginDescriptor.setPluginClass(clazz);
  94. }
  95. String version = properties.getProperty("plugin.version");
  96. if (StringUtils.isNotNullOrEmpty(version)) {
  97. pluginDescriptor.setPluginVersion(version);
  98. }
  99. String provider = properties.getProperty("plugin.provider");
  100. pluginDescriptor.setProvider(provider);
  101. String dependencies = properties.getProperty("plugin.dependencies");
  102. pluginDescriptor.setDependencies(dependencies);
  103. String requires = properties.getProperty("plugin.requires");
  104. if (StringUtils.isNotNullOrEmpty(requires)) {
  105. pluginDescriptor.setRequires(requires);
  106. }
  107. pluginDescriptor.setLicense(properties.getProperty("plugin.license"));
  108. return pluginDescriptor;
  109. }
  110. protected DefaultPluginDescriptor createPluginDescriptorInstance() {
  111. return new DefaultPluginDescriptor();
  112. }
  113. }