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

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