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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.test;
  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. private final String pluginDependencies;
  39. protected PluginZip(Builder builder) {
  40. this.path = builder.path;
  41. this.pluginId = builder.pluginId;
  42. this.pluginClass = builder.pluginClass;
  43. this.pluginVersion = builder.pluginVersion;
  44. this.pluginDependencies = builder.pluginDependencies;
  45. }
  46. public Path path() {
  47. return path;
  48. }
  49. public File file() {
  50. return path.toFile();
  51. }
  52. public String pluginId() {
  53. return pluginId;
  54. }
  55. public String pluginClass() {
  56. return pluginClass;
  57. }
  58. public String pluginVersion() {
  59. return pluginVersion;
  60. }
  61. public String pluginDependencies() { return pluginDependencies; }
  62. public Path unzippedPath() {
  63. Path path = path();
  64. String fileName = path.getFileName().toString();
  65. return path.getParent().resolve(fileName.substring(0, fileName.length() - 4)); // without ".zip" suffix
  66. }
  67. public static Properties createProperties(Map<String, String> map) {
  68. Properties properties = new Properties();
  69. properties.putAll(map);
  70. return properties;
  71. }
  72. public static class Builder {
  73. private final Path path;
  74. private final String pluginId;
  75. private String pluginClass;
  76. private String pluginVersion;
  77. private String pluginDependencies;
  78. private Map<String, String> properties = new LinkedHashMap<>();
  79. private Map<Path, byte[]> files = new LinkedHashMap<>();
  80. public Builder(Path path, String pluginId) {
  81. this.path = path;
  82. this.pluginId = pluginId;
  83. }
  84. public Builder pluginClass(String pluginClass) {
  85. this.pluginClass = pluginClass;
  86. return this;
  87. }
  88. public Builder pluginVersion(String pluginVersion) {
  89. this.pluginVersion = pluginVersion;
  90. return this;
  91. }
  92. public Builder pluginDependencies(String pluginDependencies) {
  93. this.pluginDependencies = pluginDependencies;
  94. return this;
  95. }
  96. /**
  97. * Add extra properties to the {@code properties} file.
  98. * As possible attribute name please see {@link PropertiesPluginDescriptorFinder}.
  99. */
  100. public Builder properties(Map<String, String> properties) {
  101. this.properties.putAll(properties);
  102. return this;
  103. }
  104. /**
  105. * Add extra property to the {@code properties} file.
  106. * As possible property name please see {@link PropertiesPluginDescriptorFinder}.
  107. */
  108. public Builder property(String name, String value) {
  109. properties.put(name, value);
  110. return this;
  111. }
  112. /**
  113. * Adds a file to the archive.
  114. *
  115. * @param path the relative path of the file
  116. * @param content the content of the file
  117. */
  118. public Builder addFile(Path path, byte[] content) {
  119. files.put(path, content.clone());
  120. return this;
  121. }
  122. /**
  123. * Adds a file to the archive.
  124. *
  125. * @param path the relative path of the file
  126. * @param content the content of the file
  127. */
  128. public Builder addFile(Path path, String content) {
  129. files.put(path, content.getBytes());
  130. return this;
  131. }
  132. public PluginZip build() throws IOException {
  133. createPropertiesFile();
  134. return new PluginZip(this);
  135. }
  136. protected void createPropertiesFile() throws IOException {
  137. Map<String, String> map = new LinkedHashMap<>();
  138. map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId);
  139. map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
  140. if (pluginDependencies != null) {
  141. map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, pluginDependencies);
  142. }
  143. if (pluginClass != null) {
  144. map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
  145. }
  146. if (properties != null) {
  147. map.putAll(properties);
  148. }
  149. try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(path.toFile()))) {
  150. ZipEntry propertiesFile = new ZipEntry(PropertiesPluginDescriptorFinder.DEFAULT_PROPERTIES_FILE_NAME);
  151. outputStream.putNextEntry(propertiesFile);
  152. createProperties(map).store(outputStream, "");
  153. outputStream.closeEntry();
  154. for (Map.Entry<Path, byte[]> fileEntry : files.entrySet()) {
  155. ZipEntry file = new ZipEntry(fileEntry.getKey().toString());
  156. outputStream.putNextEntry(file);
  157. outputStream.write(fileEntry.getValue());
  158. outputStream.closeEntry();
  159. }
  160. }
  161. }
  162. }
  163. }