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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. private Map<Path, byte[]> files = new LinkedHashMap<>();
  76. public Builder(Path path, String pluginId) {
  77. this.path = path;
  78. this.pluginId = pluginId;
  79. }
  80. public Builder pluginClass(String pluginClass) {
  81. this.pluginClass = pluginClass;
  82. return this;
  83. }
  84. public Builder pluginVersion(String pluginVersion) {
  85. this.pluginVersion = pluginVersion;
  86. return this;
  87. }
  88. /**
  89. * Add extra properties to the {@code properties} file.
  90. * As possible attribute name please see {@link PropertiesPluginDescriptorFinder}.
  91. */
  92. public Builder properties(Map<String, String> properties) {
  93. this.properties.putAll(properties);
  94. return this;
  95. }
  96. /**
  97. * Add extra property to the {@code properties} file.
  98. * As possible property name please see {@link PropertiesPluginDescriptorFinder}.
  99. */
  100. public Builder property(String name, String value) {
  101. properties.put(name, value);
  102. return this;
  103. }
  104. /**
  105. * Adds a file to the archive.
  106. *
  107. * @param path the relative path of the file
  108. * @param content the content of the file
  109. */
  110. public Builder addFile(Path path, byte[] content) {
  111. files.put(path, content.clone());
  112. return this;
  113. }
  114. /**
  115. * Adds a file to the archive.
  116. *
  117. * @param path the relative path of the file
  118. * @param content the content of the file
  119. */
  120. public Builder addFile(Path path, String content) {
  121. files.put(path, content.getBytes());
  122. return this;
  123. }
  124. public PluginZip build() throws IOException {
  125. createPropertiesFile();
  126. return new PluginZip(this);
  127. }
  128. protected void createPropertiesFile() throws IOException {
  129. Map<String, String> map = new LinkedHashMap<>();
  130. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId);
  131. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
  132. if (pluginClass != null) {
  133. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
  134. }
  135. if (properties != null) {
  136. map.putAll(properties);
  137. }
  138. try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(path.toFile()))) {
  139. ZipEntry propertiesFile = new ZipEntry(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME);
  140. outputStream.putNextEntry(propertiesFile);
  141. createProperties(map).store(outputStream, "");
  142. outputStream.closeEntry();
  143. for (Map.Entry<Path, byte[]> fileEntry : files.entrySet()) {
  144. ZipEntry file = new ZipEntry(fileEntry.getKey().toString());
  145. outputStream.putNextEntry(file);
  146. outputStream.write(fileEntry.getValue());
  147. outputStream.closeEntry();
  148. }
  149. }
  150. }
  151. }
  152. }