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.

PluginZip.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.plugin;
  17. import org.pf4j.PropertiesPluginDescriptorFinder;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.nio.file.Path;
  21. import java.util.LinkedHashMap;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import java.util.zip.ZipEntry;
  25. import java.util.zip.ZipOutputStream;
  26. /**
  27. * Represents a plugin {@code zip} file.
  28. * The {@code plugin.properties} file is created on the fly from the information supplied in {@link Builder}.
  29. *
  30. * @author Decebal Suiu
  31. */
  32. public class PluginZip {
  33. private final Path path;
  34. private final String pluginId;
  35. private final String pluginVersion;
  36. protected PluginZip(Builder builder) {
  37. this.path = builder.path;
  38. this.pluginId = builder.pluginId;
  39. this.pluginVersion = builder.pluginVersion;
  40. }
  41. public Path path() {
  42. return path;
  43. }
  44. public String pluginId() {
  45. return pluginId;
  46. }
  47. public String pluginVersion() {
  48. return pluginVersion;
  49. }
  50. public Path unzippedPath() {
  51. Path path = path();
  52. String fileName = path.getFileName().toString();
  53. return path.getParent().resolve(fileName.substring(0, fileName.length() - 4)); // without ".zip" suffix
  54. }
  55. public static Properties createProperties(Map<String, String> map) {
  56. Properties properties = new Properties();
  57. properties.putAll(map);
  58. return properties;
  59. }
  60. public static class Builder {
  61. private final Path path;
  62. private final String pluginId;
  63. private String pluginVersion;
  64. private Map<String, String> properties;
  65. public Builder(Path path, String pluginId) {
  66. this.path = path;
  67. this.pluginId = pluginId;
  68. }
  69. public Builder pluginVersion(String pluginVersion) {
  70. this.pluginVersion = pluginVersion;
  71. return this;
  72. }
  73. /**
  74. * Add extra properties to the {@code properties} file.
  75. */
  76. public Builder properties(Map<String, String> properties) {
  77. this.properties = properties;
  78. return this;
  79. }
  80. public PluginZip build() throws IOException {
  81. createPropertiesFile();
  82. return new PluginZip(this);
  83. }
  84. protected void createPropertiesFile() throws IOException {
  85. Map<String, String> map = new LinkedHashMap<>();
  86. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId);
  87. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
  88. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, "org.pf4j.plugin.TestPlugin");
  89. if (properties != null) {
  90. map.putAll(properties);
  91. }
  92. ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(path.toFile()));
  93. ZipEntry propertiesFile = new ZipEntry(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME);
  94. outputStream.putNextEntry(propertiesFile);
  95. createProperties(map).store(outputStream, "");
  96. outputStream.closeEntry();
  97. outputStream.close();
  98. }
  99. }
  100. }